Explain the combination of LS and dir in Linux system

Time:2021-4-8

Ls or dir command: list the contents of the current directory

The LS (short for list) command lists the contents of the current directory. The direct command is an alias of the LS command and an abbreviation of directory. Usually, the files listed will be displayed in different colors. Different colors represent different file types. The following table lists the corresponding relationship between file types and colors.

Ls also identifies specific types of files with symbols. The following table lists the common identification symbols and their descriptions.

The LS command has the following format:

Ls [options] directory or file name
The meaning of each option is as follows.

-a: List all files and subdirectories in the specified directory (including implied files).

-A: Same as – A, but not listed. And.

-b: When there is a character in the file name that cannot be displayed, the octal number of the character will be displayed.

-c: Sort by the last modification time of the file.

-C: Display in multiple columns.

-d: Displays the directory name instead of the contents under the directory. It is usually used with – L.

-f: Add the symbol after the listed file name to distinguish different types.

-R: Recursively displays files in all levels of subdirectories of a specified directory.

-s: Gives the number of blocks used for each directory entry, including indirect blocks.

-t: Sort by last modified time (new first, old last).

-l: The detailed information of the file is displayed in long format, including the type and permission of the file, the number of links, the file owner, the group to which the file owner belongs, the file size, the latest modification time and the file name. See Figure 6.4 for details.

The contents of the directory are displayed in different formats as follows:

Copy code

The code is as follows:

#Ls / / displays the contents of the directory in abbreviated format

cal_txt      finger_txt   id_txt    newgrp_txt  
suple_txt   who.txt 
finger2_txt  groups_txt   last_txt  suple3_txt 
suple_txt~  w.txt 
finger3_txt  groups_txt2  ln        suple3_txt~  w 

Copy code

The code is as follows:

#LS – L / / displays the contents of the directory in a long format, including permissions
User name, modification time, etc

 
Total 224
-rw-r–r– 1 root root  2163 07-24 13:00 cal_txt          
-rw-r–r– 1 root root   212 07-23 06:37 finger2_txt 
-rw-r–r– 1 root root     0 07-23 06:39 finger3_txt 
-rw-r–r– 1 root root   248 07-23 06:31 finger_txt 
-rw-r–r– 1 root root    35 07-23 04:22 groups_txt 
-rw-r–r– 1 root bin    140 07-23 04:32 groups_txt2 
-rw-r–r– 1 root root   144 07-23 04:11 id_txt 
-rw-r–r– 1 root root  1655 07-22 19:19 last_txt 
drwxr-xr-x 5 root root  4096 07-26 09:42 ln 
-rw-r–r– 1 root bin    143 07-23 04:24 newgrp_txt 
-rw-r–r– 1 root root 25098 07-26 19:15 suple3_txt 
-rw-r–r– 1 root root 24031 07-26 16:00 suple3_txt~ 
-rw-r–r– 1 root root 25198 07-24 20:16 suple_txt 
-rw-r–r– 1 root root 24827 07-24 20:06 suple_txt~ 
-rw-r–r– 2 root root   196 07-23 06:48 w 
-rw-r–r– 1 root root    46 07-23 06:48 who.txt 
-rw-r–r– 2 root root   196 07-23 06:48 w.txt 

Copy code

The code is as follows:

#LS – S / / displays the number of blocks used

 
Total 224
 8 cal_txt       8 groups_txt    8 ln          
32 suple_txt    8 w.txt 
 8 finger2_txt   8 groups_txt2   8 newgrp_txt 
32 suple_txt~ 
 4 finger3_txt   8 id_txt       32 suple3_txt    8 w 
 8 finger_txt    8 last_txt     28 suple3_txt~   8 who.txt
Options can be combined. For example, if you need to list all the contents of the current directory (including those implicit files that start with “.”), and output the file details on the screen in “redundant format”, you can use the option “- Al”. To display all files in the / root directory in redundant format, you can use the following command:

Copy code

The code is as follows:

# ls -al /root

drwxr-x— 18 root root  4096 07-26 16:00 .  
//Current directory
drwxr-xr-x 24 root root  4096 07-25 05:43 ..  
//Parent directory
-rw——-  1 root root   997 07-15 20:58
anaconda- ks.cfg / / normal file
-rw——-  1 root root  2827 07-24 21:32
.bash_ History / / hide files
-rw-r–r–  1 root root    24 2006-07-13
.bash_ Logout / / hide files
…  …
The file name “.” indicates the current directory, and the corresponding column lists the details of the current directory. The file name “..” indicates the upper level directory of the current directory, that is, the parent directory. The corresponding column lists the details of the parent directory. The file with “.” symbol in front of the file name is a hidden file, which will be displayed only when “- a” parameter is used.

Determine the file type with ‘ls’ and’ dir ‘commands
Another way to determine the file type is to display a long list of results with the LS and dir commands.

Use LS – L to determine the type of a file.

When you view file permissions, the first character shows the file type, and the other characters show the file permissions.

Copy code

The code is as follows:

[email protected] ~/Linux-Tricks $ ls -l

total 6908
drwxr-xr-x 2 tecmint tecmint    4096 Sep  9 11:46 BACKUP
-rw-r–r– 1 tecmint tecmint 1075620 Sep  9 11:47 crossroads-stable.tar.gz
-rwxr—– 1 tecmint tecmint 5916085 Sep  9 11:49 EDWARD-MAYA-2011-2012-NEW-REMIX.mp3
-rw-r–r– 1 tecmint tecmint   42122 Sep  9 11:49 Linux-Security-Optimization-Book.gif
-rw-r–r– 1 tecmint tecmint   17627 Sep  9 11:46 master.zip
-rw-r–r– 1 tecmint tecmint       5 Sep  9 11:48 num.txt
-rw-r–r– 1 tecmint tecmint       0 Sep  9 11:46 reggea.xspf
-rw-r–r– 1 tecmint tecmint       5 Sep  9 11:47 tmp-link
Using LS – L to determine block and character files

Copy code

The code is as follows:

[email protected] ~/Linux-Tricks $ ls -l /dev/sda1

brw-rw—- 1 root disk 8, 1 Sep  9 10:53 /dev/sda1

Copy code

The code is as follows:

[email protected] ~/Linux-Tricks $ ls -l /dev/tty1

crw-rw—- 1 root tty 4, 1 Sep  9 10:54 /dev/tty1
Use dir – L to determine the type of a file.

Copy code

The code is as follows:

[email protected] ~/Linux-Tricks $ dir -l

total 6908
drwxr-xr-x 2 tecmint tecmint    4096 Sep  9 11:46 BACKUP
-rw-r–r– 1 tecmint tecmint 1075620 Sep  9 11:47 crossroads-stable.tar.gz
-rwxr—– 1 tecmint tecmint 5916085 Sep  9 11:49 EDWARD-MAYA-2011-2012-NEW-REMIX.mp3
-rw-r–r– 1 tecmint tecmint   42122 Sep  9 11:49 Linux-Security-Optimization-Book.gif
-rw-r–r– 1 tecmint tecmint   17627 Sep  9 11:46 master.zip
-rw-r–r– 1 tecmint tecmint       5 Sep  9 11:48 num.txt
-rw-r–r– 1 tecmint tecmint       0 Sep  9 11:46 reggea.xspf
-rw-r–r– 1 tecmint tecmint       5 Sep  9 11:47 tmp-link