0

I have to delete oldest directory. But, two of directories does not have to be "included" in this action.

So, directory content looks like this:

[root@myserver backup]# ls -la
total 84
drwxr-xr-x 21 root root 4096 Jun 18 04:10 .
drwxr-xr-x 15 root root 4096 Dec 30 11:42 ..
drwxr-xr-x  9 root root 4096 Jan 11 07:25 2013
drwxr-xr-x 13 root root 4096 Jun  4 08:01 2014
drwxr-xr-x  2 root root 4096 Jun 12 04:20 2014-06-12
drwxr-xr-x  2 root root 4096 Jun 13 04:20 2014-06-13
drwxr-xr-x  2 root root 4096 Jun 14 04:20 2014-06-14
drwxr-xr-x  2 root root 4096 Jun 15 04:20 2014-06-15
drwxr-xr-x  2 root root 4096 Jun 16 04:20 2014-06-16
drwxr-xr-x  2 root root 4096 Jun 17 04:20 2014-06-17
drwxr-xr-x  2 root root 4096 Jun 18 04:20 2014-06-18
[root@myserver backup]# 

In this case directory 2014-06-12 have to be deleted, and directories 2013 and 2014 should not be deleted.

I tried by executing next command:

rm -f 'ls -l | grep 2014\- | head -1'

... but it does not deletes anything.

Can you help me how to achieve this?

Thank you in advance!

MadHatter
  • 79,770
  • 20
  • 184
  • 232
user48058
  • 863
  • 3
  • 12
  • 20

2 Answers2

2

This command works regardless of the directories' names.
To remove the third oldest directory:
ls --sort t -l | grep -v total | awk '{print $9}' | head -n -2 | tail -1 | xargs rm -rf

psimon
  • 148
  • 5
1

Try this:

ls -1tr | grep 2014\- | head -1 | xargs rm -rf
MadHatter
  • 79,770
  • 20
  • 184
  • 232
Desmond Ho
  • 29
  • 1
  • Why `-l`? That'll pull in a lot of files with odd names, like `drwxr-xr-x`, `2`, and `root`. – MadHatter Jun 18 '14 at 08:00
  • `-l` is just list more information, i rarely use `ls`, i use `ll` instead of `ls`. But some of linux version don't support it. `-l` is just a personal habbit – Desmond Ho Jun 18 '14 at 08:05
  • Thanks! One more question: what if directories are stored in directory /backup, and I have to execute command from some other directory (or cron have to execute it)? – user48058 Jun 18 '14 at 08:11
  • you may put the path after the `ls` / `ll` e.g. `ls -l /backup | ` – Desmond Ho Jun 18 '14 at 08:14
  • This doesn't work as written the output of ls -l when passed to rm can produce unexpected results and failures. @MadHatter Why didn't you downvote this obviously poor and possibly dangerous answer ? – user9517 Jun 18 '14 at 08:23
  • Iain, I agree with you that parseing `ls` is not always wise. But I do it myself quite a lot, and my feeling has always been that since I would never create directories with spaces or other special characters in their names, it's safe in directories **which I myself control**; therefore I can't object to this answer as I do it myself. If you wanted to post a more robust answer, though, I'd upvote that. – MadHatter Jun 18 '14 at 08:27
  • nope, if i run ls -l /backup | grep 2014\- | head -1 | xargs rm -rf nothing is deleted – user48058 Jun 18 '14 at 09:04
  • @MadHatter the problem here is with the `ls -l` as you note it passes lots of cruft to rm e.g. `drwxrwxr-x. 2 user user 4096 Jun 18 10:09 2014-01-01`, if it works it will be serendipitous at worst it could delete other wanted files. – user9517 Jun 18 '14 at 09:11
  • Iain, I completely agree. I think the `-l` flag is a big mistake, as I commented above. In fact, as the OP doesn't seem to have a rationale for it, let me make a small change... – MadHatter Jun 18 '14 at 09:22
  • Are you sure the path is `/backup`? Please go to the related location and use `pwd` to check the absolute path. – Desmond Ho Jun 18 '14 at 09:44
  • yes, it's /backup. it's not standard linux direcotry – user48058 Jun 18 '14 at 09:58
  • @lain, I understand the concern of the safety when using `ls`, my answer here is just for this situation. for better result, we may use `find ./ -type f -printf '%T+ %p\n' | sort | head -n 1 | awk '{print $2}' | xargs rm -rf` *Please edit the path before use – Desmond Ho Jun 18 '14 at 10:06
  • Not standard linux directory? Which OS are you using? – Desmond Ho Jun 18 '14 at 10:08
  • It doesn't meeter which directory is it, i just need to delete those directories :-) – user48058 Jun 18 '14 at 10:39
  • @Iain @MadHatter It looks like it is not en "el" '-l', it is a "one" '-1', the short form of `--format=single-column`. – dawud Jun 18 '14 at 20:41