-4

Wondering how would one go about writing a script that:

Counts the number of files in a directory.

If that number is greater than a specified number N, delete the oldest file in the directory.

For bonus feelings of good will: Only included .zip files in the count/delete.

MetaGuru
  • 719
  • 11
  • 14
  • 3
    This feels a little like we may be doing school or real work for @Ryan. Perhaps you can demonstrate what you have come up with so far. – George M May 14 '12 at 20:53
  • @uther Not for work, for a world backup on a personal Minecraft server. If you don't want to help, that's OK. So far I just have a script that copies files to a location, but it will fill up the drive unless I know how to do the script requested above. – MetaGuru May 14 '12 at 20:54
  • 3
    It's not that I don't want to help, otherwise I wouldn't spend time here. What you are asking for is fairly trivial. It would go a long way to see you make an effort to write a script first and ask for critique/improvements instead of asking someone to write the script for you from scratch. I want you to learn something. – George M May 14 '12 at 20:58
  • 2
    man `find`, man `wc`, man `ls` – Nils May 14 '12 at 21:00
  • 1
    Why all the down-votes? BTW, possibly [relevant reading](http://mywiki.wooledge.org/BashFAQ/004). – jw013 May 14 '12 at 21:28
  • 2
    When you mouse over the downvote button it says: "This question does not show any research effort". If that doesn't apply here, when does it apply? – Mikel May 14 '12 at 22:52
  • Oh, sorry, I voted to close for the wrong duplicate. I should have picked [Shell script for moving oldest files?](http://unix.stackexchange.com/q/22674) or [Bash, remove oldest files](http://unix.stackexchange.com/q/35792) – Gilles 'SO- stop being evil' May 14 '12 at 23:12
  • @uther it's only trivial if you have more experience... – MetaGuru May 15 '12 at 16:31

2 Answers2

1
N=50
[[ $( ls | wc -l ) -gt $N ]] && ls -tr | tail -n1 | tr \\n \\0 | xargs -0 echo rm

Barely tested, but I think this comes close. If you are happy with the text output, you can remove the echo to set it live.

  • ls list files
  • wc count them
  • [[ ... -gt ... ]] if
  • && then
  • ls -tr list files in reverse age
  • tail -n1 show only last line (replace 1 as needed)
  • tr \\n \\0 make \0 the delimiter between filenames (only one here)
  • xargs -0 echo rm -- append every argument (delimited by \0s) found to echo rm --
  • echo rm -- give you the opportunity to check if the result is really what you want
  • rm -- remove file (put -- before filenames to handle filenames starting with -)
Ned64
  • 8,486
  • 9
  • 48
  • 86
jippie
  • 13,756
  • 10
  • 44
  • 64
1

For example, to only perform the actions if there are more than 50 files in the folder:

shopt -s dotglob nullglob
for file in *; do
    [[ -f $file ]] && files+=( "$file" )
done
if (( ${#files[@]} > 50 )); then
    IFS= read -r -d $'\0' line < <(find . -maxdepth 1 -type f -printf '%T@ %p\0' 2>/dev/null | sort -z -n)
    rm "${line#* }"
fi
Chris Down
  • 122,090
  • 24
  • 265
  • 262