Linux命令格式及命令
Linux命令基本格式及目录处理命令
[root@localhost ~]#
localhost:主机名
~:当前所在的目录,此处为“家”目录
#:root超级用户的提示符,如果是普通用户,则为 $
命令格式
命令 [选项] [参数]
查询目录中的内容:ls
ls [选项] [文件或目录]
-a : 显示所有文件,包括隐藏文件
-l : 显示详细信息
-d : 查看目录属性
-h : 人性化显示文件大小
-i : 显示inode
[root@localhost ~]# ls
anaconda-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 -i
71259104 anaconda-ks.cfg 36099565 test
-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
目录处理命令创建目录:mkdir
mkdir -p [目录名]
[root@localhost ~]# ls
anaconda-ks.cfg test
[root@localhost ~]# mkdir otherFolder
[root@localhost ~]# ls
anaconda-ks.cfg otherFolder test
[root@localhost ~]# mkdir folder_2/test_2
mkdir: 无法创建目录"folder_2/test_2": 没有那个文件或目录
[root@localhost ~]# mkdir -p folder_2/test_2
[root@localhost ~]# ls
anaconda-ks.cfg folder_2 otherFolder test
[root@localhost ~]# ls folder_2/test_2
切换所在目录:cd
cd [目录]
-
cd ~ : 进入当前用户的家目录
-
cd-: 进入上次目录
-
cd.. : 进入上一级目录
-
cd : 回到家目录
[root@localhost ~]# ls
anaconda-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 ~]#
[root@localhost ~]# cd folder_2/test_2
[root@localhost test_2]#
[root@localhost test_2]# cd ../../otherFolder
[root@localhost otherFolder]#查询所在目录位置:pwd
pwd
[root@localhost ~]# pwd
/root
[root@localhost ~]# ls
anaconda-ks.cfg folder_2 otherFolder test
[root@localhost ~]# cd folder_2/
[root@localhost folder_2]# ls
test_2
[root@localhost folder_2]# cd test_2/
[root@localhost test_2]# pwd/root/folder_2/test_2删除空目录:rmdir
rmdir [目录名]
[root@localhost ~]# ls
anaconda-ks.cfg folder_2 otherFolder test
[root@localhost ~]# rmdir otherFolder
[root@localhost ~]# ls
anaconda-ks.cfg folder_2 test
[root@localhost ~]# rmdir folder_2
rmdir: 删除 "folder_2" 失败: 目录非空
[root@localhost ~]#删除文件或目录:rm
rm -rf [文件或目录]
-
如果不添加任何选项,那么只可以删除文件,删除时提示是否确认删除
-
如果只添加选项 -r,那么可以删除文件也可以删除目录,删除时提示是否确认删除
-
如果添加了选项 -rf,那么将不做任何提示删除文件或目录
[root@localhost ~]# ls
abc.txt anaconda-ks.cfg folder_2 test
[root@localhost ~]# rm abc.txt
rm:是否删除普通空文件 "abc.txt"?y
[root@localhost ~]# rm test
rm: 无法删除"test": 是一个目录
[root@localhost ~]# rm -r test
rm:是否删除目录 "test"?y
[root@localhost ~]# ls
anaconda-ks.cfg folder_2
[root@localhost ~]# rm -rf folder_2
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]#复制命令:cp
cp [选项] [原文件或目录] [目标目录]
-r : 复制目录
-p : 同时复制文件属性
-d : 若源文件是链接文件,则复制链接属性
-a : 包含以上所有选项,相当于 -rpd
[root@localhost ~]# ls
anaconda-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_b
cp: 略过目录"folder_a"
[root@localhost ~]# cp -r folder_a folder_b
[root@localhost ~]# ls folder_b
folder_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 ~]#
剪切或改名命令:mv
mv [原文件或目录] [目标目录]
-
如果原文件或者目录 与 目标目录在同一个目录下,那么就是重命名
-
如果不在同一个目录下,那么就是剪切
[root@localhost ~]# ls
anaconda-ks.cfg bbc.txt
[root@localhost ~]# mv bbc.txt abc.txt
[root@localhost ~]# ls
abc.txt anaconda-ks.cfg
[root@localhost ~]# mkdir test
[root@localhost ~]# ls
abc.txt anaconda-ks.cfg test
[root@localhost ~]# mv abc.txt test/
[root@localhost ~]# ls
anaconda-ks.cfg test
[root@localhost ~]# ls test/
abc.txt
[root@localhost ~]#链接命令:ln
ln -s [原文件] [目标文件]
-s : 创建软连接
-
拥有相同 i 节点和存储block块,可以看做是同一个文件
-
可通过i节点识别,i节点是相同的
-
不能跨分区
-
不能针对目录使用
-
类似windows的快捷方式
-
软链接拥有自己的i节点和block块,但是数据块只保存原文件的文件名和I节点号,并没有实际的文件数据
-
lrwxrwxrwx l为软链接(软链接的权限都为rwxrwxrwx,这只是软链接本身的权限)
-
修改任意文件,另一个都改变
-
删除原文件,软链接不能用(和windows的快捷方式一样)
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# mkdir folder
[root@localhost ~]# ls
anaconda-ks.cfg folder
[root@localhost ~]# touch bbb.txt
[root@localhost ~]# ls
anaconda-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
[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
总用量 0
lrwxrwxrwx. 1 root root 7 Nov 13 18:11 eee.txt -> bbb.txt
[root@localhost ~]# rm -rf bbb.txt
[root@localhost ~]# ll folder_b
总用量 0
lrwxrwxrwx. 1 root root 7 Nov 13 18:11 eee.txt -> bbb.txt
常用目录作用[root@localhost ~]# ls /
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys temp tmp usr var
/ 根目录 /bin 命令保存目录(普通用户权限) /sbin 命令保存目录(root权限) /boot 启动目录,包含启动相关文件,和开机有关 /dev 设备文件保存目录 /etc 配置文件保存目录 /home 普通用户家目录 /lib 系统库保存目录 /mnt 系统挂载目录 /media 挂载目录(常用于光盘挂载) /root 超级用户家目录 /tmp 临时目录 /proc 直接写入内存的 /sys 直接写入内存的 /usr 系统软件资源目录 /var 系统相关文档内容
更多免费技术资料可关注:annalin1203

浙公网安备 33010602011771号