2016年1月18日 星期一

[Linux 常見問題] Linux ls Command: Sort Files By Size

Source From Here
Question
How do I sort all *.avi or *.py files in $HOME/Download/ directory by file size using Linux ls command line utility?

How-To
The ls command is used to list directory contents under Linux and Unix like operating systems. If no options or operands are given, the contents of the current directory are displayed on the screen. By default entries are sorted alphabetically if none of the -cftuvSUX nor --sort option passed to the ls command.

The default output (sort by alphabetically)
Type the following command:
$ ls

Sample outputs:


Force sort by size option
You need to pass the -S or --sort=size option as follows:
// --sort=WORD: sort by WORD instead of name: none -U, extension -X, size -S, time -t, version -v
$ ls -S
$ ls -S -l
$ ls --sort=size -l

Sample outputs:


You will see largest file first before sorting the operands in lexicographical order. The following command will sort file size in reverse order:
$ ls -l -S | sort -k 5 -n

OR try (see comments below, thanks!):
// -r, --reverse: reverse order while sorting
$ ls -lSr

Sample outputs:


Sort output and print sizes in human readable format (e.g., 1K 234M 2G)
Pass the -h option to the ls command as follows:
// -h, --human-readable: with -l, print sizes in human readable format (e.g., 1K 234M 2G)
$ ls -lSh


沒有留言:

張貼留言

[Linux 常見問題] sudo: Sorry, you must have a tty to run sudo Error on a Linux and Unix

Source From  Here   Preface   I‘m trying to run the following command:  #   ssh  user@box.example.com  sudo command1 /path/to/file But, a...