Summary of commonly used Linux directory processing commands

December 28, 2015 1431 point heat 0 likes 0 comments

command prompt

[ root@localhost  ~]#

Root: current login user
Localhost: hostname
~: The current directory, here is the "home" directory
#: Prompt of root superuser, or ordinary user$

Command format

Command [Options] [Parameters]

Bracket [] indicates optional

Query the contents of the directory: ls

Ls [option] [file or directory]

Options:

-A: Display all files, including hidden files
-L: Display details
-D: View directory properties
-H: User friendly display of file size
-I: Display inode

According to the above options, type the command, and the results are shown as follows:

 [ root@localhost  ~] # ls anaconda-ks.cfg test [ root@localhost  ~] # ls -a .  ..   anaconda-ks.cfg  .bash_ history .bash_ logout .bash_profile  .bashrc  .cache  .config  .cshrc  .tcshrc test [ root@localhost  ~] # ls -l Total consumption 4 -rw-------.  1 root root 2752 Nov 10 02:51 anaconda-ks.cfg drwxr-xr-x. 2 root root    6 Nov 12 19:26 test [ root@localhost  ~] # ls -l anaconda-ks.cfg -rw-------.  1 root root 2752 Nov 10 02:51 anaconda-ks.cfg [ root@localhost  ~] # ls -ld test/ drwxr-xr-x. 2 root root 6 Nov 12 19:26 test / [ root@localhost  ~] # ls -lh Total consumption 4.0K -rw-------.  1 root root 2.7K Nov 10 02:51 anaconda-ks.cfg drwxr-xr-x. 2 root root    6 Nov 12 19:26 test [ root@localhost  ~] # ls -i 71259104 anaconda-ks.cfg  36099565 test

Please observe the difference between the results of ls - l and ls - lh commands

Here we need to explain:

 -rw------- . 1 root  root two .7K  Nov 10 02 :51  anaconda-ks .cfg
 drwxr-xr-x . 2 root  root six Nov 12 19 :26  test

First, the first symbol "-" (- in quotation marks) indicates the file type (there are three commonly used symbols, that is, - indicates file, d indicates directory, and l indicates soft link file). In addition, there are less commonly used symbols, that is, block device file, character device file, socket file, and management file.

In the above, we can see that anaconda-ks.cfg is a file, and test is a directory (which can be understood as the concept of a Windows folder).

Next, remove the first symbol, let's see rw------- , there are nine characters in total, which need to be divided into three groups, namely rw- , --- , --- Each group represents the permissions of u owner, g group and o others in order. In the above, they are root and root respectively. That is, the first root indicates that the owner's permission is root permission, and the second root indicates that the group's permission is also root permission. For others, there is no so-called permission.

Among them, r Means readable, w Means writable, x Indicates the permission to execute.

For better understanding, here is a table for anaconda-ks.cfg:

First three characters Middle three characters Last three characters
rw-
Permissions of owner u Permissions of group g O Permissions of others
Readable and writable No permission No permission

Then, for the test file rwxr-xr-x , please judge its permissions.

The dot "." after nine characters indicates the ACL permission, and the number 1 after it indicates the reference count. For example, if a file has a soft link (similar to a Windows shortcut), its reference count is 2.

The 2.7k after root indicates the size of the file, followed by the date, and finally the name of the file.

Directory processing commands

Create directory: mkdir

Mkdir - p [directory name]

-P: Recursive creation

 [ root@localhost  ~] # ls anaconda-ks.cfg  test [ root@localhost  ~] # mkdir otherFolder [ root@localhost  ~] # ls anaconda-ks.cfg  otherFolder  test [ root@localhost  ~] # mkdir folder_2/test_2
 mkdir : Unable to create directory "folder_2/test_2" : No such file or directory [ root@localhost  ~] # mkdir -p folder_2/test_2 [ root@localhost  ~] # ls anaconda-ks.cfg  folder_2  otherFolder  test [ root@localhost  ~] # ls folder_2/ test_2

As shown above, when mkdir does not select - p, an empty directory can be created, but a directory containing subdirectories cannot be recursively created. Add - p to create recursively.

Switch directory: cd

Cd [directory]

Operation:

  • Cd~: Enter the current user's home directory
  • Cd -: Enter the last directory
  • Cd..: Enter the upper level directory
  • Cd: return home directory
 [ root@localhost  ~] # ls anaconda-ks.cfg  folder_2  otherFolder  test [ root@localhost  ~] # cd /folder_2/test_2 [ root@localhost  test_2] # cd [ root@localhost  ~] # cd - /root/folder_2/test_2 [ root@localhost  test_2] # cd ../../ otherFolder [ root@localhost  otherFolder] # cd .. [ root@localhost  ~] #

Pay attention to clarify the concept: relative path and absolute path

Absolute path: find from the root directory level by level, and write the full path

 [ root@localhost  ~] # cd folder_2/test_2
 [ root@localhost  test_2] #

Relative path: search by referencing the current directory

 [ root@localhost  test_2] # cd ../../ otherFolder
 [ root@localhost  otherFolder] #

Query directory location: pwd

pwd

It can be said that it is the simplest command to query the location of the directory

 [ root@localhost  ~] # pwd /root [ root@localhost  ~] # ls anaconda-ks.cfg  folder_2  otherFolder  test [ root@localhost  ~] # cd folder_2/ [ root@localhost  folder_2] # ls test_2 [ root@localhost  folder_2] # cd test_2/ [ root@localhost  test_2] # pwd /root/folder_2/test_2

Delete empty directory: rmdir

Rmdir [directory name]

Only empty directories can be deleted. This command is rarely used.

 [ root@localhost  ~] # ls anaconda-ks.cfg  folder_2  otherFolder  test [ root@localhost  ~] # rmdir otherFolder [ root@localhost  ~] # ls anaconda-ks.cfg  folder_2  test [ root@localhost  ~] # rmdir folder_2
 rmdir : Delete "folder_2" Failed: directory is not empty [ root@localhost  ~] #

Delete file or directory: rm

Rm - rf [file or directory]

R indicates that files and directories can be deleted at the same time, and f indicates forced deletion

  • If you do not add any options, you can only delete the file. When deleting, you will be prompted to confirm the deletion
  • If you only add the option - r, you can delete files or directories. When deleting, you will be prompted to confirm the deletion
  • If the option - rf is added, no prompt will be given to delete files or directories
 [ root@localhost  ~] # ls abc.txt  anaconda-ks.cfg  folder_2  test [ root@localhost  ~] # rm abc.txt Rm: whether to delete ordinary empty files "abc.txt"  y [ root@localhost  ~] # rm test Rm: cannot be deleted "test" : is a directory [ root@localhost  ~] # rm -r test Rm: whether to delete the directory "test"  y [ root@localhost  ~] # ls anaconda-ks.cfg  folder_2 [ root@localhost  ~] # rm -rf folder_2 [ root@localhost  ~] # ls anaconda-ks.cfg [ root@localhost  ~] #

Copy command: cp

Cp [option] [original file or directory] [target directory]

Options:

-R: Copy directory
-P: Copy file attributes at the same time
-D: If the source file is a linked file, copy the link attribute
-A: All the above options are included, equivalent to - rpd

Adding a file name after [Target Directory] means renaming and copying.

 [ root@localhost  ~] # ls anaconda-ks.cfg  bbc.txt  folder_a  folder_b [ root@localhost  ~] # cp bbc.txt folder_a [ root@localhost  ~] # ls folder_a/ bbc.txt [ root@localhost  ~] # cp folder_a folder_b Cp: Skip directory "folder_a" [ root@localhost  ~] # cp -r folder_a folder_b [ root@localhost  ~] # ls folder_b folder_a  test_1 [ root@localhost  ~] # ll Total consumption four -rw-------. one root root two thousand seven hundred and fifty-two Nov ten  02 : fifty-one anaconda-ks.cfg -rw-r--r--. one root root zero Nov thirteen  seventeen : twenty-one bbc.txt drwxr-xr-x. two root root twenty Nov thirteen  seventeen : thirty-eight folder_a drwxr-xr-x. four root root thirty-four Nov thirteen  seventeen : thirty-nine folder_b [ root@localhost  ~] # ll folder_a Total consumption zero -rw-r--r--. one root root zero Nov thirteen  seventeen : thirty-eight bbc.txt [ root@localhost  ~] # cp -a bbc.txt folder_b [ root@localhost  ~] # ll folder_b Total consumption zero -rw-r--r--. one root root zero Nov thirteen  seventeen : twenty-one bbc.txt drwxr-xr-x. two root root twenty Nov thirteen  seventeen : thirty-nine folder_a drwxr-xr-x. two root root six Nov thirteen  seventeen : thirty-eight test_1 [ root@localhost  ~] #

It needs to be explained here that in the original file bbc.txt, the modification time is 17:21. Under normal copying, its time attribute will not be copied. We can see that the time of the copied bbc.txt is 17:38. If it needs to be copied with the attribute, add - pd or - a directly. As shown above, we copy bbc.txt to folder_b, When we view the attribute, the time attribute is consistent with the original attribute.

In the above command, ll is the abbreviation of ls - l.

Cut or Rename Command: mv

Mv [original file or directory] [target directory]

  • If the original file or directory is in the same directory as the target directory, it is renamed
  • If they are not in the same directory, they are cut

Understand through the following practices:

 [ root@localhost  ~] # ls anaconda-ks.cfg  bbc.txt [ root@localhost  ~] # mv bbc.txt abc.txt [ root@localhost  ~] # ls abc.txt  anaconda-ks.cfg [ root@localhost  ~] # mkdir test [ root@localhost  ~] # ls abc.txt  anaconda-ks.cfg  test [ root@localhost  ~] # mv abc.txt test/ [ root@localhost  ~] # ls anaconda-ks.cfg  test [ root@localhost  ~] # ls test/ abc.txt [ root@localhost  ~] #

Link command: ln

Ln - s [original file] [target file]

Generate linked file
-S: Create a soft connection

Characteristics of hard link:

  • Having the same i node and storage block block can be regarded as the same file
  • It can be identified by node i, which is the same
  • Cannot span partitions
  • Cannot be used for directory

Through the above command, it can be understood as adding a label to a certain content. You can access the content by opening the label. Hard linking, that is, generating another label, can also access the content through the label.

If the content is modified, it will be modified no matter which file is hard linked.

Features of soft links:

  • Windows like shortcuts
  • The soft link has its own i node and block block, but the data block only stores the file name and I node number of the original file, and has no actual file data
  • lrwxrwxrwx L is a soft link (the permissions of soft links are rwxrwxrwx, which is only the permissions of soft links themselves)
  • Modify any file and change the other
  • Delete the original file, and the soft link cannot be used (the same as the shortcut of Windows)

Hard link:

 [ root@localhost  ~] # ls anaconda-ks.cfg [ root@localhost  ~] # mkdir folder [ root@localhost  ~] # ls anaconda-ks.cfg  folder [ root@localhost  ~] # touch bbb.txt [ root@localhost  ~] # ls anaconda-ks.cfg  bbb.txt  folder [ root@localhost  ~] # ln bbb.txt folder/ccc.txt [ root@localhost  ~] # ll folder/ Total consumption zero -rw-r--r--. two root root zero Nov thirteen  eighteen : 08 ccc.txt [ root@localhost  ~] # ll bbb.txt -rw-r--r--. two root root zero Nov thirteen  eighteen : 08 bbb.txt

Soft link:

 [ root@localhost  ~] # mkdir folder_b [ root@localhost  ~] # ln -s bbb.txt folder_b/eee.txt [ root@localhost  ~] # ll Total consumption four -rw-------. one root root two thousand seven hundred and fifty-two Nov ten  02 : fifty-one anaconda-ks.cfg -rw-r--r--. two root root zero Nov thirteen  eighteen : ten bbb.txt drwxr-xr-x. two root root twenty Nov thirteen  eighteen : 09 folder drwxr-xr-x. two root root twenty Nov thirteen  eighteen : eleven folder_b [ root@localhost  ~] # ll folder_b Total consumption zero lrwxrwxrwx. one root root seven Nov thirteen  eighteen : eleven eee.txt -> bbb.txt [ root@localhost  ~] # rm -rf bbb.txt [ root@localhost  ~] # ll folder_b Total consumption zero lrwxrwxrwx. one root root seven Nov thirteen  eighteen : eleven eee.txt -> bbb.txt

The original file is deleted, and the arrow target of the soft link is red flashing, indicating that the target file cannot be found.

Common directory function

 [ root@localhost  ~] # ls / bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  temp  tmp  usr var

explain:

  • /Root directory
  • /Bin command save directory (common user permissions)
  • /The sbin command saves the directory (root permission)
  • /Boot startup directory, including startup related files, related to startup
  • /Dev Device File Save Directory
  • /ETC configuration file saving directory
  • /Home ordinary user home directory
  • /Save directory of lib system library
  • /Mnt system mount directory
  • /Media mount directory (usually used for CD mount)
  • /Root superuser home directory
  • /Tmp temporary directory
  • /Proc writes directly to memory
  • /Sys directly writes to memory
  • /Usr system software resource directory
  • /Related documents of var system

Gcod

If life is just like the first sight, what is the sad autumn wind painting fan

Article comments