Linux ls command

Source: Internet
Author: User
Tags file permissions

The LS command is one of the most commonly used commands under Linux to query the contents of a directory (list directory contents). This article will describe its basic usage and some typical use cases. I use the test environment for Ubuntu 16.04.

Basic Output

Enter LS directly in the command line and enter. This is the most streamlined usage, because we omit all options and parameters, and the output is the file name and directory name in the current directory:

File names and directory names are sorted alphabetically by alphabetical order, listed as units from left to right. It is important to note that the output at this time does not contain hidden files and hidden directories.
If you want to adjust how the output is sorted, you can use the-r option to reverse the order of the output:

LS -R

The name beginning with W is ranked in the first position. The-r option means that the sorting is reversed, so you can use this option to reverse the sorting results when you sort by other means.

Show hidden files

In Linux with the. directories and files at the beginning of the number (dot) are understood as hidden files. The LS command does not list these files by default unless you specify the-a option. The-a option actually shows all files, including hidden files and directories. In the user's home directory to try the LS-A command, you will find a lot of hidden directories and hidden files it!

In the. It starts with a hidden directory or file.

display more information with long format

Long formats can provide more information, such as the size of files and directories, the last updated time, the owner, and their permissions. Use the-l (lowercase letter l) option to output long-form information:

Each line displays information for a file or directory at this time. The first letter describes the file type:-For general files, D for directories, and L for symbolic links. The next 9 characters describe the file permissions information: For each user, group, and others. R for Read, W for write, X for execution, if no permission is-.

The second column contains the link count. The third and fourth columns show the owner and the owning group respectively.

Column Five is the size of the file displayed in bytes. In Linux commands, where the file size is involved, the default is to display it in bytes. This is not a good readability. So with the-h option! The full name of this option is--human-readable (for people to read). That is, K, M, G and other units to show the size of the file:

Applying the H option while applying the L option will display the file size in the appropriate units, for example, it has been shown in K.

The next three columns are the timestamp of the last modification. The order is displayed in months, days, and years. But careful observation you will find that only a few lines in the year position show the year (2017), the other lines in this position is displayed in a time. Here is a rule: the last modification time is displayed as the year if it exceeds six months, and the time is displayed for less than six months. If you want to get more precise time, you can use the--full-time option at the same time:

The time accuracy displayed is one out of 10,000 seconds, and information about the time zone is displayed.

About timestamps We also need to know the other point, the default time shown here is mtime. What if we need to show atime or CTime? The--time option is used to set the time type of time stamp display:

Compared to the previous picture, when set to Atime, the displayed time information has changed significantly.

wildcard characters

Using wildcards in the LS command sometimes produces some strange results, and then I try to explain how wildcards are used in the LS command.
Use * and? in the LS command. The first thing that happens when you wait for a wildcard is the Shell's expansion!
Bash scans every word in the command line, and if it finds a *,? or [, then the word is considered a pattern.] The pattern is then replaced with a matching alphabetical list of file names. To put it simply, Bash performs pattern matching and replaces it before the LS command executes. Then LS executes the command with the replaced parameter. As an example:

ls test*

If there is a directory named Test in the current directory and a directory named Testsql, then the command looks like this after the Bash replacement is complete:

ls Test testsql

So the above command lists all the files in both directories:

Another example is to execute the following command in the user's home directory:

ls d*

The desktop,documents and Downloads directories are generally present in the user's home directory, so d* will be expanded to "Desktop Documents Downloads", and eventually the LS execution command becomes:

ls Desktop Documents Downloads

The files in these directories will be added to the output of the command.
Well, when you understand Bash's unfolding process, I think you'll be able to explain some of the output that's going to be confusing at first sight!

With regard to wildcards, it is also important to note that the default does not match to hidden files, even if a option is specified:

If you want to match the hidden file, you need to specify it explicitly. Numbers, such as:

LS -d. *

The D option here itself is irrelevant to the hidden file, which we'll cover in more detail next.

The d option is detailed

The D option is described as: displays the directory itself without displaying its contents . So using the D option makes it easy to see the current directory or the information for the specified directory:

If you remove the D option, the contents of these directories are output.

The D option also ensures that the contents of subdirectories are not output when wildcard filtering is used:

This is a more common way to use, plus the D option ensures that the results match our expectations.

We can also filter out all subdirectories under the current directory with the D option:

ls –d */

It looks amazing, actually this is the same as the ls-d my* command we executed earlier, but this time Bash filters the name to the end of the/sign, and the name ending with the/sign is the name of the directory!

Sort

The output of the LS command is ordered alphabetically by default, but only for hidden files. Number does not participate in the collation. In addition to alphabetical order, we can also be sorted by the size of the file or the last modification time.
The S option arranges the results of the output in terms of the size of the file:

The default order is from large to small, and you can use the R option to reverse the order. It is important to note that all directories are the same size, with a total of 4096 bytes.

The T option by default arranges the results of the output as a condition of the last modified time (mtime) of the file:

The default order is the most recent modified file at the top, and you can use the R option to reverse the order. Here you can also change the time type of the long format output (such as Atime or CTime) with the--time option, and the T option will be sorted by the time type you specify.

distinguishing between files and directories

The F option can be used to differentiate between directories and files. By default p and F options behave the same. is to add a slash (/) at the end of the directory:

So in the command we can filter out the file by the slash (/) at the end:

ls -f | grep -v/ls -p | grep -V/

or filter out the directory:

ls -f | grep /ls -p | grep /$
Summary

In addition to the basic usage and common options described in this article, the LS command has many options to provide more functionality. For example I option can display the file inode information and so on. So LS is a seemingly simple but powerful command.

Linux ls command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.