View unanswered posts    View active topics

All times are UTC - 6 hours





Post new topic Reply to topic  [ 9 posts ] 
Print view Previous topic   Next topic  
Author Message
Search for:
PostPosted: Tue Apr 11, 2006 11:43 am 
Offline
Joined: Sat Jun 18, 2005 7:47 am
Posts: 73
Location: Sweden
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).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 2:17 pm 
Offline
Joined: Fri Nov 18, 2005 9:22 am
Posts: 777
Location: spencerport, ny (USA)
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 4:51 pm 
Offline
Joined: Sat Jun 18, 2005 7:47 am
Posts: 73
Location: Sweden
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!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 6:45 pm 
Offline
Joined: Tue Apr 13, 2004 6:51 pm
Posts: 890
Location: Groton, MA
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 \;

_________________
R5F1 - Dell P4 2.4Ghz 500MB - PVR250 x 2 - GeForce FX 5200 - Onboard sound/NIC 80GB ATA/250GB ATA/400GB SATA


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 7:03 pm 
Offline
Joined: Thu Mar 25, 2004 11:00 am
Posts: 9551
Location: Arlington, MA
Nobody is going to mention xargs or the newer $() version of backtick expansion?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 11:28 pm 
Offline
Joined: Tue Feb 21, 2006 7:24 am
Posts: 396
Location: Dushanbe, Tajikistan
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 7:18 am 
Offline
Joined: Fri Nov 18, 2005 9:22 am
Posts: 777
Location: spencerport, ny (USA)
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...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 7:25 am 
Offline
Joined: Sat Jun 18, 2005 7:47 am
Posts: 73
Location: Sweden
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 14, 2006 4:06 pm 
Offline
Joined: Tue Dec 20, 2005 3:35 pm
Posts: 57
Location: Seattle, WA
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


Top
 Profile  
 

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 


All times are UTC - 6 hours




Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group

Theme Created By ceyhansuyu