linux - delete file/folder older than x days -


i want delete file & folder older 7 days tried

[17:07:14 root@client01.abc.com:~]# find /tmp/ -mindepth 1 -maxdepth 1 -ctime +7 -exec ls -l {} \; 

so when run find /tmp/ -mindepth 1 -maxdepth 1 -ctime +7 -exec ls -l {} \; doesnt show dir, find /tmp/ -mindepth 1 -maxdepth 2 -ctime +7 -exec ls -l {} \; show few files in subdir.

whats right way delete files/folders older 7 days in 1 specific dir ?

you can make use of piece of code

find /tmp/* -mtime +7 -exec rm {} \; 

explanation

the first argument path files. can path, directory, or wildcard in example above. recommend using full path, , make sure run command without exec rm make sure getting right results.

the second argument, -mtime, used specify number of days old file is. if enter +7, find files older 7 days.

the third argument, -exec, allows pass in command such rm. {} \; @ end required end command.

source : http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/

for deleting folders, after emptying inside of them can rmdirinstad of rm in piece of code, if want see directories can add

type -d  

to piece of code such below:

find /tmp/*/* -mtime +7 -type d -exec rmdir {} \; 

Comments