Search This Blog

Sunday, 5 October 2014

Find Command

Find Command
The find command is extensively used as it can perform lots of operations as listed below.
To Find and Move From one folder to another:

find /path/ -type f -name "filename.txt" -exec mv {} /path/filename.txt \;

The move (mv) command will be executed only if the find command is successful.
To Find and Move From one folder to another with timestamp appended to file name:

find /path/ -type f -name "filename.txt" -exec mv {} /destination/filename_`date +%Y%m%d-%T`.txt \;
To Find and delete:

find /etl/IS/Data/source/ -type f -name "filename.txt" -exec rm -f {} \;
This command is used to delete a file only if it exits.
To Find a file is new or not:

find /etl/IS/Data/check/filename1.txt -newer /etl/IS/Data/check/filename2.txt
output if filename is newer:

output=filename1.txt
Note: Won’t return anything if filename1.txt is old
To Find and Removing Files older than Specific Days:

find /Path -mtime +7 -exec rm -f {} \;
find $LOGDIR -name "*.txt" -mtime +35 -depth -exec rm -rf {} \;

To remove the files which are older than specific days, find command can be used. This command is helpful for deleting archived files.. Below command deletes files older than 7 days.
To Find and Removing Folders older than Specific Days:

find path -name foldername* -type d -mtime +9 -depth -exec rm -rf {} \;

wildcard is used to indicate the folders which start with the specific names to be deleted if modified date is less than 9 days.

Finding a file under sub directories:

find . -name "251815a_GPS*"
find . -size +757000 -exec ls -l {} \; 2>/dev/null


No comments:

Post a Comment