LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Run command against text file (https://www.linuxquestions.org/questions/linux-newbie-8/run-command-against-text-file-610227/)

vonedaddy 12-31-2007 03:30 PM

Run command against text file
 
I recently copied my data over to my linux box from my windows box. I have all these Thumbs.db files all over the place now.

So I did this:

find / -name Thumbs.db > thumbs.txt

Now I have a text file with the location of every Thumbs.db file. Now I want to delete all those files, how can I run an rm command against the entries in this file?

kiley_rodgers 12-31-2007 03:43 PM

find / -name Thumbs.db -exec rm {} \;



you can also do
find / -name Thumbs.db -exec rm -i {} \;
if you would ilke it to ask for confirmation for each deletion.... change filename as needed.....

vonedaddy 12-31-2007 03:44 PM

Quote:

Originally Posted by kiley_rodgers (Post 3006982)
find / -name Thumbs.db -exec rm {} \;



you can also do
find / -name Thumbs.db -exec rm -i {} \;
if you would ilke it to ask for confirmation for each deletion.... change filename as needed.....



Thats also some great info, but just for kicks, lets say I wanted to run it against the file I have already?

David the H. 12-31-2007 04:18 PM

Use a simple 'for' loop:

for f in $(cat thumbs.txt); do rm $f; done

This reads the contents of thumbs.txt (use '$()' to get the output of a command, 'cat' in this case) one line at a time into variable f, then runs the rm command on the contents of the variable ($f), and loops back to do the next line.


I suggest you remember the 'for' loop syntax. It's probably the most useful bash command you can learn.

vonedaddy 12-31-2007 05:43 PM

Quote:

Originally Posted by David the H. (Post 3007013)
Use a simple 'for' loop:

for f in $(cat thumbs.txt); do rm $f; done

This reads the contents of thumbs.txt (use '$()' to get the output of a command, 'cat' in this case) one line at a time into variable f, then runs the rm command on the contents of the variable ($f), and loops back to do the next line.


I suggest you remember the 'for' loop syntax. It's probably the most useful bash command you can learn.


This is exactly what I was looking for thank you both! I will read up on the for syntax.
Is there a man page or website I can read, something you can suggest?

Tinkster 12-31-2007 07:42 PM

Aye ...
man bash
Search for loops ...

Or
http://tldp.org/LDP/abs/html/index.html



Cheers,
Tink

ghostdog74 12-31-2007 07:58 PM

Quote:

Originally Posted by David the H. (Post 3007013)
Use a simple 'for' loop:

for f in $(cat thumbs.txt); do rm $f; done

safer with this if file names have spaces
Code:

for i in "$(cat file)".....
better still, use while loop. Eliminate the use of cat
Code:

while read line
do
 # blah
done < file



All times are GMT -5. The time now is 06:52 PM.