Linux delete oldest folder

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 

Источник

Читайте также:  Linux get command list

Shell script to delete oldest files and folders

You'd have something like this:

I have a system running. It creates 1 folder for each day, naming it by date. Inside each folder, it stores videos from security cameras, naming the files by time. You’d have something like this: The folders are uploaded to the cloud, file by file, as it is created. However, my local storage limit is 16GB. This way, I’ve made a script (in bash) to keep deleting old files as storage reaches a certain percentage. This is what the script should do:

  • Detects the storage %.
    • If the storage percentage is at 75%:
      • Count the number of folders in the output’s root folder.
        • If more than one folder:
          • Go to the oldest folder and count files.
            • If it has files, delete the 10 oldest files.
            • If it is empty, go back to the output’s root folder and delete the oldest empty folder.
        • If only one folder:
          • Go to the only folder and delete the 10 oldest files.
  • The cron job will run the script every minute, but the script will only delete stuff if the main condition is met.
#! /bin/sh ##### VARIABLES ##### RootFolder="/data/output/Camera1" #Must change to your default output folder. Originally, it's "/data/output/Camera1". cd $RootFolder FileCounter=$(find . -name "*.mkv" -o -name "*.avi" -o -name "*.swf" -o -name "*.flv" -o -name "*.mov" -o -name "*.mp4" | wc -l | xargs) #Must change, if the output format if different than the basic ones available originally on MotionEyeOS. FolderCounter=$(find . -mindepth 1 -maxdepth 1 -type d | wc -l | xargs) CurrentStorage=`df -h | grep -vE "^Filesystem|tmpfs|/tmp|/boot|/home|/dev/root" | awk '' | cut -d'%' -f1` StorageThreshold=75 #Define the storage % to trigger the script. ##### FUNCTIONS ##### function multiplefolders () < echo "More than one folder detected." cd `ls -Ft | grep '/$' | tail -1` echo "Entering the oldest folder. " if [ "$FileCounter" -eq 0 ]; then echo "No Files detected." cd $RootFolder echo "Going back to the output's root folder. " rm -rf `ls -Ft | grep '/$' | tail -1` echo "Detecting and removing the oldest empty folder. "; else ls -t | tail -10 | xargs rm echo "Deleting the oldest files. "; fi >function singlefolder () < echo "Only one folder detected." echo "Entering the only folder. " cd `ls -Ft | grep '/$' | head -1` ls -t | tail -10 | xargs rm echo "Deleting the oldest files." >function checkfolders () < if [ "$FolderCounter" -gt 1 ]; then multiplefolders fi if [ "$FolderCounter" -eq 1 ]; then singlefolder fi if [ "$FolderCounter" -eq 0 ]; then echo "No folders detected. Please check if the output folder's path is correct." fi >##### SCRIPT STARTER ##### if [ $CurrentStorage -ge $StorageThreshold ]; then checkfolders else echo "Storage threshold not yet reached." fi 

Thing is that the script isn’t (apparently) counting the number of files inside the oldest folder (when more then one folder is detected) correctly.

Читайте также:  Job control with linux

Instead of going back to the root folder and delete the oldest empty folder, it keeps deleting files from the newest folder (apparently).

In other words, when you have 2 folders (the oldest one empty and the newest one with videos in it), it should delete the oldest and empty folder, but I keep getting:

More than one folder detected. Entering the oldest folder. Deleting the oldest files. 

Источник

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).

Читайте также:  Linux usb command line

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.

Источник

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?

Источник

Оцените статью
Adblock
detector