There is a fundamenal understanding problem here, The "find" command posted will look for a file called "tobedelete.txt" and delete that file (not the files listed in that file).
The code posted by "gull04" is scary.
Please post the actual directory structure in machine-readable format:
I've used this type of routine many times, in view of the original post made by yogeshkumkar and given that I pointed out that he should print out the list of the find command I'd just like to understand how my code was "scary".
I'm quite happy to admit that I'm as likely to screw up as the next guy, but in this case I can't really see what is wrong with the advice that I gave. As I'm always keen to learn, I'd appreciate your feedback.
I don't mean to be critical, I'm just interested to know - I wouldn't deliberately give someone poor advice. I really didn't think that my post was too far off the mark, I use "find" this way frequently - I'm quite happy to admit my shortfalls but in this case I thought the advice was OK. Based on the original post I didn't think this was poor advice, I was just trying to solve a problem for someone.
Perhaps I should have said that the code should be "rm -i" and you can check each file prior to deletion.
for i in `cat tobedelete.txt`
do
find . -name ${i} -exec rm -i {} \;
done
Thanks for the comments Methyl and Dave.
The answer by Dave doesnt work. it simply removes the content of the tobedeted.txt.
The actual dir structure is like
in the main_dir
there are subdirs like AAA, BBB, CCC, ....
inside AAA there are subdirs like aa1, aa2, aa3, ...
inside aa1 there are files like AAA_aa1_file1.txt, AAA_aa1_fileextra.txt, AAA_aa1_mod43a4.txt, ....
inside aa2 there are files like AAA_aa2_file1.txt, AAA_aa2_fileextra.txt, AAA_aa2_mod43a4.txt, ....
inside BBB there are subdirs like bb1, bb2, bb3, ...
inside bb1 there are files like BBB_bb1_file1.txt, BBB_bb1_fileextra.txt, BBB_bb1_mod43a4.txt, ....
inside bb2 there are files like BBB_bb2_file1.txt, BBB_bb2_fileextra.txt, BBB_bb2_mod43a4.txt, ....
and so on...up to many hundred of files. I managed to the get the filenames which I must delete.
e.g. the content of tobedeleted.txt is
AAA_aa1_fileextra.txt
BBB_bb1_file1.txt
BBB_bb1_mod43a4.txt
CCC_cc4_mfg54d2.txt
GGG_gg6_hd21s1.txt
and so on....
for i in `cat tobedelete.txt`
do
find . -name ${i} -print
done
Just for information, the tobedeleted.txt file should be in the directory where you are running the script from or you should enter the pat to the file as well such as "for i in `/path/to/file/tobedeleted.txt`"
@gull04
Your script contains a couple of common problems:
1) Uses "for" with an open-ended list.
This breaks when the command line gets too long.
Also, the "for" breaks if any filename contains a space character.
2) The "find" is not specific to files
We can turn the script round to make it a bit more robust:
For the diagnostic version we could display what we are searching for,
because I too am having trouble understanding the content of "tobedelete.txt" (or is it "tobedeleted.txt"?). There is nothing in your code which would overwrite the file.
if [ -f "tobedelete.txt" ]
then
cat tobedelete.txt | while read filename
do
echo "${filename}"
find . -type f -name "${filename}" -print
echo ""
done
else
echo "File does not exist: tobedelete.txt"
fi
@yogeshkumkar
Please post what Operating System and version you have and what Shell you are using.
You're absolutely right in your approach to making the script more robust and I must admit there were several basic assumptions made by me that were possibly optimistic.
I did assume;
IFS='
'
And again I'd say that I took yogeshkumkar at face value when I gave the advice, not that that is any excuse for sloppiness. I should have tested for file types incase there was a link or the likes, should have enclosed the variable to ensure that it worked around spaces etc.
I work in a fairly pressured production/dr/test/development environment. So have four of every machine from M9000's to T5120's where I always have the facility to test - possibly it has made me a little cavalier.
I'll try to remember that not everyone can restore Pb sized file systems in minutes if things go pear shaped
Thanks for the vote of confidence, Pb Petabyte 1024Tb - dead easy with ZFS. BTW thanks for being patient with me on the forum, I'm prepared to try - "never been wrong, never done anything".
@yogeshkumkar
DANGEROUS SCRIPT
The first "find" command posted would be a syntax error on some O/S because there is no "-name" parameter. On my O/S it produces a list of EVERY file and directory in the directory named in the first parameter. Thus your script is in danger of deleting EVERY file regardless of whether they appear in "filenames.txt".
I've commented about using "for" with open-ended lists in an earlier post.