|
Home > Archive > Unix Shell > November 2007 > rm < junkfilelist.txt
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
rm < junkfilelist.txt
|
|
| ry2ngh@gmail.com 2007-11-20, 1:26 pm |
| I have a list of files that I want to delete from a directory. Is
there a way to pipe this list to a comand line such as
$ rm < junkfilelist.txt
(this produces an error message and does not remove the files)
Or is the solution more complicated than this?
Thanks,
-Ryan
| |
| Kenny McCormack 2007-11-20, 1:26 pm |
| In article <4a185455-13da-4d00-b97b-795bf43ab6c6@n20g2000hsh.googlegroups.com>,
<ry2ngh@gmail.com> wrote:
>I have a list of files that I want to delete from a directory. Is
>there a way to pipe this list to a comand line such as
>
>$ rm < junkfilelist.txt
>
>(this produces an error message and does not remove the files)
>
>Or is the solution more complicated than this?
>
>Thanks,
>
>-Ryan
>
man xargs
| |
| Chris F.A. Johnson 2007-11-20, 7:29 pm |
| On 2007-11-20, ry2ngh@gmail.com wrote:
>
>
> I have a list of files that I want to delete from a directory. Is
> there a way to pipe this list to a comand line such as
>
> $ rm < junkfilelist.txt
>
> (this produces an error message and does not remove the files)
>
> Or is the solution more complicated than this?
rm does not read filenames from the standard input; it requires
them to be on the command line. If the file is small, you can use:
rm $( cat junkfilelist.txt )
Otherwise, use xargs:
xargs rm < junkfilelist.txt
Both methods will fail if the filenames contain spaces or other
pathological characters. In that case, you can use a loop:
while IFS= read -r file
do
rm -- "$file"
done < junkfilelist.txt
Or:
awk '{ printf "rm -v -- \"%s\"\n", $0 }' junkfilelist.txt | sh
The latter will have problems if names contain quotes. If they do,
add a gsub() statement to escape them.
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| ry2ngh@gmail.com 2007-11-20, 7:29 pm |
| Thanks Chris and Kenny, that solved my problem.
-Ryan
| |
| x77770@yahoo.com 2007-11-22, 1:44 am |
| On Nov 20, 10:56 am, ry2...@gmail.com wrote:
> I have a list of files that I want to delete from a directory. Is
> there a way to pipe this list to a comand line such as
>
> $ rm < junkfilelist.txt
>
> (this produces an error message and does not remove the files)
>
> Or is the solution more complicated than this?
>
> Thanks,
>
> -Ryan
rm `cat junkfilelist.txt`
|
|
|
|
|