Linux file and directory management


We know that the directory structure of Linux is a tree structure, and the top-level directory is the root directory /.

Other directories can be added to the tree by mounting them, and they can be removed by unmounting them.

Before starting this tutorial, we need to know what absolute paths and relative paths are.

  • Absolute path:
    The path is written starting from the root directory /, for example: /usr/share/doc this directory.

  • Relative path:
    The way to write the path is not to start with /, for example, from /usr/share/doc to /usr/share/man At the bottom, it can be written as: cd ../man This is how to write relative paths!


Common commands for processing directories

Next let’s take a look at a few common commands for processing directories:

  • ls: List the directory

  • cd: Switch directory

  • pwd: Display the current directory

  • mkdir: Create a new directory

  • rmdir: Delete an empty directory

  • cp: Copy a file or directory

  • rm: Remove files or directories

You can use man [command] to view the usage documentation of each command , such as: man cp.

ls (list directory)

In Linux systems, the ls command may be the most commonly run.

Syntax:

[root@www ~]# ls [-aAdfFhilnrRSt] 目录名称
[root@www ~]# ls [--color={never,auto,always}] 目录名称
[root@www ~]# ls [--full-time] 目录名称

Options and parameters:

  • -a: All files, including hidden files (files starting with .) are listed together Come out (commonly used)

  • -d: Only list the directory itself, rather than list the file data in the directory (commonly used)

  • - l: Long data string list, including file attributes, permissions, etc.; (commonly used)

List all files in the home directory (including attributes and hidden files)

[root@www ~]# ls -al ~

cd (Change directory)

cd is the abbreviation of Change Directory. This is the command used to change the working directory.

Syntax:

 cd [相对路径或绝对路径]
#使用 mkdir 命令创建w3cschool.cc目录
[root@www ~]# mkdir w3cschool.cc

#使用绝对路径切换到w3cschool.cc目录
[root@www ~]# cd /root/w3cschool.cc/

#使用相对路径切换到w3cschool.cc目录
[root@www ~]# cd ./w3cschool.cc/

# 表示回到自己的家目录,亦即是 /root 这个目录
[root@www w3cschool.cc]# cd ~

# 表示去到目前的上一级目录,亦即是 /root 的上一级目录的意思;
[root@www ~]# cd ..

You should be able to understand the cd command well after a few more operations.

pwd (Display the current directory)

pwd is the abbreviation of Print Working Directory, which is the command to display the current directory.

[root@www ~]# pwd [-P]
选项与参数:
-P  :显示出确实的路径,而非使用连结 (link) 路径。

范例:单纯显示出目前的工作目录:
[root@www ~]# pwd
/root   <== 显示出目录啦~

范例:显示出实际的工作目录,而非连结档本身的目录名而已
[root@www ~]# cd /var/mail   <==注意,/var/mail是一个连结档
[root@www mail]# pwd
/var/mail         <==列出目前的工作目录
[root@www mail]# pwd -P
/var/spool/mail   <==怎么回事?有没有加 -P 差很多~
[root@www mail]# ls -ld /var/mail
lrwxrwxrwx 1 root root 10 Sep  4 17:54 /var/mail -> spool/mail
# 看到这里应该知道为啥了吧?因为 /var/mail 是连结档,连结到 /var/spool/mail 
# 所以,加上 pwd -P 的选项后,会不以连结档的数据显示,而是显示正确的完整路径啊!

mkdir (Create a new directory)

If you want to create a new directory, use mkdir (make directory).

Syntax:

mkdir [-mp] 目录名称

Options and parameters:

  • -m: Configuration file permissions! Direct configuration, no need to look at the default permissions (umask)~

  • -p: Helps you directly create the required directories (including the upper-level directory) recursively!

Example: Please go to /tmp and try to create several new directories to see:

[root@www ~]# cd /tmp
[root@www tmp]# mkdir test    <==创建一名为 test 的新目录
[root@www tmp]# mkdir test1/test2/test3/test4
mkdir: cannot create directory `test1/test2/test3/test4': 
No such file or directory       <== 没办法直接创建此目录啊!
[root@www tmp]# mkdir -p test1/test2/test3/test4

With the -p option added, you can create multiple layers for you. Table of contents!

Example: Create a directory with permissions rwx--x--x

[root@www tmp]# mkdir -m 711 test2
[root@www tmp]# ls -l
drwxr-xr-x  3 root  root 4096 Jul 18 12:50 test
drwxr-xr-x  3 root  root 4096 Jul 18 12:53 test1
drwx--x--x  2 root  root 4096 Jul 18 12:54 test2

In the permissions section above, if -m is not added to force configuration properties, the system will use the default properties.

If we use -m, as in the above example we give -m 711 to give the new directory drwx--x--x permissions.

rmdir (delete empty directories)

Syntax:

 rmdir [-p] 目录名称

Options and parameters:

  • -p: Also delete the "empty" directory at the upper level

Delete the w3cschool.cc directory

[root@www tmp]# rmdir w3cschool.cc/

Example: the directory created in the mkdir example (under/tmp )Delete it!

[root@www tmp]# ls -l   <==看看有多少目录存在?
drwxr-xr-x  3 root  root 4096 Jul 18 12:50 test
drwxr-xr-x  3 root  root 4096 Jul 18 12:53 test1
drwx--x--x  2 root  root 4096 Jul 18 12:54 test2
[root@www tmp]# rmdir test   <==可直接删除掉,没问题
[root@www tmp]# rmdir test1  <==因为尚有内容,所以无法删除!
rmdir: `test1': Directory not empty
[root@www tmp]# rmdir -p test1/test2/test3/test4
[root@www tmp]# ls -l        <==您看看,底下的输出中test与test1不见了!
drwx--x--x  2 root  root 4096 Jul 18 12:54 test2

Use the -p option to delete test1/test2/test3/test4 immediately.

However, it should be noted that this rmdir can only delete empty directories. You can use the rm command to delete non-empty directories.

cp (copy files or directories)

cp means copying files and directories.

Syntax:

[root@www ~]# cp [-adfilprsu] 来源档(source) 目标档(destination)
[root@www ~]# cp [options] source1 source2 source3 .... directory

Options and parameters:

  • -a: Equivalent to the meaning of -pdr. As for pdr, please refer to the following instructions; (Commonly used )

  • -d : If the source file is an attribute of a link file, copy the link file attributes instead of the file itself;

  • -f: means force. If the target file already exists and cannot be opened, remove it and try again;

  • -i: If the target file (destination) If it already exists, the action will be asked first when overwriting (commonly used)

  • -l: Create a link file for a hard link instead of copying the file itself;

  • -p: Copy the file together with its attributes instead of using the default attributes (commonly used for backup);

  • -r: Recurse continuously Copy, used for directory copy behavior; (commonly used)

  • -s: Copy into a symbolic link file (symbolic link), that is, a "shortcut" file;

  • -u: Only upgrade destination if destination is older than source!

  • Use root identity to copy the .bashrc in the home directory to /tmp and rename it to bashr

    [root@www ~]# cp ~/.bashrc /tmp/bashrc
    [root@www ~]# cp -i ~/.bashrc /tmp/bashrc
    cp: overwrite `/tmp/bashrc'? n  <==n不覆盖,y为覆盖

    rm (remove files or directories)

    Syntax:

     rm [-fir] 文件或目录

    Options and parameters:

    • -f: It means force, ignore files that do not exist, and no warning message will appear;

    • -i: Interactive mode, the user will be asked whether to take action before deletion

    • -r: Recursive deletion! The most commonly used deletion of directories! This is a very dangerous option! ! !


    Delete the bashrc just created in the cp example!

    [root@www tmp]# rm -i bashrc
    rm: remove regular file `bashrc'? y

    If you add the -i option, it will ask you proactively to prevent you from deleting the wrong file name!

    mv (move files and directories, or change names)

    Syntax:

    [root@www ~]# mv [-fiu] source destination
    [root@www ~]# mv [options] source1 source2 source3 .... directory

    Options and parameters:

    • -f : force means force. If the target file already exists, it will be overwritten directly without asking;

    • -i : If the target file (destination) already exists, it will be asked whether to overwrite it!

    • -u: If the target file already exists and the source is relatively new, it will be upgraded (update)

    Copy a file and create a Directory, move files into the directory

    [root@www ~]# cd /tmp
    [root@www tmp]# cp ~/.bashrc bashrc
    [root@www tmp]# mkdir mvtest
    [root@www tmp]# mv bashrc mvtest

    Move a certain file to a certain directory, just do this!

    Rename the directory just now to mvtest2

    [root@www tmp]# mv mvtest mvtest2

    Linux file content view

    Use the following command in the Linux system to view the file content:

    • cat Display the file content from the first line

    • tac Display from the last line, you can see that tac is cat written backwards!

    • nl When displayed, output the line number!

    • more displays the file content page by page

    • less is similar to more, but better than more, he can go forward Turn the page!

    • head Only look at the first few lines

    • tail Only look at the tail lines

    You can Use man [command] to view the usage documentation of each command, such as: man cp.

    cat

    Display the file content starting from the first line

    Syntax:

    cat [-AbEnTv]

    Options and parameters:

    • -A: Equivalent to the integration option of -vET, which can list some special characters instead of just blanks;

    • -b: List line numbers, only for non-blank lines Line numbers are displayed, blank lines are not marked with line numbers!

    • -E: Display the ending line break byte $;

    • -n: Print the line number, together with the blank line There is a line number, which is different from the -b option;

    • -T: Display the [tab] button as ^I;

    • - v: List some invisible special characters

    Check the contents of /etc/issue:

    [root@www ~]# cat /etc/issue
    CentOS release 6.4 (Final)
    Kernel \r on an \m

    tac

    tac and The cat command is just the opposite. The file content is displayed from the last line. It can be seen that tac is cat written backwards! For example:

    [root@www ~]# tac /etc/issue
    
    Kernel \r on an \m
    CentOS release 6.4 (Final)

    nl

    Display line number

    Syntax:

    nl [-bnw] 文件

    Options and parameters:

    • -b: Specify the way to specify the line number. There are two main ways:
      -b a: Indicates that no matter whether it is a blank line or not, the line number will also be listed (similar to cat -n);
      -b t: If there is a blank line, do not list the line number on the empty line (default value);

    • -n: List the line number representation methods, there are three main ways:
      -n ln: The line number is displayed at the far left of the screen;
      -n rn: The line number is displayed at the far right of its own column without adding 0;
      -n rz: The line number is displayed at the far right of its own field, and 0 is added;

    • #-w: The number of digits occupied in the line number field.

    Example 1: Use nl to list the contents of /etc/issue

    [root@www ~]# nl /etc/issue
         1  CentOS release 6.4 (Final)
         2  Kernel \r on an \m

    more

    Flip page by page

    [root@www ~]# more /etc/man.config
    #
    # Generated automatically from man.conf.in by the
    # configure script.
    #
    # man.conf from man-1.6d
    ....(中间省略)....
    --More--(28%)  <== 重点在这一行喔!你的光标也会在这里等待你的命令

    During the running of more this program, you have several keys that you can press:

    • Space key (space): represents turning down a page;

    • Enter : Represents scrolling down "one line";

    • / string : means searching for the keyword "string" in the displayed content;

    • :f : display immediately File name and the number of lines currently displayed;

    • ##q : means leaving more immediately and no longer displaying the file content.

    • b or [ctrl]-b: means turning pages back, but this action is only useful for files, not pipelines.

    less

    Turning page by page, the following example outputs the contents of the /etc/man.config file:

    [root@www ~]# less /etc/man.config
    #
    # Generated automatically from man.conf.in by the
    # configure script.
    #
    # man.conf from man-1.6d
    ....(中间省略)....
    :   <== 这里可以等待你输入命令!

    less can be entered when running The commands are:

    • Space key: Scroll down one page;

    • [pagedown]: Scroll down one page;

    • [pageup]: Scroll up a page;

    • /string: Search downward for "string";

    • ?String : The function of searching upward for "string";

    • n : Repeat the previous search (related to / or ?!)

    • N : Repeat the previous search in reverse (related to / or ?!)

    • q : Leave the less program;

    head

    Get the first few lines of the file

    Syntax:

    head [-n number] 文件

    Options and parameters:

    • -n : followed by a number, indicating how many lines to display

    • [root@www ~]# head /etc/man.config
    By default, the first 10 lines are displayed! To display the first 20 lines, you have to do this:

    [root@www ~]# head -n 20 /etc/man.config

    tail

    Get the last few lines of the file

    Syntax:

    tail [-n number] 文件

    Options and parameters:

    • #-n: followed by a number, indicating how many lines to display

    • -f: indicating continuous detection of the following file name , you have to wait until [ctrl]-c is pressed to end the tail detection

    • [root@www ~]# tail /etc/man.config
      # 默认的情况中,显示最后的十行!若要显示最后的 20 行,就得要这样:
      [root@www ~]# tail -n 20 /etc/man.config