Linux LS Command - List Files and Directories (2024)

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

Prerequisites

  • 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

Simply type ls command on your terminal to display the contents of the current working directory.

Linux LS Command - List Files and Directories (1)

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.

Syntax:

ls [options] [file-name/directory-name]

ls command options

The main ls command options are:

OptionsDescription
-aDisplay all files including hidden files.
-lList long format
-hList in human-readable file format.
-sList size of files and directories
-rList in reverse order.
-tSort by time, showing the most recently modified files first.
-RList subdirectories recursively.
-FList 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.
-mDisplay files and directories as comma separated list.
-QList files and directories with quotation marks.
-iList directories and files with its inode number.
-XDisplay alphabetically by file extension.

Basic Usage

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

$ ls
Linux LS Command - List Files and Directories (2)

You may use pwd command to verify the absolute path of the current working directory.

Listing specific directory

Type ls <path/to/directory> to list files and directories in that directory:

$ ls Pictures
Linux LS Command - List Files and Directories (3)

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 *
Linux LS Command - List Files and Directories (4)

List content in the parent directory

Type ls .. command to list files and directories in the parent directory:

$ ls ..
Linux LS Command - List Files and Directories (5)

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 ~
Linux LS Command - List Files and Directories (6)

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.

Syntax:

ls -lls -l /path/to/directoryls -l /path/to/filename
Linux LS Command - List Files and Directories (7)

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:

NoColumnDescription
1File PermissionsContains 10 characters. The first character denotes the file type and the remaining 9 character represents the file permissions.
2Number of linksshows the number of hard links the content file or directory has
3OwnerThe owner of the file or directory
4GroupGroup owner of file or directory
5SizeContent size, by default in bytes
6DateThe last modification date of the file or directory.
7Timethe modification time of the file or directory.
8Filename/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

Type 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
Linux LS Command - List Files and Directories (8)

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 */
Linux LS Command - List Files and Directories (9)

Where the */ 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'.

Example:

$ ls -l | egrep -v '^d'
Linux LS Command - List Files and Directories (10)

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

Linux LS Command - List Files and Directories (11)

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.

Example

$ ls -a $ ls -al 
Linux LS Command - List Files and Directories (12)

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.

Example:

$ ls -Xor$ ls --sort=extension
Linux LS Command - List Files and Directories (13)

Use -S option with ls to sort by size. Example

$ ls -lSor$ ls --sort=size -l
Linux LS Command - List Files and Directories (14)

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
Linux LS Command - List Files and Directories (15)

On that note to sort by modifcation time in ascending order - add -r option to reverse the sorting order. Example:

$ ls -ltr
Linux LS Command - List Files and Directories (16)

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
Linux LS Command - List Files and Directories (17)

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.

Example

$ ls -n
Linux LS Command - List Files and Directories (18)

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.

Example

$ ls -R
Linux LS Command - List Files and Directories (19)

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.

Linux LS Command - List Files and Directories (2024)

FAQs

How do I list all files and directories in Linux? ›

-
  1. To list all files in the current directory, type the following: ls -a This lists all files, including. dot (.) ...
  2. To display detailed information, type the following: ls -l chap1 .profile. ...
  3. To display detailed information about a directory, type the following: ls -d -l .

How do I count files and directories in ls? ›

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? ›

Conclusion
  1. To list all directories in the current directory: $ ls -d */
  2. To get list all directories in a specific directory: $ ls -d /path/to/dir1/*/
  3. Get list all directories in a directory recursively: $ find /path/to/dir1/ -type d -ls.
  4. Finally, only show list of all files in in a directory recursively:
Jun 1, 2023

How to get list of all files in directory and sub directories? ›

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? ›

Steps
  1. Open File Explorer in Windows. ...
  2. Click in the address bar and replace the file path by typing cmd then press Enter.
  3. This should open a black and white command prompt displaying the above file path.
  4. Type dir /A:D. ...
  5. There should now be a new text file called FolderList in the above directory.

How do I list only files in ls? ›

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? ›

  1. Overview. In this tutorial, we'll discuss how to list only directories for a given path in Linux. ...
  2. Using ls Command. The ls command is one of the most popular commands to list the contents of the directory. ...
  3. Using dir Command. ...
  4. Using find Command. ...
  5. Using tree Command. ...
  6. Using echo Command. ...
  7. Using grep with Other Commands. ...
  8. Conclusion.
5 days ago

How do I list all folders 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. (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.

Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 5257

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.