In this article, we would discuss "ls" command in Ubuntu. We also cover its associated options and arguments.
With the help of ls command-line utility, we get to list the contents of a directory. If we open a terminal and just enter the following command -
It will show us the names of files and directories present in a particular directory. This is pretty much everything a simple "ls" command can do. However, if we want to extract more out of it then, we need to provide options and arguments.
But, first a bit about options and arguments. We need to understand the concept -
options - these are basically single letters preceded by hyphen(-). For instance -
If we want the command to recognize a word as an option then that word needs to be preceded by double hyphens(--)
Lastly, an argument on which the command-line utility can act on. This can be filename, directory name, device etc.
ls -l /etc/hosts
"ls" command in Ubuntu
Now, coming back to the purpose of the article - "ls" command.
Here, we will use various options and arguments which can be utilized effectively with ls command.
I. ls - a basically lists all the files and directories. When we say all the files and directories it includes hidden as well.
II. ls -A it lists all the file and directories which ls -a shows as output except implied . and ..
III. ls -l --block-size= this is used to scale sizes. It will list the size of files in MBs. For KBs, we can use K -
ls -l --block-size=M
ls -l --block-size=K
IV. ls --color= this is used to colorize the output. Valid arguments are -
- 'yes', 'always' and 'force' - 'no', 'never' and 'none' - 'auto', 'tty' and 'if-tty'
The above command will decolorize the output.
V. ls -l use a long list format.
VI. ls -g similar to ls -l, but doesn't display the owner.
VII. ls -h for human readable format, use it with -l or -s option
VIII. ls -i prints index number (inode) of each file/directory.
IX. ls -S to sort the files/directories by size. The largest of those will be displayed first.
X. ls --version to display the installed version number and other information.
In conclusion, ls command-line utility is a great tool if we want to analyse file/directories effectively. If you want to try more command options then, it is always good to use -