手机能建设网站wordpress按月归档
Linux之文件和目录类命令详解(2)
- 1、mv-移动文件或重命名
 - 2、find-查找文件和目录
 - 3、locate-快速查找文件
 - 4、du-显示目录或文件的磁盘使用情况
 - 5、df-显示文件系统的磁盘空间使用情况
 - 6、chmod-更改文件或目录的权限
 - 7、chown-更改文件或目录的拥有者
 - 8、tree-以树状结构列出目录内容
 
1、mv-移动文件或重命名
1.1 基本用法:
##移动文件或目录,或者重命名文件。
mv source_file destination_file
 
1.2 常用选项:
-i:在目标文件已存在时提示确认
 
mv -i old_name new_name
 
1.3 实例
[root@test test]# ll
total 4
-rw-r--r-- 1 root root 8 Nov 10 18:41 a.txt
[root@test test]# mv a.txt b.txt
[root@test test]# ll
total 4
-rw-r--r-- 1 root root 8 Nov 10 18:41 b.txt##当前系统redhat7.9,执行mv默认带-i
[root@test test]# ll
total 8
-rw-r--r-- 1 root root 8 Nov 10 18:53 b.txt
-rw-r--r-- 1 root root 8 Nov 10 18:41 c.txt
[root@test test]# mv b.txt c.txt
mv: overwrite ‘c.txt’? n
[root@test test]# ll
total 8
-rw-r--r-- 1 root root 8 Nov 10 18:53 b.txt
-rw-r--r-- 1 root root 8 Nov 10 18:41 c.txt##若仅执行mv,则不会提示确认,直接更改
[root@test test]# \mv b.txt c.txt
[root@test test]# ll
total 4
-rw-r--r-- 1 root root 8 Nov 10 18:53 c.txt 
2、find-查找文件和目录
2.1 基本用法
find /path/to/search -name filename
 
2.2 常用选项
-type f:仅查找文件-type d:仅查找目录-name pattern:根据文件名模式查找
 
2.3 实例
[root@test test]# find /root/test -type f -name "*.txt"
/root/test/c.txt
[root@test test]# find /root/ -type d -name "test"
/root/test
 
3、locate-快速查找文件
3.1 基本用法
locate filename
 
3.2 常用选项
-r:使用正则表达式实现精确匹配
 
3.3 实例
[root@test test]# locate c.txt
/root/test/c.txt
/usr/share/doc/alsa-lib-1.1.8/asoundrc.txt
/usr/share/doc/libreswan-3.25/opportunistic-v1.historic/opportunism-spec.txt
/usr/share/doc/python-kitchen-1.1.1/html/_sources/api-text-misc.txt
/usr/share/doc/qemu-kvm/qmp-spec.txt
/usr/share/doc/skkdic-20130104/edict_doc.txt
/usr/share/doc/vim-common-7.4.629/README_amisrc.txt
/usr/share/doc/vim-common-7.4.629/README_mac.txt
/usr/share/doc/vim-common-7.4.629/README_src.txt
/usr/share/vim/vim74/doc/arabic.txt.gz
/usr/share/vim/vim74/doc/os_mac.txt.gz
/usr/share/vim/vim74/doc/os_risc.txt.gz
/usr/share/vim/vim74/doc/pi_spec.txt.gz
/usr/share/vim/vim74/doc/usr_toc.txt.gz##使用正则表达式实现精确匹配
[root@test test]# locate -r '/c\.txt$'
/root/test/c.txt
[root@test test]# 
4、du-显示目录或文件的磁盘使用情况
4.1 基本用法
du directory_name
 
4.2 常用选项
-h:以人类可读的格式显示(KB, MB, GB)
-s:显示总计而非每个文件的大小
 
4.3 实例
[root@test test]# ll
total 12
-rw-r--r-- 1 root root 8 Nov 10 19:10 a.txt
-rw-r--r-- 1 root root 8 Nov 10 19:10 b.txt
-rw-r--r-- 1 root root 8 Nov 10 18:53 c.txt
[root@test test]# du -h
12K     .
[root@test test]# du -h *
4.0K    a.txt
4.0K    b.txt
4.0K    c.txt
[root@test test]# du -s
12      .
[root@test test]# du -s *
4       a.txt
4       b.txt
4       c.txt
[root@test test]# du -sh
12K     .
[root@test test]# du -sh *
4.0K    a.txt
4.0K    b.txt
4.0K    c.txt
 
5、df-显示文件系统的磁盘空间使用情况
5.1 基本用法
df
 
5.2 常用选项
-h:以人类可读的格式显示
 
5.3 实例
[root@test test]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               3.9G     0  3.9G   0% /dev
tmpfs                  3.9G     0  3.9G   0% /dev/shm
tmpfs                  3.9G   13M  3.9G   1% /run
tmpfs                  3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/rhel-root   26G  3.9G   23G  15% /
/dev/sda1             1014M  170M  845M  17% /boot
tmpfs                  797M   12K  797M   1% /run/user/42
tmpfs                  797M     0  797M   0% /run/user/0 
6、chmod-更改文件或目录的权限
6.1 基本用法
chmod 755 filename
 
6.2 常用选项
+x:添加执行权限
-x:移除执行权限
r:读取权限
w:写入权限
x:执行权限
 
6.3 实例
[root@test test]# ll
total 4
-rw-r--r-- 1 root root 8 Nov 10 19:10 a.txt
[root@test test]# chmod u+x a.txt
[root@test test]# ll
total 4
-rwxr--r-- 1 root root 8 Nov 10 19:10 a.txt
 
7、chown-更改文件或目录的拥有者
7.1 基本用法
chown user:group filename
 
7.2 常用选项
-R:递归地更改目录及目录内所有文件的所有者和组
 
7.3 实例
[root@test test]# ll
total 4
-rwxr--r-- 1 root root 8 Nov 10 19:10 a.txt
[root@test test]# chown test:test a.txt
[root@test test]# ll
total 4
-rwxr--r-- 1 test test 8 Nov 10 19:10 a.txt##更改文件目录也一样,若果想更改文件目录及目录下所有文件的权限,可加-R参数
[root@test test]# chown -R test:test test/
 
8、tree-以树状结构列出目录内容
8.1 基本用法
##通常不是在某些Linux发行版中默认安装的,使用前需进行安装
tree /path/to/directory
 
8.2 实例
[root@test ~]# tree /root
/root
├── anaconda-ks.cfg
├── Desktop
├── Documents
├── Downloads
├── initial-setup-ks.cfg
├── Music
├── Pictures
├── Public
├── Templates
├── test
│   └── a.txt
└── Videos