Linux目录相关命令
1.目录管理
绝对路径和相对路径
绝对路径为路径的全称:F:\QQ\xxx.xxx
相对路径为你现在所在文件路径与你要进入路径的地址
列如:你现在在QQ目录下,那么这个xxx.xx文件,对应我们的相对路径就为/xxx.xx
cd:切换目录命令
./:当前目录
cd..:返回上一级目录
2.ls(列出目录)
ls在Linux中是最为常用的
-a参数:all,查看全部文件,包括隐藏文件
-l参数:列出所有的文件,包括文件的属性和权限,没有隐藏文件
1 [root@iZ2zeg6rjsjbfv9ibttfdnZ ~]# cd / ####进入当前目录 2 [root@iZ2zeg6rjsjbfv9ibttfdnZ /]# ls ####列出当前目录所有的文件夹 3 bin blog boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var 4 [root@iZ2zeg6rjsjbfv9ibttfdnZ /]# ls -a ####查看当前目录所有的文件,包括隐藏文件 5 . .. .autorelabel .bash_profile.swp bin blog boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var 6 [root@iZ2zeg6rjsjbfv9ibttfdnZ /]# ls -l ####列出所有文件的属性和权限,不包括隐藏文件 7 total 64 8 lrwxrwxrwx. 1 root root 7 Apr 26 15:48 bin -> usr/bin 9 drwxr-xr-x 6 root root 4096 Jun 12 23:32 blog 10 dr-xr-xr-x. 5 root root 4096 Jun 22 16:48 boot 11 drwxr-xr-x 19 root root 2960 May 15 19:37 dev 12 drwxr-xr-x. 87 root root 4096 Jun 22 17:48 etc 13 drwxr-xr-x. 2 root root 4096 Apr 11 2018 home 14 lrwxrwxrwx. 1 root root 7 Apr 26 15:48 lib -> usr/lib 15 lrwxrwxrwx. 1 root root 9 Apr 26 15:48 lib64 -> usr/lib64 16 drwx------. 2 root root 16384 Apr 26 15:48 lost+found 17 drwxr-xr-x. 2 root root 4096 Apr 11 2018 media 18 drwxr-xr-x. 2 root root 4096 Apr 11 2018 mnt 19 drwxr-xr-x. 5 root root 4096 Jun 22 16:50 opt 20 dr-xr-xr-x 90 root root 0 May 15 19:37 proc 21 dr-xr-x---. 11 root root 4096 Jun 22 18:10 root 22 drwxr-xr-x 27 root root 840 Jun 22 17:48 run 23 lrwxrwxrwx. 1 root root 8 Apr 26 15:48 sbin -> usr/sbin 24 drwxr-xr-x. 2 root root 4096 Apr 11 2018 srv 25 dr-xr-xr-x 13 root root 0 May 16 03:37 sys 26 drwxrwxrwt. 8 root root 4096 Aug 10 03:43 tmp 27 drwxr-xr-x. 13 root root 4096 Apr 26 15:48 usr 28 drwxr-xr-x. 19 root root 4096 Apr 26 07:54 var
3.cd 切换目录命令
1 [root@iZ2zeg6rjsjbfv9ibttfdnZ /]# cd /home 2 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# cd ../usr ####相对路径进入到usr目录 3 [root@iZ2zeg6rjsjbfv9ibttfdnZ usr]# cd / 4 [root@iZ2zeg6rjsjbfv9ibttfdnZ /]# cd home 5 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# mkdir dzstudy ####创建一个目录 6 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 7 dzstudy 8 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# cd .. 9 [root@iZ2zeg6rjsjbfv9ibttfdnZ /]# cd /home/dzstudy ####绝对路径跳转 10 [root@iZ2zeg6rjsjbfv9ibttfdnZ dzstudy]# cd ~ ####回到当前的用户目录 11 [root@iZ2zeg6rjsjbfv9ibttfdnZ ~]# pwd ####显示当前所在目录 12 /root
4. pwd显示当前用户所在目录
1 [root@iZ2zeg6rjsjbfv9ibttfdnZ ~]# pwd 2 /root 3 [root@iZ2zeg6rjsjbfv9ibttfdnZ ~]# cd /bin 4 [root@iZ2zeg6rjsjbfv9ibttfdnZ bin]# pwd 5 /bin 6 [root@iZ2zeg6rjsjbfv9ibttfdnZ bin]# cd /usr/local 7 [root@iZ2zeg6rjsjbfv9ibttfdnZ local]# pwd 8 /usr/local
5.mkdir 创建一个目录
1 [root@iZ2zeg6rjsjbfv9ibttfdnZ local]# cd /home 2 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 3 dzstudy 4 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# mkdir dztest ####创建目录 5 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 6 dzstudy dztest 7 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# mkdir dztest2/dztest3/dztest4 8 mkdir: cannot create directory ‘dztest2/dztest3/dztest4’: No such file or directory 9 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# mkdir -p dztest2/dztest3/dztest4 ####创建多级目录 10 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 11 dzstudy dztest dztest2 12 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# cd dztest2 13 [root@iZ2zeg6rjsjbfv9ibttfdnZ dztest2]# ls 14 dztest3 15 [root@iZ2zeg6rjsjbfv9ibttfdnZ dztest2]# cd dztest3 16 [root@iZ2zeg6rjsjbfv9ibttfdnZ dztest3]# ls 17 dztest4
6.rmdir 删除目录
1 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 2 dzstudy dztest dztest2 3 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# rmdir dztest 4 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 5 dzstudy dztest2 6 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# rmdir -p dztest2 7 rmdir: failed to remove ‘dztest2’: Directory not empty 8 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# rmdir -p dztest2/dztest3/dztest4 9 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 10 dzstudy
rmdir仅能删除空的目录,如果修啊面存在文件,需要先删除文件,递归删除多个目录-p参数即可
7.cp 复制文件或者目录
cp 原来的地方 新的地方
1 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# cp conn_mysql.bat dzstudy ####拷贝文件至目录 2 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 3 conn_mysql.bat dzstudy 4 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# cd dzstudy 5 [root@iZ2zeg6rjsjbfv9ibttfdnZ dzstudy]# ls 6 conn_mysql.bat 7 [root@iZ2zeg6rjsjbfv9ibttfdnZ dzstudy]# cd .. 8 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# cp conn_mysql.bat dzstudy ####如果文件重复,就选择覆盖(Y)或者放弃(N) 9 cp: overwrite ‘dzstudy/conn_mysql.bat’? y
8.rm 移除文件或者目录
-f:忽略不存在的文件,不会出现警告,强制删除
-r:递归删除目录
-i:互动,删除询问是否删除
1 -rm -rf /* ####系统中的所有文件就被删除了,从删库到跑路
1 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 2 conn_mysql.bat dzstudy 3 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# rm -rf conn_mysql.bat 4 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 5 dzstudy
9.mv 移动文件或者目录,重命名文件
-f:强制
-u:只替换已经更新过的文件
1 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# clear 2 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 3 dzstudy 4 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# cd dzstudy/ 5 [root@iZ2zeg6rjsjbfv9ibttfdnZ dzstudy]# ls 6 conn_mysql.bat 7 [root@iZ2zeg6rjsjbfv9ibttfdnZ dzstudy]# mv conn_mysql.bat /home ####移动文件 8 [root@iZ2zeg6rjsjbfv9ibttfdnZ dzstudy]# cd .. 9 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 10 conn_mysql.bat dzstudy 11 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# mv conn_mysql.bat mysql.bat ####重命名文件夹 12 [root@iZ2zeg6rjsjbfv9ibttfdnZ home]# ls 13 dzstudy mysql.bat