Results 1 to 6 of 6
-
01-25-2004, 04:24 PM #1Newbie
- Join Date
- Jan 2004
- Posts
- 27
Linux command/script to run a command for every file in a directory tree?
Is there a utility that'll do this, or an easy way to script one? There is an old DOS utility (GLOBAL.EXE) that would recursively run a given command on every file in a directory.
I'm trying to use the Unix command "zip" to inject a file into all zip files in a directory (and its subdirectories). The command to do this for one file is:
zip stuff.zip addfile.txt
- results in addfile.txt being added to the contents of stuff.zip. This is fine, but I need to inject "addfile.txt" into all zipfiles in the directory tree! Any pointers?
-
01-25-2004, 04:42 PM #2Web Hosting Master
- Join Date
- Apr 2003
- Posts
- 553
How about doing it in a for loop...
for i in *.zip
do
zip $i addfile.txt
done
-
01-25-2004, 06:16 PM #3Newbie
- Join Date
- Feb 2003
- Location
- Germany, Magdeburg
- Posts
- 27
loop can't access to all files in a directory tree
You should use something like that:
find /some/path/here -type f -name '*.zip' -exec zip {} addfile.txt\;YourServerAdmin
Server Management l 24/7/365 l Linux l Windowsl 15+ years experience
sales [at] yourserveradmin.com - for general inquiries
911 [at] yourserveradmin.com - for any urgent help
-
01-25-2004, 06:28 PM #4Newbie
- Join Date
- Jan 2004
- Posts
- 27
I didn't know you could use a filename as a list item in a for loop like that! Good to know. That works great for all the zipfiles in a directory, but what would I need to add to make it work for subdirectories? The only way I can think to do it is to call a function recursively to process each subdirectory, but how would I get the directory names? Or is there an easier way?
Thanks!
<edit> Ah, I see my question was answered while I was typing this - thanks, I'll give it a try! </edit>
-
01-25-2004, 06:31 PM #5Newbie
- Join Date
- Feb 2003
- Location
- Germany, Magdeburg
- Posts
- 27
1. "find" command works for all subdirectories
2. placeholder for file name is '{}'
Use this command for tests:
find /some/path/here -type f -name '*.zip' -exec ls -l {} \;
ThanksYourServerAdmin
Server Management l 24/7/365 l Linux l Windowsl 15+ years experience
sales [at] yourserveradmin.com - for general inquiries
911 [at] yourserveradmin.com - for any urgent help
-
01-25-2004, 08:48 PM #6Newbie
- Join Date
- Jan 2004
- Posts
- 27
Works great! Slight modification to your syntax on the first example - I had to add a space between "addfile.txt" and "\;":
Code:find /some/path/here -type f -name '*.zip' -exec zip {} addfile.txt \;
Jon