954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Take input from a file and perform a command

input.txt is a file that contains filenames that have been piped into it from an external program.

for example...

input.txt has the following in it
filename1.txt
filename2.txt
filename3.txt

I would like to perform an 'rm' command on the filenames contained in input.txt

So instead of potential hours of work deleting each individual filename contained in input.txt, I would like to automate this task by a simple bash script or using another program like awk.

I have toyed with using the 'exec' command in bash and contemplated that there must be an easier way using awk or another such program.

anybody got a quick one or two liner that would demonstrate to me how I would accomplish this?

script_noob
Newbie Poster
2 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 
for i in `cat input.txt`;
do
     rm -f $i
done


...Something like that should do it. Basically, it should count each line as an item, and the $i variable is going to be the name of the file on that line. I added the -f command because I don't want it to ask me "are you sure?" every time, like some distros make bash do. If you're not as brave, then you can omit the -f on therm command.

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

That's just what I was looking for! Thanks!

script_noob
Newbie Poster
2 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 
for i in `cat input.txt`;
do
     rm -f $i
done

...Something like that should do it. Basically, it should count each line as an item, and the $i variable is going to be the name of the file on that line. I added the -f command because I don't want it to ask me "are you sure?" every time, like some distros make bash do. If you're not as brave, then you can omit the -f on therm command.

I am trying to write a script that accomplishes this same thing, but I am running into a problem.

for i in <filename>
do
     <application command> <-option> $i
done


What I'm finding is that the command is coming out something like this:
<-option>

Why is the name of the file being used as the variable instead of the text in the file?

Thanks!

rusman
Newbie Poster
20 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

you need to cat the file.

Subterraneus
Newbie Poster
9 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

input.txt is a file that contains filenames that have been piped into it from an external program.

for example...

input.txt has the following in it filename1.txt filename2.txt filename3.txt

I would like to perform an 'rm' command on the filenames contained in input.txt

So instead of potential hours of work deleting each individual filename contained in input.txt, I would like to automate this task by a simple bash script or using another program like awk.

I have toyed with using the 'exec' command in bash and contemplated that there must be an easier way using awk or another such program.

anybody got a quick one or two liner that would demonstrate to me how I would accomplish this?


you can do in two ways.
1. write a shell script so that u need to just run the script

cat rm.sh

#!\bin\sh
for i in `cat input.txt`
do
rm $i
done

then u cnage the permisssion.
command prompt>chmod 777 rm.sh
just run the shell script
command prompt>rm.sh
this will delete all the files given in the list.txt

craju45
Newbie Poster
3 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

If the only thing in the file is filenames simply do
rm `cat input.txt`
if there is more than just filenames, but say the third word of
every line is a filename then do the following:
rm `awk '{print $3}' input.txt`
(the fourth word would be $4, etc)

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You