May 22, 2021 Linux
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.
Let's look at a few common commands for working with directories:
You can use man commands to view usage documentation for individual commands, such as :man cp.
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:
List all files in the home directory (with properties and hidden files)
[root@www ~]# ls -al ~
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 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 的选项后,会不以连结档的数据显示,而是显示正确的完整路径啊!
If you want to create a new directory, use makedir.
Grammar:
mkdir [-mp] 目录名称
Options and parameters:
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.
Grammar:
rmdir [-p] 目录名称
Options and parameters:
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 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:
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为覆盖
Grammar:
rm [-fir] 文件或目录
Options and parameters:
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!
Grammar:
[root@www ~]# mv [-fiu] source destination [root@www ~]# mv [options] source1 source2 source3 .... directory
Options and parameters:
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
The following commands are used in Linux systems to view the contents of the file:
You can use man commands to view usage documentation for individual commands, such as :man cp.
The contents of the file are displayed from the first line
Grammar:
cat [-AbEnTv]
Options and parameters:
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 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)
Displays the line number
Grammar:
nl [-bnw] 文件
Options and parameters:
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
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:
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:
Remove the first few lines of the file
Grammar:
head [-n number] 文件
Options and parameters:
[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
Remove the next few lines of the file
Grammar:
tail [-n number] 文件
Options and parameters:
[root@www ~]# tail /etc/man.config # 默认的情况中,显示最后的十行!若要显示最后的 20 行,就得要这样: [root@www ~]# tail -n 20 /etc/man.config