Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Files listed in a Text File

Status
Not open for further replies.

johnnyasterisk

IS-IT--Management
Apr 2, 2008
26
IE
I have a directory with over 200,000 files in it but some file are old. I also have a table in mysql which keeps a record of every file in that directory:

Example:

DIR = Recordings
FILE = 1234567.gsm

I can run a query on my table to fine specific files and dump that into a text file. For example I might want to find all files put in this directory by john and get a txt file in the following format:

12344.gsm
12466.gsm
15234.gsm

(Above is as much as I can do what I need help on is below)

I now have a file called john.txt with 300 lines in it each line looking like this:

12344.gsm (but different numbers)

These are the file names of all files put in this directory by john and I want to remove them if they are older than 6 months.

I need help writing a shell script that will:

* read the txt file
* One line at a time check if the file is older than 6 months and if so delete it
 
This is really probably a job for find with the -mtime flag, such as:

find /path/to/dir -name '*.gsm' -mtime +182 -exec rm {} \;

which reads your directory and finds files older than 182 daya (an approx 6 months) nad feeds them to rm to delete. For safetly, do a dry run and feed it to ls -la first so that you know only the files you want are being listed:

find /path/to/dir -name '*.gsm' -mtime +182 -exec ls -la {} \;

Hope this helps. You could also feed the output of the find into a text file and delete them individually from there.

The internet - allowing those who don't know what they're talking about to have their say.
 
You can try a simple bash script like this:

#!/bin/bash
cd /
list='textfile'
source='/path_to_files'
for file in `cat $list`;
do rm -rf $source/$file;
done

Good luck! ;)
 
...I hear the thundering hooves of the uuoc posse fast approaching ;-)

The internet - allowing those who don't know what they're talking about to have their say.
 
Hey, I believe one should use whatever tools one is comfortable with. :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top