Friday 22 August 2014

UNIX: find command

FIND:
The find command is extensively used as it can perform lots of operations as listed below.

Find and Move From one folder to another:
find /etl/IS/Data/source/ -type f -name "filename.txt" -exec mv {} /etl/IS/Data/destination/filename.txt \;
The move (mv) command will be executed only if the find command is successful.
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 \;
Find and delete:
This command is used to delete a file only if it exits.
find /etl/IS/Data/source/ -type f -name "filename.txt" -exec rm -f {} \;
Finding 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

Find and Removing Files older than Specific Days:

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.

find /Path -mtime +7 -exec rm -f {} \;

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.
Eg:
find /etl/IS/Data/checkers -name dispatch* -type d -mtime +9 -depth -exec rm -rf {} \;

This will delete the folders dispatch10, dispatch55, dispatch63 from /etl/IS/Data/checkers


No comments:

Post a Comment