文件及目录操作 - cp、find
1、cp:复制文件或目录
cp命令的功能说明
cp命令用于复制文件或目录。
cp命令的语法格式
cp[OPTION]... SOURCE... DIRECTORY
cp[参数选项] [源文件或源目录] [目标文件或目录]
cp命令的选项说明
cp选项就几个,表1为cp命令的参数及说明:
表1:
cp命令的参数及说明
| 参数选项 | 解释说明(带*的为重点) |
|---|---|
| -r | 复制目录 * |
| -p | 保持文件或目录属性 |
| -a | 相当于同时使用参数-d,-p,-r * |
| -i | 提示是否覆盖的确认 |
| -d | 如果复制的源文件为链接文件,仅复制符号链接本身,且保留符号链接所指向的目标文件或目录 |
cp命令的实践操作
范例1: 无参数和带参数
-a的比较
[root@oldboyedu /test]# pwd
/test
[root@oldboyedu /test]# ll -h
total 0
drwxr-xr-x 3 root root 18 Apr 5 18:52 dir1
drwxr-xr-x 2 root root 6 Apr 4 14:51 dir2
drwxr-xr-x 2 root root 6 Apr 4 14:51 dir3
-rw-r--r-- 1 root root 0 Apr 6 08:26 file1.txt
-rw-r--r-- 1 root root 0 Apr 4 14:51 file2.txt
-rw-r--r-- 1 root root 0 Apr 4 14:51 file3.txt
[root@oldboyedu /test]# cp file1.txt file4.txt
[root@oldboyedu /test]# cp -a file1.txt file5.txt
[root@oldboyedu /test]# ll -h
total 0
drwxr-xr-x 3 root root 18 Apr 5 18:52 dir1
drwxr-xr-x 2 root root 6 Apr 4 14:51 dir2
drwxr-xr-x 2 root root 6 Apr 4 14:51 dir3
-rw-r--r-- 1 root root 0 Apr 6 08:26 file1.txt <-->源文件的属性
-rw-r--r-- 1 root root 0 Apr 4 14:51 file2.txt
-rw-r--r-- 1 root root 0 Apr 4 14:51 file3.txt
-rw-r--r-- 1 root root 0 Apr 6 08:27 file4.txt <-->没加参数的文件属性
-rw-r--r-- 1 root root 0 Apr 6 08:26 file5.txt <-->加了参数的文件属性
范例2: 使用
-i参数的例子
[root@oldboyedu /test]# cp -i file1.txt file5.txt <-->提示是否覆盖文件?
cp: overwrite ‘file5.txt’? n
[root@oldboyedu /test]# cp file1.txt file5.txt <-->没加 -i 为啥也提示?
cp: overwrite ‘file5.txt’? n
[root@oldboyedu /test]# alias cp <-->因为系统为cp做了别名
alias cp='cp -i'
[root@oldboyedu /test]# \cp file1.txt file5.txt <-->取消别名(或提示)方法1:在前面加\
[root@oldboyedu /test]# /bin/cp file1.txt file5.txt <-->取消别名(或提示)方法2:使用命令的绝对路径
范例3: 使用
-r参数复制目录
[root@oldboyedu /test]# tree dir1 dir2 <-->看一下dir1和dir2目录内容
dir1 <--> dir1目录的内容
└── sub1
└── test
dir2 <--> dir2目录的内容
2 directories, 0 files
[root@oldboyedu /test]# cp dir1 dir2 <-->显示跳过目录dir1
cp: omitting directory ‘dir1’
[root@oldboyedu /test]# cp -r dir1 dir2 <--> 使用 -r 参数
[root@oldboyedu /test]# tree dir1 dir2 <-->查看结果
dir1 <--> dir1目录的内容
└── sub1
└── test
dir2 <--> dir2目录的内容(连目录dir1本身也复制过来了)
└── dir1
└── sub1
└── test
5 directories, 0 files
范例4: 快速备份文件案例
[root@oldboyedu /test]# cp /etc/ssh/ssh_config /etc/ssh/sshd_config.ori <-->正常备份
[root@oldboyedu /test]# cp /etc/ssh/ssh_config{,.ori} <-->快速备份
2、find:查找目录下的文件或查找目录
find命令的功能说明
find命令用于查找目录下的文件或查找目录,同时可以调用其他命令执行相应的操作。
find命令的语法格式
find[-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]
find[选项] [路径] [操作语句]
find命令的选项说明
find选项很多,表1为find命令的常用的参数及说明:
表1:
find命令的参数及说明(还有很多参数,不再列举)
| 参数选项 | 解释说明 |
|---|---|
| -name | 按文件名查询 |
| -type | f查找文件;d查找目录 |
| -exec | 对查找的结果再处理 |
| -mtime | -n查找更改时间距现在n(正整数)天以内;+n查找更改时间距现在n(正整数)天以前;n查找更改时间距现在n(正整数) |
| -perm | 按照文件的权限来查找文件 |
| -size | 以文件大小查找 |
| -path | 指定路径样式,配合-prune参数排除指定目录 |
| ! | 表示取反 |
| -a | 表示取交集 |
| -o | 取并集 |
find命令的实践操作
范例1: 查找指定时间内修改过的文件
[root@oldboyedu /test]# find . -atime -1 <--> . 表示当前,查找2天内被访问的文件
.
./file1.txt
./dir1
./dir1/sub1
./dir1/sub1/test
./file4.txt
./file5.txt
./dir2
./dir2/dir1
./dir2/dir1/sub1
./dir2/dir1/sub1/test
[root@oldboyedu /test]# find /test/ -mtime -5 <-->使用绝对路径,查找5天内被修改的文件
/test/
/test/file1.txt
/test/file2.txt
/test/file3.txt
/test/dir1
/test/dir1/sub1
/test/dir1/sub1/test
/test/dir3
/test/.file4.txt
/test/file4.txt
/test/file5.txt
/test/dir2
/test/dir2/dir1
/test/dir2/dir1/sub1
/test/dir2/dir1/sub1/test
范例2: 用
-name指定关键字查找
[root@oldboyedu /test]# find /var/log/ -mtime +5 -name '*.log' <-->在/var/log/目录下查找5天以前.log结尾的文件
/var/log/anaconda/anaconda.log
/var/log/anaconda/X.log
/var/log/anaconda/program.log
/var/log/anaconda/packaging.log
/var/log/anaconda/storage.log
/var/log/anaconda/ifcfg.log
/var/log/anaconda/ks-script-klS0RP.log
/var/log/anaconda/journal.log
/var/log/vmware-network.8.log
/var/log/vmware-network.9.log
范例3: 利用
!反向查找
[root@oldboyedu /test]# find . -type d
.
./dir1
./dir1/sub1
./dir1/sub1/test
./dir3
./dir2
./dir2/dir1
./dir2/dir1/sub1
./dir2/dir1/sub1/test
[root@oldboyedu /test]# find . ! -type d <-->! 表示取反,查找不是目录的文件,注意感叹号的位置
./file1.txt
./file2.txt
./file3.txt
./.file4.txt
./file4.txt
./file5.txt
范例4: 按照目录或文件的权限来查找文件
[root@oldboyedu /test]# find /test -perm 755 <-->755是权限的数字表示方式
/test
/test/dir1
/test/dir1/sub1
/test/dir1/sub1/test
/test/dir3
/test/dir2
/test/dir2/dir1
/test/dir2/dir1/sub1
/test/dir2/dir1/sub1/test
范例5: 按大小查找文件
[root@oldboyedu /test]# find . -size +10c <-->查找当前目录下大于10字节的文件
.
./dir1
./dir1/sub1
./dir2
./dir2/dir1
./dir2/dir1/sub1
范例6: 查找文件时希望忽略某个目录
[root@oldboyedu /test]# find /test -path "/test/dir1" -prune -o -print <-->排除指定目录
/test
/test/file1.txt
/test/file2.txt
/test/file3.txt
/test/dir3
/test/.file4.txt
/test/file4.txt
/test/file5.txt
/test/dir2
/test/dir2/dir1
/test/dir2/dir1/sub1
/test/dir2/dir1/sub1/test
范例6: 忽略多个目录(了解即可)
[root@oldboyedu /test]# find /test \( -path /test/dir2 -o -path /test/dir3 \) -prune -o -print <-->注意括号的空格
/test
/test/file1.txt
/test/file2.txt
/test/file3.txt
/test/dir1
/test/dir1/sub1
/test/dir1/sub1/test
/test/.file4.txt
/test/file4.txt
/test/file5.txt
范例7:
ls -l命令放在find命令的-exec选项中执行
[root@oldboyedu /test]# find . -type f -exec ls -l {} \; <-->最后以分号作为结束标志,考虑不同意义,所以要转义加\
-rw-r--r-- 1 root root 0 Apr 6 08:26 ./file1.txt
-rw-r--r-- 1 root root 0 Apr 4 14:51 ./file2.txt
-rw-r--r-- 1 root root 0 Apr 4 14:51 ./file3.txt
-rw-r--r-- 1 root root 0 Apr 4 15:01 ./.file4.txt
-rw-r--r-- 1 root root 0 Apr 6 08:27 ./file4.txt
-rw-r--r-- 1 root root 0 Apr 6 08:39 ./file5.txt
范例8:
ls -l命令放在find命令的xargs选项中执行
[root@oldboyedu /test]# find . -type f |xargs ls -l <-->xargs是一个命令,后续会讲
-rw-r--r-- 1 root root 0 Apr 6 08:26 ./file1.txt
-rw-r--r-- 1 root root 0 Apr 4 14:51 ./file2.txt
-rw-r--r-- 1 root root 0 Apr 4 14:51 ./file3.txt
-rw-r--r-- 1 root root 0 Apr 4 15:01 ./.file4.txt
-rw-r--r-- 1 root root 0 Apr 6 08:27 ./file4.txt
-rw-r--r-- 1 root root 0 Apr 6 08:39 ./file5.txt
范例9: 使用
xargs执行mv(移动文件或目录)命令例子
[root@oldboyedu /test]# ls
dir1 dir2 dir3 file1.txt file2.txt file3.txt file4.txt file5.txt
[root@oldboyedu /test]# ls dir3
[root@oldboyedu /test]# find . -name "*.txt"|xargs -i mv {} dir3/ <-->使用 -i 参数使得 { } 代表find查找到的文件
[root@oldboyedu /test]# ls
dir1 dir2 dir3
[root@oldboyedu /test]# ls dir3
file1.txt file2.txt file3.txt file4.txt file5.txt
今天就写到这里,有什么疑问或出现什么错误,随时欢迎大神们发表评论指点迷津