Latest web development tutorials

Linux file and directory management

We know that the directory structure of Linux as a tree structure, the top of the root directory /.

You can mount other directories by adding them to the tree, you can remove them by unmounting.

Before starting this tutorial we need to know what is the absolute path and relative path.

  • Absolute path:
    Writing path from the root directory / write from, for example: / usr / share / doc directory.
  • relative path:
    Writing path, not by / write from, for example / usr / share / doc to go to / usr / share / man underneath, it can be written as: cd ../man This is the relative path written it!

Common command processing directory

Next we will look at several common command processing directory it:

  • ls: List directory
  • cd: Change directory
  • pwd: Displays the current directory
  • mkdir: create a new directory
  • rmdir: delete an empty directory
  • cp: copy files or directories
  • rm: Remove files or directories

You can use the man [command] to view the document using the individual commands, such as: man cp.

ls (list directory)

Among the Linux system, ls command is probably the most common being run.

grammar:

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

Options and parameters:

  • -a: all documents, together with the hidden file (. at the beginning of the file) are listed together to (common)
  • -d: list only the directory itself, instead of listing the data files within the directory (common)
  • -l: long serial data out, include file attributes and permissions, and so data; (common)

All the files listed under the home directory (including hidden files and attributes)

[root@www ~]# ls -al ~

cd (change directory)

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

grammar:

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

The next few times you should be able to operate more than a good understanding of the cd command.

pwd (display the directory currently resides)

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

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

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

grammar:

mkdir [-mp] 目录名称

Options and parameters:

  • -m: rights profile Oh! Direct configuration, you need to see the default permissions (umask) face ~
  • -p: to help you directly to the desired directory (including the parent directory) recursively create it!

Example: Go to / tmp under several attempts to create a new directory 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

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

Example: Creating permissions rwx - x - x directory

[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

Permissions section above, if you did not add -m to force configuration properties, the system uses the default attributes.

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

rmdir (remove empty directories)

grammar:

 rmdir [-p] 目录名称

Options and parameters:

  • -p: together on an "empty" directory is also deleted together

Remove w3cschool.cc directory

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

Example: mkdir directory will be created in the example (/ tmp under) removed!

[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, it can be immediately test1 / test2 / test3 / test4 delete.

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

cp (copy a file or directory)

That cp copy files and directories.

grammar:

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

Options and parameters:

  • -a: -pdr equivalent means, as pdr refer to the following instructions; (common)
  • -d: If the source file for the property profile link (link file), then copy the link file attribute rather than the file itself;
  • -f: to force (force) meaning, if the target file already exists and can not be opened, remove and then try again;
  • -i: If the target file (destination) already exists, when coverage will be asked and operates the (common)
  • -l: Hard-link (hard link) to create a link file, not copy the file itself;
  • -p: together with the attributes of the file copied together in the past, instead of using the default properties (backup commonly used);
  • -r: recursive continuous replication, copying directories for; (common)
  • -s: Copy files become symbolic link (symbolic link), namely "shortcut" files;
  • -u: If the destination before upgrading older than the source destination!
  • As root, copy .bashrc under the home directory to / tmp, and renamed 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)

    grammar:

     rm [-fir] 文件或目录
    

    Options and parameters:

    • -f: force is meant to ignore file does not exist, no warning message;
    • -i: interactive mode, the user will be asked whether to delete before the operation
    • -r: recursive delete ah! The most commonly used in the directory deleted! This is a very dangerous option! ! !

    The bashrc just created in the example of cp deleted!

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

    If we add -i option will take the initiative to ask Oh, you avoid deleting the wrong file name!

    mv (move files and directories, or modify the name)

    grammar:

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

    Options and parameters:

    • -f: force mandatory sense, if the target file already exists, will not be asked anything and coverage;
    • -i: If the destination file (destination) already exists, it will ask whether to overwrite!
    • -u: If the destination file already exists, and the source is relatively new, only upgrade (update)

    Copy a file, create a directory, move the file to a directory

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

    Will move a file to a directory to go, it is to do so!

    The directory name just renamed mvtest2

    [root@www tmp]# mv mvtest mvtest2
    

    Linux file content view

    Linux systems use the following command to view the contents of the file:

    • cat from the first line displays the file contents
    • tac displayed from the last row, you can see the tac is cat written backwards!
    • Nl time display, take the opportunity to output line numbers!
    • more display file contents page by page
    • Similar less with more, but better than more, he can forward flip!
    • Look at the first few lines of head
    • Look at the tail tail lines

    You can use the man [command] to view the document using the individual commands, such as: man cp.

    cat

    From the first line displays the file contents

    grammar:

    cat [-AbEnTv]
    

    Options and parameters:

    • -A: The equivalent -vET integration option which lists some special characters instead of just a blank;
    • -b: Lists the line number, line number only done for non-blank line display, blank lines are not marked line number!
    • -E: The end of the line break bytes $ displayed;
    • -n: Print the line number, along with a blank line will have line numbers, and -b options are different;
    • -T: The [tab] ^ I button to be displayed;
    • -v: list some do not see the special characters

    See to / etc / issue the contents of this file:

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

    tac

    tac with the cat command the contrary, began to show the contents of the file from the last line, you can see the tac is cat written backwards! Such as:

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

    nl

    Show Line Numbers

    grammar:

    nl [-bnw] 文件
    

    Options and parameters:

    • -b: Specifies the line number in the manner specified, there are two:
      -ba: indicates whether or not a blank line, also lists the line number (similar to cat -n);
      -bt: If there are empty lines, empty row do not list the line number (default);
    • -n: Lists the line number representation method, there are three:
      -n ln: line number in the far left of the screen display;
      -n rn: line number in their own field in the far right of the display, and does not add 0;
      -n rz: line number in their own field in the far right of the display, and add 0;
    • -w: line number field of the occupied places.

    Example 1: The lists nl / etc / issue content

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

    more

    Flip from page to 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%)  <== 重点在这一行喔!你的光标也会在这里等待你的命令
    

    In the process of running more this program, you have to press a few keys:

    • Spacebar (space): on behalf of a down turn;
    • Enter: representatives turned down "line";
    • / String: This display represents the contents of which, down the search for "string" keyword;
    • : F: shows the file name and the number of rows currently displayed at once;
    • q: Representative leave more immediately, no longer displays the contents of the file.
    • b or [ctrl] -b: representatives back flip, but this action only useful for documents on line useless.

    less

    Flip from page to page, the content of the following examples /etc/man.config output file:

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

    Command less run-time can be entered are:

    • Spacebar: Scroll down one;
    • [Pagedown]: Scroll down one;
    • [Pageup]: flip up one;
    • / String: Search down "string" function;
    • ? String: Up Search "string" function;
    • n: Repeat the previous search (and / or related?!)
    • N: inverted repeat a previous search (and / or related?!)
    • q: less leave this program;

    head

    Remove the front of the file lines

    grammar:

    head [-n number] 文件 
    

    Options and parameters:

    • -n: followed by the number that represents the meaning of a few lines display
    [root@www ~]# head /etc/man.config
    

    By default, the display 10 front row! To display the first 20 lines, you have to be like this:

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

    tail

    Remove a few lines later in the file

    grammar:

    tail [-n number] 文件 
    

    Options and parameters:

    • -n: followed by the number that represents the meaning of a few lines display
    • -f: indicates continuously monitors the connected behind the file name that you want to wait until the press [ctrl] -c will end tail detection
    [root@www ~]# tail /etc/man.config
    # 默认的情况中,显示最后的十行!若要显示最后的 20 行,就得要这样:
    [root@www ~]# tail -n 20 /etc/man.config