LinHES Forums
http://forums.linhes.org/

rm command < list of files?
http://forums.linhes.org/viewtopic.php?f=5&t=9584
Page 1 of 1

Author:  avocade [ Tue Apr 11, 2006 11:43 am ]
Post subject:  rm command < list of files?

How could I pipe a list of files to delete to the rm command? Yes, I know, very basic, but I just can't find how to do it!

rm < list.txt

Didn't work at all. Would be great if you could help me. Final problem to be solved since installing the myth2ipod script (it should really include a automatic "remove-old-files-to-save-diskspace" function).

Author:  thornsoft [ Tue Apr 11, 2006 2:17 pm ]
Post subject: 

Use with "find", like this:

find $HOME/dr/DAILY_*.zip -mtime +7 -exec rm {} \; >/dev/null 2>&1

This is from another sytem, but it's going to delete all DAILY_*.zip files over a week old. The "-exec rm" does the removal of whatever "find" found.
Use caution with this!
Notice that I qualify the full path. Less robust solutions will perform a "cd" first, to hop into a directory, then clean it out.
I have seen such scripts do horrible things. If the "cd" fails, the find/remove will chew on whatever directory you happened to be in. Probably recursively. Until it deletes something important, and the system crashes.

Author:  avocade [ Tue Apr 11, 2006 4:51 pm ]
Post subject: 

thornsoft wrote:
Use with "find", like this:

find $HOME/dr/DAILY_*.zip -mtime +7 -exec rm {} \; >/dev/null 2>&1

This is from another sytem, but it's going to delete all DAILY_*.zip files over a week old. The "-exec rm" does the removal of whatever "find" found.
Use caution with this!
Notice that I qualify the full path. Less robust solutions will perform a "cd" first, to hop into a directory, then clean it out.
I have seen such scripts do horrible things. If the "cd" fails, the find/remove will chew on whatever directory you happened to be in. Probably recursively. Until it deletes something important, and the system crashes.


That's some extreme cabbage. I'll try it out. Carefully... Thanks!

Author:  khrusher [ Tue Apr 11, 2006 6:45 pm ]
Post subject: 

backquotes ' are your friend. if you use them in a command line, the command within a set of backquotes is executed, the output of the command is inserted into the original command.

Code:
rm `cat file`


another technique is to turn your list.txt file into a shell script:

Code:
vi list.txt


while in vi do this:

Code:
:1,$s/^/rm /
:wq

then
Code:
sh list.txt


The vi step replaces the beginning of everyline with 'rm ' basically creatig a script that deletes one file per line.

I can explain more if you like.

the find command is very powerful and well worth learning. probably not required here since you have a file that contains the list of files you want to delete. find is a tool that will locate files (using lots of different criteria) and use the list of 'found files' in a command.

I use it daily at work. For example "find any log file that contains the string 'foo'"

Code:
find / -name "*.log" -exec grep -l  foo {} /dev/null \;

Author:  tjc [ Tue Apr 11, 2006 7:03 pm ]
Post subject: 

Nobody is going to mention xargs or the newer $() version of backtick expansion?

Author:  mac [ Tue Apr 11, 2006 11:28 pm ]
Post subject: 

I love unix, it is like legos. Lots of small pieces that you put together any way you like.

I often use awk to build command lines and then I send it to sh for evaluation.

cat file | awk '{print "rm "$0}' |sh

Author:  thornsoft [ Wed Apr 12, 2006 7:18 am ]
Post subject: 

Quote:
I often use awk to build command lines and then I send it to sh for evaluation.

I like that. I've added to my "tips" file, where I keep everything I know about Linux/Unix. It's a fairly short file...

Author:  avocade [ Wed Apr 12, 2006 7:25 am ]
Post subject: 

I opted to use this command in crontab -e:

20 14 * * 0,3 find /myth/ipodfeed/*.ipod.* -mtime +10 -exec rm {} \; >/dev/null 2>&1

Does everything I want (I think :)). Thanks for all the suggestions.

Author:  mz4wheeler [ Fri Apr 14, 2006 4:06 pm ]
Post subject: 

mac wrote:
I love unix, it is like legos. Lots of small pieces that you put together any way you like.

I often use awk to build command lines and then I send it to sh for evaluation.

cat file | awk '{print "rm "$0}' |sh


The above won't work if the file has more than 2000 (or so) entries. So, another method:

cat file | xargs /bin/rm

Putting the hard-coded path to "rm" prevents and funky aliases that are defined to take effect.
________
When Rover Met Bmw

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/