-1

I'm trying to create a one-liner which should remove excess files from dir to save it from consuming too much space.

ls -r --sort=time | head -${{ls | wc -l} - n} | xargs rm -f

n should stand for number of files I want to keep in directory.

I'm doing something wrong here and can't figure out what. Can someone help please?

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
Zloj
  • 2,235
  • 2
  • 18
  • 28

2 Answers2

0

That ${{...}} looks vaguely like TCL or something. It is certainly not valid shell script.

In Bash (but not in Bourne shell) you can use (( ... )) for artithmetic expression, which I guess is what you want the "- n" to do.

The syntax for process substitution is $(ls | wc -l) with round parentheses.

Merging these two, you might end up with something like (($(ls | wc -l) - n)) but you really should not be parsing ls output anyway. Look at one of the many, many duplicates of this question for better approaches.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0
( ls -t | head -n 25 ; ls ) | sort | uniq -u | sed -e 's,.*,"&",g' | xargs rm

Done the trick for me.

Delete all but the most recent X files in bash <-- here

Thanks for reply.

Community
  • 1
  • 1
Zloj
  • 2,235
  • 2
  • 18
  • 28