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?

Recommended Answers

All 7 Replies

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 the rm command.

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

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 the rm 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:
<application command> <-option> <filename>

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

Thanks!

you need to cat the file.

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

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)

rsrdfgvhgvhg6t
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.