Skip to content
Advertisement

Commands terminated with the same char

How can I fix the follow line:

for line in $(cat /root/files_to_search); do find /opt/ -iname $line -exec rm {} >> files_founded.txt ; done

The problem is that the commands of for and -exec end with a semicolon, the for command has this statement

for i in something; do command ; done (do terminate with ;)

and find statment with -exec

find /path/ -name somestring -exec command ; (-exec also finish with ;)

Advertisement

Answer

You don’t even need to use -exec echo {} since that is default action in find. You can use this for loop:

while IFS= read -r line; do
    find /opt/ -iname "$line"
done < /root/files_to_search >> files_founded.txt

There is no need to use for line in $(cat ...) either as you can read from the input using < file

See BASH FAQ on reading a file line by line

If you must use -exec in find then use:

while IFS= read -r line; do
    find /opt/ -iname "$line" -exec rm {} ;
done < /root/files_to_search >> files_founded.txt
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement