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
  • 1
  • 1
    You probably mean echo $file_count at the end? – Volker Siegel Sep 14 '14 at 12:42
  • 1
    Can you clarify which files to delete? You write "deleting the oldest file" in the text, but "the oldest ones" in the code comment, last line. If the question is clear, the question and the answers can help other users too. – Volker Siegel Sep 14 '14 at 12:54
  • Possible duplicate of this Q&A – 030 Sep 14 '14 at 14:21
-3
if [ $file_count -gt 7 ]
    rm `/bin/ls -rt | head -1`
fi
  • 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
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

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.

  • 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 8 '17 at 9:49
-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
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.