黑马《linux基础编程》学习笔记(从16到20)

十六. find常用参数

黑马《linux基础编程》学习笔记(从16到20)_第1张图片

 

[root@VM_0_15_centos home]# tree test
test
|-- son2
|   |-- a
|   |   |-- bye.txt
|   |   `-- hello.txt
|   `-- son
|       `-- hello.txt
|-- son3
|   `-- hello.txt
`-- target

4 directories, 5 files

//文件名查找
[root@VM_0_15_centos home]# find test/ -name "bye.txt"
test/son2/a/bye.txt

//文件类型查找
[root@VM_0_15_centos home]# find test/ -type d
test/
test/son3
test/son2
test/son2/a
test/son2/son

//文件大小查找
[root@VM_0_15_centos home]# find test/ -size +1k
test/
test/son3
test/son2
test/son2/a
test/son2/son

//文件创建日期查找
[root@VM_0_15_centos home]# find test/ -ctime -10
test/
test/target
test/son3
test/son3/hello.txt
test/son2
test/son2/a
test/son2/a/bye.txt
test/son2/a/hello.txt
test/son2/son
test/son2/son/hello.txt

//文件访问日期查找
[root@VM_0_15_centos home]# find test/ -atime -1

//文件修改日期查找
[root@VM_0_15_centos home]# find test/ -mtime -1

//搜索n层以下的目录
[root@VM_0_15_centos home]# find test/ -maxdepth 2
test/
test/target
test/son3
test/son3/hello.txt
test/son2
test/son2/a
test/son2/son

//搜索n层以上的目录
[root@VM_0_15_centos home]# find test/ -mindepth 1
test/target
test/son3
test/son3/hello.txt
test/son2
test/son2/a
test/son2/a/bye.txt
test/son2/a/hello.txt
test/son2/son
test/son2/son/hello.txt

 十七. find高级用法

黑马《linux基础编程》学习笔记(从16到20)_第2张图片

 

[root@VM_0_15_centos home]# cd ./test/
[root@VM_0_15_centos test]# find ./ -type d -exec ls -l {} \;
total 8
drwxr-xr-x 4 root root 4096 Oct 27 05:40 son2
drwxr-xr-x 2 root root 4096 Oct 27 05:20 son3
-rw-r--r-- 1 root root    0 Oct 27 05:18 target
total 0
-rw-r--r-- 1 root root 0 Oct 27 05:20 hello.txt
total 8
drwxr-xr-x 2 root root 4096 Oct 27 05:39 a
drwxr-xr-x 2 root root 4096 Oct 27 05:21 son
total 0
-rw-r--r-- 1 root root 0 Oct 27 05:17 bye.txt
-rw-r--r-- 1 root root 0 Oct 27 05:22 hello.txt
total 0
-rw-r--r-- 1 root root 0 Oct 27 05:21 hello.txt


//-ok比-exec更安全
[root@VM_0_15_centos test]# find ./ -type d -ok ls -l {} \;
< ls ... ./ > ?
< ls ... ./son3 > ?
< ls ... ./son2 > ?
< ls ... ./son2/a > ?
< ls ... ./son2/son > ?
[root@VM_0_15_centos test]#
[root@VM_0_15_centos test]# find ./ -type d -ok ls -l {} \;
< ls ... ./ > ? y
total 8
drwxr-xr-x 4 root root 4096 Oct 27 05:40 son2
drwxr-xr-x 2 root root 4096 Oct 27 05:20 son3
-rw-r--r-- 1 root root    0 Oct 27 05:18 target
< ls ... ./son3 > ? y
total 0
-rw-r--r-- 1 root root 0 Oct 27 05:20 hello.txt
< ls ... ./son2 > ? y
total 8
drwxr-xr-x 2 root root 4096 Oct 27 05:39 a
drwxr-xr-x 2 root root 4096 Oct 27 05:21 son
< ls ... ./son2/a > ? y
total 0
-rw-r--r-- 1 root root 0 Oct 27 05:17 bye.txt
-rw-r--r-- 1 root root 0 Oct 27 05:22 hello.txt
< ls ... ./son2/son > ? y
total 0
-rw-r--r-- 1 root root 0 Oct 27 05:21 hello.txt


//或者,这里用“管道”是更有效率的。
//管道可以简单的理解为一个“缓存区”
[root@VM_0_15_centos test]# find ./ -type d | xargs ls -l
./:
total 8
drwxr-xr-x 4 root root 4096 Oct 27 05:40 son2
drwxr-xr-x 2 root root 4096 Oct 27 05:20 son3
-rw-r--r-- 1 root root    0 Oct 27 05:18 target

./son2:
total 8
drwxr-xr-x 2 root root 4096 Oct 27 05:39 a
drwxr-xr-x 2 root root 4096 Oct 27 05:21 son

./son2/a:
total 0
-rw-r--r-- 1 root root 0 Oct 27 05:17 bye.txt
-rw-r--r-- 1 root root 0 Oct 27 05:22 hello.txt

./son2/son:
total 0
-rw-r--r-- 1 root root 0 Oct 27 05:21 hello.txt

./son3:
total 0
-rw-r--r-- 1 root root 0 Oct 27 05:20 hello.txt

 十八. grep搜索文件内容

黑马《linux基础编程》学习笔记(从16到20)_第3张图片

 

[root@VM_0_15_centos home]# grep -r "answer" ./test/
./test/son2/a/bye.txt:answer
[root@VM_0_15_centos home]# grep -r "hello" ./

^C
[root@VM_0_15_centos home]# grep -r "answer" ./test/ ~-n
./test/son2/a/bye.txt:answer
grep: ~-n: No such file or directory
[root@VM_0_15_centos home]# grep -r "answer" ./test/ -n
./test/son2/a/bye.txt:1:answer

 十九. tar的压缩和解压缩

黑马《linux基础编程》学习笔记(从16到20)_第4张图片

 

[root@VM_0_15_centos home]# tar zcvf test_tar.tar.gz a1.txt a2.txt test
a1.txt
a2.txt
test/
test/target
test/son3/
test/son3/hello.txt
test/son2/
test/son2/a/
test/son2/a/bye.txt
test/son2/a/hello.txt
test/son2/son/
test/son2/son/hello.txt


[root@VM_0_15_centos home]# ls
a1.txt     a.txt     file.c      mycal          Pointer          tom
a2.txt     cjz       hello.txt   myhome.tar.gz  test             virtualenvs
apple.txt  c.txt     hello.zip   orange.txt     TestDjango       zf
a.tar.gz   date.txt  LinkToRoot  poem           test_tar.tar.gz  zsf


[root@VM_0_15_centos home]# tar zxvf test_tar.tar.gz -C ./cjz
a1.txt
a2.txt
test/
test/target
test/son3/
test/son3/hello.txt
test/son2/
test/son2/a/
test/son2/a/bye.txt
test/son2/a/hello.txt
test/son2/son/
test/son2/son/hello.txt
[root@VM_0_15_centos home]# ls
a1.txt     a.txt     file.c      mycal          Pointer          tom
a2.txt     cjz       hello.txt   myhome.tar.gz  test             virtualenvs
apple.txt  c.txt     hello.zip   orange.txt     TestDjango       zf
a.tar.gz   date.txt  LinkToRoot  poem           test_tar.tar.gz  zsf


[root@VM_0_15_centos home]# cd cjz
[root@VM_0_15_centos cjz]# ls
a1.txt  a2.txt  test
[root@VM_0_15_centos cjz]#

 二十. rar的压缩和解压缩

黑马《linux基础编程》学习笔记(从16到20)_第5张图片

 

//首先,rar是没有安装的,我们接下来先安装一下
[root@VM_0_15_centos home]# rar
-bash: rar: command not found

[root@VM_0_15_centos home]# wget http://www.rarsoft.com/rar/rarlinux-x64-5.4.0.tar.gz
--2018-10-28 19:20:34--  http://www.rarsoft.com/rar/rarlinux-x64-5.4.0.tar.gz
Resolving www.rarsoft.com (www.rarsoft.com)... 5.135.104.108
Connecting to www.rarsoft.com (www.rarsoft.com)|5.135.104.108|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.rarlab.com/rar/rarlinux-x64-5.4.0.tar.gz [following]
--2018-10-28 19:20:38--  http://www.rarlab.com/rar/rarlinux-x64-5.4.0.tar.gz
Resolving www.rarlab.com (www.rarlab.com)... 5.135.104.98
Connecting to www.rarlab.com (www.rarlab.com)|5.135.104.98|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://www.rarlab.com/rar/rarlinux-x64-5.4.0.tar.gz [following]
--2018-10-28 19:20:40--  https://www.rarlab.com/rar/rarlinux-x64-5.4.0.tar.gz
Connecting to www.rarlab.com (www.rarlab.com)|5.135.104.98|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 531782 (519K) [application/x-gzip]
Saving to: ‘rarlinux-x64-5.4.0.tar.gz’

100%[======================================>] 531,782     10.7KB/s   in 47s

2018-10-28 19:21:29 (11.0 KB/s) - ‘rarlinux-x64-5.4.0.tar.gz’ saved [531782/531782]

[root@VM_0_15_centos home]# ls
a1.txt     cjz        hello.zip      poem                       test_tar.tar.gz
a2.txt     c.txt      LinkToRoot     Pointer                    tom
apple.txt  date.txt   mycal          rarlinux-x64-5.4.0.tar.gz  virtualenvs
a.tar.gz   file.c     myhome.tar.gz  test                       zf
a.txt      hello.txt  orange.txt     TestDjango                 zsf

//用tar解压rar的安装包
[root@VM_0_15_centos home]# tar zxvf rarlinux-x64-5.4.0.tar.gz
rar/
rar/rar.txt
rar/license.txt
rar/readme.txt
rar/order.htm
rar/whatsnew.txt
rar/acknow.txt
rar/rar
rar/unrar
rar/makefile
rar/default.sfx
rar/rarfiles.lst

[root@VM_0_15_centos home]# ls
a1.txt     c.txt       mycal          rarlinux-x64-5.4.0.tar.gz  zf
a2.txt     date.txt    myhome.tar.gz  test                       zsf
apple.txt  file.c      orange.txt     TestDjango
a.tar.gz   hello.txt   poem           test_tar.tar.gz
a.txt      hello.zip   Pointer        tom
cjz        LinkToRoot  rar            virtualenvs

[root@VM_0_15_centos home]# cd rar

[root@VM_0_15_centos rar]# ls
acknow.txt   license.txt  order.htm  rarfiles.lst  readme.txt  whatsnew.txt
default.sfx  makefile     rar        rar.txt       unrar

[root@VM_0_15_centos rar]# make
mkdir -p /usr/local/bin
mkdir -p /usr/local/lib
cp rar unrar /usr/local/bin
cp rarfiles.lst /etc
cp default.sfx /usr/local/lib

//rar安装完成
[root@VM_0_15_centos rar]# cd ..
[root@VM_0_15_centos home]# ls
a1.txt     c.txt       mycal          rarlinux-x64-5.4.0.tar.gz  zf
a2.txt     date.txt    myhome.tar.gz  test                       zsf
apple.txt  file.c      orange.txt     TestDjango
a.tar.gz   hello.txt   poem           test_tar.tar.gz
a.txt      hello.zip   Pointer        tom
cjz        LinkToRoot  rar            virtualenvs

//用rar压缩一个包
[root@VM_0_15_centos home]# rar a test_rar a1.txt s2.txt cjz

RAR 5.40   Copyright (c) 1993-2016 Alexander Roshal   15 Aug 2016
Trial version             Type RAR -? for help

Evaluation copy. Please register.

Cannot open s2.txt
No such file or directory
Creating archive test_rar.rar

Adding    a1.txt                                                      OK
Adding    cjz/.bash_history                                           OK
Adding    cjz/.cache/abrt/lastnotification                            OK
Adding    cjz/.bash_profile                                           OK
Adding    cjz/a2.txt                                                  OK
Adding    cjz/test/target                                             OK
Adding    cjz/test/son3/hello.txt                                     OK
Adding    cjz/test/son2/a/bye.txt                                     OK
Adding    cjz/test/son2/a/hello.txt                                   OK
Adding    cjz/test/son2/son/hello.txt                                 OK
Adding    cjz/.bashrc                                                 OK
Adding    cjz/a1.txt                                                  OK
Adding    cjz/.bash_logout                                            OK
Adding    cjz/test/son2/a                                             OK
Adding    cjz/test/son2/son                                           OK
Adding    cjz/.config/abrt                                            OK
Adding    cjz/.cache/abrt                                             OK
Adding    cjz/test/son3                                               OK
Adding    cjz/test/son2                                               OK
Adding    cjz/.config                                                 OK
Adding    cjz/.cache                                                  OK
Adding    cjz/test                                                    OK
Adding    cjz                                                          1%
Done

[root@VM_0_15_centos home]# ls
a1.txt     c.txt       mycal          rarlinux-x64-5.4.0.tar.gz  virtualenvs
a2.txt     date.txt    myhome.tar.gz  test                       zf
apple.txt  file.c      orange.txt     TestDjango                 zsf
a.tar.gz   hello.txt   poem           test_rar.rar
a.txt      hello.zip   Pointer        test_tar.tar.gz
cjz        LinkToRoot  rar            tom

//现在来解压它
[root@VM_0_15_centos home]# ls
]          cjz        LinkToRoot     rar                        tom
a1.txt     c.txt      mycal          rarlinux-x64-5.4.0.tar.gz  virtualenvs
a2.txt     date.txt   myhome.tar.gz  test                       zf
apple.txt  file.c     orange.txt     TestDjango                 zsf
a.tar.gz   hello.txt  poem           test_rar.rar
a.txt      hello.zip  Pointer        test_tar.tar.gz
[root@VM_0_15_centos home]# rar x test_rar.rar

RAR 5.40   Copyright (c) 1993-2016 Alexander Roshal   15 Aug 2016
Trial version             Type RAR -? for help


Extracting from test_rar.rar


Would you like to replace the existing file a1.txt
     0 bytes, modified on 2018-09-02 03:07
with a new one
     0 bytes, modified on 2018-09-02 03:07

[Y]es, [N]o, [A]ll, n[E]ver, [R]ename, [Q]uit A

Extracting  a1.txt                                                    OK
Extracting  cjz/.bash_history                                         OK
Extracting  cjz/.cache/abrt/lastnotification                          OK
Extracting  cjz/.bash_profile                                         OK
Extracting  cjz/a2.txt                                                OK
Extracting  cjz/test/target                                           OK
Extracting  cjz/test/son3/hello.txt                                   OK
Extracting  cjz/test/son2/a/bye.txt                                   OK
Extracting  cjz/test/son2/a/hello.txt                                 OK
Extracting  cjz/test/son2/son/hello.txt                               OK
Extracting  cjz/.bashrc                                               OK
Extracting  cjz/a1.txt                                                OK
Extracting  cjz/.bash_logout                                          OK
All OK
[root@VM_0_15_centos home]#

 

 

 

 

 

你可能感兴趣的:(Linux网课笔记)