文件查找与文件管理

可执行文件的搜索

l  Which

[root@bogon home]# which java

/usr/bin/java

显示一个可执行文件的完整路径

按照 alias ->$PATH 顺序查找

[root@bogon home]# which ls

alias ls='ls --color=tty'

        /bin/ls

l  Whereis

搜索一个可执行工具及其相关配置、帮助。

[root@bogon bin]# whereis java

java: /usr/bin/java /etc/java /usr/lib/java /usr/share/java

l  slocate

语法:

         slocate [关键字段]

         locate[关键字段]

         所有文件名及其所在路径包含关键字段的文件与目录都会显示

         slocate先将当前目录结构做成一个数据库,然后再在此数据库中搜索匹配记录

l  find

语法:

         find [路径] [参数] [表达式]

         从指定路径下递归向下搜索

         支持按照各种条件方式搜索

         支持对搜索到的文件进一步用指令操作

find /root -user root                       --查询root目录下,拥有者是root的文件

find /root -group root                     --查询root目录下,所属组是root的文件

find /etc -name services                  --根据文件名查找文件

find /etc -size +1000K                    --查找大于1M的文件

find /etc -size +1000K                    --查找小于1M的文件

find /etc -type f                                  --查找一般文件

find /etc -type b                                  --查找块设备文件

find /etc -type l                                    --查找链接文件

[root@bogon bin]# find /root -nogroup

[root@bogon bin]# find /root -nouser

没有拥有者也没有群组(有可能有黑客植入的程序)

 

[root@bogon home]# find /home -perm 0644    --查询权限为(读写,读,读)的文件

示例:

[root@bogon test]# touch 2000;touch 4000;touch 6000;touch 6600

[root@bogon test]# ls

2000  4000  6000  6600

[root@bogon test]# chmod 2000 2000

[root@bogon test]# chmod 4000 4000

[root@bogon test]# chmod 6000 6000

[root@bogon test]# chmod 6600 6600

[root@bogon test]# ls -l

total 16

------S--- 1 root root 0 Jul 14 05:42 2000

---S------ 1 root root 0 Jul 14 05:42 4000

---S--S--- 1 root root 0 Jul 14 05:42 6000

-rwS--S--- 1 root root 0 Jul 14 05:42 6600

[root@bogon test]# find /home/test -perm 6000

/home/test/6000

[root@bogon test]# find /home/test -perm -6000 --减号表示“缺1不可”,2进制的首位都是11

/home/test/6600

/home/test/6000

[root@bogon test]# find /home/test -perm +6000         --1的地方只要有一个是1就行

/home/test/6600

/home/test/6000

/home/test/4000

/home/test/2000

操作找到的文件

语法:

         find [路径] [参数] [表达式]

         -exec 指令 {} \ ;

         {} 代表find找到的文件

         \ 禁止转意

         ; 表示本行指令结束

[root@bogon test]# find /home/test -perm 6000 -exec chown jojo.jojo {} \ ;

常用的文件操作指令

l  wc 统计文件的行,词,字数

[root@bogon test]# ll

total 8

-rw-r--r-- 1 root root 7 Jul 15 06:02 abc

[root@bogon test]# cat abc

a

b

c

 

[root@bogon test]# wc abc

4 3 7 abc

[root@bogon test]# wc -l abc                    --行数

4 abc

[root@bogon test]# wc -w abc                 --字数

3 abc

[root@bogon test]# wc -c abc                   --字符数

7 abc

l  grep 显示文件中匹配关键字的行

[root@bogon test]# cat abc

a

b

c

helloworld

welcome

 

[root@bogon test]# grep "a" abc

a

[root@bogon test]# grep -n "e" abc

4:helloworld

5:welcome

[root@bogon test]# grep -v "a" abc

b

c

helloworld

welcome

 

[root@bogon test]# grep -nv "a" abc

2:b

3:c

4:helloworld

5:welcome

6:

l  sort 按序重排文本并显示(常用参数:-r:反向排序 –t:间隔 –k:按哪一栏排列)

l  diff 报告文本差异内容

[root@bogon test]# cat abc

A

b

C

[root@bogon test]# cat ABC

A

B

C

[root@bogon test]# diff ABC abc

2c2

< B

---

> b

l  comp 报告文本差异位置

l  uniq 去除文件中重复的行

[root@bogon test]# cat abc

A

b

b

C

[root@bogon test]# uniq abc

A

b

C

[root@bogon test]# cat abc

A

b

b

C

l  cut 显示文件中的某一列

[root@bogon test]# cat abc

a       1       j

b       2       k

c       3       c

[root@bogon test]# cut -f3 abc

j

k

c

[root@bogon test]# cat ABC

tom,tom@163.com

any,any@163.com

[root@bogon test]# cut -f2 -d, ABC

tom@163.com

any@163.com

[root@bogon test]# cut -c4-8 ABC           --只显示第4个字符到第8个字符

,tom@

,any@

l  paste 将文本按列拼接

[root@bogon test]# paste abc ABC > abc_ABC

[root@bogon test]# cat abc_ABC

a       1       j       tom,tom@163.com

b       2       k       any,any@163.com

c       3       c

[root@bogon test]# cat abc ABC > abc_ABC_2               --catpaste连接文件的区别

[root@bogon test]# cat abc_ABC_2

a       1       j

b       2       k

c       3       c

tom,tom@163.com

any,any@163.com

压缩

l  gzip gunzip          --linux标准压缩工具,对文本文件可以达到75%的压缩率

[root@bogon test]# gzip abc

[root@bogon test]# ls

abc.gz

[root@bogon test]# gunzip abc.gz

[root@bogon test]# ls

abc

[root@bogon test]# gzip abc

[root@bogon test]# gzip -d abc.gz

[root@bogon test]# ls

abc

[root@bogon test]# gzip abc

[root@bogon test]# zcat abc.gz               --可以使用zcat命令查看压缩过文件的内容

l  compressuncompress          --旧的Unix压缩工具

l  bzip2bunzip2        --更新的Linux压缩工具,比gzip有更高的压缩率

[root@bogon test]# bzip2 abc

[root@bogon test]# ls

abc.bz2

[root@bogon test]# bunzip2 abc.bz2

[root@bogon test]# ls

abc

[root@bogon test]# bzip2 abc

[root@bogon test]# ls

abc.bz2

[root@bogon test]# bzcat abc.bz2          --可以使用bzcat命令查看压缩过的文件

tar备份

用于在磁带机,软盘,zip设备上做备份。也可以备份在一个硬盘上。

主要参数:

c:将文件备份出来

v:将过程输出

x:从一个文件中解出备份

范例:

备份: tar cvf backup.tar *.txt                        --f参数后跟的是打包后的文件名(file?

         解开: tar xvf backup.tar –C backup/

压缩:

[root@bogon home]# tar -cvf test.tar test

test/

test/abc.bz2

[root@bogon home]# ls

jojo  test  test.tar

解压:

[root@bogon home]# rm -rf test

[root@bogon home]# ls

jojo  test.tar

[root@bogon home]# tar -xvf test.tar

test/

test/abc.bz2

[root@bogon home]# ls

jojo  test  test.tar

查看tar包的内容:

[root@bogon home]# tar -tf test.tar

test/

test/abc.bz2

tar 中的-C参数:可以转路径

[root@bogon home]# ls

backup  jojo  test  test.tar

[root@bogon home]# tar -xvf test.tar -C backup/

test/

test/abc.bz2

[root@bogon home]# ls -l backup/test/

total 8

-rw-r--r-- 1 root root 92 Jul 16 06:05 abc.bz2

tar 压缩共同使用

[root@bogon home]# ls

jojo  test

[root@bogon home]# tar -zcvf test.tar.gz test               --z参数就是压缩参数

test/

test/abc

[root@bogon home]# ll

total 24

drwx------ 3 jojo jojo 4096 Jun 13 21:58 jojo

drwxr-xr-x 2 root root 4096 Jul 16 06:37 test

-rw-r--r-- 1 root root  216 Jul 16 06:39 test.tar.gz                  --test比较大小,压缩成功

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值