Linux Learning (iv)-file lookup and compression

Source: Internet
Author: User
Tags bz2 file copy stdin tar extract unpack

File Lookup and compression

1. Using the Locate command
2. Using the Find command
3. Compression and decompression tools

The locate and find commands are commonly used to find matching files on a file system, and locate is a non-real-time lookup (database lookup), and find is real-time, and is detailed later. 1.locate command? 1.locate: Depending on the database lookup, the database can not be updated in real time, so locate can not be found in real time.

?? Features: Fast lookup, Fuzzy Lookup, search is the full path of the file, not only the filename, may only search the user has read and execute permissions of the directory

? 2.locate command
       locate KEYWORD              -i 不区分大小写的搜索              -n N 只列举前N个匹配项目

Example:? Locate conf???? Search for a file with "conf" in the name or path

        locate  -r  ‘\.conf$‘  正则表达式        updatedb  更新
? 3. Database Data File location
          /var/lib/mlocate/mlocate.db
2.find command? 1.find: Real-time Find tool to complete file lookups by traversing a specified path

? Features: Search speed is slightly slower, accurate lookup, may only be searched for users with Read and Execute permissions directory

? 2.find command (wildcard characters supported)
  • Based on the search hierarchy

          find  -maxdepth  level   最大搜索目录深度,指定目录为第1级              -mindepth  level   最小搜索目录深度
  • Search by file name and Inode:

          find  -name filename      find -iname filename  不区分字母大小写

          find -inum N   按inode号查找      find -samefile filename  相同inode号的文件

    Example:? find-samefile A123

          find -links N   链接数为N的文件      find -regex "PATTERN"    以PATTERN匹配整个文件路径字符串,而不仅仅是文件名称
  • According to the genus, the genus Group looks for:

          find -user USERNAME   查找属主为指定用户(UID)的文件      find -group GRPNAME   查找属组为指定组(GID)的文件      find -uid UserID   查找属主为指定的UID号的文件      find -gid GroupID   查找属组为指定的GID号的文件      find -nouser   查找没有属主的文件      find -nogroup   查找没有属组的文件
  • Find by File type:

          find -type filetype               f: 普通文件              d: 目录文件              l: 符号链接文件              s:套接字文件             b: 块设备文件            c: 字符设备文件            p: 管道文件
  • Depending on the file size, look for:

          find -size [+|-] # UNIT               常用单位(UNIT):k, M, G,c(byte)           # UNIT: (#-1, #]                如:6k 表示(5k,6k]           -# UNIT:[0,#-1]                如:-6k 表示[0,5k]           +# UNIT:(#,∞)                如:+6k 表示(6k,∞)
  • Based on time stamp:

      以“天”为单位;        find (-atime;-ctime;-mtime) [+|-] #,               #: [#,#+1)            +#: [#+1,∞]            -#: [0,#)

? reviewed earlier: Atime is accessed by access time
????????? Mtime Modified Modify Time
????????? CTime as long as atime, mtime either side is changed

        以“分钟”为单位:              -amin ;-mmin ;-cmin
  • Search by permissions:

              find -perm  [/|-] MODE                   MODE: 精确权限匹配                   /MODE:任何一类(u,g,o)对象的权限中只要能一位匹配即可,表示或的关系                  +MODE:与/MODE相同作用,但是只能应用于CentOS7之前的系统                  -MODE:每一类对象都必须同时拥有指定权限,表示与的关系             0 表示不关注

    Example:? find-perm 755 matches a file with a permission pattern of exactly 755
    ???? find-perm/222 will match any file that has write permissions for anyone
    ???? Find-perm-222 will match files that everyone has permission to write to.
    ???? FIND-PERM-002 will match other people (other) files with Write permissions

  • Combination conditions

          与: -a         或: -o        非: -not  ;   !

    De Morgan Law

          (非 A) 或 (非 B) = 非(A 且 B)      (非 A) 且 (非 B) = 非(A 或 B)

    Example:?! A-a! B =! (A-o B)
    ????! A-o! B =! (A-a B)

  • Handling actions

              find -print:默认的处理动作,显示至屏幕          find -ls:类似于对查找到的文件执行“ls -l”命令          find -delete:删除查找到的文件

    Example:? find-size +6k-delete

              find -fls file:查找到的所有文件的长格式信息保存至指定文件中          find -ok   交互式,运行之前询问          find -exec   直接运行

    Example:? find-name "*.tmp"-ok rm-f {} \;
    ???? Find-type f-name "*.sh"-exec chmod a+x {} \;
    ???? Find-name "*.conf"-exec cp-i {} {}.bak \;

? 3.xargs command
    • Because many commands do not support pipelines | To pass the parameters, which is necessary in daily work, so there is the Xargs command
      Xargs is used to generate parameters for a command, Xargs can read stdin data, and separates stdin data into arguments with a space or carriage return character.
      Popular point is that this command does not support pipelines | To pass a parameter, such as the Find command, to use the Xargs command.
    • Note: The file name or other meaning of the noun contains a space character case
      There are also some commands that cannot accept too many arguments, and if too many arguments are executed, they may fail, or they can be resolved with Xargs
      Example: LS |xargs rm
      ???? Find/sbin-perm +7000 | Xargs ls–l
      ???? Find and Xargs format: Find | Xargs COMMAND
      3. Compression and decompression tools? 1. Compression Decompression Tool

??? SOURCE Shorthand: SRC source
??? Destination shorthand: DST targets

  • Compress

    compress srcfile    将源文件压缩成压缩包,源文件消失compress -v srcfile 或compress -v -d dstfile.Z   压缩或解压缩时显示详细信息compress -c srcfile > dstfile.Z   将源文件压缩成压缩包,源文件保留compress -d dstfile.Z  或 uncompress dstfile.Z   将压缩包解压成源文件,压缩包消失compress -d -c dstfile.Z > srcfile 或 zcat dstfile.Z > srcfile   将压缩包解压成源文件,压缩包保留
  • Gzip

    gzip srcfile    将源文件压缩成压缩包,源文件消失gzip -c srcfile > dstfile.gz    将源文件压缩成压缩包,源文件保留gzip -d dstfile.gz 或gunzip dstfile.gz    将压缩包解压成源文件,压缩包消失gzip -d -c dstfile.gz > srcfile 或 zcat dstfile.gz > srcfile    将压缩包解压成源文件,压缩包保留gzip -# srcfile    (#即1-9,表示压缩比,数字越大压缩比越高,默认为6)
  • Bzip2

     bzip2 srcfile 将源文件压缩成压缩包,源文件消失 bzip2 -k srcfile 将源文件压缩成压缩包,源文件保留且权限不变 bzip2 -c srcfile > dstfile.bz2 将源文件压缩成压缩包,源文件保留但权限根据umask值而改变 bzip2 -d dstfile.bz2 或bunzip dstfile.bz2 将压缩包解压成源文件,压缩包消失 bzip2 -k -d dstfile.bz2 或bunzip -k dstfile.bz2 将压缩包解压成源文件,压缩包保留且权限不变 bzip2 -d -c dstfile.bz2 > srcfile 或 bunzip -c dstfile.bz2 > srcfile 将压缩包解压成源文件,压缩包保留但权限根据umask值而改变 bzip2 -# srcfile (#即1-9,表示压缩比,数字越大压缩比越高,默认为9) bzcat dstfile.bz2 可以预览解压后的内容,配合> 也能解压,但权限会根据umask值而改变
  • XZ [OPTION] ... FILE ...

      xz -k: keep, 保留原文件  xz -d:解压缩  xz - #:1-9,压缩比,默认为6  xzcat: 不显式解压缩的前提下查看文本文件内容
  • Zip

       zip -r dstfile.zip  srcfile   zip -r config.zip /etc/passwd /etc/group /etc/shadow /etc/gshadow   unzip config.zip  解压缩
    ? 2. Packaging Tools
  • Tar

      tar-tf mage.tar.xz Preview the file list of the compressed package tar-rf Mage.tar filename Add files to the TAR package (note that you can only add files to the TAR package and not add ) TAR-CF Mage.tar filename1 filename2 [filenamen] Package multiple files to Mage.tar tar-zcf mage.tar.gz filename1 filename2 [filename  N] Package multiple files and gzip compress to mage.tar.gz tar-jcf mage.tar.bz2 filename1 filename2 [filenamen] Package multiple files and bzip2 compress to mage.tar.bz2 tar -JCF mage.tar.xz filename1 filename2 [filenamen] Package multiple files and use XZ compression to MAGE.TAR.XZTAR-XF Mage.tar extract all files from the tar package to the current directory Tar-xf mage. TAR.XZ filename extracts only one of the files in the compressed package to the current directory tar-zxf mage.tar.gz unzip the zip package to the current directory TAR-JXF mage.tar.bz2 Unzip the BZIP2 package to the current directory TAR-JXF MAGE.TAR.XZ decompression XZ compression package to the current directory TAR-ZXF MAGE.TAR.GZ-C/tmp unzip the ZIP package to/tmp directory TAR-ZVXF mage.tar.gz extract g Z compresses the package into the current directory and displays the detailed process tar ZCVF mage.tar.gz-t yasuolist.txt-x paichu.txt-t Specifies the list of files that need to be packaged and compressed, one row per file path-x specifies the files to exclude List, one row per file Split-b 2k-d etc.tgz 4 Divide the compressed package into 4 parts in 2K cat 40* > mage.tgz merge multiple split packages named 40 to mage.tgz  
  • Cpio?
    Cpio: The file is packaged and backed up by redirection, restoring the recovery tool, which can decompress files ending with ". Cpio" or ". Tar"
    ? cpio [Options] > file name or device name
    ? cpio [Options] < file name or device name

    • Options

      -o Package a file copy into a file or export the file to the device
      -I unpack, unzip the package file or restore the backup on the device to the system
      -T Preview, view the contents of the file or output to the file on the device
      -V Displays the file name during the packaging process
      -D Unpack the directory, and automatically build the directory when Cpio restore
      -C A newer storage method

Linux Learning (iv)-file lookup and compression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.