preloader

A Linux command a day (1): ls command

post thumb
Devops
by Admin/ on 13 Jul 2021

A Linux command a day (1): ls command


The ls command is the most commonly used command in Linux. ls is short for list, and by default ls is used to print out a list of the current directory.

If ls specifies another directory, then it will display a list of files and folders in the specified directory. The ls command allows you to view not only the files contained in a Linux folder but also the file permissions (including directory, folder, and file permissions), directory information, and more.

The ls command is used a lot in daily Linux operations!

1. Command format


ls [option] [directory name]

2. Command function


List all the subdirectories and files in the target directory. 3.

3. Common parameters


-a, -all List all the files in the directory, including the implicit files starting with . including the implied files starting with .

-A is the same as -a, but does not list "." (for the current directory) and "..." (for the parent of the current directory).

-c with -lt: sort by ctime and show ctime (when the file status was last changed) with -l: show ctime but sort by name otherwise: sort by ctime

-C List items from top to bottom in each column

-WHEN can be 'never', 'always' or 'auto ' either

-d, -directory Displays directories as files, instead of showing the files under them.

-D, -dired produces results suitable for use in Emacs' dired mode

-f does not sort the output files, the -aU option takes effect and the -lst option fails

-g is similar to -l, but does not list the owner

-G, -no-group does not list any information about groups

-h, -human-readable lists the file size in an easy to understand format (e.g. 1K 234M 2G)

-si is similar to -h, but the file size is taken to the power of 1000 instead of 1024

-H, -dereference-command-line Use the real destination indicated by the symbolic link in the command line

-indicator-style=style Specify that each item name is followed by the indicator <style>: none (default), classify (-F), file-type (-p)

-i, -inode prints out the inode number of each file

-I, -ignore=style Do not print out any items that match the shell's universal character <style

-k i.e. -block-size=1K, indicates the size of the file in k bytes.

-l lists the file permissions, owner, file size, and other information in detail, in addition to the file name.

-L, -dereference When displaying information about files with symbolic links, display information about the object indicated by the symbolic link instead of the symbolic link itself

-m All items are separated by commas and fill the entire line width

-o is similar to -l, showing detailed information about the file except for group information.

-r, -reverse in reverse order

-R, -recursive lists all subdirectory levels simultaneously

-s, -size list all files in block size

-S Sort by file size

-sort=WORD The following WORDs are available 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: Show access time and sort by access time

with -l: show access time but sort by name

otherwise: sort by access time

-U no sorting; list items in the original order of the file system

-v: Sort by version

-w, -width=COLS specify your own screen width instead of using the current value

-x list items line by line instead of column by column

-X Sort by extension

-1 List only one file per line

-help Display this help message and leave

-version Displays version information and leaves

4. Common examples

Example 1

List the details of all files and directories under the /home/codelinkai folder

Command: ls -l -R /home/codelinkai

When you use ls command, you should pay attention to the format of the command: after the command prompt, the keyword of the command is first, followed by the command parameters, and there is a short horizontal line “-” before the command parameters, all the command parameters have specific functions, and you can choose one or more parameters according to your needs.

In the above command ls -l -R /home/codelinkai, ls is the command keyword, -l -R is the parameter, and /home/codelinkai is the parameter. “/home/codelinkai” is the object of the command. In this command, two parameters are used, “l” and “R”, but you can also use them together as follows.

Command: ls -lR /home/codelinkai

The result of this form is exactly the same as the above command. In addition, if the operation object of the command is located in the current directory, you can operate on the operation object directly; if it is not in the current directory then you need to give the full path of the operation object, for example, in the above example, my current folder is the codelinkai folder, and I want to operate on the codelinkai file under the home folder, I can directly type ls -lR codelinkai, or I can use ls -lR /home/codelinkai.

Example 2

To list the details of all the directories starting with “t” in the current directory, you can use the following command.

Command: ls -l t*

You can view the information of all the files in the current directory whose file name starts with “t”. In fact, in the command format, the contents inside the square brackets can be omitted. For the command ls, if you omit the command parameters and operation objects and type “ls” directly, the contents of the current working directory will be listed.

Example 3

List only the subdirectories under the file

Command: ls -F /opt/soft |grep /$

List the subdirectories under the /opt/soft file

Output:

[root@localhost opt]# ls -F /opt/soft |grep /$

jdk1.6.0_16/
subversion-1.6.1/
tomcat6.0.32/

Command: ls -l /opt/soft | grep "^d"

List the details of the subdirectories under the /opt/soft file

Output:

[root@localhost opt]# ls -l /opt/soft | grep "^d"

drwxr-xr-x 10 root root 4096 09-17 18:17 jdk1.6.0_16
drwxr-xr-x 16 1016 1016 4096 10-11 03:25 subversion-1.6.1
drwxr-xr-x 9 root root 4096 2011-11-01 tomcat6.0.32

Example 4

List all the files in the current working directory whose names start with s. The newer the file, the later it is, you can use 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 root 4096 2011-11-01 tomcat6.0.32
-rwxr-xr-x 1 root root 81871260 09-17 18:15 jdk-6u16-linux-x64.bin
drwxr-xr-x 10 root root 4096 09-17 18:17 jdk1.6.0_16
-rw-r--r-- 1 root root 205831281 09-17 18:33 apache-tomcat-6.0.32.tar.gz
-rw-r--r-- 1 root root 5457684 09-21 00:23 tomcat6.0.32.tar.gz
-rw-r--r-- 1 root root 4726179 10-10 11:08 subversion-deps-1.6.1.tar.gz
-rw-r--r-- 1 root root 7501026 10-10 11:08 subversion-1.6.1.tar.gz
drwxr-xr-x 16 1016 1016 4096 10-11 03:25 subversion-1.6.1

Example 5

List all files and directories in the current working directory; add “/” after the directory name, and add “" after the executable file name

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 path of a file 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 8

List the absolute paths of all files (including hidden files) in the current directory, without recursion for directories

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

Output:

[root@localhost opt]# find $PWD -maxdepth 1 | xargs `ls` -ld

drwxr-xr-x 8 root root 4096 10-11 03:43 /opt
drwxr-xr-x 2 root root 4096 2012-03-08 /opt/log
drwxr-xr-x 2 root root 4096 2012-03-08 /opt/script
drwxr-xr-x 5 root root 4096 10-11 03:21 /opt/soft
drwxr-xr-x 2 root root 4096 2012-03-08 /opt/src
drwxr-xr-x 4 root root 4096 10-11 05:22 /opt/svndata
drwxr-xr-x 4 root root 4096 10-09 00:45 /opt/web

Example 9

Recursively list the absolute paths of all files (including hidden files) in the current directory

Command: find $PWD | xargs ls -ld

Example 10

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 2012-10-11 03:25:58.000000000 +0800 subversion-1.6.1

Command: 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 2012-10-11 03:25 subversion-1.6.1

Extensions


Open /etc/bashrc and add the following line:

alias ls="ls --color"

The next time you start bash, you will be able to display a colored list of directories like in Slackware, where the colors mean the following:

  • blue–>directories

  • green–>executable files

  • red–>compressed files

  • light blue–>linked files

  • gray–>other files

Source


Tags:
comments powered by Disqus