ls Command in Linux / Unix – with Examples (How To) . The ls command is a Unix/Linuxcommand line tool that lists directories and files while showing detailed information about them. By default, this command shows the contents of the current directory. The ls command has multiple options that allow you to customize the output and get more detailed information about the listed files and directories.
Let’s continue reading ls Command in Linux / Unix – with Examples (How To).
When the ls command is invoked without any arguments or options lists the names of all directories and files in plain format. In its basic form, the ls command does not print information such as permissions, file types, modification dates, or even time.
ls
The output contains columns that are arranged in alphabetical order to fit across your terminal:
As we’ve already seen the default ls command output is not very informative, however, the lowercase L (-l) option instructs ls to display files in a long listing format. Here is an example:
From our output above we see that the long listing format provides us with file information such as:
The file type.
File owner, size, group, permissions.
The number of hard links to the file.
The first character of our output is -, which denotes a regular file. Values for other file types are denoted as follows:
“-” – Regular file
“b” – Block special file.
“d” – directory.
“c” – Character special file.
“n” – Network file.
“s” – Socket.
“l” – Symbolic link.
“p” – FIFO.
The nine characters that come after “-“ indicate the file permissions. The “rw-r” is for the user, “r- -“ is for the group, and “r- -“ is for others. The chmod command modifies file permissions. Here is a list of permission characters and the values they represent:
“r” – Permission to read the file.
“w” – Write a file.
“s” – setgid bit.
“x” – Execute the file.
“t” – Sticky bit.
In our case, rw-r–r– means that the user has read and write access to the file, while the group and others only have read access to the file. The character 1 after the permission characters shows the number of hard links to this file. For most files, the number of hard links is one, but this number increases as other hard links are created. Directories on the other hand have at least two hard links. The root root fields after the number 1 indicate the file owner as well as the group, followed by 242 which is the size of the file in bytes. Gur 27 2021 shows the last file modification date, with the last column being the file name.
Here is a detailed guide on how to use the ls command:
In Linux, hidden files are system files that begin with a period. Choose to only view the hidden files by appending the “a-“ option to the ls command:
ls -a
Also choose to view both the visible files as well as the hidden files with this command:
ls -la ~/
2. Sorting Output
By default, the ls command lists files in alphabetical order. Appending the – –sort flag to the ls command enables you to sort the output by size, extension, version, and time:
– -sort=size or -s – Sort file by size.
– -sort=extension or x – Sort alphabetically by extension.
– -sort=version or -v – Naturally sort version numbers.
– -sort=time or -t – Sort files by modification time.
If you want the results to display in reverse order, use the -r option.
For example, to sort the files within a direction named /home/machine1/ by modification time in the reverse order, you would use:
Invoking the ls command with the -R option instructs the system to display the contents in the subdirectories recursively:
ls -R
4. Display Files in a Human Readable Format
From the previous outputs, we see that the file and folder sizes are not easy to understand. To print the file sizes in formats that are easy to understand like, kilobytes, megabytes, or gigabytes, use the -lh option:
If you want to differentiate files from folders, use the -F option. The output for this command is such that folders display with the forward slash character (/) at the end:
ls -F
6. Display the Inode Number of Directories and Files
The inode number is a unique identifier assigned to each file or directory in a file system. This number is used by the file system to access and manage directories and files . To show the inode number of directories and files, use the -i option with the ls command:
7. Display the User ID and Group ID of Directories and Files
Also list the files and directories in a directory with numeric user and group IDs instead of their corresponding names. Using the -n option of the ls command prints User ID & Group ID of directories and files:
ls -n
8. Defining ls Command in Aliases
Aliases are modified commands in the Linux shell which you use instead of the original commands. To create an alias for the ls command follow this syntax:
alias=" ls -l"
This alias instructs the system to run the ls -l command in place of the ls command.
9. Adding Color to the ls Command Output
If you wish to colour the output display depending on file type, run this command:
ls –color
To enable colored output for a single ls command, use the --color=auto option.
To enable colored output for all ls commands, you need to add an alias to your shell’s configuration file, such as ~/.bashrc for the Bash shell or ~/.zshrc for the Zsh shell. Add the following line to the appropriate configuration file:
alias ls='ls --color=auto'
Now, whenever you run the ls command, the output is colored automatically.
Colors in the ls command output typically indicate the following:
Find out what version of ‘ls’ you are running, run the following command:
ls --v
Executing the ls – –version command also prints the ls command version.
If you want to view all options and what you achieve with the ls command execute this command:
ls --help
11. Using ls on Multiple Directories
The ‘ls’ command lists the contents of directories that are not your current directory. To do this append the path to the target directory to the ‘ls’ command. You also have the option to pass multiple directories to ‘ls’ and have the system list them in order. For instance, if you want ls to list the files in two directories, one named /home/machine1/Desktop/ and the other called /home/machine1/, here is the command to use:
ls /home/machine1/ /home/machine1/Desktop/
13. Using File patterns
Use a pattern machine to selectively list groups of files. The question mark character (?) represents any single character while the asterisk (*) represents strings of characters. To print a list of any directories and files that begin with “.gnome” use this command:
ls *.c
14. Ignoring Files
To have the system omit specific files from a listing, invoke ls with the – -hide flag. For instance, if you have a file named ‘2-square.py’ that you don’t want to list, use this command:
ls --hide=* 2-square.py
Using the -d argument followed by the directory path tells ‘ls’ to give a detailed report of the directory. Without this flag, ls will report on the files in the directory.
ls Command in Linux / Unix - with Examples (How To) Conclusion
The lscommand is an essential as it allows users to list files and directories in a conveniently. With numerous options and the ability to use wildcard patterns, it caters to a wide range of use cases, making file management and navigation more efficient. By mastering the various options and techniques outlined in this article, you significantly improve your command line productivity and streamline workflows.
Dennis is an expert content writer and SEO strategist in cloud technologies such as AWS, Azure, and GCP. He's also experienced in cybersecurity, big data, and AI.