Results 1 to 6 of 6
  1. #1

    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?

  2. #2
    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. #3
    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

  4. #4
    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. #5
    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. #6
    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

  7. Newsletters

    Subscribe Now & Get The WHT Quick Start Guide!

Related Posts from theWHIR.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •