hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Hosting Security and Technology : Linux command/script to run a command for every file in a directory tree?
Reply

Forum Jump

Linux command/script to run a command for every file in a directory tree?

Reply Post New Thread In Hosting Security and Technology Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 01-25-2004, 04:24 PM
civ civ is offline
Newbie
 
Join Date: Jan 2004
Posts: 27
Question

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?



Sponsored Links
  #2  
Old 01-25-2004, 04:42 PM
AKavanaugh AKavanaugh is offline
Web 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

  #3  
Old 01-25-2004, 06:16 PM
YourAdmin YourAdmin is offline
Newbie
 
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 10+ years experience
sales [at] yourserveradmin.com - for general inquiries
911 [at] yourserveradmin.com - for any urgent help

Sponsored Links
  #4  
Old 01-25-2004, 06:28 PM
civ civ is offline
Newbie
 
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>

  #5  
Old 01-25-2004, 06:31 PM
YourAdmin YourAdmin is offline
Newbie
 
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 {} \;
Thanks

__________________
YourServerAdmin
Server Management l 24/7/365 l Linux l Windowsl 10+ years experience
sales [at] yourserveradmin.com - for general inquiries
911 [at] yourserveradmin.com - for any urgent help

  #6  
Old 01-25-2004, 08:48 PM
civ civ is offline
Newbie
 
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 \;
Thanks to both of you for all your help.

Jon

Reply

Related posts from TheWhir.com
Title Type Date Posted
Newtek Web Hosting Listing 2014-04-17 01:32:31
Beta Release of Red Hat Enterprise Linux 7 Launched Web Hosting News 2013-12-12 13:01:56
OpenStack and the AWS API Debate Web Hosting News 2013-07-30 14:21:54
MIT Researchers Propose New Hardware Design to Encrypt Cloud Web Hosting News 2013-07-08 11:15:26
Researchers Urge System Admins to Check for New Apache Web Server Backdoor Malware Web Hosting News 2013-05-01 11:35:53


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?