LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-29-2007, 01:18 PM   #1
chantrek
LQ Newbie
 
Registered: Mar 2007
Location: massachusetts
Distribution: fedora core 6
Posts: 8

Rep: Reputation: 0
Help Piping a Text File Into rm


Hello,

I've got a big list of files that need to be deleted. I'd like to somehow pipe the list into rm so that every item can be deleted with one command.

I use the following command to get the list:
find /mnt/server/users/2007 \( -name '*.exe' -o -name '*.lnk' -o -name '*.zip' -o -name '*.rar' -o -size 388096c \) >> ~/Desktop/search.txt

The results I get are pathnames, for example:
/mnt/server/users/2007/jklimp/American History/1544/VisualBoyAdvance-1.8.0-beta3/1544.zip
/mnt/server/users/2007/jklimp/American History/1544/VisualBoyAdvance-1.8.0-beta3/1939.zip

I then take the big list and separate the results into their own files:
grep .zip search.txt > zipresults.txt
grep .exe search.txt > exeresults.txt
grep .lnk search.txt > lnkresults.txt"

I do this because there are certain files that are OK to keep and its easier to edit them out of the bigger list of files to delete.

I've tried a few things:
"cat exeresults.txt | rm" which gives me the error 'missing operand'
"xargs rm exeresults.txt" doesn't do anything

I know I could use something like
find /mnt/server/users/2007 \( -name '*.exe' -o -name '*.lnk' -o -name '*.zip' -o -name '*.rar' -o -size 388096c \) -exec rm {}

but that won't work for me because it would delete all the executables and as I said, there are some that are okay to keep.

I've thought of trying to pop each line of the text file into an array and then dump each array into its own iteration of rm or something but I'm a nooob!

I'm sure its something simple that I'm just too retarded to pick up on

Please help!

Last edited by chantrek; 03-29-2007 at 01:21 PM.
 
Old 03-29-2007, 01:34 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Use backticks:

Code:
rm `cat theFileWithTheListOfDeletables`
--- rod.
 
Old 03-29-2007, 01:35 PM   #3
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Interesting question. Here's a "getaround" method for you: a short shell script to do the job:

Code:
#!/bin/bash

# Remove files listed in a file

filelist=$(cat zipresults.txt)
rm $filelist
It could easily be done so that the script asks the user for the filename to read.

EDIT: backticks (or whatever) may be a cool invention, but I hate them because they're so annoyingly similar to the other "tics": ' ` so that one may well mistype them and have a non-working command or a script..and I've generally always wondered why on earth do programming languages still use ", ', , ` for different purposes knowing that nobody wants to tell the difference anyway? I'm sure intelligent people could have come up with some other nicer method than that. Or maybe I'm just asking for too much
(but who wants an answer like: "you're using wrong tics there, use the other ones")

Last edited by b0uncer; 03-29-2007 at 01:41 PM.
 
Old 03-29-2007, 01:40 PM   #4
chantrek
LQ Newbie
 
Registered: Mar 2007
Location: massachusetts
Distribution: fedora core 6
Posts: 8

Original Poster
Rep: Reputation: 0
Hey thanks a lot.

Just one concern:
A few of the path names have folders with spaces in them, is that going to be a problem?

Yeah, the backticks work great except that the paths with spaces in them aren't being deleted.

Last edited by chantrek; 03-29-2007 at 01:51 PM.
 
Old 03-29-2007, 02:56 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Smile

Quote:
Originally Posted by chantrek
Hey thanks a lot.

Just one concern:
A few of the path names have folders with spaces in them, is that going to be a problem?

Yeah, the backticks work great except that the paths with spaces in them aren't being deleted.
b0uncer's commentary on backticks notwithstanding, use backticks with a slightly smarter cat:

Code:
rm `awk '{printf( "\"%s\"\n",$0);}' zipresults.txt`
That should be enough ticks to tick off b0uncer.

--- rod.

EDIT: Spaces in filenames is what really ticks me off.

Last edited by theNbomr; 03-29-2007 at 02:57 PM.
 
Old 03-29-2007, 03:05 PM   #6
chantrek
LQ Newbie
 
Registered: Mar 2007
Location: massachusetts
Distribution: fedora core 6
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr
EDIT: Spaces in filenames is what really ticks me off.
agreed but these students (high school students) don't like to make removing their roms, emulators, games and other non-school type stuff, easy for us .

edit: i copied the command and it gave the following results:

[user@localhost Desktop]$ rm `awk '{printf( "\"%s\"\n",$0);}' rartest.txt`
rm: cannot remove `"/mnt/server/users/2007/abreton/graphic': No such file or directory
rm: cannot remove `design/freehand': No such file or directory
rm: cannot remove `lessons/freehand': No such file or directory
rm: cannot remove `2/strokefill_start.zip"': No such file or directory

looks like its putting each space on a new line...

Thanks for all your help.

Last edited by chantrek; 03-29-2007 at 03:10 PM.
 
Old 03-29-2007, 04:23 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Can you take away the 'rm' and the enclosing backticks,
Code:
awk '{printf( "\"%s\"\n",$0);}' zipresults.txt
and see what it prints? I am guessing they've managed to create filenames with quotes or some other special characters in them, which does make it really hard to cleanup disk space with scripts. Might have to pull out perl, for an even smarter 'cat'.
If you post output from a shell command, or source code, please enclose it in CODE tags so the formatting is preserved. It is much easier to decipher that way.

--- rod.
 
Old 03-30-2007, 12:15 AM   #8
dugas
Member
 
Registered: Jul 2004
Location: louisiana
Distribution: fedora 4/kubuntu
Posts: 116

Rep: Reputation: 15
Another way to handle filenames with spaces

<code>cat test.txt | while read x; do rm "$x"; done; </code>
 
Old 03-30-2007, 09:23 AM   #9
chantrek
LQ Newbie
 
Registered: Mar 2007
Location: massachusetts
Distribution: fedora core 6
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr
Can you take away the 'rm' and the enclosing backticks,
Code:
awk '{printf( "\"%s\"\n",$0);}' zipresults.txt
and see what it prints? I am guessing they've managed to create filenames with quotes or some other special characters in them, which does make it really hard to cleanup disk space with scripts. Might have to pull out perl, for an even smarter 'cat'.
If you post output from a shell command, or source code, please enclose it in CODE tags so the formatting is preserved. It is much easier to decipher that way.

--- rod.
I get:
Code:
[user@localhost Desktop]$ awk '{printf( "\"%s\"\n",$0);}' rartest.txt
"/mnt/user/users/2007/abreton/graphic design/freehand lessons/freehand 2/strokefill_start.zip"
Thanks.
 
Old 03-30-2007, 10:49 AM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Hmmm. That is what is was supposed to print. The objective was to enclose the whole works in quotes, so that it would be seen as a single argument. However, what you haven't shown here is the whole list of files. I am speculating that some other file in the list contained a quote or other character that makes the list look 'wrong'. Then again, maybe that newline, '\n' that I put in there should actually be a 'space'. How about
Code:
awk '{printf( "\"%s\" ",$0);}' zipresults.txt
I initially tested the code on an arbitrary text file, and put the newline in there to make it look like the original, but with quotes around each line, and I see now that that was probably the wrong strategy.
--- rod.
 
Old 03-30-2007, 11:23 AM   #11
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
The issue with the spaces comes from the IFS environment variable. If you use bOuncer's method, just
add
Code:
IFS="\n"
in front of the line "filelist=$(cat zipresults.txt)".

Simply said, the shell splits the output of the cat on the separator(s) listed in $IFS, which are normally, space, tab & newline. Obviously, we don't want to separate on spaces and tabs, so setting IFS to contain only newlines should do the trick...without having to drag in awk, sed or perl.

Edit: if the rm command (also in bOuncer's solution) complains that the command too long, use a for-loop like this:
Code:
for file in $filelist; do rm $file; done
Code:
xargs rm < zipresults.txt
might work too, but I doubt that it'll work when there're a lot of files in the list. So you might want to try that using "ls" rather than "rm" first.

Last edited by timmeke; 03-30-2007 at 11:25 AM.
 
Old 04-02-2007, 09:32 AM   #12
chantrek
LQ Newbie
 
Registered: Mar 2007
Location: massachusetts
Distribution: fedora core 6
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr
Hmmm. That is what is was supposed to print. The objective was to enclose the whole works in quotes, so that it would be seen as a single argument. However, what you haven't shown here is the whole list of files. I am speculating that some other file in the list contained a quote or other character that makes the list look 'wrong'. Then again, maybe that newline, '\n' that I put in there should actually be a 'space'. How about
Code:
awk '{printf( "\"%s\" ",$0);}' zipresults.txt
I initially tested the code on an arbitrary text file, and put the newline in there to make it look like the original, but with quotes around each line, and I see now that that was probably the wrong strategy.
--- rod.
I get
Code:
[user@localhost Desktop]$ awk '{printf( "\"%s\" ",$0);}' rartest.txt
"/mnt/server/users/2007/abreton/graphic design/freehand lessons/freehand 2/strokefill_start.zip"
I'm using a test file with only this one entry in it.
 
Old 04-02-2007, 09:44 AM   #13
chantrek
LQ Newbie
 
Registered: Mar 2007
Location: massachusetts
Distribution: fedora core 6
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dugas
<code>cat test.txt | while read x; do rm "$x"; done; </code>
Woot!
This one worked.

Thanks a lot to everyone who helped me out with this!
 
  


Reply

Tags
backticks


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 12:55 PM
Piping asx video to file Chen Linux - Newbie 2 01-07-2005 04:38 AM
Piping output to text files corbis_demon Linux - General 3 10-12-2004 04:33 AM
Piping file list through rm swingheim Linux - Newbie 4 01-17-2004 06:48 PM
Piping output to a text file joadoor Linux - Newbie 7 04-19-2002 04:50 AM


All times are GMT -5. The time now is 12:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration