Linux 基础(六)常用命令 - find & locate & which & whereis & gzip & gunzip & tar

find & locate & which & whereis & gzip & gunzip & tar

find

在指定目录下查找文件或目录

find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:

operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race

tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]
      -context CONTEXT


actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

find 目录(不指定目录,则以当前目录查找) 查找参数
最常用的查找参数是 tests 项下的

 -name
 -user
 -group
  • 如在当前目录下查找 abc.txt
find -name abc.txt
  • 通配符查询,在opt下模糊查找 tes*.txt
find /opt -name tes*.txt

locate

通过本地数据库并非实时搜索文件,定位文件,Linux系统会为每个文件建立路径对应的本地数据库。
只要完整路径中包含 关键字的都会找出来

基本语法

locate 文件(夹)

其相关文档

[root@test-centos ~]# locate --help
Usage: locate [OPTION]... [PATTERN]...
Search for entries in a mlocate database.

  -A, --all              only print entries that match all patterns
  -b, --basename         match only the base name of path names
  -c, --count            only print number of found entries
  -d, --database DBPATH  use DBPATH instead of default database (which is
                         /var/lib/mlocate/mlocate.db)
  -e, --existing         only print entries for currently existing files
  -L, --follow           follow trailing symbolic links when checking file
                         existence (default)
  -h, --help             print this help
  -i, --ignore-case      ignore case distinctions when matching patterns
  -l, --limit, -n LIMIT  limit output (or counting) to LIMIT entries
  -m, --mmap             ignored, for backward compatibility
  -P, --nofollow, -H     don't follow trailing symbolic links when checking file
                         existence
  -0, --null             separate entries with NUL on output
  -S, --statistics       don't search for entries, print statistics about each
                         used database
  -q, --quiet            report no error messages about reading databases
  -r, --regexp REGEXP    search for basic regexp REGEXP instead of patterns
      --regex            patterns are extended regexps
  -s, --stdio            ignored, for backward compatibility
  -V, --version          print version information
  -w, --wholename        match whole path name (default)

但是这个数据库并不是实时更新的,可能最近添加的文件并未收录进去,也可能已经删除的文件没来得及移除;
默认是一周更新次,如果要修改这个频率,编辑 /etc/updatedb.conf,输入以下内容

DB_UPDATE_PERIOD="daily"

可选项为:

  • daily:每天更新一次
  • weekly:每周更新一次(默认值)
  • monthly:每月更新一次
  • never:禁止自动更新,需要手动运行updatedb命令来更新

若要立即同步,也可以手动执行命令 sudo updatedb

which

查找可执行文件。

基本语法

which 可执行文件名
which java

whereis

查找执行文件或命令相关的文件,其查询目录通常在系统标准目录
/usr
/etc
等等

基本语法

whereis 可执行文件名
whereis java

gzip

压缩或解压缩文件(默认,为压缩文件),不保留源文件,压缩多个文件时分别压缩成单个压缩文件

  • -d 参数 为解压

压缩

gzip filename

保留源文件压缩

gzip filname -c > 自定义压缩文件名

解压

gzip -d filename.gz

保留源文件解压

gzip -d filename.gz -c > 自定义文件名

gunzip

直接对gzip生成的压缩文件进行解压

解压

gunzip filename.gz

zip/unzip

同样是压缩,解压工具
但是zip可以压缩目录 -r,并且默认保留源文件,压缩多个文件默认生成到一个压缩文件

基本语法

  • 压缩文件 (-r 压缩文件夹)
zip [opts] 压缩文件名 源文件1 源文件2 ....
  • 解压 (-d 指定解压到哪个目录)
unzip [opts] 压缩文件名

tar

常用于打包,归档操作。同时可以通过 参数-z 结合 gzip 使用

基本语法

tar [opts] xx.tar.gz(如果不压缩,就tar) 要打包的文件/目录1 要打包的文件/目录2 ...

tar 命令是通过 参数来决定是打包还是解压的操作,常用参数如下

命令功能
-c打包成 tar文件
-f指定(压缩后/要解压)的文件名
-z进行 gzip 压缩或解压
-x解压 tar 文件,不包含解压 .gz
-C自定义解压到哪个目录,默认解压到当前目录下压缩文件名的目录
-v展示执行过程
-P打包的目录以绝对路径方式提供(如果目录为绝对路径,且没有-P参数,会有警告信息,-P不能放在最后 -Pzcvf

还有很多参数,具体可以参考帮助文档;以下展示部分参数

tar --help

[root@test-centos test]# tar --help
Usage: tar [OPTION...] [FILE]...
GNU `tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.

Examples:
  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
  tar -tvf archive.tar         # List all files in archive.tar verbosely.
  tar -xf archive.tar          # Extract all files from archive.tar.

 Main operation mode:

  -A, --catenate, --concatenate   append tar files to an archive
  -c, --create               create a new archive
  -d, --diff, --compare      find differences between archive and file system
      --delete               delete from the archive (not on mag tapes!)
  -r, --append               append files to the end of an archive
  -t, --list                 list the contents of an archive
      --test-label           test the archive volume label and exit
  -u, --update               only append files newer than copy in archive
  -x, --extract, --get       extract files from an archive
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值