I want to delete directories and subdirectories older than 10 days.

Also, I want to store the deleted folder and file names, time details in another file.

So far my script is to delete directories, subdirectories, and file which is older than 10 days.

find /tmp/processed/* -type d -ctime +10 -exec rm -rf {} \; 

Could some one help to store the details in another file?

Thanks in advance.

1 Answer

You can run multiple commands at the same time:

find /tmp/processed/* -type d -ctime +10 -print -exec rm -rf {} \; 

You can use the information that @Ix07 posted in their answer:

find /tmp/processed/* -type d -ctime +10 -printf "%p %TY-%Tm-%Td %TH:%TM:%TS %TZ\n" -exec rm -rf {} \; 

You can redirect the output of this to a file.
Note, however, that this can give quite messy (unaligned) output when viewing. As such, I like to use the column command in combination with tabs in the printf format to 'tablify' the data:

find /tmp/processed/* -type d -ctime +10 -printf "%p\t%TY-%Tm-%Td\t%TH:%TM:%TS\t%TZ\n" -exec rm -rf {} \; | column -t 

I tested the output of the above, and it looked like this for me:

/tmp/processed $ find * -type d -ctime +10 -printf "%p\t%TY-%Tm-%Td\t%TH:%TM:%TS\t%TZ\n" -exec rm -rf {} \; | column -t bar 2019-02-07 20:35:15.8718172190 WAT foo 2019-02-07 20:35:54.5638166540 WAT 

Look at the man page for find to know more (tip: Search in the man page for -printf).

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy