Linux Phrasebook
Authors: Granneman S.
Published year: 2007
Pages: 29-32/288
Buy this book on amazon.com >>


Reverse the Order Contents are Listed

ls -r

If you don't like the default alphabetical order that -l uses, you can reverse it by adding -r (or --reverse ).


$ ls -lar ~/

-rw-------  8800 2005-10-18 19:55 .bash_history

drwxr-xr-x   592 2005-10-18 11:22 .Azureus
-rw-r--r--  1026 2005-09-25 00:11 .audacity
drwx------    72 2005-09-16 19:14 .aptitude
drwxr-xr-x   200 2005-07-28 01:31 alias


Note

Keep in mind that this is -r , not -R . -r means reverse, but -R means recursive.


When you use -l , the output is sorted alphabetically based on the name of the files and folders; the addition of -r reverses the output, but it is still based on the filename. Keep in mind that you can add -r virtually any time you use ls if you want to reverse the default output of the command and options you're inputting.



Sort Contents by File Extension

ls -X

The name of a file is not the only thing you can use for alphabetical sorting. You can also sort alphabetically by the file extension. In other words, you can tell ls to group all the files ending with .doc together, followed by files ending with .jpg , and finally finishing with files ending with .txt . Use the -X option (or --sort=extension ); if you want to reverse the sort, add the -r option (or --reverse ).

$

ls -lX ~/src

drwxr-xr-x     320 2005-10-06 22:35 backups
drwxr-xr-x    1336 2005-09-18 15:01 fonts
-rw-r--r-- 2983001 2005-06-20 02:15 install.tar.gz
-rw-r--r-- 6683923 2005-09-24 22:41 DuckDoom.zip


Folders go first in the list (after all, they have no file extension), followed by the files that do possess an extension. Pay particular attention to installdata.tar.gz it has two extensions, but the final one, the .gz , is what is used by ls .



Sort Contents by Date and Time

ls -t

Letters are great, but sometimes you need to sort a directory's contents by date and time. To do so, use -t (or --sort=time ) along with -l ; to reverse the sort, use -tr (or --sort=time --reverse ) along with -l .

$

ls -latr ~/

-rw-------   8800 2005-10-18 19:55 .bash_history
drwx------    368 2005-10-18 23:12 .gnupg
drwxr-xr-x   2760 2005-10-18 23:14 bin
drwx------    168 2005-10-19 00:13 .Skype


All of these items except the last one were modified on the same day; the last one would have been first if you weren't using the -r option and thereby reversing the results.

Note

Notice that you're using four options at one time in the previous command: -latr . You could have instead used -l -a -t -r , but who wants to type all of those hyphens? It's quicker and easier to just combine them all into one giant option. The long version of the options (those that start with two hyphens and consist of a word or two), however, cannot be combined and have to be entered separately, as in -la --sort=time --reverse .




Sort Contents by Size

ls -S

You can also sort by size instead of alphabetically by filename or extension, or by date and time. To sort by size, use -S (or --sort=size ).

$

ls -laS ~/

-rw-r--r--  109587 2005-10-19 11:53 .xsession-errors
-rw-------   40122 2005-04-20 11:00 .nessusrc
-rwxr--r--   15465 2005-10-12 15:45 .vimrc
-rw-------    8757 2005-10-19 08:43 .bash_history


When you sort by size, the largest items come first. To sort in reverse, with the smallest at the top, just use -r .


Linux Phrasebook
Authors: Granneman S.
Published year: 2007
Pages: 29-32/288
Buy this book on amazon.com >>

Similar books on Amazon