linux常用命令 目录操作(cd,mkdir,rmdir,mv,cp,rm)
cd
有一个特殊的目录被称为根目录,是整个文件系统形成的这棵树的根节点,在Linux系统中用一个单独的 “/”符号表示。
因此一个目录的绝对路径可以表示为“/d2/d3”这样的形式。
当前目录表示用户目前正在工作的目录。为了切换到文件系统中的某个目录,可以使用“cd”命令。
假设当前目录为“/d2/d3”,下图给出了cd命令的几种形式,以及执行命令之后的当前目录。
mkdir
mkdir // 创建文件夹(make directory)
语法 mkdir [目录名称]
eg: [root@localhost ~]# mkdir hello
[root@localhost ~]# ls
anaconda-ks.cfg hello
[root@localhost ~]#
mkdir -p //通过递归创建多级目录
eg: [root@localhost ~]# mkdir -p k1/k2
[root@localhost ~]# ls
anaconda-ks.cfg hello k1
[root@localhost ~]#
rmdir
rmdir // 删除空文件夹(remove empty directory)
eg: [root@localhost ~]# ls -lh
total 12K
-rw-------. 1 root root 1.5K Jan 19 20:27 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4.0K Jan 20 17:39 hello
drwxr-xr-x. 3 root root 4.0K Jan 20 17:41 k1
[root@localhost ~]# rmdir hello
[root@localhost ~]# ls
anaconda-ks.cfg k1
[root@localhost ~]#
mv
mv // 剪切和重命名操作(move)
1.重命名
eg: [root@localhost ~]# ls
anaconda-ks.cfg hello k1
[root@localhost ~]# mv hello hi
[root@localhost ~]# ls
anaconda-ks.cfg hi k1
[root@localhost ~]#
2.剪切
语法:mv [当前路径][目标路径]
eg :[root@localhost ~]# ls
anaconda-ks.cfg hello k1
[root@localhost ~]# mv k1 /tmp
[root@localhost ~]# ls /tmp
k1
[root@localhost ~]# ls
anaconda-ks.cfg hello
[root@localhost ~]#
cp
cp //拷贝(copy)
语法:cp [原文件或目录][目标目录]
-r //复制目录
-p //保留原文件属性
eg:拷贝文件夹
[root@localhost ~]# ls
anaconda-ks.cfg hello
[root@localhost ~]# ls /tmp
k1
[root@localhost ~]# cp /tmp/k1 /root
cp: omitting directory ‘/tmp/k1’
[root@localhost ~]# ls
anaconda-ks.cfg hello
[root@localhost ~]# cp -r /tmp/k1 /root
[root@localhost ~]# ls
anaconda-ks.cfg hello k1
eg:保留原文件属性
[root@localhost ~]# ls
anaconda-ks.cfg hello k1
[root@localhost ~]# ls -l
total 12
-rw-------. 1 root root 1462 Jan 19 20:27 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Jan 20 17:45 hello //原时间
drwxr-xr-x. 3 root root 4096 Jan 20 17:57 k1 //原时间
[root@localhost ~]# cp -r hello /tmp
[root@localhost ~]# cp -rp k1 /tmp
[root@localhost ~]# ls -l /tmp
total 8
drwxr-xr-x. 2 root root 4096 Jan 20 18:04 hello //时间改变
drwxr-xr-x. 3 root root 4096 Jan 20 17:57 k1 //时间仍然为原文件时间
[root@localhost ~]#
rm
rm //删除文件或文件夹(remove)
语法:rm [文件或目录]
-f //强制删除
-r //删除文件夹
eg: [root@localhost ~]# ls /tmp
hello k1
[root@localhost ~]# cp -r /tmp/hello /root
[root@localhost ~]# ls
anaconda-ks.cfg hello k1
[root@localhost ~]# rm hello
rm: cannot remove ‘hello’: Is a directory
[root@localhost ~]# rm -r hello
rm: remove directory ‘hello’? y
[root@localhost ~]# rm -rf k1
[root@localhost ~]# ls
anaconda-ks.cfg