is there a way to make a script look at a dirctory, compare that to a list of file name in a text file and delete the actual files that do NOT appear in the text file of file names?
nowaday, people use perl because
a) they will not learn 'c'
b) perl is very good+performant, simply because it's 'c'.
but, they still dont think performant...
- suppose this directory grows to Xthousend entries
the chomp statemen will be executed Xthousend times.
it does not matter how big the @files is, chomp it ONCE.
- immediately break the loop if FOUND
- read the output of 'readdir' in an array, like you did
with 'path/to/file' AND SORT both, its more code... and
performance.
- if you don't want to sort, let the system do it for you
'system("ls");' gives sorted output.
- using sorted lists (and little more code) you don't need
each time, to restart the list on the top.
- good code needs time, once writted, gives time.
FINALLY, why re-invent the wheel ?????
let the system do the job.
assumed your list is called must_list, create an is_list
entering: ls >is_list
then use: comm -13 must_list is_list
look at the output, see man pages comm, diff, cmp ...
arn0ld's solution is incredible. It's also a little daunting. I would definitely have a go with that first.
BUT you could always:-
1. create a temporary named KEEP
2. move all the files that exist in your text file to that directory
3. delete any that are left
4. move the files in the KEEP directory back to the original directory
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.