Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Linux file and directory management


May 22, 2021 Linux


Table of contents


Linux file and directory management

We know that Linux's directory structure is tree-like, and the top-level directory is root /.

Other directories can add them to the tree by mounting, and they can be removed by unloading them.

Before we begin this tutorial we need to know what an absolute path is with a relative path.

  • Absolute path:
    The path is written by the root / write, for example: /usr/share/doc this directory.
  • Relative path:
    The path is not written by / written, for example by /usr/share/doc to /usr/share/man when it is to be written as: cd .. /man This is how the relative path is written!

Common commands for working with directories

Let's look at a few common commands for working with directories:

  • ls: List the directories
  • cd: Switch directories
  • pwd: Displays the current directory
  • mkdir: Create a new directory
  • rmdir: Delete an empty directory
  • cp: Copy a file or directory
  • rm: Remove the file or directory
  • mv: Move files with directories, file renames

You can use man commands to view usage documentation for individual commands, such as :man cp.

ls (listing directory)

In Linux systems, ls commands are probably the most frequently run.

Grammar:

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

Options and parameters:

  • -a: All files are listed along with the hidden file (files starting with . . . ) (commonly used)
  • -d: List only the directory itself, not the file data in the directory (commonly used)
  • -l: Long string of data listed, including file properties and permissions and other data;

List all files in the home directory (with properties and hidden files)

[root@www ~]# ls -al ~

cd (switch directory)

cd is an abbreviation for Change Directory, which is the command used to transform the working directory.

Grammar:

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

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

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

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

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

The next few more operations should be a good understanding of the cd command.

pwd (shows the current directory)

pwd is an abbreviation for Print Working Directory, which is the command that displays the directory where it is currently located.

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

Grammar:

mkdir [-mp] 目录名称

Options and parameters:

  • -m: Profile permissions! Directly configured without looking at the face of the default permission (umask).
  • -p: Helps you pass back the directory you need (including the next level of directory) directly to create it!

Example: Try creating several new directories at /tmp:

[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 

Add this -p option to help you create your own multi-tiered directory!

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

[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 you do not add -m to force the property to be configured, the default property is used.

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

rmdir (delete empty directory)

Grammar:

 rmdir [-p] 目录名称

Options and parameters:

  • -p : Deleted along with the "empty" directory at the next level

Delete w3cschool.cn directory

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

Example: Remove the directory created in the mkdir sample (under /tmp)!

[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 

With the -p option, you can delete test1/test2/test3/test4 once.

Note, however, that this rmdir can only delete empty directories, and you can use the rm command to delete non-empty directories.

cp (copy file or directory)

cp is the copy of files and directories.

Grammar:

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

Options and parameters:

  • -a : equivalent to -pdr, as far as pdr is referred to as follows;
  • -d: If the source file is a link file property, the link file attribute is copied instead of the file itself;
  • -f: For the meaning of force, if the target file already exists and cannot be opened, remove it and try again;
  • -i: If the target file (destination) already exists, the action is first asked when it is overwritten (commonly used)
  • -l: Create a hard link, not copy the file itself;
  • -p: Copy the past with the properties of the file instead of using the default properties (which are commonly used for backup);
  • -r: Retransmitting continuous replication for directory replication behavior;
  • -s: copy as a symbolic link, i.e. a "shortcut" file;
  • -u: If destination is older than source, upgrade destination!
  • Copy the .bashrc in the home directory to /tmp and rename it bashr as root

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

    Grammar:

     rm [-fir] 文件或目录
    

    Options and parameters:

    • -f: that is, force means ignoring files that do not exist without a warning message;
    • -i: Interactive mode that asks the user if they are acting before deleting it
    • -r: Handed back to delete ah! M ost commonly used in directory deletion! This is a very dangerous option!!!

    Remove the bashrc that you just created in the example of cp!

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

    If you add the -i option, you'll actively ask to avoid deleting the wrong gear name!

    mv (move files and directories, or modify names)

    Grammar:

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

    Options and parameters:

    • -f: Force enforcement means that if the target file already exists, it will not be asked and will be directly overwritten;
    • -i: If the target file (destination) already exists, you'll be asked if it's overwritten!
    • -u: If the target file already exists and the source is newer, it will be upgraded (update)

    Copy a file, create a directory, and move the file to 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 file to a directory, and that's it!

    Change the name of the directory you just had to mvtest2

    [root@www tmp]# mv mvtest mvtest2
    

    View the contents of the Linux file

    The following commands are used in Linux systems to view the contents of the file:

    • Cat starts with the first line displaying the contents of the file
    • tac starts on the last line and you can see that tac is cat's reverse write!
    • nl When displayed, output line number down the road!
    • More a page-by-page display of the contents of the file
    • Less is similar to more, but better than more, he can turn the page forward!
    • Head looks only at the first few lines
    • Tail only looks at the tail a few lines

    You can use man commands to view usage documentation for individual commands, such as :man cp.

    cat

    The contents of the file are displayed from the first line

    Grammar:

    cat [-AbEnTv]
    

    Options and parameters:

    • -A: equivalent to the integration option of -vET, which lists some special characters instead of blanks;
    • -b: List line numbers, only for non-blank lines to do line number display, blank lines do not mark line numbers!
    • -E: Show the broken line bytes at the end;
    • -n: Print the travel number, along with the blank line will also have a line number, unlike the option -b;
    • -T: Show the button of the button with the button of . . . . .
    • -v: Lists some special characters that you can't see

    Look at the contents of the /etc/issue file:

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

    tac

    tac is the opposite of the cat command, and the contents of the file start on the last line and you can see that tac is cat's reverse write! Such as:

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

    nl

    Displays the line number

    Grammar:

    nl [-bnw] 文件
    

    Options and parameters:

    • -b: There are two main ways to specify a line number:
      -b a: indicates that the line number (similar to cat -n) is also listed, whether empty or not;
      -b t: If there is an empty line, do not list the line number (the default) on the empty line;
    • -n: There are three main ways to list line numbers:
      -n ln : the line number is displayed on the far left side of the screen;
      -n rn: The line number is displayed on the far right side of its own column without 0;
      -n rz: the line number is displayed on the far right side of its own column, plus 0;
    • -w: The number of bits occupied by the line number column.

    Example 1: List the contents of /etc/issue with nl

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

    more

    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 operation of this program, you have several keystrokes that you can press:

    • Blank key (space): for turning down a page;
    • Enter: for turning down a line;
    • / String : represents the keyword "string" in this display;
    • :f: immediately show the file name and the number of rows currently displayed;
    • q: The representative leaves more immediately and no longer displays the contents of the file.
    • b or sctrl-b: represents turning the page back, but this action is only useful for files and not for pipelines.

    less

    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
    ....(中间省略)....
    :   <== 这里可以等待你输入命令! 

    The commands that can be entered by the less runtime are:

    • Blank key: turn one page down;
    • pagedown: turn one page down;
    • Pageup: Turn up one page;
    • / String: the function of searching down for "string";
    • ? String: the function of searching up for "string";
    • n: Repeat the previous search (related to /or ?!) )
    • N : Reverse the previous search (related to /or ?!) )
    • q: Leave the less program;

    head

    Remove the first few lines of the file

    Grammar:

    head [-n number] 文件 
    

    Options and parameters:

    • -n: Followed by a number that represents the meaning of a few lines
    [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

    Remove the next few lines of the file

    Grammar:

    tail [-n number] 文件 
    

    Options and parameters:

    • -n: Followed by a number that represents the meaning of a few lines
    • -f: indicates that the file name that is followed by the continuous detection does not end the tail detection until the following button is pressed
    [root@www ~]# tail /etc/man.config
    # 默认的情况中,显示最后的十行!若要显示最后的 20 行,就得要这样:
    [root@www ~]# tail -n 20 /etc/man.config