文件查找:

1.  which    查找可以执行文件,只搜索$PATH里的目录

$ which ls
/bin/ls

which只搜索$PATH里的目录,如果搜索当前目录的文件是没有任何结果的

$ ls -l
总用量 0
-rw-rw-r-- 1 huanghao huanghao 0 3月 1 12:24 a.txt
$ which a.txt

2. whereis    与which差不多,只不过它会查找帮助文件,还会查找库文件

$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz $ whereis libip6tc.so.0
libip6tc.so: /lib/libip6tc.so.0 #搜索一个文件,不管文件存在不存在,返回结果永远是不带扩展名的文件名+ :
$ whereis b.txt
b:

3. slocate,  locate

这两个放在一起说,是因为slocate实际上软链接到了locate,它俩是一样的,ubuntu里是mlocate

# locate passwd
/etc/passwd
/etc/passwd-
/etc/cron.daily/passwd
/etc/init/passwd.conf
/etc/pam.d/chpasswd
/etc/pam.d/passwd
/etc/security/opasswd
/home/huangxm/passwd
/home/huangxm/passwd-
/home/huangxm/cron.daily/passwd
/home/huangxm/init/passwd.conf

它把所有包含passwd的文件都查找出来了,我们新建一个文件newfile.txt,查找一下

# locate newfile.txt

却没有返回任何结果,可以newfile.txt明明存在,但它是刚刚创建的。实际上locate并不是在文件系统里查找文件,而是在它的数据库/var/lib/slocate/slocate.db中查,ubuntu 是 /vra/lib/mlocate/mlocate.db

接下来更新一下该数据库

# updatedb
# locate newfile.txt
/home/huanghao/test/newfile.txt

找到了,所以如果数据库不更新,locate是无法查找到新创建的文件的;但是总不能每次使用都更新db吧。 所以linux中每天有计划任务来更新db,看一下

# pwd
/etc/cron.daily
# ll mlocate
-rwxr-xr-x 1 root root 435 6月 20 2013 mlocate*

在每天的任务中有一个mlocate*的任务,打开看一下

#! /bin/bash

set -e

[ -x /usr/bin/updatedb.mlocate ] || exit 0

if which on_ac_power >/dev/null 2>&1; then
ON_BATTERY=0
on_ac_power >/dev/null 2>&1 || ON_BATTERY=$?
if [ "$ON_BATTERY" -eq 1 ]; then
exit 0
fi
fi # See ionice(1)
if [ -x /usr/bin/ionice ] &&
/usr/bin/ionice -c3 true 2>/dev/null; then
IONICE="/usr/bin/ionice -c3"
fi flock --nonblock /run/mlocate.daily.lock $IONICE /usr/bin/updatedb.mlocate
~

先不管能不能看懂,最后有updatedb的操作。

4. find   这个就是踏踏实实的从硬盘查找文件了

# find newfile.txt             #在当前目录中查找

# find / -name a.txt        #从/开始查找文件a.txt

# find / -name "*newfile*'"     #从/开始查找所有包含newfile的文件

# find / -name "*newfile*'"   -ls      #查找并列出文件属性

# find / -name "*newfile*'"   -exec file {} \;  #查找并执行file命令 {}的意思是将查找的结果放进去

# find / -name "*newfile*'" -ok rm {} \;   #查找并删除,-ok是提示需要交互, -exec是不提示,直接执行

find /home -user shrek -ls     #查找属于用户shrek的文件并列出来

find /home -user shrek -a -group shrek -a -type d -ls    #查找属于用户shrek并且属于组shrek的目录 -a是and的意思

#其它参数
-user -group -type -perm 权限 -size 大小 -mtime 时间
-a and -o or

详细看一下权限查找

#  find / -perm +777  -type d -ls

权限是用9位二进制数表示的,+777 的意思是指9位中任何一位有1的权限,通俗点说就是任意权限

# find / -perm -777 -type d –ls

-777 的意思是所有位都是1,即查找777权限的目录,这个操作还是很有用的。

5. grep   这个是查找文件内容的

# grep root /etc/passwd   #在passwd中查找包含root的行
root:x:0:0:root:/root:/bin/bash
# grep -R root /etc              #在/etc下所有文件及子文件中查找
/etc/anacrontab:HOME=/root
/etc/anacrontab:LOGNAME=root
/etc/cron.weekly/man-db: chown man:root /var/cache/man || true
/etc/shadow:root:$6$TXA2sjeg$/yQGd91.Fq0kF6RNkT.sHkCzdwGs8yU4UczEnVAO.Td7sBweenU.R0Gcn2DBwXhos/n6tircXjxWkl.3voaLA.:16847:0:99999:7:::

上面递归查找时显示了很多内容,如果我们只想显示包含查找内容的文件,可加l参数

# grep -R -l root /etc
/etc/anacrontab
/etc/cron.weekly/man-db
/etc/shadow
/etc/sysctl.d/10-kernel-hardening.conf
/etc/passwd
/etc/group
/etc/cron.daily/man-db
/etc/cron.daily/apt
/etc/grub.d/20_linux_xen
/etc/grub.d/30_os-prober
/etc/grub.d/20_memtest86+
/etc/grub.d/05_debian_theme

总结一下:

1.  如果查找可执行文件,用which

2.  locate查找文件速度非常快,但是需要更新db,如果没有更新,可能查不到最新的文件

3.  find 很强,但是查找速度慢

4.  如果要查找文件中的内容,用grep

文件压缩及解压

1. gzip      压缩成.gz, 原文件会被删除 , 压缩率较高

gunzip   解压

gzip –d   和上面一样,解压

$ ls -l
总用量 0
-rw-rw-r-- 1 huanghao huanghao 0 3月 1 12:24 a.txt
-rw-rw-r-- 1 huanghao huanghao 0 3月 1 12:30 newfile.txt $ gzip a.txtt$ ls -l
总用量 4
-rw-rw-r-- 1 huanghao huanghao 26 3月 1 12:24 a.txt.gz
-rw-rw-r-- 1 huanghao huanghao 0 3月 1 12:30 newfile.txt
$ gzip -d a.txt.gz
$ ll
总用量 8
drwxrwxr-x 2 huanghao huanghao 4096 3月 1 13:55 ./
drwxr-xr-x 19 huanghao huanghao 4096 3月 1 12:24 ../
-rw-rw-r-- 1 huanghao huanghao 0 3月 1 12:24 a.txt
-rw-rw-r-- 1 huanghao huanghao 0 3月 1 12:30 newfile.txt

bzip2     bunzip2        bunzip2 –d     压缩率比gzip高

$ bzip2 a.txt
$ ls -l
总用量 4
-rw-rw-r-- 1 huanghao huanghao 14 3月 1 12:24 a.txt.bz2
-rw-rw-r-- 1 huanghao huanghao 0 3月 1 12:30 newfile.txt $ bzip2 -d a.txt.bz2
$ ls -l
总用量 0
-rw-rw-r-- 1 huanghao huanghao 0 3月 1 12:24 a.txt
-rw-rw-r-- 1 huanghao huanghao 0 3月 1 12:30 newfile.txt

tar命令

tar本来是打包命令,是将一系列文件及文件夹打包,现在tar命令也可以用来压缩,先看一下有哪些参数

-c 创建打包文件
-v 将打包过程输出
-x 解包
-r 将文件添加到已存在的tar包中
-t 查看包内容
-z 压缩
$ tar cvf a.tar .
./
./a.txt
tar: ./a.tar: 文件是归档文件;未输出
./newfile.txt

后面的.是指当前目录, 将当前目录打包成a.tar,并存放在当前目录中

$ ls -l
总用量 12
-rw-rw-r-- 1 huanghao huanghao 10240 3月 1 14:00 a.tar #这个是打包文件
-rw-rw-r-- 1 huanghao huanghao 0 3月 1 12:24 a.txt
-rw-rw-r-- 1 huanghao huanghao 0 3月 1 12:30 newfile.txt

来看一下打包文件的内容

$ tar tvf a.tar
drwxrwxr-x huanghao/huanghao 0 2016-03-01 14:00 ./
-rw-rw-r-- huanghao/huanghao 0 2016-03-01 12:24 ./a.txt
-rw-rw-r-- huanghao/huanghao 0 2016-03-01 12:30 ./newfile.txt
$ tar cvf b.tar ~/test
tar: 从成员名中删除开头的“/”
/home/huanghao/test/
/home/huanghao/test/a.txt
/home/huanghao/test/a.tar
/home/huanghao/test/newfile.txt
$ tar -tvf b.tar
drwxrwxr-x huanghao/huanghao 0 2016-03-01 14:00 home/huanghao/test/
-rw-rw-r-- huanghao/huanghao 0 2016-03-01 12:24 home/huanghao/test/a.txt
-rw-rw-r-- huanghao/huanghao 10240 2016-03-01 14:00 home/huanghao/test/a.tar
-rw-rw-r-- huanghao/huanghao 0 2016-03-01 12:30 home/huanghao/test/newfile.txt

这里压缩包的内容里文件都带上了路径,注意这两种方式的区别。

当然也可以将不同目录的文件打包到一个文件中:

$ tar cvf b.tar a.txt /etc/passwd
a.txt
tar: 从成员名中删除开头的“/”
/etc/passwd

解压:

$ tar xvf a.tar             #解压到当前文件夹
./
./a.txt
./newfile.txt
$ tar xvf a.tar -C ./a #解压到当前目录的目录a中
./
./a.txt
./newfile.txt

打包并压缩’

$ tar cvfz c.tar.gz /etc/passwd a.txt     #也可以打包压缩不同目录的文件
tar: 从成员名中删除开头的“/”
/etc/passwd
a.txt

同样解压也可以用-C解压到其它目录中

$ tar xvfz c.tar.gz -C ./a
etc/passwd
a.txt

linux - 文件查找及压缩的更多相关文章

  1. linux基础—课堂随笔04_文件查找和压缩

    文件查找和压缩 文件查找 1.locate   这个命令是对其生成的数据库进行遍历(生成数据库的命令:updatedb),这一特性决定了用locate查找文件速度很快,但是locate命令只能对文件进 ...

  2. Linux文件查找.md

    Linux 文件查找 在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 w ...

  3. 07.进程管理+作业控制+文件查找与压缩+文件压缩与打包+tar打包解包+NFS

    进程管理 程序放在磁盘上叫文件,把它复制到内存,并在cpu运行,就叫进程, 进程多少也反映当前运行程序的多少 进程在系统中会为每个进程生成一个进程号,在所有的进程中有一个特殊进程即init进程, 它是 ...

  4. Linux文件查找命令find用法整理(locate/find)

    Linux文件查找查找主要包括:locate和find 1.locate 用法简单,根据数据库查找,非实时,用法: locate FILENAME 手动更新数据库(时间可能较长) updatedb 2 ...

  5. linux文件查找find命令

    linux文件查找find命令 1.文件查找 基本介绍 在文件系统上查找符合条件的文件 linux上常见的文件查找工具:find命令 查找分类 实时查找 精确查找 基本语法 find  [option ...

  6. linux 文件查找,which,whereis,locate,find

    linux 文件查找,which,whereis,locate,find 一:which 主要用于查找可执行命令的所在位置: 如图,查找命令 ls的目录: 二:whereis 主要用于查找命令的帮助文 ...

  7. Linux文件查找命令find,xargs详述【转】

    转自:http://blog.csdn.net/cxylaf/article/details/4069595 转自http://www.linuxsir.org/main/?q=node/137 Li ...

  8. Linux 文件查找

    在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 whereis wher ...

  9. Linux文件查找与打包

    一.文件查找 locate与find是经常使用的Linux 命令,刚接触Linux时对这两个命令的使用傻傻的分不清.现在我们来对比一下两个命令到底有哪些区别. 1.1 locate locate让使用 ...

随机推荐

  1. ZOJ 3903 Ant(数学,推公示+乘法逆元)

    Ant Time Limit: 1 Second      Memory Limit: 32768 KB There is an ant named Alice. Alice likes going ...

  2. codeforce 630N Forecast

    N. Forecast time limit per test 0.5 seconds memory limit per test 64 megabytes input standard input ...

  3. [iOS基础控件 - 6.10.4] 项目启动原理 项目中的文件

    A.项目中的常见文件 1.单元测试Test   2.Frameworks(xCode6 创建的SingleView Project没有) 依赖框架   3.Products 打包好的文件   4. p ...

  4. [iOS基础控件 - 6.9.3] QQ好友列表Demo TableView

    A.需求 1.使用plist数据,展示类似QQ好友列表的分组.组内成员显示缩进功能 2.组名使用Header,展示箭头图标.组名.组内人数和上线人数 3.点击组名,伸展.缩回好友组   code so ...

  5. Quality Center 使用IE8异常浏览器打开

    需要装2个软件 1.  Microsoft Visual C++ 2005 Redistributable Package (x64) 2.  dotnetfx35.exe 配置IE的选项 使用兼容模 ...

  6. WS103C8例程——串口2【worldsing笔记】

    在超MINI核心板 stm32F103C8最小系统板上调试Usart2功能:用Jlink 6Pin接口连接WStm32f103c8的Uart2,PC机向mcu发送数据,mcu收到数据后数据加1,回传给 ...

  7. POJ 3694 Network (tarjan + LCA)

    题目链接:http://poj.org/problem?id=3694 题意是给你一个无向图n个点,m条边,将m条边连接起来之后形成一个图,有Q个询问,问将u和v连接起来后图中还有多少个桥. 首先用t ...

  8. CodeForces 705C Thor (模拟+STL)

    题意:给定三个操作,1,是x应用产生一个通知,2,是把所有x的通知读完,3,是把前x个通知读完,问你每次操作后未读的通知. 析:这个题数据有点大,但可以用STL中的队列和set来模拟这个过程用q来标记 ...

  9. js写的替换字符串(相当于js操作字符串的一个练习)

    1.达到的效果 1./main_1.do,/left_1.do -> main:1,left:1 2./tbl_type/v_list_{id}.do -> tbl_type:list:{ ...

  10. Linux命令行访问网页

    找到个好资料,备份行: http://hi.baidu.com/oyvfhp/blog/item/3aa5ced5b40563d351da4bb0.html   CURL --- 命令行浏览器 这东西 ...