Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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")
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...
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.