- Linux — Save only recent 10 folders and delete the rest
- Shell script to delete directories older than n days
- 5 Answers 5
- delete folders older than 1 day
- 5 Answers 5
- What command can delete folders older than X days or X months?
- 3 Answers 3
- You must log in to answer this question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
Linux — Save only recent 10 folders and delete the rest
I have a folder that contains versions of my application, each time I upload a new version a new sub-folder is created for it, the sub-folder name is the current timestamp, here is a printout of the main folder used (ls -l |grep ^d):
drwxrwxr-x 7 root root 4096 2011-03-31 16:18 20110331161649 drwxrwxr-x 7 root root 4096 2011-03-31 16:21 20110331161914 drwxrwxr-x 7 root root 4096 2011-03-31 16:53 20110331165035 drwxrwxr-x 7 root root 4096 2011-03-31 16:59 20110331165712 drwxrwxr-x 7 root root 4096 2011-04-03 20:18 20110403201607 drwxrwxr-x 7 root root 4096 2011-04-03 20:38 20110403203613 drwxrwxr-x 7 root root 4096 2011-04-04 14:39 20110405143725 drwxrwxr-x 7 root root 4096 2011-04-06 15:24 20110406151805 drwxrwxr-x 7 root root 4096 2011-04-06 15:36 20110406153157 drwxrwxr-x 7 root root 4096 2011-04-06 16:02 20110406155913 drwxrwxr-x 7 root root 4096 2011-04-10 21:10 20110410210928 drwxrwxr-x 7 root root 4096 2011-04-10 21:50 20110410214939 drwxrwxr-x 7 root root 4096 2011-04-10 22:15 20110410221414 drwxrwxr-x 7 root root 4096 2011-04-11 22:19 20110411221810 drwxrwxr-x 7 root root 4096 2011-05-01 21:30 20110501212953 drwxrwxr-x 7 root root 4096 2011-05-01 23:02 20110501230121 drwxrwxr-x 7 root root 4096 2011-05-03 21:57 20110503215252 drwxrwxr-x 7 root root 4096 2011-05-06 16:17 20110506161546 drwxrwxr-x 7 root root 4096 2011-05-11 10:00 20110511095709 drwxrwxr-x 7 root root 4096 2011-05-11 10:13 20110511100938 drwxrwxr-x 7 root root 4096 2011-05-12 14:34 20110512143143 drwxrwxr-x 7 root root 4096 2011-05-13 22:13 20110513220824 drwxrwxr-x 7 root root 4096 2011-05-14 22:26 20110514222548 drwxrwxr-x 7 root root 4096 2011-05-14 23:03 20110514230258
I’m looking for a command that will leave the last 10 versions (sub-folders) and deletes the rest. Any thoughts?
Shell script to delete directories older than n days
To they bare any relation on their actual creation/modification time? Because find could do it without looking at the name then.
What do you mean by «older than»? Are you referring to the time the directory was created, the time its contents were last changed, or something else? Be careful with some of the answers below; ctime is the inode change time. For a directory, it changes when files are added or removed from the directory.
5 Answers 5
This will do it recursively for you:
find /path/to/base/dir/* -type d -ctime +10 -exec rm -rf <> \;
Explanation:
- find : the unix command for finding files / directories / links etc.
- /path/to/base/dir : the directory to start your search in.
- -type d : only find directories
- -ctime +10 : only consider the ones with modification time older than 10 days
- -exec . \; : for each such result found, do the following command in .
- rm -rf <> : recursively force remove the directory; the <> part is where the find result gets substituted into from the previous part.
Alternatively, use:
find /path/to/base/dir/* -type d -ctime +10 | xargs rm -rf
Which is a bit more efficient, because it amounts to:
rm -rf dir1; rm -rf dir2; rm -rf dir3; .
With modern versions of find , you can replace the ; with + and it will do the equivalent of the xargs call for you, passing as many files as will fit on each exec system call:
find . -type d -ctime +10 -exec rm -rf <> +
-mtime was better for me as it checks content changes rather than permission changes, otherwise this was perfect.
The more efficient approach can backfire if you have too many folders to delete: stackoverflow.com/questions/11289551/…. For the same reason, in order to avoid deletion of the base folder it’s better to use -mindepth 1 (rather than /path/to/folder/* ).
If you want to delete all subdirectories under /path/to/base , for example
/path/to/base/dir1 /path/to/base/dir2 /path/to/base/dir3
but you don’t want to delete the root /path/to/base , you have to add -mindepth 1 and -maxdepth 1 options, which will access only the subdirectories under /path/to/base
-mindepth 1 excludes the root /path/to/base from the matches.
-maxdepth 1 will ONLY match subdirectories immediately under /path/to/base such as /path/to/base/dir1 , /path/to/base/dir2 and /path/to/base/dir3 but it will not list subdirectories of these in a recursive manner. So these example subdirectories will not be listed:
/path/to/base/dir1/dir1 /path/to/base/dir2/dir1 /path/to/base/dir3/dir1
So , to delete all the sub-directories under /path/to/base which are older than 10 days;
find /path/to/base -mindepth 1 -maxdepth 1 -type d -ctime +10 | xargs rm -rf
delete folders older than 1 day
I want to delete all the folders that are older than 1 day with following command: find /test -mmin +1440 | xargs rm -rf But the output of find lists /test (and would remove it accordingly). How can I find only the sub dirs of /test ? ( -maxdepth / -mindepth not available in AIX)
Consider an «old» directory containing «new» directories. Should these «new» ones still all be deleted because they’re within the «old» directory? Or do we need to avoid deleting an «old» directory that contains «new» directories?
What do you consider to be the age of a directory? Note that the modification time of a directory file only reflect the last time an entry was added, removed or renamed in it. It is not updated when any of the files (of type regular or directory or other) linked in it are modified. In particular, any change made to subdirectories or their content doesn’t affect the modification time of a directory.
5 Answers 5
As @meuh said in his comment, you could use /test/* instead of /test . Your command could then look similar to this:
find /test/* -type d -mmin +1440 | xargs rm -rf
In this case only the subfolders of /test would be removed.
find ‘s output cannot be processed reliably (let alone by xargs for which blanks and quotes are also a problem). You should use the -exec predicate instead.
find /test/. ! -name . -type d -mtime +0 -exec rm -rf <> \; -prune
(we use -prune for the directories that we successfully remove so that find doesn’t complain that they’re suddenly gone).
In any case, note that the modification time (as checked by -mtime above) of a directory file only reflects the last time an entry was added, removed or renamed in it.
It is not updated when the content of any of the files (of type regular or directory or other) linked in it are modified. In particular, any change made to subdirectories or their content doesn’t affect the modification time of a directory.
Note that all of -mindepth , -maxdepth and -mmin are GNU extensions (though they are supported in some other implementations).
The standard equivalent of find . -maxdepth 1 would be:
For -mindepth 1 -maxdepth 1 :
For directories other than . , use find some/dir/. . as above.
For other values of depths, you can use -path , but note that since it has only been recently added to the standard, some systems (like AIX) still don’t have it.
What command can delete folders older than X days or X months?
I know a command to do the same with files, but what about folders (and all the files in it of course)?
3 Answers 3
The command you refer is find I suppose.
You should use the command -exec rm -r <> \; and add the -depth option.
The -r option to rm remove directories with all the content.
The -depth option tell find to elaborate content of folders before the folder itself.
That’s almost perfect, thanks. Is there a way so it doesn’t delete the actual folder I’m searching in? So only folders within the folder get deleted? I’m currently doing «find «/home/user/folder/» -mtime +7 -exec rm -r <> \;», but it deleted the folder called «folder» as well if the time matches. I only want subfolders deleted older than 7 days.
find ./dirc/* -mtime +x -type f -delete
- ./dirc/* : is your directory (Path)
- -mtime +x : older than x days
- -type f : only files
- -delete : no surprise. Remove it to test before like rm
Think that’s not realy true, because if you change or create a file in this folder before you try to remove it, it will not be removed. Because mtime is the modification time and the modification will be done at this time you create or change a file in this directory. Afaik but let me know if I’m false at Linux there is no parameter for creation time.
You must log in to answer this question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533
Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.