Home > php教程 > PHP开发 > body text

Basic Linux command format and directory processing commands

高洛峰
Release: 2016-11-14 12:38:31
Original
1476 people have browsed it

Command prompt

[root@localhost ~]#

root: current logged in user
localhost: host name
~: current directory, this is the "home" directory
#: root super user prompt, If it is an ordinary user, it is $

command format

command [options] [parameters]

brackets [] indicates optional

query the contents of the directory: ls

ls [options] [file or directory]

Options:
-a: Display all files, including hidden files
-l: Display detailed information
-d: View directory attributes
-h: Humanized display of file size
-i: Display inode

According to the above options, Type in the command and the displayed results are as follows:

[root@localhost ~]# lsanaconda-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总用量 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总用量 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 -i71259104 anaconda-ks.cfg  36099565 test
Copy after login

Please pay attention to the difference between the results of the ls -l and ls -lh commands

An explanation is needed here:

-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
Copy after login

First of all, the first symbol "-" (- within the quotation marks) ), indicating the file type (there are three commonly used ones, namely - means file, d means directory, l means soft link file), and there are also less commonly used ones, which are block device files, character device files, socket files, and management files. .

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

Secondly, excluding the first symbol, let’s look at rw-------. There are nine characters in total, which need to be divided into three groups, namely rw-,---,---, each group In order, they represent the owner of u, the group to which g belongs, and the permissions of other people. In the above, they correspond to root and root respectively. That is, the first root means that the owner's permissions are root permissions, and the second root means that the permissions of the group it belongs to are also root permissions. For others, there is no such permission at all.

Among them, r means readable, w means writable, and x means execution permission.

In order to make it more clear, here is a table for the anaconda-ks.cfg file:

Basic Linux command format and directory processing commands

Then, for the test file rwxr-xr-x, readers are asked to judge its permissions by themselves.

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

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

Directory processing command

Create directory: mkdir

mkdir -p [directory name]

-p : Recursive creation

[root@localhost ~]# lsanaconda-ks.cfg  test

[root@localhost ~]# mkdir otherFolder[root@localhost ~]# lsanaconda-ks.cfg  otherFolder  test

[root@localhost ~]# mkdir folder_2/test_2mkdir: 无法创建目录"folder_2/test_2": 没有那个文件或目录

[root@localhost ~]# mkdir -p folder_2/test_2[root@localhost ~]# lsanaconda-ks.cfg  folder_2  otherFolder  test

[root@localhost ~]# ls folder_2/test_2
Copy after login

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

Switch directory: cd

cd [directory]

Operation:

cd ~ : Enter the current user’s home directory

cd-: 进入上次目录

cd.. : 进入上一级目录

cd : 回到家目录

[root@localhost ~]# lsanaconda-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 ~]#
Copy after login

注意理清概念:相对路径和绝对路径

绝对路径:从根目录一级级找下去,需要写全路径

[root@localhost ~]# cd folder_2/test_2
[root@localhost test_2]#
Copy after login

相对路径:参照当前所在目录进行查找

[root@localhost test_2]# cd ../../otherFolder
[root@localhost otherFolder]#
Copy after login

查询所在目录位置:pwd

pwd

可以说是最简单的命令了,查询所在目录的位置

[root@localhost ~]# pwd/root

[root@localhost ~]# lsanaconda-ks.cfg  folder_2  otherFolder  test
[root@localhost ~]# cd folder_2/[root@localhost folder_2]# lstest_2
[root@localhost folder_2]# cd test_2/[root@localhost test_2]# pwd/root/folder_2/test_2
Copy after login

删除空目录:rmdir

rmdir [目录名]

只能删除空目录,这个命令用得比较少。

[root@localhost ~]# lsanaconda-ks.cfg  folder_2  otherFolder  test

[root@localhost ~]# rmdir otherFolder[root@localhost ~]# lsanaconda-ks.cfg  folder_2  test

[root@localhost ~]# rmdir folder_2rmdir: 删除 "folder_2" 失败: 目录非空
[root@localhost ~]#
Copy after login

删除文件或目录:rm

rm -rf [文件或目录]

r 表示可以同时删除文件和目录,f表示强制删除

如果不添加任何选项,那么只可以删除文件,删除时提示是否确认删除

如果只添加选项 -r,那么可以删除文件也可以删除目录,删除时提示是否确认删除

如果添加了选项 -rf,那么将不做任何提示删除文件或目录

[root@localhost ~]# lsabc.txt  anaconda-ks.cfg  folder_2  test

[root@localhost ~]# rm abc.txtrm:是否删除普通空文件 "abc.txt"?y

[root@localhost ~]# rm testrm: 无法删除"test": 是一个目录
[root@localhost ~]# rm -r testrm:是否删除目录 "test"?y

[root@localhost ~]# lsanaconda-ks.cfg  folder_2

[root@localhost ~]# rm -rf folder_2[root@localhost ~]# lsanaconda-ks.cfg
[root@localhost ~]#
Copy after login

复制命令:cp

cp [选项] [原文件或目录] [目标目录]

选项:
-r : 复制目录
-p : 同时复制文件属性
-d : 若源文件是链接文件,则复制链接属性
-a : 包含以上所有选项,相当于 -rpd

在[目标目录]后面加上文件名,就是改名复制。

[root@localhost ~]# lsanaconda-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_bcp: 略过目录"folder_a"[root@localhost ~]# cp -r folder_a folder_b[root@localhost ~]# ls folder_bfolder_a  test_1

[root@localhost ~]# ll总用量 4-rw-------. 1 root root 2752 Nov 10 02:51 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 Nov 13 17:21 bbc.txt
drwxr-xr-x. 2 root root   20 Nov 13 17:38 folder_a
drwxr-xr-x. 4 root root   34 Nov 13 17:39 folder_b

[root@localhost ~]# ll folder_a总用量 0-rw-r--r--. 1 root root 0 Nov 13 17:38 bbc.txt

[root@localhost ~]# cp -a bbc.txt folder_b[root@localhost ~]# ll folder_b总用量 0-rw-r--r--. 1 root root  0 Nov 13 17:21 bbc.txt
drwxr-xr-x. 2 root root 20 Nov 13 17:39 folder_a
drwxr-xr-x. 2 root root  6 Nov 13 17:38 test_1
[root@localhost ~]#
Copy after login

这里需要解释一下的是,在原文件 bbc.txt 中,其修改时间为 17:21,在普通复制下,它的时间这个属性是不会被复制,我们可以看到复制后的bbc.txt的时间为17:38,如果需要连同属性一起复制,那么就加上 -pd 或者 直接 -a,如上所示,我们把bbc.txt复制到folder_b,这时我们查看属性的时候,时间属性和原属性是一致的。

在上述命令中,ll 是 ls -l 的简写。

剪切或改名命令:mv

mv [原文件或目录] [目标目录]

如果原文件或者目录 与 目标目录在同一个目录下,那么就是重命名

如果不在同一个目录下,那么就是剪切

通过以下实践理解:

[root@localhost ~]# lsanaconda-ks.cfg  bbc.txt

[root@localhost ~]# mv bbc.txt abc.txt[root@localhost ~]# lsabc.txt  anaconda-ks.cfg

[root@localhost ~]# mkdir test[root@localhost ~]# lsabc.txt  anaconda-ks.cfg  test

[root@localhost ~]# mv abc.txt test/[root@localhost ~]# lsanaconda-ks.cfg  test

[root@localhost ~]# ls test/abc.txt
[root@localhost ~]#
Copy after login

链接命令:ln

ln -s [原文件] [目标文件]

生成链接文件
-s : 创建软连接

硬链接的特征:

拥有相同 i 节点和存储block块,可以看做是同一个文件

可通过i节点识别,i节点是相同的

不能跨分区

不能针对目录使用

通过上述命令,可以理解为为某个内容添加一个标签,通过打开这个标签就可以进入这个内容,硬连接,即再生成一个标签,同样可以通过这个标签进入这个内容。

如果内容被修改,那么不管从硬链接的哪个文件进入,都是被修改的。

软链接的特征:

类似windows的快捷方式

软链接拥有自己的i节点和block块,但是数据块只保存原文件的文件名和I节点号,并没有实际的文件数据

lrwxrwxrwx l为软链接(软链接的权限都为rwxrwxrwx,这只是软链接本身的权限)

修改任意文件,另一个都改变

删除原文件,软链接不能用(和windows的快捷方式一样)

硬链接:

[root@localhost ~]# lsanaconda-ks.cfg
[root@localhost ~]# mkdir folder[root@localhost ~]# lsanaconda-ks.cfg  folder
[root@localhost ~]# touch bbb.txt[root@localhost ~]# lsanaconda-ks.cfg  bbb.txt  folder
[root@localhost ~]# ln bbb.txt folder/ccc.txt[root@localhost ~]# ll folder/总用量 0-rw-r--r--. 2 root root 0 Nov 13 18:08 ccc.txt
[root@localhost ~]# ll bbb.txt -rw-r--r--. 2 root root 0 Nov 13 18:08 bbb.txt
Copy after login

软链接:

[root@localhost ~]# mkdir folder_b[root@localhost ~]# ln -s bbb.txt folder_b/eee.txt[root@localhost ~]# ll 总用量 4-rw-------. 1 root root 2752 Nov 10 02:51 anaconda-ks.cfg
-rw-r--r--. 2 root root    0 Nov 13 18:10 bbb.txt
drwxr-xr-x. 2 root root   20 Nov 13 18:09 folder
drwxr-xr-x. 2 root root   20 Nov 13 18:11 folder_b
[root@localhost ~]# ll folder_b总用量 0lrwxrwxrwx. 1 root root 7 Nov 13 18:11 eee.txt -> bbb.txt
[root@localhost ~]# rm -rf bbb.txt [root@localhost ~]# ll folder_b总用量 0lrwxrwxrwx. 1 root root 7 Nov 13 18:11 eee.txt -> bbb.txt
Copy after login

删除了原文件,软链接的箭头目标为红色一闪一闪,表示找不到目标文件。

常用目录作用

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

说明:

/ 根目录

/bin 命令保存目录(普通用户权限)

/sbin 命令保存目录(root权限)

/boot 启动目录,包含启动相关文件,和开机有关

/dev 设备文件保存目录

/etc 配置文件保存目录

/home 普通用户家目录

/lib 系统库保存目录

/mnt 系统挂载目录

/media 挂载目录(常用于光盘挂载)

/root 超级用户家目录

/tmp 临时目录

/proc 直接写入内存的

/sys 直接写入内存的

/usr 系统软件资源目录

/var 系统相关文档内容


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template