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 the 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
- Any Linux distro installed.
- Basic understanding of command line.
- The system should have directories and files.
What is ls in Linux
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.
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 files 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
ls command on your terminal to display the contents of the current working directory.
Now almost all Linux terminals display files and directories in colors to differentiate each other. If you wish you can change the color of ls.
ls [options] [file-name/directory-name]
ls command options
The main ls command options are:
|Display all files including hidden files.|
|List long format|
|List in human-readable file format.|
|List size of files and directories|
|List in reverse order.|
|Sort by time, showing the most recently modified files first.|
|List subdirectories recursively.|
|List files and directories name appended with their types using special characters. For example '/' will be appended to directory name to indicate that it is directory.|
|Display files and directories as comma separated list.|
|List files and directories with quotation marks.|
|List directories and files with its inode number.|
|-||Display alphabetically by file extension.|
Lets look into some basic usage of ls command.
Listing files and directories
The default behavior of
ls command is to display files and directories under the current directory
You may use pwd command to verify the absolute path of the current working directory.
Listing specific directory
ls <path/to/directory> to list files and directories in that directory:
$ ls Pictures
Here ls list all the names of files and directories within the directory named Pictures. However, if you include a trailing slash ("/") after the directory name, it will still work correctly.
You can recursively list all files and directory inside a specific directory using * (wildcard):
$ ls sample *
List content in the parent directory
ls .. command to list files and directories in the parent directory:
$ ls ..
The ".." represents the parent directory itself. This way you can easily interact with files and directories one level above your current working directory. You may use
ls ../.. to list files two levels above.
Common Options with Examples
Let's look into some of the common options of ls command with its examples.
List files in the user’s home directory
You can combine ~ with ls to quickly view files and directories of the current user's home directory.
$ ls ~
If your home directory is /home/user, ls ~ allows to view contents of it without having to specify the full path.
List files in long format (ls -l)
The ls -l command list files and directories in along format - that includes additional information about each file and directory.
ls -lls -l /path/to/directoryls -l /path/to/filename
ls -l output format:
-rw-r--r-- 1 user group size date time filenamedrwxr-xr-x 2 user group size date time directory
Each column in the output:
|1||File Permissions||Contains 10 characters. The first character denotes the file type and the remaining 9 character represents the file permissions.|
|2||Number of links||shows the number of hard links the content file or directory has|
|3||Owner||The owner of the file or directory|
|4||Group||Group owner of file or directory|
|5||Size||Content size, by default in bytes|
|6||Date||The last modification date of the file or directory.|
|7||Time||the modification time of the file or directory.|
|8||Filename/Directory name||The actual name of the file or directory.|
By default ls -l shows all symbolic links and its linked file or directory name. This is indicated by link-filename -> target-file-or-directory-name. You can use ls -L to dereference the symbolic link (not show the linked targets).
If you are interested with more user friedly and feature rich listing of files and directories - you can refer to exa. The ls wont show the above mentioned column headers names, whereas exa has that feature.
List file size in human readable format
ls -lh to print out a long listing format of files and directories with size in a human readable format. The h option adjusts the file sizes to a more readable format, such as using kilobytes (KB), megabytes (MB), or gigabytes (GB) instead of raw byte counts.
$ ls -lh
List just directories
You can use -d option with ls to list only directories. For example to list only directories in the current directory , type:
$ ls -d */
*/ pattern matches any directory name because
* is a wildcard that represents any characters, and
/ indicates that it should match directories.
List only files
To list only files - there is no straight forward commad using ls. We can combine with egrep to exclude line start with 'd'.
$ ls -l | egrep -v '^d'
List files with their sizes
Use -s option with ls to list files and directories displaying its size in blocks
To list files and directories with their sizes, type
ls -s command:
$ ls -s
Remember the sizes displayed are the allocation sizes in blocks, not the actual sizes of the files or directories in terms of the number of bytes they occupy. A block is equals to a KiloByte ie 4 indiciates 4 x 64KB.
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.
$ ls -a
$ ls -al
Hidden files or directories in Linux are generally used for configuration file ( eg .bashrc), user specific settings (eg .bash_history) or version control (eg .git directory) requirements.
Ls command - Sort
Use ls with -X or --sort=extension to list file and directories, sort alphabetically by extension.
$ ls -Xor$ ls --sort=extension
Use -S option with ls to sort by size. Example
$ ls -lSor$ ls --sort=size -l
Use ls with -t or --sort=time option to list file and directories by modification time. By default most recently modified files appear first.
$ ls -ltor$ ls --sort=time -l
On that note to sort by modifcation time in ascending order - add -r option to reverse the sorting order. Example:
$ ls -ltr
Redirect ls output to a File
To redirect output to a file we can use output redirection operator '>". For example to redirect the output of ls -l to a file named results.txt, type:
$ ls -l results.txt
You can open the "results.txt" file to view the directory listing using a text editor or display the contents using a command like cat results.txt.
List UID and GID of Files
Use ls with -n option to list and directories with numeric user (UID) and group IDs (GID) . By default ls shows user name group names.
$ ls -n
List files in all directories and subdirectories
To recursively list all files and directories in tree formart use -R with ls command. This would be in a hierarchical format.
$ ls -R
The subdirectories will be indicated by colon (:) at the end of their names. And its contents will be shown just below it.
ls ** is an alternative to ls - R. But shows list files and directories recursively. The pattern ** is known as "globstar" or recursive globbing. This feature depends on shell and its versions.
For more detailed information and additional options, you can refer to man ls or type the same from the terminal.
- To list all files in the current directory, type the following: ls -a This lists all files, including. dot (.) ...
- To display detailed information, type the following: ls -l chap1 .profile. ...
- To display detailed information about a directory, type the following: ls -d -l .
To determine how many files there are in the current directory, put in ls -1 | wc -l. This uses wc to do a count of the number of lines (-l) in the output of ls -1.How can you list all the directories using ls command? ›
- To list all directories in the current directory: $ ls -d */
- To get list all directories in a specific directory: $ ls -d /path/to/dir1/*/
- Get list all directories in a directory recursively: $ find /path/to/dir1/ -type d -ls.
- Finally, only show list of all files in in a directory recursively:
By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively.How do I list all files and directories in Unix? ›
The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.How do I find all empty files and directories in Linux? ›
To find all the empty directories in the current directory, use the following command: find. −type d −empty −print | xargs rm −rf. Add the −delete option to remove them.How do I count the number of files and directories? ›
Right-click on the folder and select the “Properties” option. The “Properties” window will open and you will be able to see the number of files and subdirectories located in the directory selected.How do I count the number of files and folders? ›
How to count the files in a folder, using Command Prompt (cmd) You can also use the Command Prompt. To count the folders and files in a folder, open the Command Prompt and run the following command: dir /a:-d /s /b "Folder Path" | find /c ":".How do you count all files and subdirectories in Linux? ›
Sometimes, we may want to count number of files in a directory and all its subdirectories. In such cases, we can use '-R' option with 'ls' or 'find' command to list files recursively and then use 'wc' command to count number of files.How can I get a list of all directories? ›
- Open File Explorer in Windows. ...
- Click in the address bar and replace the file path by typing cmd then press Enter.
- This should open a black and white command prompt displaying the above file path.
- Type dir /A:D. ...
- There should now be a new text file called FolderList in the above directory.
ls is a Linux and Unix command that allows you to list all files and directories in a directory. You can also use ls -d to view only directories and use ls -f to view only files as shown above.How do I list only directories in Linux? ›
- Overview. In this tutorial, we'll discuss how to list only directories for a given path in Linux. ...
- Using ls Command. The ls command is one of the most popular commands to list the contents of the directory. ...
- Using dir Command. ...
- Using find Command. ...
- Using tree Command. ...
- Using echo Command. ...
- Using grep with Other Commands. ...
ls -- Display a list of files and directories. Typing just "ls" will give you a list of all the files and subdirectories in the current directory. (Technically it will show you only the "visible" files: those whose names begin with a character other than a period.)How do I list all files in multiple directories? ›
To have ls list the files in a directory other than the current directory, pass the path to the directory to ls on the command line. You can also pass more than one directory to ls , and have them listed one after the other.How do I list all files recursively in ls? ›
Use ls command to list files recursively. You can change the default behavior of the ls command to list files recursively by using the -R option. As you can see, it shows the contents of every subdirectory one by one. That's uppercase R .How to get the list of all files in a folder and subfolders in Linux? ›
ls -- Display a list of files and directories. Typing just "ls" will give you a list of all the files and subdirectories in the current directory.What is the Linux command to list all the directories? ›
ls (list) – the ls command is used to list the directory contents in the Linux system. By default, the ls command displays the content of the current directory.Which command is used to list all files and folders in Linux terminal? ›
The ls command is used to list files. "ls" on its own lists all files in the current directory except for hidden files.Which command is used to list all the files in a directory in Linux? ›
The ls command in linux is used for listing out all the files and directories within a directory.