Register
It is currently Fri Nov 28, 2014 9:31 pm

Script to delete named file in multiple folders


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue Jun 22, 2010 8:31 am   

Joined: Tue Jun 22, 2010 8:28 am
Posts: 2
Hi.
Im using "ls > filename" to make a file, "filename" that has the names of the maps listed in it, one pr line. I need a script that can go through that file to delete named file/files/folders in those maps listed in that file.


Top
 Profile  
 PostPosted: Wed Jun 23, 2010 3:05 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 574
Hi,

the 'ls >file' is useless
Code:
#!/bin/bash

shopt -s nullglob
files2del=( /path/* )
directories=( '/path1/dir1' '/path2/dir2' )
for file in "${files2del[@]}"
do
   for dir in "${directories[@]}"
   do
      test -d "$dir/$file" && rm -R "$dir/$file"
      test -f "$dir/$file" && rm "$dir/$file"
   done
done


Top
 Profile  
 PostPosted: Wed Jun 23, 2010 5:11 am   

Joined: Tue Jun 22, 2010 8:28 am
Posts: 2
Thx for reply.
Your script holds that I need to fill inn the path to all the directories i whanna check. If i got hundreds of directories, thats exactly the stuff i dont whanna type in. The rest of the script looks nice.

I got a phyton script on an other forum that can do it, so if u dont have the time, dont bother.
The phyton script looks like this:

# -*- coding: utf-8 -*-
import os

# Get file name
filnavn = raw_input("Navn på filen du vil slette: ")

# Interate through every folder
mapper = os.listdir(os.curdir)
for mappe in mapper:
if os.path.isdir(mappe):
filer = os.listdir(os.curdir + '/' + mappe + '/')

# Iterate through every file in folder
for fil in filer:
if fil == filnavn:
print "Sletter filen fra mappen: " + mappe
os.remove(os.curdir + '/' + mappe + '/' + fil)


I'm sorry for the use of norwegian in this one.


Top
 Profile  
 PostPosted: Wed Jun 23, 2010 5:51 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 574
Code:
curent_dir=/needs/to/be/defined
directories=( $(for dir $current_dir/*; do [ -d "$dir" ] && echo "$dir"; done) )

or probably faster:
Code:
curent_dir=/needs/to/be/defined
directories=( $(find $current_dir -maxdepth 1 -type d) )


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 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:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP