LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 08-01-2012, 08:46 PM   #1
imayneed
Member
 
Registered: Jul 2012
Distribution: Mint 9 Gnome, Mint 13 KDE
Posts: 60

Rep: Reputation: Disabled
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 08:50 PM.
 
Old 08-02-2012, 01:50 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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.
 
Old 08-02-2012, 02:04 AM   #3
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by imayneed View Post
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.
Old 08-02-2012, 02:38 AM   #4
imayneed
Member
 
Registered: Jul 2012
Distribution: Mint 9 Gnome, Mint 13 KDE
Posts: 60

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-02-2012, 03:33 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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.
Old 08-02-2012, 07:39 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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 07:42 AM.
 
1 members found this post helpful.
Old 08-02-2012, 08:11 AM   #7
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by imayneed View Post
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 ..
 
Old 08-02-2012, 08:53 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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 08:56 AM.
 
1 members found this post helpful.
Old 08-02-2012, 09:13 AM   #9
imayneed
Member
 
Registered: Jul 2012
Distribution: Mint 9 Gnome, Mint 13 KDE
Posts: 60

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by divyashree View Post
I gave you two ways one using ls and another using find ... You can use anyone ..
Thank you.
 
Old 08-02-2012, 09:18 AM   #10
imayneed
Member
 
Registered: Jul 2012
Distribution: Mint 9 Gnome, Mint 13 KDE
Posts: 60

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jschiwal View Post
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. View Post
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.
 
Old 08-03-2012, 02:46 PM   #11
carlosdesedas
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Rep: Reputation: Disabled
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 09:06 PM. Reason: double post
 
Old 08-03-2012, 11:36 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Quote:
Originally Posted by carlosdesedas View 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.

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.
 
Old 08-04-2012, 04:16 PM   #13
carlosdesedas
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Rep: Reputation: Disabled
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
 
Old 08-05-2012, 08:53 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,247

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Well, this
Code:
x#!/bin/sh
isn't a valid shebang line, and its definitely not python
Code:
./test.py: line 2: fg: no job control
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using terminal command -Find files in a folder and copy them to a different folder j-jock Linux - General 4 11-28-2011 03:20 AM
What command to delete all files and subfolders, but not root folder ? proleader Linux - Newbie 10 09-16-2011 06:02 AM
[SOLVED] Bash command to remove all files within a specific folder shayno90 Linux - Newbie 21 10-21-2010 12:55 PM
Find folders with ONLY one specific file in and delete file and folder - How ? gedi1 Linux - Newbie 5 10-21-2009 09:09 PM
BASH: how to automate deletion of oldest folder big_mike_jones Programming 18 11-22-2005 03:59 PM


All times are GMT -5. The time now is 05:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration