干货:5个文件目录操作相关Linux命令详解
Linux中用 pwd 命令来查看”当前工作目录“的完整路径。简单得说,每当你在终端进行操作时,你都会有一个当前工作目录。
在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置。
[root
@localhost ~]# pwd
/root
[root
@localhost ~]#
[root
@localhost ~]# cd /opt/soft/
[root
@localhost soft]# pwd
/opt/soft
[root
@localhost soft]#
[root
@localhost soft]# cd /etc/init.d
[root
@localhost init.d]# pwd
/etc/init.d
[root
@localhost init.d]# pwd -P
/etc/rc.d/init.d
[root
@localhost init.d]#
[root
@localhost init.d]# /bin/pwd
/etc/rc.d/init.d
[root
@localhost init.d]# /bin/pwd --help
[root
@localhost init.d]# /bin/pwd -P
/etc/rc.d/init.d
[root
@localhost init.d]# /bin/pwd -L
/etc/init.d
[root
@localhost init.d]#
[root
@localhost init.d]# cd /opt/soft
[root
@localhost soft]# mkdir removed
[root
@localhost soft]# cd removed/
[root
@localhost removed]# pwd
/opt/soft/removed
[root
@localhost removed]# rm ../removed -rf
[root
@localhost removed]# pwd
/opt/soft/removed
[root
@localhost removed]# /bin/pwd
/bin/pwd: couldn
't find directory entry in “..” with matching i-node
[root@localhost removed]# cd
[root@localhost ~]# pwd
/root
[root@localhost ~]#
1. 命令格式:
cd [目录名]
2. 命令功能:
切换当前目录至dirName
3. 常用范例
实例1:进入系统根目录
命令:
cd /
输出:
[root@localhost ~]# cd /
说明:
进入系统根目录,上面命令执行完后拿ls命令看一下,当前目录已经到系统根目录了
命令:
cd .. 或者 cd .. //
输出:
[root@localhost soft]# pwd
/opt/soft
[root@localhost soft]# cd ..
[root@localhost opt]# cd ..//
[root@localhost /]# pwd
/
说明:
进入系统根目录可以使用“ cd .. ”一直退,就可以到达根目录
命令:
cd ../.. //
输出:
[root@localhost soft]# pwd
/opt/soft
[root@localhost soft]# cd ../.. //
[root@localhost /]# pwd
/
[root@localhost /]#
说明:使用cd 命令实现进入当前目录的父目录的父目录。
例2:使用 cd 命令进入当前用户主目录
“当前用户主目录”和“系统根目录”是两个不同的概念。进入当前用户主目录有两个方法。
命令1:
cd
输出:
[root@localhost soft]# pwd
/opt/soft
[root@localhost soft]# cd
[root@localhost ~]# pwd
/root
命令2:
cd ~
输出:
[root@localhost ~]# cd /opt/soft/
[root@localhost soft]# pwd
/opt/soft
[root@localhost soft]# cd ~
[root@localhost ~]# pwd
/root
例3:跳转到指定目录
命令:
cd /opt/soft
输出:
[root@localhost ~]# cd /opt/soft
[root@localhost soft]# pwd
/opt/soft
[root@localhost soft]# cd jdk1.6.0_16/
[root@localhost jdk1.6.0_16]# pwd
/opt/soft/jdk1.6.0_16
[root@localhost jdk1.6.0_16]#
说明:
跳转到指定目录,从根目录开始,目录名称前加 / ,当前目录内的子目录直接写名称即可
例4:返回进入此目录之前所在的目录
命令:
cd -
输出:
[root@localhost soft]# pwd
/opt/soft
[root@localhost soft]# cd -
/root
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd -
/opt/soft
[root@localhost soft]#
例5:把上个命令的参数作为cd参数使用。
命令:
cd !$
输出:
[root@localhost soft]# cd !$
cd -
/root
[root@localhost ~]# cd !$
cd -
/opt/soft
[root@localhost soft]#
extension -X status -c none -U time -t size -S atime -u time -t access -u version -v use -u
[root
@localhost opt]# ls -F /opt/soft |grep /$
jdk1.6.0_16/
subversion-
1.6.1/
tomcat6.0.32/
[root
@localhost opt]# ls -l /opt/soft | grep
"^d"
drwxr-xr-x
10 root root
4096
09-
17
18:
17 jdk1.6.0_16
drwxr-xr-x
16
1016
1016
4096
10-
11
03:
25 subversion-
1.6.1
drwxr-xr-x
9 root root
4096
2011-
11-
01 tomcat6.0.32
[root
@localhost opt]# ls -ltr s*
src:
总计
0
script:
总计
0
soft:
总计
350644
drwxr-xr-x
9 root root
4096
2011-
11-
01 tomcat6.0.32
-rwxr-xr-x
1 root root
81871260
09-
17
18:
15 jdk-
6u16-linux-x64.bin
drwxr-xr-x
10 root root
4096
09-
17
18:
17 jdk1.6.0_16
-rw-r--r--
1 root root
205831281
09-
17
18:
33 apache-tomcat-
6.0.32.tar.gz
-rw-r--r--
1 root root
5457684
09-
21
00:
23 tomcat6.0.32.tar.gz
-rw-r--r--
1 root root
4726179
10-
10
11:
08 subversion-deps-
1.6.1.tar.gz
-rw-r--r--
1 root root
7501026
10-
10
11:
08 subversion-
1.6.1.tar.gz
drwxr-xr-x
16
1016
1016
4096
10-
11
03:
25 subversion-
1.6.1
[root
@localhost opt]# ls -AF
log/ script/ soft/ src/ svndata/ web/
[root
@localhost opt]# ls | sed
"s:^:`pwd`/:"
/opt/log
/opt/script
/opt/soft
/opt/src
/opt/svndata
/opt/web
[root
@localhost opt]# find $PWD -maxdepth
1 | xargs ls -ld
drwxr-xr-x
8 root root
4096
10-
11
03:
43 /opt
drwxr-xr-x
2 root root
4096
2012-
03-
08 /opt/log
drwxr-xr-x
2 root root
4096
2012-
03-
08 /opt/script
drwxr-xr-x
5 root root
4096
10-
11
03:
21 /opt/soft
drwxr-xr-x
2 root root
4096
2012-
03-
08 /opt/src
drwxr-xr-x
4 root root
4096
10-
11
05:
22 /opt/svndata
drwxr-xr-x
4 root root
4096
10-
09
00:
45 /opt/web
[root
@localhost soft]# ls -tl --time-style=full-iso
总计
350644
drwxr-xr-x
16
1016
1016
4096
2012-
10-
11
03:
25:
58.000000000 +
0800 subversion-
1.6.1
[root
@localhost soft]# ls -ctl --time-style=
long-iso
总计
350644
drwxr-xr-x
16
1016
1016
4096
2012-
10-
11
03:
25 subversion-
1.6.1
1. 蓝色-->目录 2. 绿色-->可执行文件 3. 红色-->压缩文件 4. 浅蓝色-->链接文件 5. 灰色-->其他文件
linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录。
1.命令格式:
mkdir [选项] 目录...
2.命令功能:
通过 mkdir 命令可以实现在指定位置创建以 DirName(指定的文件名)命名的文件夹或目录。要创建文件夹或目录的用户必须对所创建的文件夹的父文件夹具有写权限。并且,所创建的文件夹(目录)不能与其父目录(即父文件夹)中的文件名重名,即同一个目录下不能有同名的(区分大小写)。
3.命令参数:
-m, --mode=模式,设定权限<模式> (类似 chmod),而不是 rwxrwxrwx 减 umask
-p, --parents 可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立好那些尚不存在的目录,即一次可以建立多个目录;
-v, --verbose 每次创建新目录都显示信息
--help 显示此帮助信息并退出
--version 输出版本信息并退出
4.命令实例:
实例1:创建一个空目录
命令:
mkdir test1
输出:
[root@localhost soft]# cd test
[root@localhost test]# mkdir test1
[root@localhost test]# ll
总计 4drwxr-xr-x 2 root root 4096 10-25 17:42 test1
[root@localhost test]#
实例2:递归创建多个目录
命令:
mkdir -p test2/test22
输出:
[root@localhost test]# mkdir -p test2/test22
[root@localhost test]# ll
总计 8drwxr-xr-x 2 root root 4096 10-25 17:42 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
[root@localhost test]# cd test2/
[root@localhost test2]# ll
总计 4drwxr-xr-x 2 root root 4096 10-25 17:44 test22
[root@localhost test2]#
实例3:创建权限为777的目录
命令:
mkdir -m 777 test3
输出:
[root@localhost test]# mkdir -m 777 test3
[root@localhost test]# ll
总计 12drwxr-xr-x 2 root root 4096 10-25 17:42 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
[root@localhost test]#
说明:
test3 的权限为rwxrwxrwx
实例4:创建新目录都显示信息
命令:
mkdir -v test4
输出:
[root@localhost test]# mkdir -v test4
mkdir: 已创建目录 “test4”
[root@localhost test]# mkdir -vp test5/test5-1
mkdir: 已创建目录 “test5”
mkdir: 已创建目录 “test5/test5-1”
[root@localhost test]#
实例5:一个命令创建项目的目录结构
参考:http://r6e.cn/gRMc
命令:
mkdir -vp scf/{lib/,bin/,doc/{info,product},logs/{info,product},service/deploy/{info,product}}
输出:
[root@localhost test]# mkdir -vp scf/{lib/,bin/,doc/{info,product},logs/{info,product},service/deploy/{info,product}}
mkdir: 已创建目录 “scf”
mkdir: 已创建目录 “scf/lib”
mkdir: 已创建目录 “scf/bin”
mkdir: 已创建目录 “scf/doc”
mkdir: 已创建目录 “scf/doc/info”
mkdir: 已创建目录 “scf/doc/product”
mkdir: 已创建目录 “scf/logs”
mkdir: 已创建目录 “scf/logs/info”
mkdir: 已创建目录 “scf/logs/product”
mkdir: 已创建目录 “scf/service”
mkdir: 已创建目录 “scf/service/deploy”
mkdir: 已创建目录 “scf/service/deploy/info”
mkdir: 已创建目录 “scf/service/deploy/product”
[root@localhost test]# tree scf/
scf/
|-- bin
|-- doc
| |-- info
| `-- product
|-- lib
|-- logs
| |-- info
| `-- product
`-- service
`-- deploy
|-- info
`-- product
12 directories, 0 files
[root@localhost test]#
[root
@localhost test1]# ll
总计
4
-rw-r--r--
1 root root
56
10-
26
14:
31 log.log
root
@localhost test1]# rm log.log
rm:是否删除 一般文件 “log.log”? y
root
@localhost test1]# ll
总计
0[root
@localhost test1]#
[root
@localhost test1]# ll
总计
4
-rw-r--r--
1 root root
23
10-
26
14:
40 log1.log
[root
@localhost test1]# rm -f log1.log
[root
@localhost test1]# ll
总计
0[root
@localhost test1]#
[root
@localhost test1]# ll
总计
8
-rw-r--r--
1 root root
11
10-
26
14:
45 log1.log
-rw-r--r--
1 root root
24
10-
26
14:
45 log2.log
[root
@localhost test1]# rm -i *.log
rm:是否删除 一般文件 “log1.log”? y
rm:是否删除 一般文件 “log2.log”? y
[root
@localhost test1]# ll
总计
0[root
@localhost test1]#
[root
@localhost test]# ll
总计
24drwxr-xr-x
7 root root
4096
10-
25
18:
07 scf
drwxr-xr-x
2 root root
4096
10-
26
14:
51 test1
drwxr-xr-x
3 root root
4096
10-
25
17:
44 test2
drwxrwxrwx
2 root root
4096
10-
25
17:
46 test3
drwxr-xr-x
2 root root
4096
10-
25
17:
56 test4
drwxr-xr-x
3 root root
4096
10-
25
17:
56 test5
[root
@localhost test]# rm -r test1
rm:是否进入目录 “test1”? y
rm:是否删除 一般文件 “test1/log3.log”? y
rm:是否删除 目录 “test1”? y
[root
@localhost test]# ll
总计
20drwxr-xr-x
7 root root
4096
10-
25
18:
07 scf
drwxr-xr-x
3 root root
4096
10-
25
17:
44 test2
drwxrwxrwx
2 root root
4096
10-
25
17:
46 test3
drwxr-xr-x
2 root root
4096
10-
25
17:
56 test4
drwxr-xr-x
3 root root
4096
10-
25
17:
56 test5
[root
@localhost test]#
[root
@localhost test]# rm -rf test2
[root
@localhost test]# ll
总计
16drwxr-xr-x
7 root root
4096
10-
25
18:
07 scf
drwxrwxrwx
2 root root
4096
10-
25
17:
46 test3
drwxr-xr-x
2 root root
4096
10-
25
17:
56 test4
drwxr-xr-x
3 root root
4096
10-
25
17:
56 test5
[root
@localhost test]#
[root
@localhost test]# touch -- -f
[root
@localhost test]# ls -- -f
-f[root
@localhost test]# rm -- -f
rm:是否删除 一般空文件 “-f”? y
[root
@localhost test]# ls -- -f
ls: -f: 没有那个文件或目录
[root
@localhost test]#
[root
@localhost test]# touch ./-f
[root
@localhost test]# ls ./-f
./-f[root
@localhost test]# rm ./-f
rm:是否删除 一般空文件 “./-f”? y
[root
@localhost test]#
[root
@localhost test]# myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv
"$@" $D && echo
"moved to $D ok"; }
[root
@localhost test]# alias rm=
'myrm'
[root
@localhost test]# touch
1.log
2.log
3.log
[root
@localhost test]# ll
总计
16
-rw-r--r--
1 root root
0
10-
26
15:
08
1.log
-rw-r--r--
1 root root
0
10-
26
15:
08
2.log
-rw-r--r--
1 root root
0
10-
26
15:
08
3.log
drwxr-xr-x
7 root root
4096
10-
25
18:
07 scf
drwxrwxrwx
2 root root
4096
10-
25
17:
46 test3
drwxr-xr-x
2 root root
4096
10-
25
17:
56 test4
drwxr-xr-x
3 root root
4096
10-
25
17:
56 test5
[root
@localhost test]# rm [
123].log
moved to /tmp/
20121026150901 ok
[root
@localhost test]# ll
总计
16drwxr-xr-x
7 root root
4096
10-
25
18:
07 scf
drwxrwxrwx
2 root root
4096
10-
25
17:
46 test3
drwxr-xr-x
2 root root
4096
10-
25
17:
56 test4
drwxr-xr-x
3 root root
4096
10-
25
17:
56 test5
[root
@localhost test]# ls /tmp/
20121026150901/
1.log
2.log
3.log
[root
@localhost test]#
上面的操作过程模拟了回收站的效果,即删除文件的时候只是把文件放到一个临时目录中,这样在需要的时候还可以恢复过来。
END
官方站点:www.linuxprobe.com
Linux命令大全:www.linuxcool.com
刘遄老师QQ:5604922
Linux技术交流群:193666693
(新群,火热加群中……)
想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!
