Normal ls command without any switch
Without any switch ls command shows all files and directories names in single line separated by space.
1
2
3
4
|
root@kerneltalks # ls
directory1 directory2 testfile1 testfile2
|
Long listing using ls -l
For more detailed information, use long listing. That is using -l switch with ls command.
1
2
3
4
5
6
7
8
|
root@kerneltalks # ls -l
total 16
drwxr–xr–x 2 root admin 4096 Sep 14 18:07 directory1
drwxr–xr–x 2 root admin 4096 Sep 14 18:07 directory2
–rw–r—r— 1 root admin 8 Sep 14 18:08 testfile1
–rw–r—r— 1 root admin 51 Sep 14 18:08 testfile2
|
Information is displayed column wise where –
First column is file/directory permission details
Second column is tree count
Third column is owner of the file/directory
Forth column is group of file/directory
Fifth column is size in blocks
Sixth,seventh column is Date
Eight column has last modification time of file/directory
Last column is file or directory name.
Listing hidden files using ls
Normal ls command wont display hidden files. Hidden files in Linux are files whose names starts with .
These files can be listed using -a switch.
1
2
3
4
5
6
7
8
9
10
11
|
root@kerneltalks # ls -al
total 32
drwxr–xr–x 4 root admin 4096 Sep 14 18:08 .
drwxrwxrwt. 11 root root 12288 Sep 14 18:07 ..
drwxr–xr–x 2 root admin 4096 Sep 14 18:07 directory1
drwxr–xr–x 2 root admin 4096 Sep 14 18:07 directory2
–rw–r—r— 1 root admin 15 Sep 14 18:08 .account_detail
–rw–r—r— 1 root admin 8 Sep 14 18:08 testfile1
–rw–r—r— 1 root admin 51 Sep 14 18:08 testfile2
|
You can see in above output, hidden file .account_detail (name starts with .) is listed.
Listing human readable file sizes
In long listing we have seen that file size is displayed in block size. This is not user friendly format since you have to convert blocks to conventional byte size. Easy human readable format like KB, Mb,GB is available with switch -h. Using this file sizes will be displayed in human readable format.
1
2
3
4
5
6
7
8
|
root@kerneltalks # ls -hl
total 16K
drwxr–xr–x 2 root admin 4.0K Sep 14 18:07 directory1
drwxr–xr–x 2 root admin 4.0K Sep 14 18:07 directory2
–rw–r—r— 1 root admin 8 Sep 14 18:08 testfile1
–rw–r—r— 1 root admin 51 Sep 14 18:08 testfile2
|
Here size is displayed as 4K for directories i.e. 4 Kilobyte.
Listing inode numbers of files
Inodes are the numbers assigned to each file/directory in Linux system. Once can view them using -i switch.
1
2
3
4
|
root@kerneltalks # ls -i
18 directory1 30 directory2 32 testfile1 43 testfile2
|
Numbers 18, 30, 32 and 43 are respective inodes of those files and directories on right.
Sorting files with time of last modify time
This is one of the most widely used switch format of ls command. Switch used are -l (long listing), -r (reverse sort), -t (sort with modification time). Due to reverse sort, latest updated file will be shown at the bottom of output.
1
2
3
4
5
6
7
8
|
root@ kerneltalks # ls -lrt
total 16
drwxr–xr–x 2 root admin 4096 Sep 14 18:07 directory1
drwxr–xr–x 2 root admin 4096 Sep 14 18:07 directory2
–rw–r—r— 1 root admin 8 Sep 14 18:08 testfile1
–rw–r—r— 1 root admin 51 Sep 14 18:08 testfile2
|
Listing file owners with their IDs
Normal long listing shows owner and group as their names. To list owner and group as UID and GID you can use -n switch.
1
2
3
4
5
6
7
8
|
root@kerneltalks # ls -n
total 16
drwxr–xr–x 2 0 512 4096 Sep 14 18:07 directory1
drwxr–xr–x 2 0 512 4096 Sep 14 18:07 directory2
–rw–r—r— 1 0 512 8 Sep 14 18:08 testfile1
–rw–r—r— 1 0 512 51 Sep 14 18:08 testfile2
|
Listing directories by appending / to their names
ls command without argument lists all files and directory names. But without long listing (in which directories has their permission string starts with d) you wont be able to identify directories. So here is a tip. Use -p switch. It will append / to all directory names and you will be identify them easily.
1
2
3
4
|
root@kerneltalks # ls -p
directory1/ directory2/ testfile1 testfile2
|
You can see both directories has / appended to their names.
Listing directories recursively
Long listing or normal ls command shows you only directories residing in current directory (from where you are running command). To view files inside those directories you need to run ls command recursively i.e using -R switch.
1
2
3
4
5
6
7
8
9
10
11
|
root@kerneltalks # ls -R
.:
directory1 directory2 testfile1 testfile2
./directory1:
file1 file2
./directory2:
file3 file4
|
In output you can see –
First part . means current directory and then list of files/directories within it.
Second part says ./directory1 and then list of files/directories within it.
Third part lists files/directories within ./directory2.
So it listed all the content of both directories which resides on our present working directory.
Sorting files by file size
Sorting list with their size. Use -S switch. It will sort in descending order i.e. high size files being at top.
1
2
3
4
5
6
7
8
|
root@kerneltalks # ls -lS
total 16
drwxr–xr–x 2 root admin 4096 Sep 14 18:16 directory1
drwxr–xr–x 2 root admin 4096 Sep 14 18:16 directory2
–rw–r—r— 1 root admin 51 Sep 14 18:08 testfile2
–rw–r—r— 1 root admin 8 Sep 14 18:08 testfile1
|
Listing only owners of files
Want to list only owners of files? Use -o switch. Group wont be listed in output.
1
2
3
4
5
6
7
8
|
root@kerneltalks # ls -o
total 16
drwxr–xr–x 2 root 4096 Sep 14 18:16 directory1
drwxr–xr–x 2 root 4096 Sep 14 18:16 directory2
–rw–r—r— 1 root 8 Sep 14 18:08 testfile1
–rw–r—r— 1 root 51 Sep 14 18:08 testfile2
|
Listing only groups of files
Opposite of above. Group will be listed and users wont be listed for -g switch.
1
2
3
4
5
6
7
8
|
root@kerneltalks # ls -g
total 16
drwxr–xr–x 2 admin 4096 Sep 14 18:16 directory1
drwxr–xr–x 2 admin 4096 Sep 14 18:16 directory2
–rw–r—r— 1 admin 8 Sep 14 18:08 testfile1
–rw–r—r— 1 admin 51 Sep 14 18:08 testfile2
|