- ls Command in Linux for Listing Files
- 1) Run ls without parameter
- 2) Use long listing format
- 3) Show the size of file
- 4) Sort file size
- 5) Scaling size
- 6) Show hidden files
- 7) List directory entries only
- 8) Print entries without owner information
- 9) Print entries without group information
- 10) Print UID and GID
- 11) Print with no color
- 12) Print the index number of each file
- 13) Add / (slash sign) to mark directory
- 14) Reverse order while sorting
- 15) List subdirectories recursively
- 16) Sort by extension
- 17) List by modification time
- 18) List your home directory
- 19) List parent directory
- 20) File Search
- 21) Print the version of ls command
- 21) Print the version of ls command
- 21) Print the version of ls command
- 21) Print the version of ls command
- How to Use the ls Command in Linux
- Listing Directory Contents with ls:
- Listing Hidden Files with ls:
- Enable and Disable Colored Output:
- Long Listing Format of ls:
- Changing the File Size Unit in Long Listing Format of ls:
- Printing Human Readable File Sizes in Long Listing Format of ls:
- Printing inode Numbers with ls:
- Printing SELinux Contexts with ls:
- Getting Help:
ls Command in Linux for Listing Files
If you are a regular Linux user, there must hardly be a day when you haven’t used ls command. A very simple yet powerful command used for listing files and directories. By default, it will list the content of the current directory.
In this article, I will show you ls command in Linux to list files with examples.
1) Run ls without parameter
Running ls without parameter will display a list of files and or directories only. No other information can be seen from the output
2) Use long listing format
Using -l character (small L letter), will display a long listing of the content of current directory ( i.e. not only prints the name of the file, but also some attributes such as owner, group owner, link count, permissions).
Here’s how to read the output :
1st column
The first letter d does the content is directory or file. On the screenshot above, Desktop, Documents, Downloads and lynis-1.3.8 are directories. If it — (minus sign) it means that the content is file. While if it equal with l (small L character), means the content is link file.
The next 9 character is about file permission. With the first 3 rwx characters are for Owner of the file, the second 3 characters are for Group owner of the file and the last 3 characters are for worldwide access to the file.
2nd column
This tells us about how many link to this file
3rd column
This tells us about who is the owner of the file/directory
4th column
This tells us about who the group owner of the file/directory
5th column
This tells us about the size of the file/directory in bytes unit. Except for directories, the size will always count as 4096 bytes
6th column
This tells us about the last time and date the file is modified
7th column
This tells us the filename or directory name
3) Show the size of file
Reading size in bytes unit can confuse us. To read 6,5 M is easier compare with reading 6727680 byte. To do this, we can use -h combine with -l parameter. -h parameter means human readable
Another parameter that can do this is —si parameter. This parameter is similar with -h parameter, but —si powers 1000 while -h powers 1024.
4) Sort file size
After we can show the file size, we may want to sort it by file size. We can use -S parameter to to this. The list will be sort by the largest file size first.
5) Scaling size
Ls can scale size by before printing them using —block-size=SIZE. Where SIZE are :
- K = Kilobyte
- M = Megabyte
- G = Gigabyte
- T = Terabyte
- P = Petabyte
- E = Exabyte
- Z = Zettabyte
- Y = Yottabyte
For example, we want to scale size using Megabyte units. So the syntax will be like this :
6) Show hidden files
In Linux, a file begins with “.” (dot sign) is a hidden file. To show it on ls command, we can use -a parameter.
7) List directory entries only
If we want to list directory entries only, we can use -d parameter.
8) Print entries without owner information
To do this, we can use -g parameter.
9) Print entries without group information
While -g is to suppress owner information, -G will suppress group information
10) Print UID and GID
If we want to know the UID and GID of owner and group owner, we can do it using ls command with -n parameter. Here’s a sample.
From the example above, we know that user pungki has UID = 1000 and GID = 1000. While root group has GID = 0
11) Print with no color
Some Linux distribution is enable color options to ls command. This will make ls print the list in color. If you don’t want it, you can use —color=never parameter.
12) Print the index number of each file
To print the index number or known as inode number, we can use -i parameter. The index number will appear at first column.
13) Add / (slash sign) to mark directory
Appends forward slash to the directories. To do this, use -p parameter.
14) Reverse order while sorting
You may also want to list entries in reverse order. To do this, we can use -r parameter.
15) List subdirectories recursively
With -R parameter, you can list directory including its subdirectories.
16) Sort by extension
You can sort the list by extension using -X parameter or —sort=extension.
Or
17) List by modification time
Using -t parameter will sort the list by modification time which the newest first.
18) List your home directory
For listing your home directory, we can short the directory using «
» (tilde sign). So you don’t have to type your full directory name. Let say if the home directory name is /home/pungki, then
sign have the meaning with /home/pungki.
19) List parent directory
Whenever you are inside a directory, you can also list the parent directory without need to type directory full name. Here’s a sample.
This will list the contents of the directory one level above.
This will list the contents of the directory two level above.
20) File Search
a) Search files ending with letter ‘l»
b) Search files ending with 5 characters
Note — The directory should have files with 5 characters in length otherwise it wont give any outputs.
In the above examples, it didn’t display one file because it had 6 character
Recursively search for the same
c) Search using regex
21) Print the version of ls command
To print it, use —version parameter
That’s some parameters which can be used on day-to-day operation. Of course, you can always consult with ls manual page by typing man ls or ls —help on your console to explore more detail.
21) Print the version of ls command
To print it, use —version parameter
That’s some parameters which can be used on day-to-day operation. Of course, you can always consult with ls manual page by typing man ls or ls —help on your console to explore more detail.
21) Print the version of ls command
To print it, use —version parameter
That’s some parameters which can be used on day-to-day operation. Of course, you can always consult with ls manual page by typing man ls or ls —help on your console to explore more detail.
welcome@12345 welcome@34567 welcome@65467 welcome@65678 welcome@65789
21) Print the version of ls command
To print it, use —version parameter
That’s some parameters which can be used on day-to-day operation. Of course, you can always consult with ls manual page by typing man ls or ls —help on your console to explore more detail.
Источник
How to Use the ls Command in Linux
Listing Directory Contents with ls:
You can list the contents of your current working directory with ls. This is the most common usage of ls.
To list the contents of your current working directory, run ls as follows:
As you can see, the directory contents of my current working directory (which is the login user’s HOME directory by default) is listed.
You can also list the contents of another directory using the full/absolute path or relative path of that directory.
For example, let’s say you want to list the contents of the /etc directory. To do that, run ls as follows:
As you can see, the contents of the /etc directory are listed.
Again, let’s say, you want to list the contents of the
/Downloads directory using the relative path of the directory from your current working directory which the login user’s HOME (
) directory by default.
To do that, run ls as follows:
As you can see, the contents of the
/Downloads directory are listed.
Listing Hidden Files with ls:
On Linux, files and directories starting with . (such as .profile, .bashrc, .local/ etc) are hidden files and directories. By default, the ls command does not list them. But you can use the -a option of ls to list hidden files and directories.
As you can see, the hidden files and directories are listed.
You can also use the -A option of ls to list hidden files and directories. The good side of this option is that, it does not list the . (current directory) and .. (parent directory) directories as -a option does. It may be helpful for shell scripting.
If you compare the output with the previous one, you should see that there is no more . and .. directory in the list.
Enable and Disable Colored Output:
You can enable and disable the colored output of the ls command using the –color option. The –color option takes 3 values, never, always and auto.
On almost every modern Linux distribution, the colored output is enabled by default. If you don’t like the colored output of ls, use –color=never with ls.
If for any reason, you don’t have colored output by default, you can use –color=always with ls.
If you only want colored output when the standard output (STDIN) is connected to a terminal, then you can use –color=auto.
For example, let’s disable the colored output of ls,
As you can see, the colored output is disabled.
Long Listing Format of ls:
The ls command has an awesome long listing format. In the long listing format, you can see the file and directory permissions, file size, the owner and primary group of the files or directories, the number of hard links or the depth of the directory, last modification date, and the name of the files and directories.
The -l option of ls is used to enable long listing format.
As you can see, all the information I told you about is displayed nicely.
This is the directory or file permission section. For directories, this section starts with d and for files, it starts with –. Then the first 3 characters are for user permissions, then the next 3 characters are for group permissions, then the last 3 characters are for others permissions. The character r = read, w = write and x = execute permission. – just means no value is set.
This section represents the number of hard links for files and the depth for directories.
You have the owner and primary group of the file or directory respectively here.
Here, you have the total file size in bytes. You can change the unit with other ls options which I will cover in the next section of this article.
Here, you have the last modification date of the file or directory.
Finally, you have the filename or the directory name.
Changing the File Size Unit in Long Listing Format of ls:
In ls long listing format, by default the file size unit used is bytes. But, you can easily change that with the –block-size option of ls.
To set the unit to kilobytes, use –block-size=k
To set the unit to megabytes, use –block-size=M
To set the unit to gigabytes, use –block-size=G
To set the unit to petabytes, use –block-size=P
As you can see, the file sizes are printed in megabyte unit. This will give you incorrect results as the file sizes are rounded.
Printing Human Readable File Sizes in Long Listing Format of ls:
In the earlier section, you saw how specifically defining file size unit gives incorrect results. Luckily, you can use the -h option of ls to print the file sizes in human readable format, that is, the unit will change depending on the actual file size. So, you will get the correct file size output.
As you can see, the required file size units are used. The output is also accurate.
Printing inode Numbers with ls:
You can print the inode number of files and directories with the -i option of ls.
As you can see, the inode number of all the files and directories are printed before the file and directory name.
Printing SELinux Contexts with ls:
You can print the SELinux contexts of files and directories with the -Z option of ls if you have SELinux enabled on your machine.
As you can see, the SELinux contexts are printed.
Getting Help:
For more information on ls, you may check out the man page of ls with the following command:
So, that’s how you use the ls command in Linux. Thanks for reading this article.
Источник