Operating System - HP-UX
1827207 Members
2719 Online
109716 Solutions
New Discussion

Re: How can I use the content of a file for a command?

 
SOLVED
Go to solution
Remko Oosenbrug
Contributor

How can I use the content of a file for a command?

I have a file named verw.txt. The content is a list of files in a directory. How can i use the content of verw.txt with the rm command.
Something like rm
OH No. It's happening again
5 REPLIES 5
Andreas Voss
Honored Contributor
Solution

Re: How can I use the content of a file for a command?

Hi,

use:
rm `cat verw.txt`

Regards
federico_3
Honored Contributor

Re: How can I use the content of a file for a command?

try :

cat file.txt | xargs rm

it should work fine!


Federico
Louis Mushandu
Occasional Contributor

Re: How can I use the content of a file for a command?

I assume what you are trying to do in this example is remove the files. My suggestion would be a simple for loop.

for files in `cat verw.txt`
do
rm $files
done

I hope that this answers your question.
Kofi ARTHIABAH
Honored Contributor

Re: How can I use the content of a file for a command?

If your verw.txt contains a LOT of files, you are better off using:

cat verw.txt | xarg rm

becaus there is a limit to the number of parameters that rm can handle when used in rm `cat verw.txt`
nothing wrong with me that a few lines of code cannot fix!
John Palmer
Honored Contributor

Re: How can I use the content of a file for a command?

The `command` construct is deemed 'archaic' in the man pages (man sh-posix and man ksh).

The more 'politically correct' construct is $(command) but both work equally well.

Thus rm $(verw.txt) will also work but beware that the shell has a limit of how many arguments can be passed to a command so if verw.txt has a lot (several thousand) of records this could fail.

This is where xargs scores as it only supplies the arguments a record at a time to the command you supply.

e.g. cat verw.txt | xargs rm

This does result in the 'rm' command being invoked for every record in verw.txt so it is not so efficient as the single rm call in the first example.