Following post is an attempt to list most used and interesting use of unix find command.
you might find all of tutorials about this command but I tired to list down the once I used in my day to day life.
General syntax:
find /path [expression]
find files under a directory ignoring case
find /usr -iname sample.txt
To find a directory in "sample" folder.
find / -type d -name sample
find all files ending with .txt and print first 3 results
find . -name "*.txt" | head -n 3
find all files ending with ".java" or ".cpp"
find . -name "*.java" -o -name "*.cpp"
find all files with specific format extension which are modified in last 1 days.
to find all files modified in last 1 day with cpp extension.
find . -name "*.cpp" -mtime -1
Replace all occurrence of a string all occurrence in all files
find ./ -type f -exec sed -i 's/3094/3081/' {} \;
Find all files on the specific directory not modified in 1 years (365days)
find /dummy -mount -mtime 365| head -n 5
Find path of file where a particular string is found , in current directory
find . -name \* -exec grep "VISIBILITY_HIDDE" '{}' \; -print
Find path of strings in .cpp files
find / -name \*.cpp -exec grep "debug" '{}' \; -print
Run strings on every file on the system that ends with ".pwd" (case insensitive)
find / -iname "*.pwd" | xargs strings $1 | sort
Limit depth of directory traversal
find . -maxdepth 2 -name "*.txt"
it limits the depth of the command.
FIND AND XARGS command
find / -print | xargs ls -ld
Xargs executes its arguments as commands, and reads standard input to specify arguments to that command
Delete the first file ending in ".swp" or "~" that have not been modified in 24 days starting in this directory.
find . -mount -mtime +24 \( -name "*.swp" -o -name "*~" \) | head -n 1 | xargs rm $1
List all empty direcories:
find /tmp -type d -empty
find all empty directories and remove them in a specific directory.
find /tmp -type d -empty -exec rmdir {} \;
Rarely used but interesting commands:
To find list of files that got changed with last 1 day.
find / -mtime -1
Reference: http://www.cs.colostate.edu/~cs556/projects/project-1/Exercise.html
No comments:
Post a Comment