0

Daily downloads are stored in a directory.

A script has been created which counts how many files reside in this folder.

The part I am struggling with is deleting the oldest file in the directory if the number of files exceeds seven.

How should I proceed?

# If I tared it up would the target exist
tar_file=$FTP_DIR/`basename $newest`.tar
if [-s "$tar_file" ]
then
    echo Files Found
else
    echo No files
fi

# tar it up
tar -cf tar_file.tar $newest

# how many tar files do I now have
fc=$(ls | wc -l)
echo $fc

# If more than 7 delete the oldest ones
030
  • 1,557

4 Answers4

0

I know it's an old question. But landed here and sharing in case someone else needs it.

#!/bin/bash
FTP_DIR=/var/lib/ftp # change to location of dir
FILESTOKEEP=7

ls -1t $FTP_DIR/*.tar | awk -v FILESTOKEEP=$FILESTOKEEP '{if (NR>FILESTOKEEP) print $0}' | xargs rm

Basically I list all file in a single column -1 sort by modification time, newest first -t

Then using awk I set a var with the number of files specified by $FILESTOKEEP and print record greater than that.

Passing the results to xargs to which will run rm on each line of output by awk

This will work if the script is not called within the folder that holds the file since using a wildcard in ls will force to print the expanded path.

-1

Code

[vagrant@localhost ~]$ cat rm_gt_7_days
d=/tmp/test
fc=$(ls $d| wc -l)

if [ "$fc" -gt "7" ]; then
        find $d/* -mtime +7 -exec rm {} \;
fi

Test

Directory and files

mkdir /tmp/test && \
for f in `seq 7`; do touch /tmp/test/test${f}; done && \
touch -t 201203101513 /tmp/test/test8

Overview

[vagrant@localhost /]$ ll /tmp/test
total 0
-rw-r--r--. 1 root root 0 Sep 14 12:47 test1
-rw-r--r--. 1 root root 0 Sep 14 12:47 test2
-rw-r--r--. 1 root root 0 Sep 14 12:47 test3
-rw-r--r--. 1 root root 0 Sep 14 12:47 test4
-rw-r--r--. 1 root root 0 Sep 14 12:47 test5
-rw-r--r--. 1 root root 0 Sep 14 12:47 test6
-rw-r--r--. 1 root root 0 Sep 14 12:47 test7
-rw-r--r--. 1 root root 0 Mar 10  2012 test8

Execution of script and outcome

[vagrant@localhost ~]$ sh rm_gt_7_days
[vagrant@localhost ~]$ ll /tmp/test
total 0
-rw-r--r--. 1 root root 0 Sep 14 12:47 test1
-rw-r--r--. 1 root root 0 Sep 14 12:47 test2
-rw-r--r--. 1 root root 0 Sep 14 12:47 test3
-rw-r--r--. 1 root root 0 Sep 14 12:47 test4
-rw-r--r--. 1 root root 0 Sep 14 12:47 test5
-rw-r--r--. 1 root root 0 Sep 14 12:47 test6
-rw-r--r--. 1 root root 0 Sep 14 12:47 test7

Sources

  1. https://askubuntu.com/questions/62492/how-can-i-change-the-date-modified-created-of-a-file
  2. http://www.dreamsyssoft.com/unix-shell-scripting/ifelse-tutorial.php
  3. http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/
  4. http://www.cyberciti.biz/tips/how-to-generating-print-range-sequence-of-numbers.html
030
  • 1,557
  • This deletes files by how long ago the last modification was, and could delete all files. The example also finds files in other directories than /tmp/test, like /tmp/testfoo/bar.txt. – Volker Siegel Sep 14 '14 at 13:01
  • @VolkerSiegel Issue regarding /tmp/test* solved by changing it to /tmp/test/* – 030 Sep 14 '14 at 13:10
  • @VolkerSiegel According to this Q&A the creation date is stored nowhere – 030 Sep 14 '14 at 13:13
  • That's true - what I mean is that the question is not about how long ago the last file modification or creation was. – Volker Siegel Sep 14 '14 at 13:16
  • @VolkerSiegel Regarding sudo. This can be omitted indeed. The answer has been updated. – 030 Sep 14 '14 at 13:23
  • @VolkerSiegel Ok. I will update the answer once the question has been clarified. – 030 Sep 14 '14 at 13:26
-1

Not sure if you are trying to delete the tar files or the $newest files. But for general files in a single directory:

ls -t1 $dir | tail -n +8 | while read filename; do rm "$filename"; done

This should take care of if there are a) less than 7 files and b) whitespace in the filename (except a newline).

The tail command can take a positive to get the last lines starting from the beginning.

Arcege
  • 22,536
  • Good solution, except that ls -t1 $dir won't give absolute paths, so rm won't delete anything unless $dir == .. You could surround this with pushd $dir / popd. – schieferstapel Feb 08 '17 at 09:49
-3
if [ $file_count -gt 7 ]
    rm `/bin/ls -rt | head -1`
fi
hymie
  • 1,710
  • 2
    This deletes the newest file, and does not work with space in the names. – Volker Siegel Sep 14 '14 at 12:56
  • 1
    Re newest file: fixed. Re spaces: I never claimed that I was going to write a bulletproof solution. I gave him a line that would do what he wanted. If you're going to do something foolish like use spaces in your file names, then you deserve what you get. All you probably need are some double-quotes in the right place though to handle them. – hymie Sep 15 '14 at 18:19
  • 2
    How is using space in filenames stupid? You may not have control over the file names used by software you need. The OP does not even know the names before, as it is about downloads. – Volker Siegel Sep 15 '14 at 18:48