The very powerful ‘ls’ command

The very powerful ‘ls’ command

An ls (pronouced as el es) utility appeared in the original version of AT&T UNIX. Today, two popular versions of ls are the Free Software Foundation’s (part of the GNU coreutils package) and the one released by various BSD variants, such as FreeBSD, OpenBSD, NetBSD, and Apple Computer’s Darwin. Both are free software and open source.

The ls command is one of the most used and important command that we as linux user and linux/unix administrator can’t live without. It is the basic utility that a beginner must learn and one of the commands that a Unix/Linux administrator should not forget. In this article, we will explore on how we can use the ls more efficiently and take advantage about its power.

1. Opening the last file edited.

To open the last edited file in the current directory use the combination of ls, head and vi commands as shown below. ls -t sorts the file by modification time, showing the last edited file first. head -1 picks up this first file.

$ nano `ls -t | head -1`

Be aware with the tick `, because it is important to make the command work.

2. Display one file per line.

To display files and directories per line, use -1.

$ ls -1
3. Display all information To display a list of information about the file/directory, use -l.
$ ls -l
drwxr-xr-x 2 johndoe staff 68 Feb 18 2011 Applications
drwx——+ 30 johndoe  staff 1020 Feb 17 16:21 Desktop
drwx——+ 126 johndoe  staff 4284 Feb 17 15:01 Documents
drwx——+ 63 johndoe  staff 2142 Feb 19 22:25 Downloads
-rw-r–r– 1 johndoe staff 20960 Jun 6 2011 xincoClientConfig.dat
1st Character – File Type: First character specifies the type of the file. A hyphen (-) in the 1st character indicates that this is a normal file while a letter d means it is a directory.
normal file
d directory
s socket file
l link file
Column 1 – File Permissions: Next 9 character specifies the files permission. Each 3 characters refers to the read, write, execute permissions for user, group and world.
Column 2 – Number of links: Second field specifies the number of links for that file. In this example, 2 indicates only two link to this directory.
Column 3 – Owner: Third field specifies owner of the file. In this example, this files and directory is owned by username ‘johndoe’.
Column 4 – Group: Fourth field specifies the group of the file. In the example above, this file belongs to ‘staff’ group.
Column 5 – Size: Fifth field specifies the size of file. In the example above, ’64’,’1020′, ‘4284’, ‘2142’, ‘936’ indicates the directory and file size respectively.
Column 6 – Last modified date & time: Sixth field specifies the date and time of the last modification of the file.
Column 7 – File name: The last field is the name of the file.
4. Display file size in a human readable format. User ls -lh (h stands for human readable form), to display file siez in easy to read format (M for MB, K for KB, G for GB).
$ ls -l
-rw-r–r– 1 johndoe staff 20960 Jun 6 2011 xincoClientConfig.dat
$ ls -lh
-rw-r–r– 1 johndoe staff 20K Jun 6 2011 xincoClientConfig.dat
5. Display Directory Information When you use “ls -l” you will get the details of directories content. But if you want the details of directory then you can use -d option.

Leave a Reply