ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Shell命令-文件及目录操作之cp、find

2019-04-13 10:55:23  阅读:293  来源: 互联网

标签:oldboyedu dir2 dir1 Shell test txt cp root find


文件及目录操作 - 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

今天就写到这里,有什么疑问或出现什么错误,随时欢迎大神们发表评论指点迷津

标签:oldboyedu,dir2,dir1,Shell,test,txt,cp,root,find
来源: https://blog.51cto.com/14068656/2378130

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有