LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-27-2016, 06:24 PM   #1
timl
Member
 
Registered: Jan 2009
Location: Sydney, Australia
Distribution: Fedora,CentOS
Posts: 737

Rep: Reputation: 154Reputation: 154
question on find -exec


Hi, I want to archive files I update on a sporadic basis but I am struggling with "find -exec"

Quote:
[tim@riverside ~]$ find /mnt/doco/Docs/Resume -mtime -1 -exec cp {} /mnt/doco/Docs/Resume/goldy/ | rm {} \;
find: missing argument to `-exec'
rm: cannot remove '{}': No such file or directory
rm: cannot remove ';': No such file or directory
I also tried

Quote:
[tim@riverside ~]$ find /mnt/doco/Docs/Resume -mtime -1 -exec cp {} /mnt/doco/Docs/Resume/goldy/ | -exec rm {} \;
find: missing argument to `-exec'
bash: -exec: command not found...
However this works
Quote:
[tim@riverside ~]$ find /mnt/doco/Docs/Resume -mtime -1 -exec cp {} /mnt/doco/Docs/Resume/goldy/ \;
Is it correct to include "-exec" two times from the same "find" or should I issue 2 commands? I could probably make life easy and just use "mv" but it would be good to develop a few bash skils.

Cheers
 
Old 04-27-2016, 06:52 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,004

Rep: Reputation: 804Reputation: 804Reputation: 804Reputation: 804Reputation: 804Reputation: 804Reputation: 804
I dont think you can use the pipe to the exec command, so '|-exec' ... wont work as the pipe gets the output of the find command and tries to pipe it to '-exec' which is not a valid command, the -exec arg to find should really only be used with simple commandsvif you need somthing more complex,,mwith pipes to other commands etc I would suggest a wrapper script and -exec that.
 
Old 04-27-2016, 06:54 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,626

Rep: Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134
Please do not use [QUOTE] tags except to quote from other messages. It makes it hard to quote you in a reply. Use [CODE] ... [/CODE] tags instead.
Quote:
Originally Posted by timl View Post
Code:
find /mnt/doco/Docs/Resume -mtime -1 -exec cp {} /mnt/doco/Docs/Resume/goldy/ | rm {} \;
What do you think that pipe symbol is doing? It causes the shell to split that line into two separate commands, with the find command ending (incompletely) before the pipe symbol and the rest of the line being treated as a separate command removing a nonexistent file named "{}". If you want find to execute two external commands, both constructs have to be complete and end with "\;"
Code:
find /mnt/doco/Docs/Resume -mtime -1 -exec cp {} /mnt/doco/Docs/Resume/goldy/ \; -exec rm {} \;
The rm command will be executed only if the cp succeeds. That could also be done by telling find itself to delete the file:
Code:
find /mnt/doco/Docs/Resume -mtime -1 -exec cp {} /mnt/doco/Docs/Resume/goldy/ \; -delete
(Either one is a near-equivalent to a mv command.)

If you really meant that "|" to be a pipe, why would you be piping the stdout of cp (which does not write anything to its stdout) to the stdin of rm (which does not read from its stdin)?

Last edited by rknichols; 04-27-2016 at 07:02 PM. Reason: Add, "If you really meant ..."
 
1 members found this post helpful.
Old 04-27-2016, 07:24 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,757
Blog Entries: 4

Rep: Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520
Ordinarily, I let find produce a list of filenames, which I then pipe into the xargs command . . .
 
Old 04-27-2016, 08:25 PM   #5
timl
Member
 
Registered: Jan 2009
Location: Sydney, Australia
Distribution: Fedora,CentOS
Posts: 737

Original Poster
Rep: Reputation: 154Reputation: 154
Thanks all, I decided to RTFM now I understand the command a bit more and it seems "-depth" is a useful switch also (in liaison with -delete").

"xargs"!! There is something else to research. Not really sure how that works but now may be the time to try. Thanks for the heads up

Cheers
 
Old 04-27-2016, 08:31 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,626

Rep: Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134
Quote:
Originally Posted by timl View Post
Thanks all, I decided to RTFM now I understand the command a bit more and it seems "-depth" is a useful switch also (in liaison with -delete").
The authors of find thought that combination was important too, so much so that "-delete" implies "-depth".
 
Old 04-28-2016, 06:52 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,757
Blog Entries: 4

Rep: Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520Reputation: 3520
xargs stands for "execute with arguments."

This command reads lines from STDIN, assembles these into commands (as specified on the xargs command-line), and spawns shells to execute them. Some versions can execute multiple shells in parallel.

grep -rilw foobar /here | xargs -I{} mv {} /there

This example first uses the "grep" command to locate all files in "/here" which contain the word "foobar." It pipes that output to xargs, to execute a "mv" command to move the files "/there." "{}" is specified (using the "-I" option ...) as a placeholder where the name (read from STDIN) is inserted into the command.

So, if it reads the string "tomcat," it will execute mv tomcat /there ... and so on.

Last edited by sundialsvcs; 04-28-2016 at 06:53 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Script question with find -exec linuxbird Linux - Newbie 12 05-15-2011 03:21 AM
Find with -exec argument - not giving proper output..how to find... hinetvenkat Linux - Server 4 01-25-2010 06:19 AM
Shell Scripting Question (mv via find/-exec) ddenton Linux - General 3 11-21-2008 08:10 AM
find -exec syntax: bug in find? mfcarroll Programming 5 06-21-2007 07:13 PM
find -exec question eantoranz Linux - General 1 06-26-2004 10:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:39 AM.

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