LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-08-2016, 02:34 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
how to use find with exec to copy files into another location?


I got this far
Code:
[userx@Voided NTFS750]$ find /media/userxtra/userx/ -type f -iname "MyFIle*.mp4" -exec cp -v {}
but I am not sure how to write the source destination and leave it the same name

normally it is just
Code:
cp -v /home/user/someDirectory/"H*.mp4" /destination/
[userx@Voided NTFS750]$ find /media/userxtra/userx/ -type f -iname "MyFIle*.mp4" -exec cp -v {}
find: missing argument to `-exec'
[/code]

Code:
[userx@Voided NTFS750]$ find /media/userxtra/userx/ -type f -iname "MyFIle*.mp4" -exec cp -v {} \
>
Code:
[userx@Voided NTFS750]$ find /media/userxtra/userx/ -type f -iname "MyFIle*.mp4" -exec cp -v {} /run/media/userx/NTFS750/+
find: missing argument to `-exec'
 
Old 10-08-2016, 03:43 PM   #2
timl
Member
 
Registered: Jan 2009
Location: Sydney, Australia
Distribution: Fedora,CentOS
Posts: 750

Rep: Reputation: 156Reputation: 156
Have you tried:

Quote:
find /media/userxtra/userx/ -type f -iname "MyFIle*.mp4" -exec cp -v {} /run/media/userx/NTFS750/ \;
I tried something similar and it worked on my system

Quote:
[tim@comedian ~]$ find /mnt/doco/Docs/Resume -maxdepth 1 -type f -mtime -1 -exec cp {} /mnt/doco/Docs/Resume/oldy/
find: missing argument to `-exec'
[tim@comedian ~]$ find /mnt/doco/Docs/Resume -maxdepth 1 -type f -mtime -1 -exec cp {} /mnt/doco/Docs/Resume/oldy/ \
> ^C
[tim@comedian ~]$ find /mnt/doco/Docs/Resume -maxdepth 1 -type f -mtime -1 -exec cp {} /mnt/doco/Docs/Resume/oldy/ \;
[tim@comedian ~]$
HTH
 
Old 10-08-2016, 03:48 PM   #3
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
When you use exec, you must end with either \; or \+. This is the missing argument find was complaining about above.

\; will run the specified command for each result of find.
\+ ("append") will run the specified command once, placing all results into one command line argument, which can run into command line length restrictions. If you think you may be dealing with long file names, use the former.

To demonstrate the above,

Code:
anthony@miranda:~/tmp$ cat > showme.sh << exit
#!/usr/bin/env bash

echo "arguments: x   \${@}   x"
exit
anthony@miranda:~/tmp$ touch {0..9}.file
anthony@miranda:~/tmp$ find -type f -print
./1.file
./6.file
./5.file
./4.file
./7.file
./8.file
./3.file
./9.file
./2.file
./0.file
anthony@miranda:~/tmp$ find -type f -exec ./showme.sh "{}" \;
arguments: x   ./1.file   x
arguments: x   ./6.file   x
arguments: x   ./5.file   x
arguments: x   ./4.file   x
arguments: x   ./7.file   x
arguments: x   ./8.file   x
arguments: x   ./3.file   x
arguments: x   ./9.file   x
arguments: x   ./2.file   x
arguments: x   ./0.file   x
anthony@miranda:~/tmp$ find -type f -exec ./showme.sh "{}" \+
arguments: x   ./1.file ./6.file ./5.file ./4.file ./7.file ./8.file ./3.file ./9.file ./2.file ./0.file   x
So, your command should be:

Code:
find /media/userxtra/userx/ -type f -iname "MyFIle*.mp4" -exec cp -v {} \;

Last edited by goumba; 10-08-2016 at 04:11 PM. Reason: Added, later improved, example
 
Old 10-08-2016, 03:56 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by timl View Post
Have you tried:



I tried something similar and it worked on my system



HTH
I 'll try that i think i did, but too I was already cd into the destination dive in term
plus the word similar indicates what? just different file types or command?
 
Old 10-08-2016, 03:57 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by goumba View Post
When you use exec, you must end with either \; or \+. This is the missing argument find was complaining about above.

\; will run the specified command for each result of find.
\+ will run the specified command once, which can run into command line length restrictions. If you think you may be dealing with long file names, use the former.

To demonstrate the above,

Code:
anthony@miranda:~/tmp$ touch {0..9}.file
anthony@miranda:~/tmp$ find -type f -print
./1.file
./6.file
./5.file
./4.file
./7.file
./8.file
./3.file
./9.file
./2.file
./0.file
anthony@miranda:~/tmp$ find -type f -exec echo "{}" \;
./1.file
./6.file
./5.file
./4.file
./7.file
./8.file
./3.file
./9.file
./2.file
./0.file
anthony@miranda:~/tmp$ find -type f -exec echo "{}" \+
./1.file ./6.file ./5.file ./4.file ./7.file ./8.file ./3.file ./9.file ./2.file ./0.file
So, your command should be:

Code:
find /media/userxtra/userx/ -type f -iname "MyFIle*.mp4" -exec cp -v {} \;
the escape and end semi-colon i'll try that too.
 
Old 10-08-2016, 04:00 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
@timl @goumba thanks it was the
Code:
 \;
that was missing for the most part, minus actual destination if I wasn't already there in term .. thanks

nope I just ran it again without the destination and it complained had to put it back in even though I was already there.

Last edited by BW-userx; 10-08-2016 at 04:04 PM.
 
Old 10-08-2016, 04:11 PM   #7
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Quote:
Originally Posted by BW-userx View Post
@timl @goumba thanks it was the
Code:
 \;
that was missing for the most part, minus actual destination if I wasn't already there in term .. thanks

nope I just ran it again without the destination and it complained had to put it back in even though I was already there.
cp always wants the destination, this isn't MS-DOS/Windows. If you're already in the directory, you use . (a single dot).
 
Old 10-08-2016, 04:22 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by goumba View Post
cp always wants the destination, this isn't MS-DOS/Windows. If you're already in the directory, you use . (a single dot).
I must be getting altimers but I can't remember
 
Old 10-08-2016, 07:33 PM   #9
LinuxIsCool2use
LQ Newbie
 
Registered: Oct 2016
Posts: 4

Rep: Reputation: Disabled
@ OP

I use this '{}' '+' when using find. The '{}' can handle filenames with spaces and/or other odd characters in the filenames.


Code:
find  <source-path> -type f -iname <pattern to match> cp -v -t <destination-path> '{}' '+'
-t target directory and -v is verbose

Also, this works as well

Code:
find  <source-path> -type f -iname <pattern to match> -print0 | xargs -0 cp -v -t <destination-path>
The -print0 will handle spaces and odd characters in filenames.

Last edited by LinuxIsCool2use; 10-08-2016 at 07:35 PM.
 
  


Reply

Tags
exec, find command



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find files with pattern, then copy those files to another directory ncsuapex Programming 4 08-13-2010 03:38 PM
Help with `find -exec` : Remove selected files from a backup folder bobkatz Linux - Server 12 01-29-2010 10:29 AM
How to find files and copy the found files to the floppy in one command justmehere Linux - Newbie 11 05-04-2008 11:29 PM
find the exact location of a running exec file cahdecah General 4 08-16-2007 10:53 AM
Setup is unable to find a hard disk location to store temporary files. Klas Linux - Software 2 02-14-2006 06:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration