Unix Shell - Copying a bunch of images to one directory.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2007 > Copying a bunch of images to one directory.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Copying a bunch of images to one directory.
Dave Kelly

2007-11-24, 7:17 pm

In stead of reinventing the wheel, someone has to have needed to do
this in the past. Does this command line code exist somewhere I can
download/look at.

I have a directory with over a hundred directories of images in them.
I need to copy all the .png, .gif. JPEG, .jpg files to another
directory in order to burn a CD for a slide show at a Christmas
party.

Can anyone help

TIA
Dave
Chris F.A. Johnson

2007-11-24, 7:17 pm

On 2007-11-24, Dave Kelly wrote:
>
>
> In stead of reinventing the wheel, someone has to have needed to do
> this in the past. Does this command line code exist somewhere I can
> download/look at.
>
> I have a directory with over a hundred directories of images in them.
> I need to copy all the .png, .gif. JPEG, .jpg files to another
> directory in order to burn a CD for a slide show at a Christmas
> party.


If just copying them to the other directory is fine:

cp ./*.png ./*.gif ./*.JPEG ./*.jpg /path/to/other/directory

You may also want to look at mkisofs.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
William Park

2007-11-24, 7:17 pm

Dave Kelly <daveekelly1@embarqmail.com> wrote:
> In stead of reinventing the wheel, someone has to have needed to do
> this in the past. Does this command line code exist somewhere I can
> download/look at.
>
> I have a directory with over a hundred directories of images in them.
> I need to copy all the .png, .gif. JPEG, .jpg files to another
> directory in order to burn a CD for a slide show at a Christmas
> party.


find -name '*.jpg' -o -name '*.jpeg' | xargs cp --target-directory=/tmp/myporn
or
cp */*.{jpg,jpeg} /tmp/myporn
:-)

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
Dave Kelly

2007-11-24, 7:17 pm

Chris and William...........

I may have worded my problem badly.
[vbcol=seagreen]

Does you examples step down into each directory? Return to the main
directory and continue looking for the next directory?

Thanks for the feedback.
Dave


Janis Papanagnou

2007-11-24, 7:17 pm

Dave Kelly wrote:
> Chris and William...........
>
> I may have worded my problem badly.
>
>
>
>
> Does you examples step down into each directory? Return to the main
> directory and continue looking for the next directory?


William's 'find' example collects the files from all subdirectories;
wasn't that what you wanted? Did you try the suggestions?

Janis

>
> Thanks for the feedback.
> Dave
>
>

Dave Kelly

2007-11-24, 7:17 pm

On Nov 24, 4:40 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:

> William's 'find' example collects the files from all subdirectories;
> wasn't that what you wanted? Did you try the suggestions?
>
> Janis


T hat is exactly what I want. No I did not try the suggestion because
of my ignorance of how find works.
Will do so immediatly.

If things do not work as expected, I will come back for further
education. Otherwise assume everything is as it should be. ( As it is
in all cases where there in no operator error.)

Let me say thanks to everyone on this group for all the help they have
given me over the past 8 years. This old man appreciates it.

Dave

Dave Kelly

2007-11-26, 1:36 am

On Nov 24, 4:40 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> William's 'find' example collects the files from all subdirectories;
> wasn't that what you wanted? Did you try the suggestions?
>
> Janis
>

Tried this:
find -name '{*.jpg,*.png,*.JPG,*.gif}' -o -name
'{*.jpg,*.png,*.JPG,*.gif}' | xargs cp --target-directory=~/
xmasparty07
And got this error
cp: missing file argument

Tried this:
SilverNail:~/tffwebsite/httpdocs/images# find -name '*.jpg' -o -name
'*.jpg' | xargs cp --target-directory=~/xmasparty07
And got this error
cp: `~/xmasparty07': specified destination directory does not exist

Tried this:
find -name '*.jpg' --o name '*.jpg' | xargs cp --target-directory=/
root/xmasparty07
And got this error
cp: cannot stat `enhanced': No such file or directory
cp: cannot stat `1.jpg': No such file or directory
cp: cannot stat `./Belize2005/Norbert': No such file or directory
cp: cannot stat `Bonefish': No such file or directory
cp: cannot stat `at': No such file or directory

So I tried --o name "'*.jpg'" and -o name '"*.jpg"' and
still get the cannot stat errors.

Paul Colquhoun

2007-11-26, 1:37 am

On Sun, 25 Nov 2007 17:28:35 -0800 (PST), Dave Kelly <daveekelly1@embarqmail.com> wrote:
| On Nov 24, 4:40 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
| wrote:
|> William's 'find' example collects the files from all subdirectories;
|> wasn't that what you wanted? Did you try the suggestions?
|>
|> Janis
|>
| Tried this:
| find -name '{*.jpg,*.png,*.JPG,*.gif}' -o -name
| '{*.jpg,*.png,*.JPG,*.gif}' | xargs cp --target-directory=~/
| xmasparty07
| And got this error
| cp: missing file argument
|
| Tried this:
| SilverNail:~/tffwebsite/httpdocs/images# find -name '*.jpg' -o -name
| '*.jpg' | xargs cp --target-directory=~/xmasparty07
| And got this error
| cp: `~/xmasparty07': specified destination directory does not exist
|
| Tried this:
| find -name '*.jpg' --o name '*.jpg' | xargs cp --target-directory=/
| root/xmasparty07
| And got this error
| cp: cannot stat `enhanced': No such file or directory
| cp: cannot stat `1.jpg': No such file or directory
| cp: cannot stat `./Belize2005/Norbert': No such file or directory
| cp: cannot stat `Bonefish': No such file or directory
| cp: cannot stat `at': No such file or directory
|
| So I tried --o name "'*.jpg'" and -o name '"*.jpg"' and
| still get the cannot stat errors.


The stat errors look like the typical result of files with spaces in
their names. Find locates them correctly, but xargs breaks them up on
the space characters.

Assuming your versions support the options, try this:

find -name '*.jpg' -o -name '*.gif' -print0 | \
xargs -0 cp --target-directory=/root/xmasparty07

The '-print0' and '-0' contain a numeric 0, not the upper case letter O

This uses a NULL character (ASCII zero) as the word separator instead of
spaces.


--
Reverend Paul Colquhoun, ULC. http://andor.dropbear.id.au/~paulcol
Asking for technical help in newsgroups? Read this first:
http://catb.org/~esr/faqs/smart-questions.html#intro
Janis

2007-11-26, 7:36 am

On 26 Nov., 02:28, Dave Kelly <daveekel...@embarqmail.com> wrote:
> On Nov 24, 4:40 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:> William's 'find' example collects the files from all subdirectories;
>
>
> Tried this:
> find -name '{*.jpg,*.png,*.JPG,*.gif}' -o -name
> '{*.jpg,*.png,*.JPG,*.gif}' | xargs cp --target-directory=~/
> xmasparty07
> And got this error
> cp: missing file argument
>
> Tried this:
> SilverNail:~/tffwebsite/httpdocs/images# find -name '*.jpg' -o -name
> '*.jpg' | xargs cp --target-directory=~/xmasparty07
> And got this error
> cp: `~/xmasparty07': specified destination directory does not exist
>
> Tried this:
> find -name '*.jpg' --o name '*.jpg' | xargs cp --target-directory=/
> root/xmasparty07
> And got this error
> cp: cannot stat `enhanced': No such file or directory
> cp: cannot stat `1.jpg': No such file or directory
> cp: cannot stat `./Belize2005/Norbert': No such file or directory
> cp: cannot stat `Bonefish': No such file or directory
> cp: cannot stat `at': No such file or directory
>
> So I tried --o name "'*.jpg'" and -o name '"*.jpg"' and
> still get the cannot stat errors.


Does the man page of your find command specifies to write two slashes
with option -o? Option 'name' requires a slash -name. Quoting of the
find arguments is necessary to prevent the shell to expand the names
to a matching file in the current directory and thereby producing
errors or wrong results; use just one pair of quotes, either single or
double (the latter in case you want to have shell variables inside the
string expanded before find is called). Try

find . -name "*.jpg" -o -name "*.gif" -o -name "*.png" -print0 |
xargs -0 cp --target-directory=/root/xmasparty07

Add more filename (patterns) by using a -o ("or") and -name.
(The 0 is a zero digit in case your font obscures it.)

Janis
Othmar Wigger

2007-11-26, 7:36 am

The -exec option of find handles spaces in filenames correctly:

find <fromdir> -name \*.png -o -name \*.gif -o -name \*.JPEG -exec
cp {} <destdir> \;

And it also works with non-GNU versions of find and cp.
Rikishi 42

2007-11-26, 7:36 am

On 2007-11-26, Dave Kelly <daveekelly1@embarqmail.com> wrote:
> On Nov 24, 4:40 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
> Tried this:
> find -name '{*.jpg,*.png,*.JPG,*.gif}' -o -name
> '{*.jpg,*.png,*.JPG,*.gif}' | xargs cp --target-directory=~/
> xmasparty07
> And got this error
> cp: missing file argument
>
> Tried this:
> SilverNail:~/tffwebsite/httpdocs/images# find -name '*.jpg' -o -name
> '*.jpg' | xargs cp --target-directory=~/xmasparty07
> And got this error
> cp: `~/xmasparty07': specified destination directory does not exist
>
> Tried this:
> find -name '*.jpg' --o name '*.jpg' | xargs cp --target-directory=/
> root/xmasparty07
> And got this error
> cp: cannot stat `enhanced': No such file or directory
> cp: cannot stat `1.jpg': No such file or directory
> cp: cannot stat `./Belize2005/Norbert': No such file or directory
> cp: cannot stat `Bonefish': No such file or directory
> cp: cannot stat `at': No such file or directory
>
> So I tried --o name "'*.jpg'" and -o name '"*.jpg"' and
> still get the cannot stat errors.


Try this:
find source_dir/ -iname "*.jpg" -exec cp -v '{}' target_dir/ ';'
find source_dir/ -iname "*.png" -exec cp -v '{}' target_dir/ ';'

The iname option ignores case.
Remove the -v option from cp, if you want less display (and more speed).


--
There is an art, it says, or rather, a knack to flying.
The knack lies in learning how to throw yourself at the ground and miss.
Douglas Adams
Rikishi 42

2007-11-26, 1:24 pm

On 2007-11-26, Rikishi 42 <skunkworks@rikishi42.net> wrote:
> Try this:
> find source_dir/ -iname "*.jpg" -exec cp -v '{}' target_dir/ ';'
> find source_dir/ -iname "*.png" -exec cp -v '{}' target_dir/ ';'
>
> The iname option ignores case.
> Remove the -v option from cp, if you want less display (and more speed).


Some other post taught me the -o option of find, which I didn't know.

So, the following should do:
find source/ -iname "*.jpg" -o -iname "*.png" -exec cp -v '{}' target/ ';'

Have fun!

--
There is an art, it says, or rather, a knack to flying.
The knack lies in learning how to throw yourself at the ground and miss.
Douglas Adams
parv

2007-11-27, 7:33 am

in message <5e6fa$4748943c$d1520b22$4446@PRIMUS.CA>,
wrote William Park ...

> Dave Kelly <daveekelly1@embarqmail.com> wrote:
>
> find -name '*.jpg' -o -name '*.jpeg' | xargs cp --target-directory=/tmp/myporn


That find(1)s only the files with suffix of 'jpeg' here on FreeBSD
(6-STABLE). To find files named '*.jpg', need to collect the -name
options ...

find . \( -name '*.jpg' -o -name '*.jpeg' \) -print0 \
| xargs -0 -I % cp % dir


- parv

--
Email address is broken.
Geoff Clare

2007-11-27, 1:29 pm

parv wrote:

>
> That find(1)s only the files with suffix of 'jpeg' here on FreeBSD
> (6-STABLE).


Really? If true, that's a bug in FreeBSD's find.

With an explicit -print (or -print0) on the end, you would only get
the "*.jpeg" matches:

find . -name '*.jpg' -o -name '*.jpeg' -print # wrong!

but with no action specified, the implicit -print should apply to both.
POSIX says:

... if the given expression does not contain any of the primaries
-exec, -ok, or -print, the given expression shall be effectively
replaced by:

( given_expression ) -print

Of course, implementations that support the non-standard -print0 primary
also include it in the list of primaries that suppress the implicit -print.

--
Geoff Clare <netnews@gclare.org.uk>
parv

2007-11-28, 7:36 am

in message <eojt15-8v1.ln1@leafnode-msgid.gclare.org.uk>,
wrote Geoff Clare ...

> parv wrote:
>
>
> Really? If true, that's a bug in FreeBSD's find.
>
> With an explicit -print (or -print0) on the end, you would only get
> the "*.jpeg" matches:
>
> find . -name '*.jpg' -o -name '*.jpeg' -print # wrong!
>
> but with no action specified, the implicit -print should apply to both.


Sorry, I was immensly wrong. What you state above is how actually
things work on FreeBSD too. Thanks much for the correction.


- parv


> POSIX says:
>
> ... if the given expression does not contain any of the primaries
> -exec, -ok, or -print, the given expression shall be effectively
> replaced by:
>
> ( given_expression ) -print
>
> Of course, implementations that support the non-standard -print0 primary
> also include it in the list of primaries that suppress the implicit -print.
>


--

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com