美文网首页
Shell Find

Shell Find

作者: lucientlau | 来源:发表于2017-09-18 21:15 被阅读0次

    Print filepath

    $ find . -print
    

    This command use '\n' as the div.

    $ find . -print0
    

    The -print0 means use '\0' as div.

    Example:

    lucient@lucient-X405UA:~/Downloads$ find . -print
    .
    ./Anaconda3-4.4.0-Linux-x86_64.sh.part
    ./scriptfifo
    ./script1_1.sh
    ./output.session
    ./out.txt
    ./tmp.txt
    ./timing.log
    lucient@lucient-X405UA:~/Downloads$ find . -print0
    ../Anaconda3-4.4.0-Linux-x86_64.sh.part./scriptfifo./script1_1.sh./output.session./out.txt./tmp.txt./timing.loglucient@lucient-X405UA:~/Downloads$ 
    lucient@lucient-X405UA:~/Downloads$ 
    

    Find filename using regexp

    lucient@lucient-X405UA:~/Downloads$ find . -name "*.txt" -print
    ./out.txt
    ./tmp.txt
    

    If use -iname means ignore capital case.

    Find filepath

    lucient@lucient-X405UA:~/Downloads$ find / -path  "*home.d*" -print
    find: ‘/boot/efi’: Permission denied
    find: ‘/sys/kernel/debug’: Permission denied
    find: ‘/sys/fs/fuse/connections/8388630’: Permission denied
    find: ‘/sys/fs/fuse/connections/8388628’: Permission denied
    find: ‘/sys/fs/fuse/connections/8388629’: Permission denied
    find: ‘/lost+found’: Permission denied
    find: ‘/tmp/systemd-private-65221f602c0344458e17bc814c1cd212-colord.service-ovcLg0’: Permission denied
    find: ‘/tmp/systemd-private-65221f602c0344458e17bc814c1cd212-rtkit-daemon.service-sYTC6U’: Permission denied
    find: ‘/tmp/systemd-private-65221f602c0344458e17bc814c1cd212-systemd-timesyncd.service-kfuu0s’: Permission denied
    /usr/share/app-install/desktop/caja:caja-home.desktop
    /usr/share/applications/nautilus-home.desktop
    ^C
    lucient@lucient-X405UA:~/Downloads$ 
    
    

    The -regex is like -path, it use regular expression

    lucient@lucient-X405UA:~/Downloads$ find . -regex ".*\(\.txt\|\.sh\)$"
    ./script1_1.sh
    ./out.txt
    ./tmp.txt
    

    Negation Find

    Use ! to negation find file
    example find file whose name is not end with .txt

    lucient@lucient-X405UA:~/Downloads$ find . ! -name "*.txt" -print
    .
    ./Anaconda3-4.4.0-Linux-x86_64.sh.part
    ./scriptfifo
    ./script1_1.sh
    ./output.session
    ./timing.log
    lucient@lucient-X405UA:~/Downloads$ 
    

    Set search fold maxdepth mindepth

    Using

    ... -maxdepth n # n is number

    set the max search depth
    Using

    ... -mindepth n # n is number

    set the minimum search depth

    Find according file type

    lucient@lucient-X405UA:~/Downloads$ find . -type f -print
    ./Anaconda3-4.4.0-Linux-x86_64.sh.part
    ./script1_1.sh
    ./output.session
    ./out.txt
    ./tmp.txt
    ./timing.log
    lucient@lucient-X405UA:~/Downloads$ 
    

    Table of file type

    FileType Parameter
    normal file f
    link symbol l
    directory d
    char device c
    block device b
    socket s
    Fifo p

    Find file according time

    Table of time parameters

    time paramteter [unit is day] parameter [unit is min]
    last access time -atime -amin
    last modify time -mtime -mmin
    last authority modify -ctime -cmin

    *Example

    lucient@lucient-X405UA:~/Downloads$ find . -type f -atime 7 -print
    lucient@lucient-X405UA:~/Downloads$ find . -type f -atime -7 -print
    ./Anaconda3-4.4.0-Linux-x86_64.sh.part
    ./script1_1.sh
    ./output.session
    ./out.txt
    ./tmp.txt
    ./timing.log
    lucient@lucient-X405UA:~/Downloads$ find . -type f -atime +7 -print
    lucient@lucient-X405UA:~/Downloads$ 
    

    Note

    7 means modify is just happened seven days ago[range is one day]
    +7 means modify is happened seven days before[range is many days]
    -7 means modify is happened within seven days

    Find according file size

    lucient@lucient-X405UA:~/Downloads$ find . -size +2k
    .
    ./Anaconda3-4.4.0-Linux-x86_64.sh.part
    lucient@lucient-X405UA:~/Downloads$ find . -size -2k
    ./scriptfifo
    ./script1_1.sh
    ./out.txt
    ./tmp.txt
    ./timing.log
    lucient@lucient-X405UA:~/Downloads$ find . -size 2k
    ./output.session
    lucient@lucient-X405UA:~/Downloads$ 
    
    

    The size unit table

    parameter unit
    b block
    c byte
    w word
    k kB
    M MB
    G GB

    Find and delete

    lucient@lucient-X405UA:~/Downloads$ ls
    Anaconda3-4.4.0-Linux-x86_64.sh.part  out.txt       timing.log
    deletedemo.txt                        script1_1.sh  tmp_sum.md5
    output.session                        scriptfifo    tmp.txt
    lucient@lucient-X405UA:~/Downloads$ find . -name "deletedemo.*" -delete 
    lucient@lucient-X405UA:~/Downloads$ ls
    Anaconda3-4.4.0-Linux-x86_64.sh.part  out.txt       scriptfifo  tmp_sum.md5
    output.session                        script1_1.sh  timing.log  tmp.txt
    

    Find according authority

    
    lucient@lucient-X405UA:~/Downloads$ ll
    total 34496
    drwxr-xr-x  2 lucient lucient     4096 9月  19 20:24 ./
    drwxr-xr-x 19 lucient lucient     4096 9月  19 20:20 ../
    -rw-------  1 lucient lucient 35290070 9月  19 20:23 Anaconda3-4.4.0-Linux-x86_64.sh.part
    -rw-rw-r--  1 lucient lucient     1120 9月  18 20:33 output.session
    -rw-rw-r--  1 lucient lucient      167 9月  19 04:15 out.txt
    -rwxrwxr-x  1 lucient lucient       50 9月  18 20:29 script1_1.sh*
    prw-rw-r--  1 lucient lucient        0 9月  18 20:56 scriptfifo|
    -rw-rw-r--  1 lucient lucient      566 9月  18 20:33 timing.log
    -rw-rw-r--  1 lucient lucient       42 9月  18 21:48 tmp_sum.md5
    -rw-rw-r--  1 lucient lucient       51 9月  19 04:10 tmp.txt
    lucient@lucient-X405UA:~/Downloads$ find . -type f -perm 664 -print
    ./output.session
    ./tmp_sum.md5
    ./out.txt
    ./tmp.txt
    ./timing.log
    lucient@lucient-X405UA:~/Downloads$ find . -type f -perm 662 -print
    lucient@lucient-X405UA:~/Downloads$ find . -type f -perm 775 -print
    ./script1_1.sh
    

    Find and exec command

    
    lucient@lucient-X405UA:~/Downloads$ find . -name "*.txt" -exec cat {}
    find: missing argument to `-exec'
    lucient@lucient-X405UA:~/Downloads$ find . -name "*.txt" -exec cat {}\;
    find: missing argument to `-exec'
    lucient@lucient-X405UA:~/Downloads$ find . -name "*.txt" -exec cat {} \;
    this is a sample text
    this is a sample text line2 
    this is a sample text
    this is a sample text line2 
         1  this is a sample text
         2  this is a sample text line2 
    this is a sample text
    this is a sample text line2 
    lucient@lucient-X405UA:~/Downloads$ ls
    Anaconda3-4.4.0-Linux-x86_64.sh.part  out.txt       scriptfifo  tmp_sum.md5
    output.session                        script1_1.sh  timing.log  tmp.txt
    lucient@lucient-X405UA:~/Downloads$ cat out.txt 
    this is a sample text
    this is a sample text line2 
    this is a sample text
    this is a sample text line2 
         1  this is a sample text
         2  this is a sample text line2 
    lucient@lucient-X405UA:~/Downloads$ cat tmp.txt 
    this is a sample text
    this is a sample text line2 
    
    

    相关文章

      网友评论

          本文标题:Shell Find

          本文链接:https://www.haomeiwen.com/subject/wsfisxtx.html