-->
  • Recent Articles

    Practical examples of Unix/Linux "Find" command for Everyone

    Practical examples of "Find" command  for Everyone

    find

    1. Find a file name called javac
    find / -name javac

    2. Find a file name containing  GPG
    find / -name *GPG*

    3. Find files with modification time is more than 7 days 
    find . -type f -mtime +7 -name {} \;

    4. Find files with accessed time is more than 7 days 
    find . -type f -atime +7 -name {} \;

    5. Find files which are changed more than 7 days 
    find . -type f -ctime +7 -name {} \;

    6. Find all empty file within the current directory
    find . -type f -empty

    7. Find files with .jpg extension in current directory and delete them. We can achieve this with -exec aswell.
    find . -name "*.jpg" -delete

    8. Find all directories whose name is test in root directory


    find / -type d -name test

    9. Find all the files whose permissions are 755.


    find . -type f -perm 0755 -print

    10. Find all the files without permission 755.


    find / -type f ! -perm 755

    11. Find Directories with 777 Permissions and change their permission to 755


    find . -type d -perm 777 -print -exec chmod 755 {} \;




    Example:

    [root@unisonunix02 test]# mkdir test2 ; chmod 777 test2
    [root@unisonunix02 test]# ls -ld .
    drwxr-xr-x 3 root root 19 Oct  9 10:39 .
    [root@unisonunix02 test]#
    [root@unisonunix02 test]# ls -latr
    total 4
    drwxrwxrwt. 11 root root 4096 Oct  9 10:37 ..
    drwxrwxrwx   2 root root    6 Oct  9 10:39 test2
    drwxr-xr-x   3 root root   19 Oct  9 10:39 .
    [root@unisonunix02 test]#
    [root@unisonunix02 test]# find . -type d -perm 777 -print -exec chmod 755 {} \;./test2
    [root@unisonunix02 test]# ls -latr
    total 4
    drwxrwxrwt. 11 root root 4096 Oct  9 10:37 ..
    drwxr-xr-x   2 root root    6 Oct  9 10:39 test2
    drwxr-xr-x   3 root root   19 Oct  9 10:39 .
    [root@unisonunix02 test]#

    12. To find all file called test under current root directory of owner root.


    find . -user root -name test

    Example:


    [root@unisonunix02 test]# touch test2/test
    [root@unisonunix02 test]# find . -user root -name test./test2/test
    [root@unisonunix02 test]#


    To find all files that belongs to user nginx under current directory.


    find . -user nginx

    Example:


    [root@unisonunix02 test]# ls -la test2/
    total 0
    drwxr-xr-x 2 root  root  31 Oct  9 10:46 .
    drwxr-xr-x 3 root  root  19 Oct  9 10:39 ..
    -rw-r--r-- 1 root  root   0 Oct  9 10:44 test
    -rw-r--r-- 1 nginx nginx  0 Oct  9 10:46 test2
    [root@unisonunix02 test]# find . -user nginx
    ./test2/test2
    [root@unisonunix02 test]#


    13. To find all files that belongs to group root under current directory.

    Example:
    [root@unisonunix02 test]# ls -la test2/
    total 0
    drwxr-xr-x 2 root  root  31 Oct  9 10:46 .
    drwxr-xr-x 3 root  root  19 Oct  9 10:39 ..
    -rw-r--r-- 1 root  root   0 Oct  9 10:44 test
    -rw-r--r-- 1 nginx nginx  0 Oct  9 10:46 test2
    [root@unisonunix02 test]# find . -user nginx./test2/test2
    [root@unisonunix02 test]#
    [root@unisonunix02 test]# find . -group root
    ../test2./test2/test
    [root@unisonunix02 test]#



    14. To find all the files which are modified more than 60 days back and less than 100 days.
    find . -mtime +50 -mtime -100
    Example:
    [root@unisonunix02 tmp]# find . -mtime +50 -mtime -100
    ./systemd-private-XXXXXXXXX-amkkJR
    ./systemd-private-XXXXXXX-amkkJR/tmp
    [root@unisonunix02 tmp]#

    15. To find all the files which are changed in last 1 hour.


    find . -cmin -60

    Example:
    [root@unisonunix02 tmp]# find . -cmin -60
    ../test./test/test2
    ./test/test2/test
    ./test/test2/test2

    16. To find all files less than 2MB.
    find . -size -2M

    Example:
    [root@unisonunix02 tmp]# find . -size -2M
    .
    ./.font-unix
    ./.Test-unix
    ./.XIM-unix
    ./.ICE-unix
    ./.X11-unix

    17. To find all files less than 6MB and greater than 2MB.


    find . -size +2M -size -6M

    Example:
    [root@unisonunix02 tmp]# find . -size +2M  -size -6M
    ./agent.rpm
    [root@unisonunix02 tmp]#

    18. Find compressed Files with modification time more than 7 days with their attributes
    find . -type f -mtime +7 -name '*.gz' -exec ls -latr {} \;

    19. Find compressed Files with modification time more than 7 days which their size in human readable format
    find . -type f -mtime +7 -name '*.gz' -exec ls -lh {} \;

    20. Find files with modification time more than 7 days  and copy them to another folder.
     find . -type f -mtime +7 -name '*.gz' -exec cp -pr {} /backup/ \;

    21. Find files with modification time more than 7 days  and copy them to another server.
     find . -type f -mtime +7 -name '*.gz' -exec scp -pr {} unisonunix01:/apicbackup/ \;


    No comments