• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • Life always offers you a second chance ... Its called tomorrow !!!

    Tuesday, April 08, 2014

    10 Practical Examples for using FIND Command in Linux

    Here I have tried to show you a list of options or arguments which you can use with find command to narrow down your search queries.

    1. Find all the files with respect to their permission


    using octal method
    # find / -perm 755 -type f
    using symbolic method
    # find / -perm u=rwx,g=rx,o=rx -type f
    Find all files with executable permission
    # find / -executable -type f
    Find all files with writable permission
    # find / -writable -type f
    Find all files with readable permission
    # find / -readable -type f

    2. Locate files with respect to their special permission


    Find all the executable files inside /usr/bin, /bin/, /usr/sbin/, /sbin/ with SGID
    # find /usr/bin/ /bin/ /usr/sbin/ /sbin/ -perm +2000 -execuatble
    /usr/bin/ssh-agent
    /usr/bin/locate
    /usr/bin/wall
    /usr/bin/write

    Find all the executable files inside /usr/bin, /bin/, /usr/sbin/, /sbin/ with SUID
    # find /usr/bin/ /bin/ /usr/sbin/ /sbin/ -perm +4000 -execuatble
    /usr/bin/ksu
    /usr/bin/gpasswd
    /usr/bin/chsh
    /usr/bin/chage
    /usr/bin/sudo

    3. Locate files with respect to size


    Find all the files with more than 30MB size
    # find / -size +30M -type f
    Find all the files with more than 1GB size
    # find / -size +1G -type f
    Find all the files with more than 6KB size
    # find / -size +6k -type f
    NOTE: In case you want to find files/directories with size less than, use "-" sign instead of "+" sign in above examples

    4. Find files with respect to their owners


    Find all the files whose user owner is deepak
    # find / -user deepak -type f
    Find all the files whose group owner is deepak
    # find / -group deepak -type f
    NOTE: You can also use -uid and -gid argument respectively instead of -user and -group

    5. Saving o/p to file with extra parameter


    This command will locate for all the files/directories with user owner deepak and save the o/p to file.txt (the o/p will resemble same as ls -l)
    # find / -user deepak  -fls file.txt
    # cat file.txt
       210    4 drwx------   9 deepak   deepak       4096 Apr  5 20:26 /home/deepak
       868    4 -rw-r--r--   1 deepak   deepak        176 Jul 18  2013 /home/deepak/.bash_profile
      7053    4 -rw-r--r--   1 deepak   deepak        124 Jul 18  2013 /home/deepak/.bashrc
      7078    4 drwxrwxr-x   2 deepak   deepak       4096 Apr  5 20:26 /home/deepak/mywork
      7083    4 -rw-------   1 deepak   deepak         99 Apr  5 20:27 /home/deepak/.bash_history
      7080    4 drwxrwxr-x   2 deepak   deepak       4096 Apr  5 20:26 /home/deepak/work
      7073    4 drwxr-xr-x   2 deepak   deepak       4096 Nov 12  2010 /home/deepak/.gnome2
      7074    4 drwxr-xr-x   4 deepak   deepak       4096 Mar 14 02:38 /home/deepak/.mozilla

    NOTE: If the files does not exist it will be created, and if it already exists then it will be overwritten

    This command will just save the o/p to file.txt
    # find / -user deepak  -fprint file.txt
    # cat file.txt
    /home/deepak
    /home/deepak/.bash_profile
    /home/deepak/.bashrc
    /home/deepak/mywork
    /home/deepak/.bash_history
    /home/deepak/work
    /home/deepak/.gnome2
    /home/deepak/.mozilla

    6. Add an extra argument along with find command


    This command will show you long list details of all the files with size more than 50MB
    # find / -size +50M  -ls
    270138 65588 -rw-r--r--   1 root     root     67159234 Nov 23 00:20 /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_64/jre/lib/rt.jar
    134253 96796 -rw-r--r--   1 root     root     99158576 Mar 14 02:39 /usr/lib/locale/locale-archive
      7291    0 -rw-------   1 root     root     134217728 Apr  5 16:49 /sys/devices/pci0000:00/0000:00:0f.0/resource1
      7292    0 -rw-------   1 root     root     134217728 Apr  5 20:17 /sys/devices/pci0000:00/0000:00:0f.0/resource1_wc

    7. Find files irrespective of case


    This command will find all the files/directories inside /home with name "test" irrespective of case
    # find /home/ -iname test
    /home/deepak/test
    /home/deepak/Test

    8. Find files/directory with respect to their name


    Find all the files/directories with name "test"
    # find /home/ -name test
    /home/deepak/test

    9. Find files using wildcard


    This command will search all the files with .txt extentsion
    # find / -name *.txt -type f
    This command will search all the files from file[0-9] with .txt extentsion
    # find / -name file?.txt -type f

    10. Find files with respect to their modification/access time


    Find all the directories accessed before 5 min
    # find / -amin -5 -type d
    Find all the directories accessed 60 min back from current time
    # find / -amin +60 -type d
    Find all the files/directories accessed exactly 5 min back
    # find / -amin -type d
    Find all the files/directories accessed 5 days back
    # find / -atime -5 -type d
    NOTE: You can run all the above commands as per the modification time just by replacing "-atime" with "-mtime"

    Complex example


    Locate all files with name test.tx inside /home/deepak and remove them
    # find /home/deepak/ -name test.txt -type f | xargs rm -f

    or

    # find /home/deepak/ -name test.txt -type f -exec rm -f {} \;


    Please do let me know your feedback or in case I missed any thing.

    Related Articles
    8 examples to help you understand top command usage in Unix/Linux

    Follow the below links for more tutorials

    Configure Red Hat Cluster using VMware, Quorum Disk, GFS2, Openfiler
    Tutorial for Monitoring Tools SAR and KSAR with examples in Linux
    How to configure Samba 4 Secondary Domain Controller
    How to secure Apache web server in Linux using password (.htaccess)
    How to register Red Hat Linux with RHN (Red Hat Network )
    Red hat Enterprise Linux 5.5 Installation Guide (Screenshots)
    Why is Linux more secure than windows and any other OS
    What is the difference between "su" and "su -" in Linux?
    What is swappiness and how do we change its value?
    How to log iptables messages in different log file
    What are the s and k scripts in the etc rcx.d directories
    How to check all the currently running services in Linux
    How to auto start service after reboot in Linux
    What is virtual memory, paging and swap space?

    0 comments:

    Post a Comment