Find Command examples

Find Command is one of the most important and frequently used command utility in Unix like operating systems. It is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments.

Find all file whose name is justgeek.txt

# find -name justgeek.txt

Find all files whose name starts with justgeek

# find -name justgeek.*

Find all Directories whose name is justgeek

# find -type d -name justgeek

NOTE: you can use iname instead of name if you do want to match exact case.

Find all Directories whose name is justgeek in /home

# find /home -type d -name justgeek

Find all html file in /home folder

# find /home -type f -iname "*.html"

Find all files whose name ends with .tmp in /home folder and delete them.

# find /home -type f "*.tmp" -exec rm -fv {} \;

Find Empty files

# find /tmp -type f -empty

Find empty directories

# find /tmp -type d -empty

Find all files whose size is more than 500 MB

# find /home/justgeek -type f -size +500M

Find particular files types with size more than 100 MB and delete it.

# find / -type f -name message.logs -size +100M -exec rm -fv {} \;

Find files older than 24 hours and delete them

# find /tmp/logs -type f -mmin +1440 -exec rm -fv {} \;

Find all files older then 60 Mins and move them to a different folder

# find /tmp/logs*tar -mmin +60 -exec mv {} /home/backup \;

Script to format server list into ansible format.

Script to format list of servers into ansible format.
Example:
if you have a text file which has list of servers as below.

app.server.com
app2.server.com
web1.server.com
web2.server.com
[app]
app.server.com
app2.server.com
[web]
web1.server.com
web2.server.com

How to use it ?
create a file generateInventory.sh with the contents below and it the same directory create a file named raw_inventory.txt with list of the servers and run the script using ./generateInventory.sh and you will get ansible format Inventory file will generated.

# Script to format list of servers into ansible format.
file=raw_inventory.txt
 last=''
 while read nameX
 do label="${nameX%%[.0-9]*}"
    if [[ "$label" != "$last" ]]
    then echo "[$label]" # or printf "\n$label\n" for a separator line
         last="$label"
    fi
    echo "$nameX"
 done < $file > Inventory