You can use something like this:
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).Code:for i in * do gzip $i done
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!
You can use something like this:
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).Code:for i in * do gzip $i done
[quote author=pam link=board=9;threadid=6196;start=0#58698 date=1043656339]
You can use something like this:
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).Code:for i in * do gzip $i done
[/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 {} \;.
[quote author=pam link=board=9;threadid=6196;start=0#58698 date=1043656339]
You can use something like this:
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).Code:for i in * do gzip $i done
[/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![]()
[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 ...
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
[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.
[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.
Bookmarks