Delete only oldest folder in directory with bash

Hello everyone...

I have a backup system copying a folder into a linux machine.

I would like to prepare a script that will delete ONLY the oldest folder among all the others without touching other files / folders.

Example:

folderA
folderB
folderC
folderD

Folder A is the oldest one so it gets completely deleted recursively and all remains is:

folderB
folderC
folderD

How can I do that?

Thanks
LVL 1
ltpittAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
If 'oldest' is determined by modification time
rm -r `ls -Ft | grep '/$' | tail -1`
0
ltpittAuthor Commented:
Not working...

I have 3 folders:
E 2015-03-11 09;22;10 (Full)  E 2015-03-11 22;13;17 (Full)  E 2015-03-13 02;05;58 (Full)

And if I run your command:
ls -Ft | grep '/$' | tail -1

I get:
E 2015-03-13 02;05;58 (Full)/

I should get instead the oldest one...

a ls -lha gives:
drwxr-xr-x 0 root root 0 Mar  3 09:31 E 2015-03-11 09;22;10 (Full)
drwxr-xr-x 0 root root 0 Mar  3 09:31 E 2015-03-11 22;13;17 (Full)
drwxr-xr-x 0 root root 0 Mar  3 09:31 E 2015-03-13 02;05;58 (Full)

Very strange because the folders name have date and time of creation.
0
woolmilkporcCommented:
If your folders always follow the shown naming convention you can do this to display the oldest one:

ls -ha | sort  -k10,11r | tail -1

To remove it try this:

rm -r "$(ls -ha | sort  -k10,11r | tail -1)"

Please note the quotation marks!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ltpittAuthor Commented:
Yes they do...

Same problem...

If I do:
ls -ha | sort  -k10,11r | tail -1

I get:
E 2015-03-13 02;05;58 (Full)
0
woolmilkporcCommented:
Sorry, there was a typo!

ls -lha | sort  -k10,11r | tail -1

of course!
0
woolmilkporcCommented:
Without "-l" of "ls" we must use

ls -ha | sort  -k2,3r | tail -1
0
ozoCommented:
If 'oldest' is determined by the name,
rm -r "`ls -F | grep '/$' | head -1`"
0
woolmilkporcCommented:
I'm not sure we can assume that the folder name will always start with an "E".
0
ltpittAuthor Commented:
Thanks for the help!

Both commands from woolmilkporc give wrong results:
ls -lha | sort  -k10,11r | tail -1

gives:
Total 0

and:
ls -ha | sort  -k2,3r | tail -1

gives:
..

ozo's command seems to do the job!

I'll test it a bit...
0
woolmilkporcCommented:
ls  | sort  -k2,3r | tail -1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
I'm not sure we can assume that all ls entries will be folders containing spaces.
ls -d *\ *\ * | sort  -k2,3r | tail -1
may be preferable if not
0
woolmilkporcCommented:
So we could combine our solutions:

ls -Fd *\ *\ * | grep '/$'  | sort  -k2,3r | tail -1
0
ozoCommented:
with a grep, we don't need the glob, so we could also combine:
ls -F | grep ' .* .*/$'  | sort  -k2,3r  | tail -1
0
woolmilkporcCommented:
Yep.
0
ltpittAuthor Commented:
ls -F | grep ' .* .*/$'  | sort  -k2,3r  | tail -1

produces nothing...
0
woolmilkporcCommented:
What do you see with

ls -Fb1

(please note it's the digit "1", not the letter "l")

and

ls -Fb | grep "/$"

?

Please post the results!

Or is there a directory whose name consists of just spaces? In this case you should see a "/" instead of "nothing", and running "rm -r" against it would be very destructive, obviously.
0
ltpittAuthor Commented:
ls -Fb1

Here is the result:

.
..
E\ 2015-03-11\ 22;13;17\ (Full)
E\ 2015-03-11\ 09;22;10\ (Full)
E\ 2015-03-13\ 02;05;58\ (Full)


ls -Fb | grep "/$"

Just gives me back a sad face :(

I always copy and paste commands so I don't commit errors
0
ltpittAuthor Commented:
ps

There are spaces, as you can see
0
woolmilkporcCommented:
"-F" of "ls" should display a slash at the end of each directory name, but it's not present, obviously.
Which is your OS? Does it have a special version of "ls"?
Or did you set an alias?

Run

alias ls

and post the result

and/or retry the command with:

\ls -F1

or

/bin/ls -F1
0
ltpittAuthor Commented:
who are you, a magician?

I have:

alias ls='ls --color=auto'

Is this the cause of the problem?
0
woolmilkporcCommented:
Not sure, but I don't believe so. Did you try "/bin/ls" or "\ls"?
0
ltpittAuthor Commented:
Here i am...

/bin/ls -F1

gives back:
E 2015-03-11 09;22;10 (Full)/
E 2015-03-11 22;13;17 (Full)/
E 2015-03-13 02;05;58 (Full)/

and

/bin/ls -Fb | grep "/$"

gives:
E\ 2015-03-11\ 09;22;10\ (Full)/
E\ 2015-03-11\ 22;13;17\ (Full)/
E\ 2015-03-13\ 02;05;58\ (Full)/
0
woolmilkporcCommented:
There we are!

I don't think it's due to the "--color" thing. What do you see with "which ls"?

Anyway, please retry our command(s) with "/bin/ls -F" instead of "ls -F" alone.
0
ltpittAuthor Commented:
My liege!

It works!

Testing it right away.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
In case you need an alternative:

echo rm -rf $(ls -tr --group-directories-first | head -1)

(remove the echo if you see the correct path)
0
ltpittAuthor Commented:
Everyone was awesome and contributed to perfect solutions
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.