Linux Command (1): ls command, linux Command ls

Source: Internet
Author: User
Tags sorted by name

Linux Command (1): ls command, linux Command ls

The ls command is the most commonly used command in linux.

The ls command is short for list. By default, ls is used to print the list of the current directory. If ls specifies other directories, the list of files and folders in the specified directory is displayed.

By using the ls command, you can view not only the files contained in linux folders, but also the file permissions (including the directory, folder, and file permissions) to view the 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 =: add the indicator after each project name <mode>: none (default), classify (-F), file-type (-p)

-I,-inode: print the inode number of each file

-I,-ignore = the style does not print any items that match the shell universal character <style>

-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:

[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:

[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:

[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.