Results 1 to 8 of 8

Thread: Bash :: Command recursion to a directories' contents

  1. #1
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Bash :: Command recursion to a directories' contents

    Hey all!

    Another lame-ass bash question again.

    I want to basically do about 5 commands to a shitload of files. How can I pipe properly the dir listing to the commands? For example at the end I wanna gzip (dont need to tar) every file up. How could I apply all files in the 'ls' listing to 'gzip -v $filename' ?

    Thaks a bunch!

  2. #2

    Re:Bash :: Command recursion to a directories' contents

    You can use something like this:
    Code:
    for i in *
    do
    gzip $i
    done
    There is a more succinct way of doing it using "find", but I can never remember it (check the man page if you want to follow it up).

  3. #3
    Senior Member
    Join Date
    Sep 2002
    Posts
    421

    Re:Bash :: Command recursion to a directories' contents

    [quote author=pam link=board=9;threadid=6196;start=0#58698 date=1043656339]
    You can use something like this:
    Code:
    for i in *
    do
    gzip $i
    done
    There is a more succinct way of doing it using "find", but I can never remember it (check the man page if you want to follow it up).

    [/quote]
    You mean like so? find . -name 'pattern*' -exec gzip {} \;

    This would be a first approximation. find is indeed very powerful when you use the -exec switch. The "{}" stands for the path to the file that find returns so if you need to call gzip with certain flags they would be inserted before the {} as in ... -exec gzip -5 {} \;.

  4. #4
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re:Bash :: Command recursion to a directories' contents

    [quote author=pam link=board=9;threadid=6196;start=0#58698 date=1043656339]
    You can use something like this:
    Code:
    for i in *
    do
    gzip $i
    done
    There is a more succinct way of doing it using "find", but I can never remember it (check the man page if you want to follow it up).

    [/quote]

    Perfect. I just fixed it (added all of the commands) and it worked like a charm. I saw a completely different way (grep, head, for loop) that confused the shit out of me, and couldnt for the life of me recall it today.

    And of course simplicity prevailed

  5. #5
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re:Bash :: Command recursion to a directories' contents

    [quote author=demian link=board=9;threadid=6196;start=0#58699 date=1043660769]
    You mean like so? find . -name 'pattern*' -exec gzip {} \;

    This would be a first approximation. find is indeed very powerful when you use the -exec switch. The "{}" stands for the path to the file that find returns so if you need to call gzip with certain flags they would be inserted before the {} as in ... -exec gzip -5 {} \;.
    [/quote]

    Thanks -- I am keeping this written down and close by. Pam's is easy to remember. But I can see the usefulness of it in the future. Especially when I get reemployed as an admin ...

  6. #6

    Re:Bash :: Command recursion to a directories' contents

    Hmm I was wondering if you were including folders in this mess... if so how could you do an ls without showing folders too? I could get all the files to be displayed such:

    for i in $(ls -p)
    do
    echo $i
    done

    So I tried:

    for i in $(ls -p)
    do
    echo $i | grep '/$'
    done

    which printed only folders.... but I couldn't figure out how to negate the regex... Which would allow for just files... But anyway, I knew something much more simple (such as the find solution) could be done, but I just wasn't thinking (2AM) heh

  7. #7
    Senior Member
    Join Date
    Sep 2002
    Posts
    421

    Re:Bash :: Command recursion to a directories' contents

    [quote author=trieder link=board=9;threadid=6196;start=0#58719 date=1043681762]
    ... I couldn't figure out how to negate the regex... Which would allow for just files... [/quote]

    grep has the -v switch which inverts the matches. That would do, I guess. I'm a big time find-fan so I'd do find . -type d -exec ... for directories and find . -type f ... for regular files.

  8. #8
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re:Bash :: Command recursion to a directories' contents

    [quote author=trieder link=board=9;threadid=6196;start=0#58719 date=1043681762]
    Hmm I was wondering if you were including folders in this mess...
    [/quote]

    I didnt really need recursion, although that would be nice. I have several folders with the filenames that needs to be dumped to a file, the individiaul files md5'ed and logged, most of the data zipped up, and possibly moved (1/3 the data gets moved). As you can see this is helping out alot.

    So in the end, I got a daily md5 list, file list (with sizes and last mod dates), that along with all of the individual elements gzipped up. And for come things like the logs, they get backed up to a different dir.

Similar Threads

  1. command in bash script to check if tar is currupt or not
    By expresstom in forum Linux - Software, Applications & Programming
    Replies: 2
    Last Post: 05-15-2011, 09:07 PM
  2. Help comparing contents of 2 files
    By trickster in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 10-25-2005, 09:53 PM
  3. bash command for 'ipconfig' equivalent?
    By Phaete in forum Linux - Hardware, Networking & Security
    Replies: 5
    Last Post: 05-26-2002, 05:58 AM
  4. BASH:  run command over long list, appnd 2 file
    By Blaqb0x in forum Linux - Software, Applications & Programming
    Replies: 1
    Last Post: 04-25-2002, 05:30 PM
  5. 2 questions with recursion
    By in forum Linux - Software, Applications & Programming
    Replies: 11
    Last Post: 04-15-2002, 03:00 AM

Bookmarks

Posting Permissions

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