A Command to Delete the Oldest Sub-folder in a Specific Folder
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You can use the output of the command ls -rt -1 | head -n 1 to determine the oldest file or directory. If there are only sub directories this will give you the correct entry, else you have to test if the result is a file or a directory.
You might have to use rm -rf (if directories are not empty) in which case you must be very sure that you execute it from the correct directory. You can wipe all your data or with some bad luck your OS.
Hence I prefer to place it in a script so additional error checking is easy (easier) to implement.
A space in the result will cause problems. Imagine if the result was `directory c' and you had a `directory' and 'c'. You would be deleting the wrong directories. Another problem is if the result begins with a dash. Quoting will take care of the first possibility, but not he second. Your posted example won't cause either problem however.
If you use ls, make the listing as explicit as possible. You want the oldest directory. So only list directories.
ls -trd /sd2/Clones/*/
While using `ls' before a pipe is poor form, sometimes there is a use for it. Such as "ls -u" to eliminate sorting, which can take a long time if you have 100,000 files in the directory. Using the * wild card as in "for file in *.jpg" in such a case would probably cause you to run out of memory.
In the answer using the `find' command with `-printf', you can place double quotes around the filenames in the printf expression.
Always consider the edge cases. Sometimes you know what to expect because the filenames are regular.
A space in the result will cause problems. Imagine if the result was `directory c' and you had a `directory' and 'c'. You would be deleting the wrong directories. Another problem is if the result begins with a dash. Quoting will take care of the first possibility, but not he second. Your posted example won't cause either problem however.
If you use ls, make the listing as explicit as possible. You want the oldest directory. So only list directories.
ls -trd /sd2/Clones/*/
While using `ls' before a pipe is poor form, sometimes there is a use for it. Such as "ls -u" to eliminate sorting, which can take a long time if you have 100,000 files in the directory. Using the * wild card as in "for file in *.jpg" in such a case would probably cause you to run out of memory.
In the answer using the `find' command with `-printf', you can place double quotes around the filenames in the printf expression.
Always consider the edge cases. Sometimes you know what to expect because the filenames are regular.
Thank you. This seems beyond my knowledge, but I will have to learn things anyway. Thank you so much.
Quote:
Originally Posted by David the H.
Please don't use ls for this. It's not safe. And all the command substitutions used above are particularly weak, since they are not quoted.
I'm trying to use this command suggested above with the specific folder to be more safe...
rm -r $(ls -t -r /share/documents | head -n 1)
the problem is that inside that folder, the other folders have spaces in them... so I get bash errors... "bad interpreter: no such file or directory" and cannot delete them... can someone help me with modifying this command to accept folders with spaces to be removed?
By the way, I'm trying to do this on a Qnap 459Pro NAS box...
I'm a total Linux newbie but I really try before asking....
Thanks!
Chale
Last edited by carlosdesedas; 08-03-2012 at 09:06 PM.
Reason: double post
the problem is that inside that folder, the other folders have spaces in them... so I get bash errors... "bad interpreter: no such file or directory" and cannot delete them... can someone help me with modifying this command to accept folders with spaces to be removed?
Didn't you read my previous post? This is exactly what the links I gave before address. Parsing ls for filenames (or metadata), and using $(..) to try to insert them into another command, simply cannot safely be done when the values contain whitespace and other metacharacters. You must use other techniques.
In addition, you appear to have moved on from your original question. If you have new requirements, then please explain in detail what you are trying to do now.
Sorry David... and thanks for your help... I'm not the same person that started the thread.... Anyway... Here's what I'm trying now and I'm still getting errors... the script's name I run is test.py and it contains:
Search for files in a directory hierarchy. The default PATH is
the current directory; default EXPRESSION is '-print'
EXPRESSION may consist of:
-follow Dereference symbolic links.
-name PATTERN File name (leading directories removed) matches PATTERN.
-print Print (default and assumed).
-type X Filetype matches X (where X is one of: f,d,l,b,c,...)
-perm PERMS Permissions match any of (+NNN); all of (-NNN);
or exactly (NNN)
-mtime TIME Modified time is greater than (+N); less than (-N);
or exactly (N) days
Remove (unlink) the FILE(s). You may use '--' to
indicate that all following arguments are non-options.
Options:
-i always prompt before removing each destination
-f remove existing destinations, never prompt
-r or -R remove the contents of directories recursively
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.