How to Run “ls” Command in Linux?
“ls” is a basic command that any Linux user should know. The ls command displays basic information about files and folders inside the file system. It’s included in the GNU core utility kit which comes standard with all Linux distributions.
Syntax:
$ ls [ option ] [ file_name ]
Use “ls” Command:
List All Files
To see all the files and folders, run “ls” without any options or arguments.
Display Current Directory:
$ pwd displays the current working directory.
“ls” Command with Different Options:
List Root Directory:
Display Directory:
To display the specific directory, pass the directory name as an argument with the “ls” command.
$ ls /Directory_name
$ ls /home
It will display all the subdirectories/ files.
Display Files in Long List Format:
$ -l (Lowercase L) gives detailed information of all files and directories.
It prints the files and directories with all information.
drwxr-xr-x: file access.
2: number of links to file.
User: File owner
User: File group
4096: File size
Mar 6 06:39: Date and time
Desktop: File/Folder name
Display Specific File or Directory Content:
To display specific Directory or file information, pass the argument with the –l option.
Display Hidden Files:
In Linux, a file that begins with Dot(.) are hidden files.
To display the hidden files, Use -a option with “ls”:
Display the Content of Hidden Files:
Use a combination of –a and -l to display the content of all hidden files. (Files with. Dot is hidden).
Display Files in Reverse Order:
User –r option to display the files in reverse order:
Display Most Recent Modified Files and Directories:
-t options sort the files according to their modification dates. Recently, modified files will be displayed on top.
Display the Latest Modified Files:
To look at the contents of recently edited files and directories in reverse order, use the –r option.
Display Files in Human-readable Order:
Use -h option to present the files in a readable format for humans.
Display the Long List Format in Human-readable Format
Use –l and –h options to display the size of files in a human-readable format:
Display the Files Alphabetically by Extension:
Use the -x option to display the files or directories alphabetically by extension.
Display Files Size:
Use the –s option with the “ls” command to see the size of files and folders:
Sort by Size:
The -lS option sorts files and directories by their size. The larger files will be displayed first followed by smaller files.
Display UID and GID:
To display the (User ID) and (Group ID) of files and folder. Use the option –n with “ls” command:
Append Character with Files:
Use –F option with “ls” command to add character at the end with each directory.
Check ls Version:
To check the version of the “ls” command, use the –version as a parameter.
Display Files and Directories Recursively:
The -R option shows the files recursively. This means we will list folders and their contents recursively which will take us to the deepest path.
Add “/” Character with All Directories:
-p option append “/” character with each directory.
Display the Last Edited File:
To print the latest modified file in a directory user, use head with “ls” command:
Display the Oldest File:
To show the oldest file in a directory combine tail with “ls”.
Display Files Inode Number:
Use –I option to print the files/Directories in inode number. As you can see, the files’ name is preceded by a number
Display One File/Directory in One Line:
We may set out that only one file is specified in a single line. This is commonly used to include a file list as an input to other commands or programs.
Conclusion:
The “ls” command can display a variety of details about the files and sort them out using a variety of options and perform recursive listing. In this article, we have executed the “ls” command with different options.