Friday Oct 24

The very powerful 'ls' command

Attention: open in a new window. PDFPrintE-mail
User Rating: / 3
PoorBest 

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
Applications
Desktop
Documents
Downloads
xincoClientConfig.dat

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.


Comments 

 
0 #7 knudsen 2013-08-19 23:33
Quote:
#1 aeonmike 2012-02-22 08:12 mas powerful ito pre.. rm -rf /


bwaaaahaha nice tip for the Linux newby Sir Mike!

For the long deep directories,

ls -l|less

or ls -l first-few-letters-of-file-name*
 
 
0 #6 lordfrancis3 2012-02-22 09:46
Quoting aeonmike:
oo nga eh. hehe


pa po kasi tapos.. hehehe.. medyo busy pa eh..
 
 
0 #5 aeonmike 2012-02-22 09:26
Quoting HansCheska:
mas maganda sana kung naka publish yung article. Hindi pa published.



oo nga eh. hehe
 
 
0 #4 lordfrancis3 2012-02-22 09:01
hindi pa published, hindi pa po kasi tapos..
 
 
0 #3 HansCheska 2012-02-22 08:46
mas maganda sana kung naka publish yung article. Hindi pa published.
 
 
0 #2 lordfrancis3 2012-02-22 08:44
Bwisit ka.. wag yan.. hahaha..
 
 
+1 #1 aeonmike 2012-02-22 08:12
mas powerful ito pre..


rm -rf /
 

You need to register in order for you to post a comment.