UNIX help?
Feb. 13th, 2006 01:42 pmfinally, a work-related question:
Is there an automated way to search subdirectories for .rtf and .pdf files, and gzip them if they are found? Most of the rtf files will be in subdirectories called "rtf", and the pdf files will be in subdirectories called "pdf".
(I've done too much of this the good old fashioned way, and it's making me stupid.)
Is there an automated way to search subdirectories for .rtf and .pdf files, and gzip them if they are found? Most of the rtf files will be in subdirectories called "rtf", and the pdf files will be in subdirectories called "pdf".
(I've done too much of this the good old fashioned way, and it's making me stupid.)
no subject
Date: 2006-02-13 06:46 pm (UTC)no subject
Date: 2006-02-13 06:51 pm (UTC)find . -name '*.ps' -exec gzip {} \;
no subject
Date: 2006-02-13 06:53 pm (UTC)no subject
Date: 2006-02-13 06:59 pm (UTC)find walks through the subidrectories and every time it finds a file with a matching name, it calls gzip replacing the curly braces with the filename.
no subject
Date: 2006-02-13 07:02 pm (UTC)no subject
Date: 2006-02-13 07:17 pm (UTC)And you absolutely have to escape that semicolon at the end, or the -exec section will refuse to believe that it has been terminated.
no subject
Date: 2006-02-13 07:33 pm (UTC)find / -name "*.pdf" -print 2> /dev/null -ok gzip -f "{}" \;
"2> /dev/null" nukes the error output which I find makes it easier to read when not running as root.
"-ok" instead of "-exec" queries you y/n on each file so that you can be sure it's doing the right thing. (obviously, if it's for a lot of files, once it's doing the right thing, ^c out and run it with -exec)
And yeah, what fest_sylvain said about terminating it properly, although if you double-quote the search expression I don't think you need to worry about it there.
no subject
Date: 2006-02-13 07:50 pm (UTC)If there are no files which match that wild-carded expression, then it will be passed along literally.
FWIW
Date: 2006-02-13 08:03 pm (UTC)I have to work on Windoze boxes more than I want to, and there's a free set of tools to be found at cygwin.org that sets up a bash type shell on windows. I use it for everything.
Anyhow, if you want to work at this in a home environment, having a shell on a windows box could seriously help you get practiced.
k.
... oh and I'd help with the script, but...
Date: 2006-02-13 08:03 pm (UTC)no subject
Date: 2006-02-13 08:21 pm (UTC)find /whatever/rtf -name "*.rtf" -print 2> /dev/null -ok gzip -f "{}" \;
and since it queries y/n for each file, any bad hits can be dealt with on a case by case basis.
(I'm not sure what would match "*.pdf" that could be a problem. Now, "*.pdf*", THAT could be a problem.)
Anyway, sure, the \ wouldn't hurt.
no subject
Date: 2006-02-13 08:43 pm (UTC)(find . -name \*.rtf ; find . -name \*.pdf) | tar cvfzT output.tgz -
Terse explanation of above:
The first two commands, in the parens, get executed in series, each one finding one type of files; putting in in parens gets them both executed by the same shell, thus the output ends up all together on stdout; tars "-T" option tells it to read a list of files to tar up from stdin.
no subject
Date: 2006-02-13 09:19 pm (UTC)no subject
Date: 2006-02-13 09:36 pm (UTC)no subject
Date: 2006-02-13 09:45 pm (UTC)no subject
Date: 2006-02-13 10:29 pm (UTC)