| | |
Take input from a file and perform a command
![]() |
•
•
Join Date: Nov 2005
Posts: 2
Reputation:
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?
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?
Shell Scripting Syntax (Toggle Plain Text)
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.
Alex Cavnar, aka alc6379
•
•
Join Date: Feb 2006
Posts: 17
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by alc6379
Shell Scripting Syntax (Toggle Plain Text)
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.
Shell Scripting Syntax (Toggle Plain Text)
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!
•
•
Join Date: Mar 2006
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by script_noob
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)
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)
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- Ignoring xmlns namespaces in the input xml file (XML, XSLT and XPATH)
- Getting all data from an input and output file (C++)
- Reading in a .dat file from SSH shell command line (Java)
- input data from a file into an Array (C)
Other Threads in the Shell Scripting Forum
- Previous Thread: re
- Next Thread: extracting db2 table records to csv
| Thread Tools | Search this Thread |






