A collection of "find" command examples for Unix/Linux

<<<<<<<The find command is used to locate files on a Unix or Linux system.It can search the entire filesystem to find files and directories according to the search criteria you specify. Besides using the find command to locate files, you can also execute other Linux commands (grep, mv, rm, etc.) on the files and directories you find, which makes find extremely powerful.>>>>>>>

  • 1.  find / -name xyz
               This will search the whole system for any files named xyz and display their 
                          path names.Here we are using the criterion -name with the argument xyz to
                          tell find  to perform a name search for  the file name xyz.
      
    2. find /tmp /var/tmp . $HOME -name xyz
                          You can specify as many places to search as you wish. 
    
    3. find / -type f -mtime -7 | xargs tar -rf weekly_incremental.tar
                          Here's an example using two search criteria, will find any regular files
                          ( i.e., not directories or other special files) with the criteria “‑type f”, 
                          and  only  those modified seven or fewer days ago (“‑mtime ‑7”).
    4.  find / -name xyz | xargs /bin/rm -f
    5.  find / -name xyz -exec /bin/rm -f '{}' \;       # same thing
    6.  find / -name xyz -delete                               # same if using Gnu find
                           Another use of xargs is illustrated above.  This command will 
                           efficiently remove all files named xyz from your system (provided you 
                           run the command as root of course) 
    7.  find . -iname xyz            # find xyz, Xyz, XYz, xyZ etc, case-insensitive searching

    
    
    8.   find . -iname xyz  -type d     # Same thing but only directory
      
    9.   find . -type f \( -name "*.c" -o -name "*.sh" \) 
                            find files with different extensions
      
    10. find . -type f \( -name "*php" -o -name "*jsp" -o -name "*html" \)
                            find files with different extensions for three patterns.
     
    11. find . -type f -name "*.mp3" -exec cp {} /tmp/MusicFiles \;
                            cp *.mp3 files to /tmp/MusicFiles
     
     12. find dir1 dir2 dir3 dir4 -type d -exec cp header.shtml {} \; 
                            copy the file header.shtml to those dirs 
     
    13. find . -type d -name xyz -exec rm -r {} \; 
                           remove all subdirectories named "xyz" under current dir
     
    14. find . -mtime 1               # 24 hours
    15. find . -mtime -7              # last 7 days
    16. find . -mtime -7 -type f      # just files
    17. find . -mtime -7 -type d      # just dirs 
                            find files by modification time 
     
     
     

Comments

Post a Comment

Thank you for visiting my blog.

Popular posts from this blog

Defining Audit Rules

AIX Install packages, upgrade, patching commands

Oracle Database Quick Installation steps 11g Release 2 for Linux x86-64