Useful Unix Commands and Tips - Part 1

In this series of blog posts, I will provide useful Unix commands and tips that are useful in everyday sysadmin life in general and weblogic/oracle middleware admins in particular. I am writing these blogs based on personal notes gathered over years mostly from web. Unfortunately, I didn’t maintain source of the articles/websites. Extremely sorry for not giving due credits.
Let us start the series with find command. I am dedicating whole blog post to find as it is versatile and useful.
find command is used to search files/directories in a directory. Using find exec option e.g., renamme, copy.
1) To find all files and directories in a given directory and sub directories.
find . # Finds all the files and directories in current directory.It searches current dir if none is specified
find /abc/xyz # Finds all the files and directories in directory named /abc/xyz
2) We can limit search results only to either files or directories.
find . -type f # limits the results to only files. f here stands for file. "." stands for current dir.find . -type d # limits the results to only dirs. d here stands for directories. "." stands for current dir.
3) To find all files with a name that contains string log:
find . -type f -name "sys.log" # -name is used to pass file name.find . -type f -name "*log" # Find all files that end with string log.find . -type f -name "log*" # Find all files that begin with string log.find . -type f -name "*log*" # Find all files that contain string log.
4) To ignore case while searching:
find . -type d -iname "logs" # Find all dirs in current dir with name logs. -iname makes search case insensitive.
5) Search content in files that was modified in x mins:
find . -type f -mmin -240 -exec grep "timed out" {} ;
6) To avoid displaying error messages while executing find command. This is especially useful when you don’t permissions on some of the files.
find . -type d -name "Hello.html" 2>/dev/null # 2>/dev/null redirects stderr to /dev/null. /dev/null throws away whatever it receives.
7) To find files based on their size:
find . -size 0 -print #To find files with zero sizefind . -size +2000k -exec du -h {} ; | sort -rn | head -10 # Find top 10 files whose size is greater than 2 MB
8) Find number of files/dirs in a directory and its sub-directories assuming you have required permissions:
find . -type f | wc -l # Total number of files.find . -type d | wc -l # Total number of dirs.

9) If file names contain spaces and want to use xargs instead of exec, the following command can be used to avoid wrong results:

find . -type f -name "*.txt" -print0 | xargs -0 grep "hello ramesh"find . -type f -name "*.txt" -exec grep "hello ramesh" {} + # Same can be achieved using -exec option
10) Rename files using find.
find . -maxdepth 2 -type f -name '*DAT'  -not -path "./backup_files/*" -print0 | xargs --null -I{} mv {} {}_09May19#Here we are renaming all the files that end with DAT by appending _09May19. Also, limiting serarch depth to 2 levels to avoid accidentally moving other files.
11) To move files modified more than 300 minutes ago:
mv `find . *.log -mtime +300 -exec ls {} ; |awk -F"/" '{print $NF}';` /prd_staging/odi_archive_bk_14Apr18/
12) To find files modified between two time periods:
find . -type f -newermt '2016-12-14 10:00' ! -newermt '2016-12-14 14:00'
13) Linux locate command is alternative to find and is much faster. It uses DB for storing file details and DB is updated once in a day. So, it is much faster but will not give latest data.

Comments

Popular posts from this blog

HOW WE REDUCED SOA OSB PROVISIONING FROM 4 DAYS TO 4 HOURS

NOT ABLE TO START RABBITMQ CLUSTER: CANNOT DECLARE A QUEUE ‘~S’ ON NODE ‘~S’: ~255P

SOA SUITE 12.2.1.4 INSTALLATION: GOT EXCEPTION WHEN AUTO CONFIGURING THE SCHEMA COMPONENT(S) WITH DATA OBTAINED FROM SHADOW TABLE