The ls command lists the names of all (or selected) files in a directory, and tells you
about the attributes of those files. Here are some examples:
ls recipes lists the files in another directory using relative addressing.
ls /usr/bin lists the files in another directory using absolute addressing.
By default, ls prints a simple, columnar list of your files; but it will list your files in many different formats if you add one or more flags on the command line. To use any of them, simply type the ls command at the prompt, followed by a space, and then add a switch by typing a hyphen followed by the flag, like this:
$ ls -F
Here are some of the most commonly used ls flags:
a Lists all files, including hidden ones.
l Displays the file list in long format, including file details like size, time stamp, and owner.
F Adds a slash after the name for directories, an asterisk for executables, and an at sign (@) for linked files.
r Reverses the sort order (alphabetic or time).
t Sorts the list by the time each file was created.
Here's a look at the contents of a directory named animals, using some of these flags:
$ ls -F animals
cat_info cow_info dog_info
pig_info slugs/ zippity*
In this case, ls returns just the file names in columnar format, but with a few marks, thanks to the -F flag. The slash suffix on slugs indicates that it is a directory, and zippity is identified as an executable.
$ ls -al animals
-rw-r--r-- 1 hermie users 1758 Mar 17 23:17 .hoohah
-rw-r--r-- 1 hermie users 45090 Mar 23 23:17 cat_info
-rw-r--r-- 1 hermie users 64183 Feb 14 22:07 cow_info
-rw-r--r-- 1 hermie users 115032 Jan 06 11:14 dog_info
-rw-r--r-- 1 hermie users 248 Jan 16 09:18 pig_info
drwxr-xr-x 1 hermie users 1024 Feb 28 06:12 slugs
-rwxr-xr-x 1 hermie users 45198 Jan 23 11:14 zippity
Here, ls has displayed all files in the animals directory, including the hidden hoohah file. In addition to the file name, the file's permissions, owner, group, size in bytes, and date and time of last modification are displayed. (More on permissions later in this section.)
$ ls -lrt animals
-rw-r--r-- 1 hermie users 115032 Jan 06 11:14 dog_info
-rw-r--r-- 1 hermie users 248 Jan 16 09:18 pig_info
-rwxr-xr-x 1 hermie users 45198 Jan 23 11:14 zippity
-rw-r--r-- 1 hermie users 64183 Feb 14 22:07 cow_info
drwxr-xr-x 1 hermie users 1024 Feb 28 06:12 slugs
-rw-r--r-- 1 hermie users 45090 Mar 23 23:17 cat_info
This time, the files are sorted by time stamp, in reverse order. If you have lots of files in a directory, this is a handy way to find out which are the oldest and newest. Also notice that the -l, -r, and -t switches were combined into -lrt in the preceding command. We could just as well have issued the command ls -l -r -t animals.
Previous Lesson: Navigating the File System
Next Lesson: Displaying Linux Files
Comments (most recent first)
Charles (27 Aug 2010, 17:50)
If you don't mind me asking, why does ls -R *sh not list all the files in
current and sub-directories that end in sh?
Bob Rankin (17 Aug 2010, 10:09)
@Gurkin - Sounds like the stick bit. See
http://en.wikipedia.org/wiki/Sticky_bit
Raman (17 Aug 2010, 06:10)
These lessons are really helpful...
thnx....
Gurkin Max (19 Apr 2010, 13:39)
I have listed files attribute for /var/spool/cron/crontab :
drwx-wx--T 2 root crontab 4096 2010-03-17 21:56 crontabs What does T flag mean?
Pawan Kumar G (16 Apr 2010, 05:55)
Sir,
Good Evening, Very interesting, informative, helpful and Grate. Grateful to you, Thanking you.
AJITH KUMAR G (27 Feb 2010, 10:32)
Sir,
Good Evening, Very interesting, informative, helpful and Grate. Grateful to you, Thanking you. |
|
|
![]() |
|
| <Send This Link to a Friend> <Bookmark This Page> | ||