码迷,mamicode.com
首页 > 其他好文 > 详细

文件检索、压缩和归档

时间:2014-09-26 08:18:49      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:文件内容操作、文件检索、压缩和归档

 

文件检索、压缩和归档

 

. 实验目标

 

 熟练文件内容操作,压缩和归档,程序和文件检索

 

. 实验步骤

 

一.文件内容操作

 

1. 显示文件全部内容—— cat 命令

   格式:cat  选项  文件名

   命令选项 : -n —每一行显示行号包括空行 

[root@localhost ~]# cat /etc/hosts      //查看文件内容比较少的

[root@localhost ~]# cat -n /etc/hosts     //每一行前显示行号

[root@localhost ~]# cat /root/install.log //显示文件全部内容

[root@localhost ~]# cat -n /root/install.log //-n每一行前显示行号

[root@localhost ~]# cat -n /etc/passwd      //显示passwd行号

注:(1)cat查看文件不是目录 

(2)查看文件内容比较少的

(3)内容较多时阅读不完整

 

 

2.  More命令——可以分页显示

       格式:more  选项   文件名

[root@localhost ~]# more /root/install.log  //可以分页显示

[root@localhost ~]# type ls  //指定命令

[root@localhost ~]# ls --help //命令帮助(外部)

[root@localhost ~]# ls --help | more  //分页显示

[root@localhost ~]# cat /root/install.log | more //利用管道|对输出信息进行分页

[root@localhost ~]# more /root/install.log   //分页显示

 

 

3.  Less命令——分页显示(比more更全面)

       格式:less   选项   文件名

[root@localhost ~]# less /root/install.log   //使用less阅读大文件

 

4. head命令——默认显示文件头10行

       格式:head -n 数字 显示头n行 

[root@localhost ~]# head /etc/passwd   //默认显示文件头十行

[root@localhost ~]# head -n 2 /etc/passwd  //截取文件首2行

 

5. tail命令——默认显示文件尾10行

   格式: tail  -n 数字显示尾n行

[root@localhost ~]# tail /var/log/messages  //默认显示文件尾10行

[root@localhost ~]# tail -n 2 /etc/passwd  //截取文件尾2行

[root@localhost ~]# head -n 12 /etc/passwd | tail -n 5  //截取文

                                                     件中间8-12行

       注:截取中间:先head截取前12行,在从截取的这12行尾数5行,去两者的交集

 

 

6.  Wc命令——统计

       格式: wc  选项    文件名

        选项:  -l 统计行号

[root@localhost ~]# wc /etc/passwd     //统计

[root@localhost ~]# wc -l /etc/passwd  //计算文本的总行数或统计有多少个用户账号      

 

7. grep命令——输出包含指定字符串的行

      格式:grep    选项    查找条件    目标文件

      选项: -i忽略大小写

-v取反

^root以root开头

root$以root结尾

^$匹配空行

   -E查找多个关键字,匹配其中任意一个都输出

[root@localhost ~]# ls -l /etc/*.conf | wc -l//目录下文件个数

[root@localhost ~]# cat /etc/hosts

[root@localhost ~]# grep 127.0.0.1 /etc/hosts//输出包含指定字符串

                                                              的行

[root@localhost ~]# grep --color 127.0.0.1 /etc/hosts//检索

[root@localhost ~]# grep -v 127.0.0.1 /etc/hosts    //取反

[root@localhost ~]# grep root /etc/passwd//检索文本内容

[root@localhost ~]# grep Root /etc/passwd

[root@localhost ~]# grep -i Root /etc/passwd   //忽略大小写

[root@localhost ~]# dmesg | grep eth //查看启动消息

[root@localhost ~]# dmesg | grep sda

[root@localhost ~]# grep "^#" /etc/hosts        //^以……开头

[root@localhost ~]# grep -v "^#" /etc/hosts  //除……

[root@localhost ~]# grep "bash$" /etc/passwd  //$以……结尾

[root@localhost ~]# grep -v "^#" /etc/login.defs|grep -v "^$"

[root@localhost ~]# grep -Ev "^#|^$" /etc/login.defs //^$空行, 

[root@localhost ~]# grep -c "/bin/bash$" /etc/passwd

                                      //计算以/bin/bash$ 结尾的用户个数

[root@localhost ~]# grep -E "127.0.0.1|localhost6" /etc/hosts 

                  //查看多个关键字,匹配其中任意一个都输出

 

二.压缩和归档操作

 

 

1.zip 压缩 

 格式:zip  压缩包名   被压缩的文件名

  -r—指定对目录压缩

  解压:  

   格式:unzip 文件名.zip解压缩

    -d指定解压位置

[root@localhost ~]# ls -lh

[root@localhost ~]# ls -lh > gztest.txt

[root@localhost ~]# cat gztest.txt 

[root@localhost ~]# zip gztest gztest.txt //压缩

[root@localhost ~]# ls -lh gztest*

[root@localhost ~]# rm gztest.txt 

[root@localhost ~]# ls -lh gztest.txt

[root@localhost ~]# unzip gztest.zip   //解压缩

[root@localhost ~]# ls -lh gztest.txt

[root@localhost ~]# mkdir bak

[root@localhost ~]# ls bak

[root@localhost ~]# unzip -d bak gztest.zip //指定解压位置

[root@localhost ~]# ls bak/

[root@localhost ~]# zip -r bak bak

[root@localhost ~]# rm -r bak

[root@localhost ~]# unzip bak.zip

注:解压后原文件仍保留

 

 

2. gzip命令

      压缩

  格式:  gzip 文件名

  解压缩

      格式:gzip -d 文件名.gz

[root@localhost ~]# ls -lh gztest.txt 

[root@localhost ~]# gzip gztest.txt    //压缩

[root@localhost ~]# ls -lh gztest.*

[root@localhost ~]# gzip -d gztest.txt.gz   //解压缩

[root@localhost ~]# ls -lh gztest.*

[root@localhost ~]# bzip2 gztest.txt       //压缩

[root@localhost ~]# ls -lh gztest.*

[root@localhost ~]# bzip2 -d gztest.txt.bz2   //解压缩

[root@localhost ~]# ls -lh gztest.*

 

3. tar命令

      制作归档

      格式:tar  选项   归档文件    原文件或目录

      释放归档

      格式:tar  选项   归档文件    [-C目标目录]

      选项:

    -c创建tar包

-x解包

-t查看

-f使用归档文件

-z调用gzip

-j调用bzip2

-C指定解压位置

--remove打完包后删除原文件

tar -zcf xxx.tar.gz xxx调用gzip压缩

tar -jcf xxx.tar.bz2 xxx调用bzip2压缩

 

tar -ztf xxx.tar.gz查看xxx.tar.gz里面文件

tar -jtf xxx.tar.bz2查看xxx.tar.bz2里面文件

 

tar -zxf xxx.tar.gz解压xxx.tar.gz

tar -jxf xxx.tar.bz2解压xxx.tar.bz2

补充:

file查看文件格式

[root@localhost ~]# cd /opt/

[root@localhost opt]# rm -rf /opt/*

[root@localhost opt]# ls

[root@localhost opt]# mkdir test

[root@localhost opt]# cp /boot/* test/

[root@localhost opt]# cp -rf /boot/* test/

[root@localhost opt]# \cp -rf /boot/* test/

[root@localhost opt]# ls

[root@localhost opt]# tar -cf test.tar test/   //创建tar包

[root@localhost opt]# rm test.tar 

[root@localhost opt]# tar -cvf test.tar test/ //创建并查看详细信息

[root@localhost opt]# tar -tf test.tar  //查看包内使用归档文件

root@localhost opt]# gzip test.tar            //压缩

[root@localhost opt]# ls

[root@localhost opt]# gzip -d test.tar.gz         //解压缩

[root@localhost opt]# ls

[root@localhost opt]# rm test

[root@localhost opt]# rm -r test 

[root@localhost opt]# rm -rf test

[root@localhost opt]# tar -xf test.tar//解开.tar格式的使用归档文件

[root@localhost opt]# ls

使用bzip2对test目录进行压缩...

[root@localhost opt]# tar -zcf test.tar.gz test //制作tar.gz包 文件

gzip

[root@localhost opt]# ls

[root@localhost opt]# tar -jcf test.tar.bz2 test//制作.tar.b2包 文件

bzip2

[root@localhost opt]# ls

[root@localhost opt]# file test.tar.gz //查看文件格式

[root@localhost opt]# file test.tar.bz2 

[root@localhost opt]# ls

[root@localhost opt]# rm -rf test

[root@localhost opt]# tar -zxf test.tar.gz//解包

[root@localhost opt]# ls

[root@localhost opt]# ls -ld /root/test

[root@localhost opt]# tar -jxf test.tar.bz2 -C /root/ //释放

[root@localhost opt]# ls -ld /root/test

 

三.程序和文件检索

 

   1. 执行命令路径的变量PATH

 

      echo $PATH查看PATH的值

      作用:

  执行任何命令的时候会去从PATH的值(路径)中去搜寻是否有该命令。有就执行,  

      没有就告诉你找不到这个命令

[root@localhost ~]# echo $PATH

/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:

/root/bin

 

2. which命令——专门用于查找命令

  格式:which  [-a] 程序名

[root@localhost ~]# which ls cd   //专门用于查找命令(外部命令)

 

3. locate命令——查找速度快,需要通过updatedb生成数据库

格式:locate    选项    关键字

 

[root@localhost ~]# updatedb //更新数据库

[root@localhost ~]# touch myhttpd.conf//新建

[root@localhost ~]# ls

[root@localhost ~]# locate myhttpd.conf //查找速度快

[root@localhost ~]# updatedb 

[root@localhost ~]# locate myhttpd.conf //定位

[root@localhost ~]# rm myhttpd.conf 

[root@localhost ~]# locate myhttpd.conf 

[root@localhost ~]# updatedb 

[root@localhost ~]# locate myhttpd.conf 

 

 

4.  find命令

格式:find [路径] [条件]

默认不指定路径,就是当前路径

-type 类型

f文件

d目录

l链接

b块设备文件

c字符设备文件

 

-name名字

 

-size 大小(单位c表示byte,k表示1024bytes)

+ 大于

-小于

-a多个条件同时满足

-o多个条件满足一条即可

 

-mtime 文件内容修改

       n   n为数字,意义在n天之前的“一天之内”被更改过的文件

       +n  列出在n天之前(不含n天本身)被更改过的文件名

       -n  列出在n天之内(含n天本身)被更改过的文件名

+4代表大于等于5天前的文件名   find /var -mtime +4

-4代表小于等于4天内的文件名    find /var -mtime -4

4则是代表4~5那一天的文件名    find /var -mtime 4

-exec command

eg: find /boot -size +2048k -exec ls -l {} \;

{}find找到的内容

-exec到\;代表find额外命令开始到结束

;特殊字符,需要转义

 

[root@localhost ~]# find /boot -type l

[root@localhost ~]# file /boot/grub/menu.lst

[root@localhost ~]# ls -l /boot/grub/menu.lst 

[root@localhost ~]# find /boot -type d//查找类型 d目录

[root@localhost ~]# find / -name passwd

[root@localhost ~]# find / -name *passwd*   //只要passed全部查找

[root@localhost ~]# find / -name *passwd    //查找passwd结尾

[root@localhost ~]# find / -name passwd*   //查找passwd.

[root@localhost ~]# find / -name passwd????//查找passwd后。。

[root@localhost ~]# find /etc -name "resolv*conf"//按文件名称查找

[root@localhost ~]# find /dev -type c -a -name "tty[1-3]"

                              //c字符设备 -a多个条件需同时满足

[root@localhost ~]# ls -lh /boot/*

[root@localhost ~]# find /boot -size +2M

[root@localhost ~]# cp install.log install.new

[root@localhost ~]# ls -lh install.???

[root@localhost ~]# find -name "install.???" -mtime +30查找30天

                                            前   //按内容修改时间

[root@localhost ~]# find /boot -size +2M//大小  +大于 ,-小于

[root@localhost ~]# find /boot -size +2M -exec ls -lh {} \;

 

 

 

本文出自 “9255610” 博客,请务必保留此出处http://9265610.blog.51cto.com/9255610/1558281

文件检索、压缩和归档

标签:文件内容操作、文件检索、压缩和归档

原文地址:http://9265610.blog.51cto.com/9255610/1558281

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!