Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
08-01-2012, 07:46 PM
|
#1
|
Member
Registered: Jul 2012
Distribution: Arch, Kubuntu
Posts: 76
Rep:
|
A Command to Delete the Oldest Sub-folder in a Specific Folder
I usually have 2 sub-folders in a specific folder.
I am trying to find out a command that will delete ONLY the oldest directory.
The name of the folders change. So, I need to find a command to delete the folders by folder name as I explained below or maybe may creation date.
For example, if I have
/sdc2/Clones/9999-2012-10-14-gsfgsd
/sdc2/Clones/9999-2012-10-11-gsfgsd
I want this command to delete 9999-2012-10-11-gsfgsd; but leave the 999-2012-10-14-gsfgsd alone.
Thank you.
Last edited by imayneed; 08-01-2012 at 07:50 PM.
|
|
|
08-02-2012, 12:50 AM
|
#2
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797
|
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.
That output can be used in a rmdir command.
|
|
|
08-02-2012, 01:04 AM
|
#3
|
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386
Rep:
|
Quote:
Originally Posted by imayneed
I usually have 2 sub-folders in a specific folder.
I am trying to find out a command that will delete ONLY the oldest directory.
The name of the folders change. So, I need to find a command to delete the folders by folder name as I explained below or maybe may creation date.
For example, if I have
/sdc2/Clones/9999-2012-10-14-gsfgsd
/sdc2/Clones/9999-2012-10-11-gsfgsd
I want this command to delete 9999-2012-10-11-gsfgsd; but leave the 999-2012-10-14-gsfgsd alone.
Thank you.
|
You can do it with following ways:
1.
Code:
rm -r $(find /sdc2/Clones -maxdepth 1 -type d -printf '%T@ %p\n' | sort -n | head -1 | cut -f2- -d" ")
2.
Quote:
rm -r $(ls -t -r | head -n 1)
|
|
|
1 members found this post helpful.
|
08-02-2012, 01:38 AM
|
#4
|
Member
Registered: Jul 2012
Distribution: Arch, Kubuntu
Posts: 76
Original Poster
Rep:
|
Thank you for the replies.
Sorry for asking this. I know both commands are rm -r but do I use both commands or either one?
I mean do I use :
Code:
rm -r $(find /sdc2/Clones -maxdepth 1 -type d -printf '%T@ %p\n' | sort -n | head -1 | cut -f2- -d" ")
rm -r $(ls -t -r | head -n 1)
or just
Code:
rm -r $(ls -t -r | head -n 1)
is enough?
I guess I will try to see.
Thank you so much again.
Thank you.
|
|
|
08-02-2012, 02:33 AM
|
#5
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797
|
Code:
rm -r $(ls -t -r -1 | head -n 1)
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.
|
|
2 members found this post helpful.
|
08-02-2012, 06:39 AM
|
#6
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
The proper way to script this can be found here:
How can I get the newest (or oldest) file from a directory?
http://mywiki.wooledge.org/BashFAQ/099
(Isn't it strange how the same types of questions tend to come up in clusters? I just answered another similar post with the same link.)
Last edited by David the H.; 08-02-2012 at 06:42 AM.
|
|
1 members found this post helpful.
|
08-02-2012, 07:11 AM
|
#7
|
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386
Rep:
|
Quote:
Originally Posted by imayneed
Thank you for the replies.
Sorry for asking this. I know both commands are rm -r but do I use both commands or either one?
I mean do I use :
Code:
rm -r $(find /sdc2/Clones -maxdepth 1 -type d -printf '%T@ %p\n' | sort -n | head -1 | cut -f2- -d" ")
rm -r $(ls -t -r | head -n 1)
or just
Code:
rm -r $(ls -t -r | head -n 1)
is enough?
I guess I will try to see.
Thank you so much again.
Thank you.
|
I gave you two ways one using ls and another using find ... You can use anyone ..
|
|
|
08-02-2012, 07:53 AM
|
#8
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
Last edited by jschiwal; 08-02-2012 at 07:56 AM.
|
|
1 members found this post helpful.
|
08-02-2012, 08:13 AM
|
#9
|
Member
Registered: Jul 2012
Distribution: Arch, Kubuntu
Posts: 76
Original Poster
Rep:
|
Quote:
Originally Posted by divyashree
I gave you two ways one using ls and another using find ... You can use anyone ..
|
Thank you.
|
|
|
08-02-2012, 08:18 AM
|
#10
|
Member
Registered: Jul 2012
Distribution: Arch, Kubuntu
Posts: 76
Original Poster
Rep:
|
Quote:
Originally Posted by jschiwal
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.
The proper way to script this can be found here:
How can I get the newest (or oldest) file from a directory?
http://mywiki.wooledge.org/BashFAQ/099
(Isn't it strange how the same types of questions tend to come up in clusters? I just answered another similar post with the same link.)
|
Thank you for the link. I bookmarked it.
After reading the replies; I realized that I need to learn more commands and understand them and their usage.
Thank you all.
|
|
|
08-03-2012, 01:46 PM
|
#11
|
LQ Newbie
Registered: Aug 2012
Posts: 2
Rep:
|
Hi all. I find this post and group great!
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 08:06 PM.
Reason: double post
|
|
|
08-03-2012, 10:36 PM
|
#12
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Quote:
Originally Posted by carlosdesedas
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.
One more link for you:
How can I find and deal with file names containing newlines, spaces or both?
http://mywiki.wooledge.org/BashFAQ/020
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.
|
|
|
08-04-2012, 03:16 PM
|
#13
|
LQ Newbie
Registered: Aug 2012
Posts: 2
Rep:
|
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:
x#!/bin/sh
rm -r $(find /share/documents -type d -printf '%T@ %p\n' | sort -n | head -1 | cut -f2- -d" ")
when I run it I get:
./test.py: line 2: fg: no job control
BusyBox v1.01 (2012.06.14-17:24+0000) multi-call binary
Usage: find [PATH...] [EXPRESSION]
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
BusyBox v1.01 (2012.06.14-17:24+0000) multi-call binary
Usage: rm [OPTION]... FILE...
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
|
|
|
08-05-2012, 07:53 PM
|
#14
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397
|
Well, this
isn't a valid shebang line, and its definitely not python
Code:
./test.py: line 2: fg: no job control
|
|
|
All times are GMT -5. The time now is 10:07 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|