Piping results of a 'find' command to a 'mv' command...

Piping results of a 'find' command to a 'mv' command...

Post by Shawn Mi » Fri, 21 Mar 2003 22:16:21



Is there a way to do the following without scripting:

Pipe the contents of a 'find' command to a 'mv' command, so that the
results of 'find', example:

find / -iname '*simpsons*' 2>>/dev/null

/home/shawn/downloads/simpsons1.mpg
/home/shawn/simpsons/Halloween Special IX.mpg
/home/shawn/unsorted/simpsons02.mpg

are all moved to a folder of my choosing:

Something like:
find / -iname '*simpsons*' 2>>/dev/null | mv /home/shawn/tv/simpsons

Can anyone help?  If scripting is required, I don't mind, but I
thought that if there was an easy way, I'd like to use it.

If scripting is required, is there an easy way to do it just by
creating a script containing a list of commands and running it after
chmod +x?  Or would I have to do something with Perl in order to loop
through the results of the 'find'?

I know a little Perl, but if there is some other type of scripting
preferred for this type of thing, please tell me the name of it and
where I might find a little documentation.

Thank you for any help you can give.  And please, even if the answer
can be derived from a particular command, do not simply type 'man
commandName'.  A little description/advice would be very nice.

Shawn

 
 
 

Piping results of a 'find' command to a 'mv' command...

Post by Rob Roger » Sat, 22 Mar 2003 00:04:11



> Is there a way to do the following without scripting:

> Pipe the contents of a 'find' command to a 'mv' command, so that the
> results of 'find', example:

> find / -iname '*simpsons*' 2>>/dev/null

> /home/shawn/downloads/simpsons1.mpg
> /home/shawn/simpsons/Halloween Special IX.mpg
> /home/shawn/unsorted/simpsons02.mpg

> are all moved to a folder of my choosing:

> Something like:
> find / -iname '*simpsons*' 2>>/dev/null | mv /home/shawn/tv/simpsons

The command you're looking for is 'xargs'. xargs runs a command once for
each line that comes in on standard in. The only problem is that the STDIN
arg is always appended to the end, and mv usually lists the source before
the destination, so you have to use a slightly different option with mv.
From `man mv` we get "mv [OPTION]... --target-directory=DIRECTORY SOURCE"
which lets us put the directory first and the source (which we'll get from
xargs) last.

find / -iname '*simpsons*' 2>>/dev/null | xargs mv \
 --target-directory=/home/shawn/tv/simpsons

That command should be what you're looking for

Rob Rogers

 
 
 

Piping results of a 'find' command to a 'mv' command...

Post by Luis Angel Fdez. Fdez » Sat, 22 Mar 2003 03:17:44


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

        A las 13:16 del jueves 20 de marzo,

        hablando de Piping results of a 'find' command to a 'mv' command...
        en comp.os.linux.help dijo...

Quote:> Is there a way to do the following without scripting:

[snip]

> Something like:
> find / -iname '*simpsons*' 2>>/dev/null | mv /home/shawn/tv/simpsons

  Maybe...

  find / -iname '*simpson*' -exec mv {} /home/shawn/tv/simpsons \;

  The space and \; before /home/shawn... are required.

Quote:> Thank you for any help you can give.  And please, even if the answer
> can be derived from a particular command, do not simply type 'man
> commandName'.  A little description/advice would be very nice.

  man find ;)

  That's all.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE+egXIsrzbHhG/hp8RAoBdAKCbvAzaM8AENGzUt3ESDUeS0UxIJgCdHO0d
CPiy3k+ihQUoTZb5O2KnbXw=
=fcej
-----END PGP SIGNATURE-----

--
Slackware 8.1 AMD Athlon(tm) Processor 598.854MHz
Kernel 2.4.19 Linux User #99754
up 52 days, 14:55, 8 users, load average: 0.20, 0.16, 0.21
http://muxin.yi.org/~koxo/ http://muxin.no-ip.org/~koxo/

 
 
 

Piping results of a 'find' command to a 'mv' command...

Post by Shawn Mi » Tue, 25 Mar 2003 00:36:47


Thanks, it works.  


  Maybe...

  find / -iname '*simpson*' -exec mv {} /home/shawn/tv/simpsons \;

  The space and \; before /home/shawn... are required.

This works great.

The suggestion from Rob Rogers

find / -iname '*simpsons*' 2>>/dev/null | xargs mv \
 --target-directory=/home/shawn/tv/simpsons

This does not work.  The problem is that when I run it, it breaks the
returned values into many pieces.  Example, when I tried to move any
file with 'horse' in the name:

Here are some existing files:
/home/shawn/storage/comedy/schimmel/Comedy - Robert Schimmel -
Introduction Horse
Farts.mp3

/home/shawn/storage/music/America/History Greatest Hits/01_A Horse
With No Name.mp3

/home/shawn/storage/music/Dave Matthews Band/Dave Mathews Band &
Rolling Stones -
Wild Horses(live).mp3

/home/shawn/storage/music/MP3 (Unsorted)/The Sundays - Wild Horses
[FEAR SOUNDTRACK].mp3

/home/shawn/the sundays - wild horses (1).mp3

Results:

mv: cannot stat `Greatest': No such file or directory
mv: cannot stat `Hits/01_A': No such file or directory
mv: cannot stat `Horse': No such file or directory
mv: cannot stat `With': No such file or directory
mv: cannot stat `No': No such file or directory
mv: cannot stat `Name.mp3': No such file or directory
mv: cannot stat `/home/shawn/storage/music/Dave': No such file or
directory
mv: cannot stat `Matthews': No such file or directory
mv: cannot stat `Band/Dave': No such file or directory
mv: cannot stat `Mathews': No such file or directory
mv: cannot stat `Band': No such file or directory
mv: cannot stat `&': No such file or directory
mv: cannot stat `Rolling': No such file or directory
mv: cannot stat `Stones': No such file or directory
mv: cannot stat `-': No such file or directory
mv: cannot stat `Wild': No such file or directory
mv: cannot stat `Horses(live).mp3': No such file or directory
mv: cannot stat `/home/shawn/storage/music/MP3': No such file or
directory
mv: cannot stat `(Unsorted)/The': No such file or directory
mv: cannot stat `Sundays': No such file or directory
mv: cannot stat `-': No such file or directory
mv: cannot stat `Wild': No such file or directory
mv: cannot stat `Horses': No such file or directory
mv: cannot stat `[FEAR': No such file or directory
mv: cannot stat `SOUNDTRACK].mp3': No such file or directory
mv: cannot stat `/home/shawn/the': No such file or directory
mv: cannot stat `sundays': No such file or directory
mv: cannot stat `-': No such file or directory
mv: cannot stat `wild': No such file or directory
mv: cannot stat `horses': No such file or directory
mv: cannot stat `(1).mp3': No such file or directory
mv: cannot stat `/home/shawn/the': No such file or directory
mv: cannot stat `sundays': No such file or directory

Any one have any suggestions as to why this is happening?  Not too
important, since Luis Fdez. helped me out, but I thought that maybe it
would be a nice intellectual challenge for one of you shell hackers
out there.

Thanks again to the group, for once again coming through.

Shawn

 
 
 

Piping results of a 'find' command to a 'mv' command...

Post by Stu » Tue, 25 Mar 2003 08:54:55


The second one doesn't work because you have whitespace in the filenames, so
you need to add -print0 to the find and --null to the xargs:

find / -iname '*simpsons*' 2> /dev/null | xargs --null mv \
  --target-directory=/home/shawn/tv/simpsons

There are a few other ways to achieve the same result, like this for
instance:

find / -iname '*simpsons*' -printf " \"%p\" " 2> /dev/null | xargs mv \
  --target-directory=/home/shawn/tv/simpsons

That being said, I prefer the find ... -exec ... \; way myself.

Stu


> Thanks, it works.


>   Maybe...

>   find / -iname '*simpson*' -exec mv {} /home/shawn/tv/simpsons \;

>   The space and \; before /home/shawn... are required.

> This works great.

> The suggestion from Rob Rogers

> find / -iname '*simpsons*' 2>>/dev/null | xargs mv \
>  --target-directory=/home/shawn/tv/simpsons

> This does not work.  The problem is that when I run it, it breaks the
> returned values into many pieces.  Example, when I tried to move any
> file with 'horse' in the name:

<snip>
 
 
 

1. usage of 'foreach' and 'find' commands

I have files with blanks in their name.
for example: a/b\ c\ d
if I do

find a
I get
a
a/b c d

and if I do

foreach x (`find a`)
ls $x
end

I get:
b c d
a/b: No such file or directory
c: No such file or directory
d: No such file or directory

How can I make the foreach/find pair work?

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

2. Linux+Win95+Laptop on Fat32?

3. How to hide from 'who', 'finger', 'ps' commands?

4. sound card and printer

5. Help with 'user', 'w', 'who' commands

6. calling all qmail experts

7. '-i' option for 'cp' and 'mv'

8. test

9. Is there any way to UNDO the 'mv' command?

10. Need to find info on the 'FIND' command

11. Usind 'find' command to find certain text within a file

12. weird result in the 'w' command on solaris 2.6

13. Confused by 'free' command results.