Linux LS Command - List Files in a Directory + Options + Output
Abstract: or home directory List files and directories in the current working directory The default behavior of ls command is to display files and directories u
Linux is a free, open-source Unix-like operating system based on the main component Linux kernel. Linux command line gives a fast and efficient user experience.
The ls command is a basic command used in Linux operating system. It allows users to list files and directories in the filesystem. The ls tool is packaged under the GNU core utilities (coreutils package).
In this tutorial, we learn about ls command in Linux and some of its important options with practical examples
Prerequisites- Any Linux distro installed
- Basic understanding for command line
- System should have directories and files
Ls command is one of the most frequently used Linux commands. Just like File explorer for GUI, ls command is a CLI version of it. Commonly used to list directory contents.
Ls command is generally used to list files and directories in the file system, other useful features are to list hidden files, list file size, list file by date and time, list files in reverse, and list files UID and GID.
By default ls command list files in alphabetical order.
How to use ls command Simply type ls
command on your terminal to display the contents of the current working directory.
Now all almost all Linux terminals display files and directories in colors to differentiate each other. If you wish you can change the color of ls.
Syntax:
ls [options] [file-name/directory-name]
ls command options
The main ls command options are:
OptionsDescriptionls -a
Display all files including hidden filesls -l
List long format mostly used to display file permissionsls -lh
List with columns in human-readable file formatls -ls
List columns with readable file sizels -r
Sort contents in the reverse orderls -t
Sort by time, newest at topls -R
List Subdirectories Recursivelyls -F
Append indicator - Adding "/" at the end of each directoryls -m
After each directory and file, a comma is addedls -Q
All directories and files list with quotation marksls -i
List directories and files with inode numberls -X
Display alphabetically by file extension
Let's check some of the basic ls commands usage with examples.
List Files in a DirectoryThe ls command is most frequently used to list files in a directory. This could be current, specific, root, parent directory, or home directory
List files and directories in the current working directoryThe default behavior of ls
command is to display files and directories under the current directory
Type ls
to list files and directories in that directory:
List content in the parent directory
Type ls ..
command to list files and directories in the parent directory:
You may also ls ../..
to list files two levels above.
The ls -l
command list the content of the directory in table format with 9 columns.
ls -l /path/to/directory
List in long format the directory contentsls -l /path/to/file
List in long format the file information
ls command columns names:
1. File type
2. Content permissions
3. Number of hard links to the content
4. Owner of the file or directory
5. Group owner of the file or directory
6. Content size in bytes
7. Last modified date and time of the content
8. The actual file or directory name
ls command doesn't display the column name headers, but you can use modern replacement for ls - exa.
Let's describe the output of ls -l command.
First column: Contains 10 characters. The first character denotes the file type and the remaining 9 character represents the file permissions.
File type character would be any of the following:
-
- Regular fileb
- Block special filec
- Character special filed
- Directoryl
- Symbolic linkn
- Network filep
- FIFOs
- Socket
After the file type, the content permission is represented in 9 characters. The first 3 character shows the read(r)/write(w)/execute(x) permission of the owner, the next 3 characters for the group, and the last 3 characters for others. There are two flags that represent setgid/setguid and sticky bit.
Second column: This shows the number of hard links the content file or directory has.
Third column: The owner of the file or directory.
Fourth column: Group owner of file or directory.
Fifth column: Content size, by default in bytes
Sixth column: The last modification date and time of the content
Last column: The name of the file or directory.
To modify file permissions use chmod command and to change ownership of files and directories use chown command.
List file size in human readable formatType ls -lh
to print out a long listing format of files and directories with size in a human readable format:
You can use du command to estimate the disk usage of files and directories in the Linux file system.
List just directoriesTo list only directories using ls command, type ls -d */
command.
To list files only using ls command, type ls -l | egrep -v '^d'
command
List files with their sizes
To list files and directories with their sizes, type ls -s
command:
Display hidden files
In Linux, a hidden file is any file that starts with a dot. By default ls command won't show hidden files.
To view hidden files run ls command with -a
the option. This will list all files including hidden files.
For example ls -a
or ls -al
command:
The output of ls command can be sorted using the --sort option.
Type ls -X
or --sort=extension
to sort alphabetically by extension:
Type ls -S
or --sort=size
to sort by file size:
Type ls -t
or --sort=time
to sort by last modification time (top will have recently modified):
Type ls -ltr
means to list files and directories with the last modification time in the reverse order sorted.
Type ls > results.txt to redirect the output of ls command to a text file.
You can use cat results.txt
command to display the contents of the file.
To list files and directories along with their UID and GID, type ls -n
command.
Type ls - R
command to list files in all directories and subdirectories recursively:
The list may be large so you can use ls -lR | more
to display one screen at a time and allows to scroll up and down through the page.
In this tutorial, we learned about ls command and its use in Linux. In case you forget any command you can type ls --help or man ls to display ls command manual page.