Custom Search

Ubuntu ls command

The ls command is perhaps the mostly used command if you are working in the Ubuntu terminal. It is used to list directory contents. In other words, the ls command is used to view contents in a directory. There are many options available in ls command. The ls command options help users to list or view the way they prefer.

If you read my previous Ubuntu permission post, then you should have noticed one of the ls command options that I used, which is the
ls -l option. Actually the -l is an option to view long listing format. That way we can see the details of the file, such as the permission and file ownership. Below are examples of other useful ls command options normally used in Ubuntu.

Example 1 - Ubuntu standalone ls command, no option.



ls command is one of Ubuntu commands that can be used without an option. As you can see in the picture above, the ls command without option will list the directory contents. A directory, file and link is identified with different colors.

Example 2 - Ubuntu ls command with -a option.

luzar@ubuntu:/$ ls -a
.    boot   etc     initrd.img  media  proc  sbin  tmp  vmlinuz
.. cdrom home lib mnt .rnd srv usr
bin dev initrd lost+found opt root sys var

luzar@ubuntu:/$


The -a option lists all contents in directory including hidden files. A few years ago, people hide their nasty file using . and whitespace. But today, that trick won't work anymore.

Example 3 - Ubuntu ls command with -F option.

luzar@ubuntu:~$ ls -F
file.txt* folder/
luzar@ubuntu:~$


The -F option prints file types.

Example 4 - Ubuntu ls command with -g option.

luzar@ubuntu:~$ ls -g
total 4
-rwxrwxrwx 1 luzar 0 2008-10-21 03:43 file.txt
drwx------ 3 luzar 4096 2008-10-21 11:14 folder
luzar@ubuntu:~$


The -g option prints long listing like -l but without owner. To prints owner without group, use -o.

Example 5 - Ubuntu ls command with -h option.

luzar@ubuntu:~$ ls -h
file.txt folder
luzar@ubuntu:~$ ls -hl
total 4.0K
-rwxrwxrwx 1 luzar luzar 0 2008-10-21 03:43 file.txt
drwx------ 3 luzar luzar 4.0K 2008-10-21 11:14 folder
luzar@ubuntu:~$


The -h option itself means nothing. But combine with -l, it prints size in human readable format.

Example 6 - Ubuntu ls command with -i option.

luzar@ubuntu:~$ ls -i
8201 file.txt 8200 folder
luzar@ubuntu:~$


The -i option prints the index number of each file.

Example 7 - Ubuntu ls command with -m option.

luzar@ubuntu:~$ ls -m /var
backups, cache, lib, local, lock, log, mail, opt, run, spool, tmp, www
luzar@ubuntu:~$


The -m option prints a comma separated list of entries.

Example 8 - Ubuntu ls command with -r option.

luzar@ubuntu:~$ ls -r /var
www tmp spool run opt mail log lock local lib cache backups
luzar@ubuntu:~$ ls -rl /var
total 40
drwxr-xr-x 2 root root 4096 2008-10-19 15:17 www
drwxrwxrwt 2 root root 4096 2008-06-13 10:14 tmp
drwxr-xr-x 4 root root 4096 2008-10-19 15:17 spool
drwxr-xr-x 11 root root 400 2008-10-21 09:22 run
drwxr-xr-x 2 root root 4096 2008-10-19 15:10 opt
drwxrwsr-x 2 root mail 4096 2008-10-20 13:53 mail
drwxr-xr-x 10 root root 4096 2008-10-21 09:22 log
drwxrwxrwt 3 root root 60 2008-10-21 09:22 lock
drwxrwsr-x 2 root staff 4096 2008-06-13 10:14 local
drwxr-xr-x 27 root root 4096 2008-10-19 15:19 lib
drwxr-xr-x 9 root root 4096 2008-10-19 15:17 cache
drwxr-xr-x 2 root root 4096 2008-10-19 15:10 backups

luzar@ubuntu:~$


The -r option prints directory content reversely.

Example 9 - Ubuntu ls command with -R option.

luzar@ubuntu:~$ ls -R
.:
file.txt folder

./folder:
new_file.txt new_folder

./folder/new_folder:
luzar@ubuntu:~$


The -R option lists subdirectory recursively. As from the example, we can also see the content in the folder directory.

Example 10 - Ubuntu ls command with -s option.

luzar@ubuntu:~$ ls -s
total 4
0 file.txt 4 folder
luzar@ubuntu:~$


The -s option sorts content by file size.

That's ten examples of useful Ubuntu ls command option guides that you can use to manage your system. Another useful options are:

-t = to sort content by modification time.
-X = to sort alphabetically by extention.
-v = to sort by version.

No comments:

Post a Comment

Please keep comment relevant and strictly no spam will be tolerated. Thank you.