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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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`
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.
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!
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

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)
woolmilkporcCommented:
Sorry, there was a typo!

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

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

ls -ha | sort  -k2,3r | tail -1
ozoCommented:
If 'oldest' is determined by the name,
rm -r "`ls -F | grep '/$' | head -1`"
woolmilkporcCommented:
I'm not sure we can assume that the folder name will always start with an "E".
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...
woolmilkporcCommented:
ls  | sort  -k2,3r | tail -1

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
woolmilkporcCommented:
So we could combine our solutions:

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

produces nothing...
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.
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
ltpittAuthor Commented:
ps

There are spaces, as you can see
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
ltpittAuthor Commented:
who are you, a magician?

I have:

alias ls='ls --color=auto'

Is this the cause of the problem?
woolmilkporcCommented:
Not sure, but I don't believe so. Did you try "/bin/ls" or "\ls"?
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)/
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.
ltpittAuthor Commented:
My liege!

It works!

Testing it right away.
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)
ltpittAuthor Commented:
Everyone was awesome and contributed to perfect solutions
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.