[Command] find common Linux commands

find - search for files in a directory hierarchy

find command to find the file in the specified directory.

Any string parameters located before will be considered directory name you want to find.

If you use this command does not set any parameters, the find command will find subdirectories and files in the current directory. And will look into all the subdirectories and files are displayed.

 

Syntax:
  the Find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path ...] [expression The]

 

parameter:

  • -type file types:
    • File f (file),
    • Directory d (directory),
    • String c (character),
    • Block b (block),
    • s(socket),
    • l (link) symbolic link
  • -name name lookup then press the "filename", support for regular expressions and wildcards
  • -mtime find the file (modify) in accordance with mtime the file modification time, the document was modified last time
  • -atime access time (access), the database file was last accessed time
  • -ctime time change (change), the contents of the file was modified last time
  •  ! Negate
    • All files find. -Type f! -Name 'file.txt' is not the name of file.txt
  • -perm 644,755 permission lookup
  • -size n File Size
    • find. -size + 10k 10k file larger than
    • File size is n units, b represents the block group 512 yuan, c denotes the number of characters, k denotes kilo bytes, w is two bytes
  • -exec command execution
    • find /data -type f -name 'a.txt' -exec rm {} \; 
    • In the specified directory / data types below to find the file file, named a.txt file, the contents will be found to find the {}, the delete command.

 

Example:

Delete files found

[root@oldboy data]# echo test >a.txt
[root@oldboy data]# ls
a.txt  oldboy.txt
[root@oldboy data]# find /data -type f -name 'a.txt' -exec rm {} \;   
[root@oldboy data]# ls
oldboy.txt
[root@oldboy data]# find /data -type f -name 'oldboy.txt' -exec rm {} \;
[root@oldboy data]# ls

 

 

 

Create a batch file .txt 1-10, all .txt files found by the Find command to the xargs command via the pipe character, all the files in a row, and then forcibly removed by the rm -f.

[root@oldboy data]# touch {1..10}.txt
[root@oldboy data]# ls
10.txt  2.txt  4.txt  6.txt  8.txt
1.txt   3.txt  5.txt  7.txt  9.txt
[root@oldboy data]# find /data -type f -name '*.txt' |xargs
/data/2.txt /data/5.txt /data/3.txt /data/9.txt /data/7.txt 
/data/1.txt /data/4.txt /data/10.txt /data/8.txt /data/6.txt
[root@oldboy data]# find /data -type f -name '*.txt' |xargs rm -f
[root@oldboy data]# ls

When you delete a file or directory in the script, use ** ** production environment is generally:

  • And with the use of xargs
    • Deleted in / logs directory, modified the previous 15 days '.log' files
      • find /logs -type f -name '\*.log' -mtime +15 |xargs rm -f
    • Deleted in / logs directory to modify the ending time of oldboy a further 30 days ago directory
      • find /logs-type d -name '\*oldboy' -mtime +30 |xargs rm -rf
  • Use the find -exec
    • Looking for changes in the time / logs directory in the previous 5 files and delete them
      • find /logs -type f -name '\*.log' -mtime +5 -exec rm {} \;

 

 

 

  • Find -mtime modify time by modification time
    • +7 representatives of seven days ago [long ago, up to 7 days ago)
    • 7 represents the most recent day 7 day
    • -7 representative of the last seven days (7 days, and today now)

Important note: the Find command is a very important production environment in a command, go to master .

 

Guess you like

Origin www.cnblogs.com/zoe233/p/11815624.html