Use the ls command in Linux

Source: Internet
Author: User
Tags sorted by name
The ls command is one of the most common commands in linux. the dir commands in ls and dos are used to list files in directories. let's take a look at the usage of ls.

Full name: List indicates the meaning of a List. when we learn something, we need to know why, when you know what this thing is, your mind will think of a lot of things to learn quickly.
 
1. ls-a lists all files under the file, including "Hidden files starting. .. indicates that a parent directory exists ).
2. ls-l lists detailed information about a file, such as the creator, creation time, and read/write permission list of the file.
3. add a character at the end of each file to ls-F to indicate the type of the file. "@" Indicates symbolic link, "|" indicates operating OS, "/" indicates directory, and "=" indicates socket.
4. ls-s prints the file size after each file. Size)
5. ls-t sort files by Time (Time)
6. ls-A lists all files except ".." and.
7. ls-R lists all the sub-directory files in the directory, which is equivalent to the "recursive" implementation in our programming.
8. ls-L lists the File link names. Link)
9. ls-S sort by file size

The above are some usage of the ls command. Of course there are still a lot of examples that are not listed. you can use it as a man. Help to learn English
Ls can combine the pipeline operator "|" to perform complex operations. For example: ls | less is used to implement the paging of the file list, ls
 
Calculate the number of files in the current directory
Ls-| * | grep "^-" | wc-|
 
Finally, let's talk about the meanings of some file colors in linux.
Green ----> indicates the executable file. (green indicates the pass .)
Red ----> indicates the compressed file
Dark blue ----> represents the Directory
Light blue -----> indicates a linked file
Gray ----> Other files

The ls command is the most commonly used command in linux. Is the ls command short for list? By default, is ls used to print the list of the current directory? If ls specifies another directory? The list of files and folders in the specified directory is displayed. Can I use the ls command to view not only files contained in linux folders? What is the permission to view files (including directories, folders, and files )? View directory information. Ls commands are widely used in daily linux operations!

1. command format:

Ls [option] [directory name]

2. command functions:

List all subdirectories and files in the target directory.

3. common parameters:

-A,-all: list all files in the directory, including hidden files starting.
-A is the same as-a, but does not list "." (indicating the current directory) and "." (indicating the parent directory of the current directory ).
-C combination-lt: ctime-based sorting and ctime-based display (time when the last File state is changed) combination-l: ctime is displayed, but ctime is sorted by name. otherwise: ctime-based sorting
-C. list projects from top to bottom in each column
-Color [= WHEN]: determines whether to use color to distinguish files. WHEN can be either 'Never ', 'Always', or 'auto '.
-D,-directory displays the directory like a file, instead of the file under it.
-D and-dired generate results suitable for the dired mode of Emacs
-F does not sort the output files. the-aU option takes effect and the-lst option is invalid.
-G is similar to-l, but the owner is not listed.
-G,-no-group does not list any information about the group.
-H,-human-readable list the file size in an easy-to-understand format (for example, 1 K 234 M 2G)
-Si is similar to-h, but the file size is equal to the power of 1000 instead of 1024.
-H,-dereference-command-line use the symbolic link in the command column to indicate the real destination
-Indicator-style = specifies that the indicator is added after each project name. <方式> : None (default), classify (-F), file-type (-p)
-I,-inode: print the inode number of each file
-I,-ignore = the style does not print any shell-compliant universal characters <样式> Project
-K is-block-size = 1 K, which indicates the file size in the form of k bytes.
-L in addition to the file name, the file permission, owner, file size, and other information are listed in detail.
-L,-dereference: when the file information of the symbolic link is displayed, the object indicated by the symbolic link is displayed, not the information of the symbolic link itself.
-M all items are separated by commas (,) and the whole row width is filled.
-O is similar to-l, and displays details of files except group information.
-R,-reverse in reverse order
-R,-recursive lists all sub-directory layers at the same time

-S and-size are used to list the size of all objects in the unit of block size.
-S sort by file size
-Sort = Below is an optional WORD and the corresponding options they represent:
Extension-X status-c
None-U time-t
Size-S atime-u
Time-t access-u
Version-v use-u
-T sort by file modification time
-U with-lt: Display access time and sort by access time
Combined with-l: Display the access time, but sort by name
Otherwise: Sort by access time
-U is not sorted; items are listed in the original order of the file system.
-V sort by version
-W,-width = COLS: specify the screen width without using the current value.
-X: list projects row by row instead of column.
-X sort by extension
-1. only one file is listed in each row.
-Help: displays the help information and leaves.
-Version: displays the version information and leaves.

4. common examples:

Example 1: list all the files and directories in the/home/peidachang folder.

Command: ls-l-R/home/peidachang
When using the ls command, pay attention to the command format: after the command prompt, the first is the command keyword, the next is the command parameter, there must be a short line "-" before the command parameter, all command parameters have specific functions. you can select one or more parameters as needed. the command parameters are followed by the command operation objects. In the preceding command "ls-l-R/home/peidachang", "ls" is the command keyword and "-l-R" is the parameter, "/home/peidachang" is the operation object of the command. In this command, two parameters are used: "l" and "R". of course, you can also use them together, as shown below:
Command: ls-lR/home/peidachang
The execution results in this form are exactly the same as those in the preceding command form. In addition, if the operation object of the command is located in the current directory, you can perform operations on the operation object directly. if the operation object is not in the current directory, you must provide the complete path of the operation object. for example, in the preceding example, my current folder is the peidachang folder. to operate the peidachang file in the home folder, I can directly enter ls-lR peidachang or ls-lR/home/peidachang.

Example 2: list the details of all directories starting with "t" in the current directory. run the following command:

Command: ls-l t *
You can view the information of all files whose names start with "t" in the current directory. In fact, in the command format, the content in square brackets can be omitted. for the command ls, if the command parameters and Operation objects are omitted, enter "ls" directly ", the contents of the current working directory are listed.

Example 3: Only list subdirectories under a file

Command: ls-F/opt/soft | grep/$
List subdirectories under the/opt/soft file
Output:


Copy codeThe code is as follows:
[Root @ localhost opt] # ls-F/opt/soft | grep/$
Jdk1.6.0 _ 16/
Subscription-1.6.1/
Tomcat6.0.32/

Command: ls-l/opt/soft | grep "^ d"
List the sub-directories under the/opt/soft file
Output:


Copy codeThe code is as follows:
[Root @ localhost opt] # ls-l/opt/soft | grep "^ d"
Drwxr-xr-x 10 root 4096 09-17 jdk1.6.0 _ 16
Drwxr-xr-x 16 1016 1016 4096 10-11 subversion-1.6.1
Drwxr-xr-x 9 root 4096 2011-11-01 tomctomcat6.0.32

Example 4: list all files whose names start with "s" in the current working directory. to restore the files, run the following command:

Command: ls-ltr s *
Output:
[Root @ localhost opt] # ls-ltr s *
Src:
Total 0

Script:
Total 0

Soft:
Total 350644
Drwxr-xr-x 9 root 4096 2011-11-01 tomctomcat6.0.32
-Rwxr-xr-x 1 root 81871260 09-17 jdk-6u16-linux-x64.bin
Drwxr-xr-x 10 root 4096 09-17 jdk1.6.0 _ 16
-Rw-r -- 1 root 205831281 09-17 apache-tomcat-6.0.32.tar.gz
-Rw-r -- 1 root 5457684 09-21 00:23 tomcat6.0.32.tar.gz
-Rw-r -- 1 root 4726179 10-10 subversion-deps-1.6.1.tar.gz
-Rw-r -- 1 root 7501026 10-10 subversion-1.6.1.tar.gz
Drwxr-xr-x 16 1016 1016 4096 10-11 subversion-1.6.1

Example 5: list all files and directories under the current working directory. add "/" After the directory name, and add "*" after the executable file "*"

Command: ls-AF
Output:
[Root @ localhost opt] # ls-AF
Log/script/soft/src/svndata/web/
Example 6: calculate the number of files and directories in the current directory
Command:
Ls-l * | grep "^-" | wc-l --- number of files
Ls-l * | grep "^ d" | wc-l --- Number of directories
Example 7: list the absolute paths of objects in ls
Command: ls | sed "s: ^: 'pwd '/:"
Output:
[Root @ localhost opt] # ls | sed "s: ^: 'pwd '/:"
/Opt/log
/Opt/script
/Opt/soft
/Opt/src
/Opt/svndata
/Opt/web

Example 9: list the absolute paths of all files (including hidden files) in the current directory without recursion.

Command: find $ PWD-maxdepth 1 | xargs ls-ld

Output:


Copy codeThe code is as follows:
[Root @ localhost opt] # find $ PWD-maxdepth 1 | xargs ls-ld
Drwxr-xr-x 8 root 4096 10-11/opt
Drwxr-xr-x 2 root 4096/opt/log
Drwxr-xr-x 2 root 4096 2012-03-08/opt/script
Drwxr-xr-x 5 root 4096 10-11/opt/soft
Drwxr-xr-x 2 root 4096/opt/src
Drwxr-xr-x 4 root 4096 10-11/opt/svndata
Drwxr-xr-x 4 root 4096 10-09/opt/web

Example 10: recursively list the absolute paths of all files (including hidden files) in the current directory
Command: find $ PWD | xargs ls-ld

Example 11: specify the file Time output format
Command:
Ls-tl -- time-style = full-iso
Output:
[Root @ localhost soft] # ls-tl -- time-style = full-iso
Total 350644
Drwxr-xr-x 16 1016 1016 4096 03:25:58. 000000000 + 0800 subversion-1.6.1

Ls-ctl -- time-style = long-iso
Output:
[Root @ localhost soft] # ls-ctl -- time-style = long-iso
Total 350644
Drwxr-xr-x 16 1016 1016 4096 subversion-1.6.1

Extension:
1. display the color directory list
Open/etc/bashrc and add the following line:
Alias ls = "ls -- color"
The color directory list will be displayed as shown in Slackware when bash is started next time. the color meanings are as follows:
1. blue --> Directory
2. green --> executable file
3. red --> compressed file
4. light blue --> link file
5. Gray --> Other files

Related Article

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.