How to use the ls command to list files and directories on linux
The ls is a Linux shell command, used to list information about files and directories within the file system. By default ls utility is installed on all Linux distributions. This tutorial explains how to use the ls command with examples.
How to Use the ls Command#
Following is the basic syntax for the ls command:
If no options or arguments used with ls command, it will show a list of the names of all files in the current working directory:
By default, the files will be listed in alphabetical order.
If you want to list the content of a specific directory, pass the path to the directory as an argument with ls command. For example, to list the content of the /etc directory you would type type:
You also can pass the multiple directories path in a single command separated by space:
Make sure the user in which you are logged in should have read permission otherwise it will show the error message, ls can’t open the directory.
Long Listing Format#
By default the ls command shows only the names of the files and directories. You can use -l option with ls to show files in long listing format. Following details will be displayed when long listing format used.
- The file type
- The file permissions
- Number of hard links to the file
- File owner
- Group of File
- File size
- Date and Time
- File name
For example, to view details of /etc/hosts :
In above output, the first character shows the file type. In our example, the first character is – which indicates a regular file. Values for other file types are as follows:
- – – Regular file
- b – Block special file
- c – Character special file
- d – Directory
- l – Symbolic link
- n – Network file
- p – FIFO
- s – Socket
After that, nine characters are displaying the permission of the file. Out of nine, first three characters are for user, next three for the group and last three for others. As per your requirement you can change the file permissions using chmod command. Below characters are used for permissions:
- r – File read permission
- w – Permission to write to the file
- x – Permission to execute the file
- s – setgid bit
- t – sticky bit
In our last example, rw-r–r– means that the user can read and write the file, and the group and others can only read the file. The number 1 after the permission characters shows the number of hard links to this file.
The next two fields root root are showing the file owner and the group, followed by the size of the file ( 597 bytes). To show the size of file in human readable format use -h option. You can change the file owner using the chown command.
After that Sep 14 2019 is the last file modification date and time. In last column shows name of file.
Show Hidden Files#
By default, the ls command will not show hidden files. In Linux, a hidden file is any file that begins with a dot ( . ).
Use -a option to display all files including hidden files.
Sorting the Output#
As we seen before, by default ls command shows output in alphabetical order. The –sort option allows you to sort the output by extension, size, time and version.
- –sort=extension (or -X ) – sort alphabetically by extension.
- –sort=size (or -S) – sort by file size.
- –sort=time ( or -t) – sort by modification time.
- –sort=version (or -v) – Natural sort of version numbers.
To sort result in reverse order use the -r option.
For example, to sort files of /var directory by modification time in reverse sort order you would use:
List Subdirectories Recursively#
The -R option tells the ls command to display the contents of the subdirectories recursively:
Conclusion#
The ls command lists information about files and directories.
To know more about ls command visit the GNU Coreutils page or type man ls in your terminal.
If you have any questions or feedback, feel free to leave a comment.
If our content helps you, please consider buying us a coffee
The ls command in Linux is likely one of the first commands you ever need to use. In this article, we’ll go over the command and commonly used parameters.
My preferred set of options is as follows:
Let’s dig into each option individually, and explain why the entire glob of options is helpful.
Let’s look at a basic ls output.
You’ll notice it has some default color scheming but is otherwise a bare listing of files and directories. No specific details. You can check where the coloring is configured by typing in:
Options
Now let’s look at the [-a] entry.
The [-a] option allows us to see hidden files, as well as the top directory [..] and the current directory [.] permissions. Let’s add the [-l] option now.
More info now. We can see the file permissions, the owner and group, size in bytes, and date last modified. Helpful, but we can add more useful information. Let’s make it human-readable.
Now the size formats to readable information. Instead of 4096, we see 4.0K. This is less accurate, but normally we don’t need down-to-the-byte size accuracy. Next step, I prefer to see files sorted by time modified.
Okay, so the last thing modified is on top, I prefer it on the bottom. That’s where I’ll look first, for the most recent object modified. Let’s reverse the sort.
ls -arthl output
Now the last file I edited is on the bottom. I work with SELinux often, so if that’s your boat the [-Z] flag is a lifesaver. Let’s change up the ordering of the flags, I prefer a pronounceable arrangement. Zee-alter-h.
ls -Zaltrh output
And there’s the security context I need to know. Or in this case, a “?”. Now if you see a “?”, don’t worry. You simply are not using SELinux enforcement. If you run the same ls with options on AWS, you’ll see a set of security contexts. That’s the combination I find myself using the most. Some other helpful options are sorting by size and listing recursively.
Sort by Size
ls -Sharl output
There, my largest file is on the bottom in a human-readable format. Followed by the root directories.
List Recursively
Sometimes, I need to view an entire directory. ls has a recursive option, [-R]. I recommend dropping the [-a] option as it will show the [.] and [..] directories many times, and isn’t helpful.
ls -HaltrR output
That’s everything sorted by time, with the last modified directory on the bottom. Sorting by size with [-S] will put the largest directory on the bottom.
That’s it. You now know how to view all your files and directories in Linux along with information about them. Sort for the largest files and directories, or most recently modified. In Linux, the ls command is helpful for troubleshooting any recent changes, clearing out large directories, and much more.
Samuel Berry
I’m a writer, in the sense that there are words written and things needing explaining. Years of schooling, running on twelve now, taught me one thing, I like taking the complicated down to complex. So, I’m writing about Linux. One of those things that starts as complicated, and after a few years turns into complex. Until the next new thing rolls out anyways. Working in IT, I learned advocating for your product is the only way to ensure adoption. Providing user manuals, pictures, diagrams, and everything else possible to our community. That’s what builds the “user-friendly” experience. Not only design, but inclusion. Talk to me about Linux, I’m here to learn by teaching.
The ls command in Linux is likely one of the first commands you ever need to use. In this article, we’ll go over the command and commonly used parameters.
My preferred set of options is as follows:
Let’s dig into each option individually, and explain why the entire glob of options is helpful.
Let’s look at a basic ls output.
You’ll notice it has some default color scheming but is otherwise a bare listing of files and directories. No specific details. You can check where the coloring is configured by typing in:
Options
Now let’s look at the [-a] entry.
The [-a] option allows us to see hidden files, as well as the top directory [..] and the current directory [.] permissions. Let’s add the [-l] option now.
More info now. We can see the file permissions, the owner and group, size in bytes, and date last modified. Helpful, but we can add more useful information. Let’s make it human-readable.
Now the size formats to readable information. Instead of 4096, we see 4.0K. This is less accurate, but normally we don’t need down-to-the-byte size accuracy. Next step, I prefer to see files sorted by time modified.
Okay, so the last thing modified is on top, I prefer it on the bottom. That’s where I’ll look first, for the most recent object modified. Let’s reverse the sort.
ls -arthl output
Now the last file I edited is on the bottom. I work with SELinux often, so if that’s your boat the [-Z] flag is a lifesaver. Let’s change up the ordering of the flags, I prefer a pronounceable arrangement. Zee-alter-h.
ls -Zaltrh output
And there’s the security context I need to know. Or in this case, a “?”. Now if you see a “?”, don’t worry. You simply are not using SELinux enforcement. If you run the same ls with options on AWS, you’ll see a set of security contexts. That’s the combination I find myself using the most. Some other helpful options are sorting by size and listing recursively.
Sort by Size
ls -Sharl output
There, my largest file is on the bottom in a human-readable format. Followed by the root directories.
List Recursively
Sometimes, I need to view an entire directory. ls has a recursive option, [-R]. I recommend dropping the [-a] option as it will show the [.] and [..] directories many times, and isn’t helpful.
ls -HaltrR output
That’s everything sorted by time, with the last modified directory on the bottom. Sorting by size with [-S] will put the largest directory on the bottom.
That’s it. You now know how to view all your files and directories in Linux along with information about them. Sort for the largest files and directories, or most recently modified. In Linux, the ls command is helpful for troubleshooting any recent changes, clearing out large directories, and much more.
Samuel Berry
I’m a writer, in the sense that there are words written and things needing explaining. Years of schooling, running on twelve now, taught me one thing, I like taking the complicated down to complex. So, I’m writing about Linux. One of those things that starts as complicated, and after a few years turns into complex. Until the next new thing rolls out anyways. Working in IT, I learned advocating for your product is the only way to ensure adoption. Providing user manuals, pictures, diagrams, and everything else possible to our community. That’s what builds the “user-friendly” experience. Not only design, but inclusion. Talk to me about Linux, I’m here to learn by teaching.
By Balaji S
Introduction to Linux List Directories
We know that the Linux system is made up of files and directories. At any stage, while working on the Linux command mode you may want to list all files and subdirectories in the directory. There are many ways to list the contents of the directories in our Linux system. In this article, we will discuss about what are the command and options that are used to list directories in the Linux system.
Commands Used to List Directories
There are many commands used to list files and directories in the Linux system. Let us discuss them one by one.
Web development, programming languages, Software testing & others
1. ls Command
The ls command is the basic command used to list files and directories within the Linux file system. But if you want to list only directories, ls command offers some options. Let us discuss them with examples.
How to List Directories Using Wildcards
The simplest method to list directories is using wildcards.
Output:
To list directories in the Long listing format use the -l option.
Output:
Using grep and -f option
The -F option adds a trailing forward slash. This option allows us to grep directories by ‘grep’ing lines that ends with forward slash.
using grep and -l option
While using the long listing option –l we can grep the lines that starts with d. we can print only the directory names by using the following command.
2. Using dir Command
The dir command is used to list the contents of the directory. The syntax is as follows.
Output:
The forward slash in the above output indicates the directory and others indicate files. To print the detailed information of each file and directory use the -l command-line option.
Output:
In the above output, d indicates directories.
3. Using printf Command
This command is used for formatting and printing text. It will display the given parameters in the specified format or execute the command depending on the option specified. To display the contents of current working directory run the following command.
The output will display both files and directories.
4. Using find Command
The find command helps us to look up for files for which we know only the approximate names.In simple words the find command searches for a file in the current working directory and recursively through the subdirectories that matches the given search criteria. You can search files by name, extension, group, modification date, permissions etc.
To display all files which are present in the current working directory, use the following command.
The dot (.) symbol indicates the current working directory.
Output:
How to List only Directories Using Find Command
If you want to search only the directories and skip the file names use the -type d option as shown below.
find / -type d -name “apk” -ls
5. Listing Directories Using Stat Command
This command is used to display the information of files and filesystem. With the help of this command, we can find the properties of file such as file size, permissions, modified date, etc.
Use the following command line to list the files and directories
6. Listing Directories Using Grep Command
This command is used for searching text files using regular expressions. To list the contents of the directory using grep command run the following command.
7. Listing Directories Using Lsattr Command
To list the files and directories in the current directory use the following command.
This command displays only the contents of the directory just down one level which means it cannot display other subdirectories and its contents and also hidden files.
8. Listing Directories Using Getfacl Command
The getfacl and setfacl are two of the mostt important and useful commands every system administrator should know to set the Access Control Lists of the files and directories.
To display the files and directories in the current working directory, just run the following command.
The key features of this command is that it not only lists the contents of the directory but also other details such as
- Owner of the file or directory
- All the group names which has access to the file or directory
- Access rights to group, owner and others.
9. Listing Directories Using for Loop
This is another method to list the contents of the directory. Use the following command shown below.
for i in *; do echo $i; done
10. Listing Directories Using Vim Editor
In addition to viewing and editing files, vim editor can also be used for listing files and directories. We already know that everything is considered as a file in Linux. This implies that a directory is also a file which contains some list of files and subdirectories.
To list the files and directories in the current working directory using vim editor, run the command shown below.
The dot (.) at the end of the command implies the current working directory. Inorder to list the information of a specific directory, run the following command.
For Example:
We can use the up and down keys to navigate through the screen. To list the contents of the subdirectory, place the cursor on the subdirectory and then press ENTER key.
11. Listing Directories Using Tree Command
This command is used to list the directory contents in a tree like format.
The above command will list the files and subdirectories of the current directory. To list the directory contents down one level, run the following command.
Recommended Articles
This is a guide to Linux List Directories. Here we also discuss the Introduction and commands used to list directories along with different examples and its code implementation. You may also have a look at the following articles to learn more –
Linux Training Program (16 Courses, 3+ Projects)
Jun 9, 2020 · 2 min read
The ls command in Linux is the fastest way to get familiarized with the directory you are in. Upon e n tering the command into the terminal, you will see all the immediately visible files and directories (folders) within your current working directory. The ls command has several parameters and arguments you can utilize that will display the information how you want to see it. With ls there are ways to get a lot of detailed information on a wide range of files, or just the names of a few files that meet specified criteria. Today we’re just going to explore the utilization of ls for finding files with a specific file format!
The simplest way to do this is
…where ‘abc’ is the file extension you are looking for (.txt .html .c you get the idea). Whatever file extension you type will be displayed by the terminal. Notice in the image below when I type ls by itself, I see every file and directory, but when I add the wildcard, period, and file extension, suddenly it filters out any files not ending in .c
So what is really going on when we enter this command into the terminal? Well, the terminal reads the ls command first. After reading the ls command, it looks for any arguments that follow. In this case we input an asterisk, which serves as a wildcard. Wildcards mean anything before or after they are placed. It’s important to understand the wildcard concept. If I put ‘ *h’ it means “anything, then the letter H”. If there were no such results, nothing would turn up. If a file ended in the letter H, it would appear in the results. If I add another wildcard ‘ *h*’ then the terminal will interpret it to mean any file that contains the letter H, regardless of what comes before or after. If I put ‘ Y*’ it means the letter Y and anything that follows — I could use this to search for files starting with Y.
So the terminal interprets the wildcard to mean anything in its place, then it looks for .c Then after curating the list of all files ending with the .c it is printed to your terminal window!
Wildcards make searching, formatting, filtering, and organizing very easy in a terminal environment.
ls by itself displays all immediately visible files and directories! Adding arguments and parameters will help show the specific information you want to see.
The ls command is one of the basic commands that any Linux user should know. It is used to list information about files and directories within the file system. The ls utility is a part of the GNU core utilities package which is installed on all Linux distributions.
In this tutorial, we will show you how to use the ls command through practical examples and detailed explanations of the most common ls options.
How to Use the ls Command
The syntax for the ls command is as follows:
When used with no options and arguments, ls displays a list of the names of all files in the current working directory:
The files are listed in alphabetical order:
To list files in a specific directory, pass the path to the directory as an argument to the ls command. For example, to list the contents of the /etc directory you would type type:
You can also pass multiple directories and files to the ls command separated by space:
If the user you are logged in doesn’t have read permissions to the directory, you will get a message saying that ls can’t open the directory:
The ls command has a number of options. In the sections below, we will explore the most commonly used options.
Long Listing Format
The default output of the ls command shows only the names of the files and directories, which is not very informative.
The -l ( lowercase L) option causes ls to print files in a long listing format.
When the long listing format is used, the ls command will display the following file information:
- The file type
- The file permissions
- Number of hard links to the file
- File owner
- File group
- File size
- Date and Time
- File name
Consider the following example:
Let’s explain the most important columns of the output.
The first character shows the file type. In our example, the first character is – which indicates a regular file. Values for other file types are as follows:
- – – Regular file
- b – Block special file
- c – Character special file
- d – Directory
- l – Symbolic link
- n – Network file
- p – FIFO
- s – Socket
The next nine characters are showing the file permissions. The first three characters are for the user, the next three are for the group, and the last three are for others. You can change the file permissions with the chmod command. The permission character can take the following value:
- r – Permission to read the file
- w – Permission to write to the file
- x – Permission to execute the file
- s – setgid bit
- t – sticky bit
In our example, rw-r–r– means that the user can read and write the file, and the group and others can only read the file. The number 1 after the permission characters is the number of hard links to this file.
The next two fields root root are showing the file owner and the group, followed by the size of the file ( 337 ), shown in bytes. Use the -h option if you want to print sizes in a human-readable format. You can change the file owner using the chown command.
Oct 4 11:31 is the last file modification date and time.
The last column is the name of the file.
Show Hidden Files
By default, the ls command will not show hidden files. In Linux, a hidden file is any file that begins with a dot ( . ).
To display all files including the hidden files use the -a option:
Sorting the Output
As already mentioned, by default the ls command is listing the files in alphabetical order.
The –sort option allows you to sort the output by extension, size, time and version:
- –sort=extension (or -X ) – sort alphabetically by extension.
- –sort=size (or -S ) – sort by file size.
- –sort=time ( or -t ) – sort by modification time.
- –sort=version (or -v ) – Natural sort of version numbers.
If you want to get the results in the reverse sort order use the -r option.
For example, to sort the files in the /var directory by modification time in the reverse sort order you would use:
It’s worth mentioning that the ls command is not showing the total space occupied by the contents of the directory. To get the size of a directory use the du command.
List Subdirectories Recursively
The -R option tells the ls command to display the contents of the subdirectories recursively:
When using Linux, you are going to almost always be hooked to the terminal. And one of the common commands that you’ll have at the back of your hand will be the “ls” command.
Table of Contents
What is the ls command in Linux?
The “ls” command is short for “List” and is used to list directories and files in the folder where the command is run. Simple isn’t it?
Now just like it is with everything else in Linux, this command has multiple options for listing files. Let’s go over the most common options that you’ll use very often.
You can read through the manual pages for a comprehensive understanding of the command (or you can use the “–help” option to get a simplified list of available options).
How to use the ls command?
It’s a pretty straightforward command, but combined with the available options, it can be pretty useful. Let’s understand some common usage of the ls command.
1. ls command default or basic usage
To simply list the files in a directory, you can enter the command as is, and you’ll receive an output with the list of files in a tabulated format as the default setting.
2. Listing all files with the ls command
The default usage does not show you the hidden files in the directory that you’re working in. Even if you haven’t manually hidden any files, Linux saves configuration files as hidden (by adding a period before the name of the file). We do this with the use of the “-a” option. Give the below command a try and see what hidden files you find.
You see, in the same directory, I had the “.Xauthority” and “.xsession” configuration files hidden which are now revealed with the use of the ls -a command.
3. Getting a detailed list of files
On multiple occasions, you will need to figure out what permissions a user has for a file or folder. When you log in as “root” you have full access to any file that you want, but as a standard user, that is not the case. We can find the full list of files with the “-l” option. Since I’m on a full access system, I do not have to worry about file permissions but you can run the following command to see what kind of access you have.
4. Listing files in a sorted format
Let’s introduce a new concept to this tutorial. Combining command options. You can combine options by either writing them separately like “ls -l -a” or “ls -la”. Both will provide the same output. When sorting, we’ll want the full detailed list of files so if we sort for size, we can also see the size instead of just the sorted list.
Here are a few options you can use to sort your lists (the Linux terminal is case sensitive, so -S is not the same as -s):
- -S sorts by size
- -t sorts by modification time
- -r reverses the default order of the file list
- -h will show the file size in a human-readable format i.e. kb, MB, GB etc. (this is not for sorting but helps when sorting by size)
I’ll combine all the above options with the -l option so we see the full detailed list.
5. Listing all files recursively
In the above examples, you must have observed two listed files are highlighted in blue color. Those are folders. What if I want to get a list of all the files within my current working folder, along with all the files in all the folders too? The ls command makes it simple for us to do. With the use of the “-R” option, you can get just that. It will first list all the files within the current working directory (cwd which is also another command in Linux), and then list the folders and the files within them. Let’s see a quick example.
Conclusion
You should now have a good and practical understanding of the use of the ls command in Linux. Play around with the options, use the “–help” option to find out more comprehensive options that the command offers. If you’re further interested in learning about the command, Linux manual pages for the ls command provide you with really detailed information.
To access the man pages, just enter “man ”. For example, to read man pages for ls, enter:
ls command in Linux is used to list the directory contents. If a directory name is not specified then the contents of the current working directory are displayed.
Syntax
Example of How to use ls command in Linux?
First, we use ls without specifying any directory name. So it displays the contents of the current working directory which is cse325. Next, we use ls by giving the directory name which is IPC. So, it displays the contents of IPC directory.
Using Options with ls command
There are many options that are available with ls command. Here we will discuss the most common ones only.
1. Displaying hidden files also.
-a: option displays all the files within a directory including the hidden files also.
The output with -a option shows extra entries that start with a dot “.” For example .secret.
2. Displaying long-list
-l: option displays the long list of the files and sub-directories. The long list contains detailed information about the files and sub-directories.
Hence, use the -l option to know extra information about the files like user name, group name, size etc.
3. List the directories itself
-d: By default the ls command displays the directory content. But what if you want to list the directory name itself or you want to display the long list about the directory and not its content? The answer is -d option.
4. Disable coloured output
-f: options shows the output without using the default colour scheme
5. Print long list without user name
-g: same as -l option but it does not print the name of the owner/user
6. Print the index number of the file
-i: prints the inode number of the file
7. Print UID and GID
-n: same as -l option but instead of printing the user and group names, it prints the UserID (UID) and GroupID(GID)
8. Identify directories explicitly
-p: prints “/” character after the directories
So the “/” helps to identify the directory immediately.
9. Reverse sort the output
-r: prints the output in reverse alphabetical order
10. List the directories recursively
-R: option displays the directory contents in recursive manner.
With -R option the contents of current directory are displayed. Also, the content of subdirectories CPU_Scheduling, Disk_Scheduling, IPC are also displayed.
11. Sort by file size
-S: option sorts the output by file size. Hence, the output is sorted with the largest files listed first
12. Sort by modification time
-t: sorts the output based on file modification time
Some helpful Questions on ls command in Linux
Q1. What is the output when the argument to ls command is a file name?
Q2. Can you list contents of two directories simultaneously using ls?
Q3. Consider a Directory D1 having two files f1 and f2. Write the command to
1. Print the details about the files f1 and f2
2. Print the detail about the directory D1
3. Know the inode number of only f1
4. Know the size of f2
PPT on ls command
Video on ls command
Introduction
In this article, we are going to learn how to use the Linux ls command in Linux. Basically, ls command is a basic command in Linux used to List files and directories. ls command comes with so many arguments and features like you can sort files and directories by Date, by Size, able to check hidden files and directories, permissions, inode information and so on.
So let’s have a look at useful and very important Linux ls command with examples.
Some useful Linux ls command arguments :
- l – List files and directories in Long Listing Format.
- a – To check hidden files and directories.
- i – To check Inode Informations.
- n – List UID and GID number of Owner and Groups to which the files and directories are belongs.
- g – List group of files and directories to which they belongs.
- G – List users and not groups of files and directories to which they belongs.
- h – List the size of the files and directories in Human Readable format.
Linux ls command with Examples
You can use ls command with argument -l to get more advance information related files and directories like Permissions, Owner, Group, Size, Created and Modified month, date and time, file name…etc…
To list details of a particular file refer the below command.
To list the content of a particular directory refer the below command.
You can check hidden files and directories by using Linux ls command with argument -a.
To check inode number of files and directories use ls command with option -i.
OR you can use the -i argument with -l as shown on below command.
ls command with argument -o will show owner of the files and directories and will not show the groups.
Linux ls command with option -m will list the files and directories separated by comma.
ls command with option -Q will list files and directories in which names of files and directories are enclosed in quotes. Refer the sample output below.
you can list directories recursively using Linux ls command with the argument -R. Recursively means it will list all the directory with is all subdirectory in a tree format.
ls command with
will switch you to home directory.
Linux ls command with -t will list the files and directories by it’s modification date in ascending order, means the higher will be come first.
You can sort the files and directories by it size using ls command with argument -S.
ls command with option -h will list the size of the files and directories in Human Readable format for Example 20K (20 KB), 15M (15 MB).
ls command with option -n will show the UID (User ID) and GID (Group ID) numbers of file and directory.
If you want to list only Owner of the files and directories to which they are belongs and not groups then use Linux ls command with argument -G.
ls command with -g will list the group of the files and directories to which they are belongs.
ls command with -F will list the directories with a /at end. Refer the sample output below.
For more reference on Linux ls related commands and it’s arguments you can run below commands in your linux system.