linux - 文件查找及压缩
文件查找:
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 - 文件查找及压缩的更多相关文章
- linux基础—课堂随笔04_文件查找和压缩
文件查找和压缩 文件查找 1.locate 这个命令是对其生成的数据库进行遍历(生成数据库的命令:updatedb),这一特性决定了用locate查找文件速度很快,但是locate命令只能对文件进 ...
- Linux文件查找.md
Linux 文件查找 在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 w ...
- 07.进程管理+作业控制+文件查找与压缩+文件压缩与打包+tar打包解包+NFS
进程管理 程序放在磁盘上叫文件,把它复制到内存,并在cpu运行,就叫进程, 进程多少也反映当前运行程序的多少 进程在系统中会为每个进程生成一个进程号,在所有的进程中有一个特殊进程即init进程, 它是 ...
- Linux文件查找命令find用法整理(locate/find)
Linux文件查找查找主要包括:locate和find 1.locate 用法简单,根据数据库查找,非实时,用法: locate FILENAME 手动更新数据库(时间可能较长) updatedb 2 ...
- linux文件查找find命令
linux文件查找find命令 1.文件查找 基本介绍 在文件系统上查找符合条件的文件 linux上常见的文件查找工具:find命令 查找分类 实时查找 精确查找 基本语法 find [option ...
- linux 文件查找,which,whereis,locate,find
linux 文件查找,which,whereis,locate,find 一:which 主要用于查找可执行命令的所在位置: 如图,查找命令 ls的目录: 二:whereis 主要用于查找命令的帮助文 ...
- Linux文件查找命令find,xargs详述【转】
转自:http://blog.csdn.net/cxylaf/article/details/4069595 转自http://www.linuxsir.org/main/?q=node/137 Li ...
- Linux 文件查找
在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 whereis wher ...
- Linux文件查找与打包
一.文件查找 locate与find是经常使用的Linux 命令,刚接触Linux时对这两个命令的使用傻傻的分不清.现在我们来对比一下两个命令到底有哪些区别. 1.1 locate locate让使用 ...
随机推荐
- linux高级数据存储
linux内此存储模式由5部分组成,自低向上的顺序: 物理卷,内核块设备驱动,内核文件系统驱动,虚拟文件系统,应用程序数据结构; 系统中所有的文件仅按此模式存储,无论是数据还是元数据,均在此模式下统一 ...
- 【转】CocoaPods的安装以及遇到的坑
一.CocoaPods是什么? CocoaPods是一个用Ruby写的.负责管理iOS项目中第三方开源库的工具,CocoaPods能让我们集中的.统一管理第三方开源库,为我们节省设置和更新第三方开源库 ...
- HDU 5805 NanoApe Loves Sequence (模拟)
NanoApe Loves Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5805 Description NanoApe, the ...
- Spring Filter components in auto scanning
In this Spring auto component scanning tutorial, you learn about how to make Spring auto scan your c ...
- linux下vi命令的使用
linux vi命令详解 刚开始学着用linux,对vi命令不是很熟,在网上转接了一篇. vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单 ...
- CCS3.3入门
CCS3安装包下载地址:http://pan.baidu.com/share/link?shareid=1887452332&uk=1799203308&qq-pf-to=pcqq.c ...
- python的sys.path
python检测不到模块: No module named 是因为模块没有在sys.path中,查看sys.path的方法 import sys sys.path 发现确实没有加载到模块. windo ...
- 在SQL Server 2014下面使用的SQL2000的Northwind和Pubs示例数据库
在SQL Server 2014下面使用的SQL2000的Northwind和Pubs示例数据库 http://files.cnblogs.com/files/lxzhangying/SQl2000S ...
- Windows下sqlmap的使用_01
环境:win8.1 64位 一.下载 首先,需下载SqlMap以及适用于Windows系统的Python.下载地址如下: 1.1.SqlMap下载地址:https://github.com/ ...
- Hibernate 中createQuery与createSQLQuery
本文原址 : http://stta04.javaeye.com/blog/377633 hibernate 中createQuery与createSQLQuery 昨晚帮同事看代码到凌晨2点多,今早 ...