Home How To 10 Ways to You Can Use ls Command in Linux Terminal

10 Ways to You Can Use ls Command in Linux Terminal

by Unallocated Author

ls is a two letter command both Unix users and sysadmins cannot live without. Whether you use it 1 time a day or 1000 times a day, knowing the power of the ls command can make your command line journey more enjoyable.

ls is basically a command used to have a look at the list of files and folders in the present directory. But, you can do a lot more than a list of files with this command. Let’s explore a few of them.

Display One File Per Line Using ls -1

The -1 is used at the end to show single entry per line.

$ ls -1
bin
boot
cdrom
dev
etc
home

 

Display Compete Information About Directories/Files Using ls -l

To show complete information about files, use -l at the end.

$ ls -l
-rw-r----- 1 sid hi-dev 9275204 apr 03 15:27 pythontest.txt.gz

Display The File Size in Readable Format Using ls -lh

The ls –lh (where h stands for human readable format) is used to see the size of folders in MB, KB etc.

$

ls -lh

-rw-r----- 1 sid hi-dev 9275204 14.7KB apr 03 15:27 pythontest.txt.gz

Order The Files Based on Last Modified Time Using ls -lt

You can sort the list of files in the order of modification in time.

$

ls -lt

total 76
drwxr-xr-x 121 root root  4096 Jun 22 07:36 etc
drwxrwxrwt 14 root root 4096 Jun 22 07:05 tmp
drwxr-xr-x 13 root root 4096 Jun 20 22 07:04 root
drwxr-xr-x  13 root root 13780 Jun 23:12 dev

Order The Files Based on Last Modified Time (In Reverse Order)

In the place of lt, type ltr.

$

ls -lt

total 76
drwxr-xr-x 13 root root 13780 Jun 23:12 dev
drwxr-xr-x 13 root root 4096 Jun 20 22 07:04 root
drwxrwxrwt 14 root root 4096 Jun 22 07:05 tmp
drwxr-xr-x 121 root root  4096 Jun 22 07:36 etc

Display Files Recursively Using ls -R

$ ls  -R /etc/sysconfig/networking
/etc/sysconfig/networking/profiles:
default
/etc/sysconfig/networking: 
devices profiles

Hide The Control Characters Using ls -q

If you want to print question mark instead of the non-graphics control characters use the -q option.

ls -q

Display Directory Information Using ls -ld

If you use “ls -l” you’ll get the details of all the directories content. But, if you want details of the directory then you can use the -d option as.

$

ls -ld /etc

drwxr-xr-x 21 root root 4096 Jun 15 07:02 /etc

Open The Last Edited File Using ls -t

$ vi second-long-file.txt
$ vi first-long-file.txt
$

vi `ls -t | head -1`

This opens the “first long file”, as it is the last edited one.

Display File UID and GID Using ls -n

Lists the output like -l, but shows the uid and gid in numeric format instead of names.

$ ls -l ~/.bash_profile
-rw-r--r--  1 ramesh ramesh 909 Feb  8 11:48 /home/ramesh/.bash_profile
$ ls -n ~/.bash_profile
-rw-r--r--  1

511 511

 909 Feb  8 11:48 /home/ramesh/.bash_profile

 

You may also like