Well i am really pissed off :( I have a file called test.txt. and here it is:
"/var/lib/backup.log"
"/var/lib/backup2.log"
double quotes are included in the file each at beginning and end of the directory and i can not remove them.
i am trying to write a script to remove files in test.txt. like this:
for del in `cat test.txt` ; do
rm -f $del
done
but it does not work as expected :(
it gives this error:
rm: cannot access "/var/lib/backup.log": No such file or directory
rm: cannot access "/var/lib/backup.log2": No such file or directory
It's easy to rustle up a quick Perl script
and run it thus:
Although you've specified bash in the above, I'm not sure if you really want a bash-only solution.
Note that this will handle whitespaces in file names etc.
I guess
eval
command will do that work for you:This will just remove the quote character from the beginning and the end of the read entry, which is better than blindly removing all quote characters (since they can appear in filenames, of course).
And, regarding your initial code, PLEASE ALWAYS USE QUOTES until you really know when and when not.
The following one-liner should do it:
Here,
tr
translates all"
to null (\0
), where the input is from the file namedtest.txt
. Finally,rm
is supplied with the results.The following Perl one-liner can be used for the same too:
Searches and replaces
"
from the each line read fromtest.txt
. Then,unlink
removes the file.Or,
Escape spaces, remove
"
and delete the file.