Linux ls command is used to list the files. In short, the word list is termed as ls. The ls command is used in Linux and UNIX like operating systems to list the files.
If you don't specify any option it lists all the files in the current working directory. By default, it searches in the current working directory.
Default command
ls
List Contents of Directories and Subdirectories
ls *
Search a particular file
ls filename.txt
ls command options
The ls command can be executed with different options. Various options are listed below.
ls [options]
ls -a lists all files including hidden files and file names starting with a dot.
ls -a [path]
ls -d shows the list of directories only, not its contents.
List only Directories
ls -d */
ls -l gives a long format of files. It lists filenames in the following format permissions, the number of hard links to the content, content owner, content owner group, file size, last modified date, and filename.
ls -l [path]
ls -lh
to list files in Bytes(B), KiloBytes(KB), MegaBytes(MB), GigaBytes(GB) etc. When the file content size is larger than 1024 bytes it will convert to other size formats. -l shows files in bytes and the -h option shows files in human readable format. So use a combination of both options i.e., -lh
ls -lhS
to list files in descending order according to their content size.
ls -R is used to show the recursive listing of files in directories and subdirectories
ls -R [path]
ls -r
will list the files in reverse order.
ls -lX
will group files with the same extensions to display.
ls -F is used to distinguish the directories and executale files by appending "/" to the directories and "*" to the executable files.
ls -F [path]
ls ~
lists the contents of the user's home directory.
ls /
lists the contents of the root directory.
ls --version
displays the version of ls command
ls ../
dislays the contents of the parent directory.
ls ../..
shows the content level two level up.