LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash scripting (https://www.linuxquestions.org/questions/programming-9/bash-scripting-235532/)

Gunslinger_ROL 09-26-2004 06:35 PM

Bash scripting
 
anyone know how I could do a find command like

find ~ -name 'iads' -print

and -exec a mv command

like i want to find every occurances of iads and move the files to another locations////sending standard errors to the trash

??
thanks

CroMagnon 09-26-2004 07:26 PM

Code:

find ~ -name 'iads' -exec mv \{\} location \;
Note the space between 'location' and the escaped semicolon!

CroMagnon 09-26-2004 07:28 PM

Oops, forgot to add "2>/dev/null" on the end to redirect stderr. Also, you can still use -print if you want to see the list of files as they're moved.

twantrd 09-28-2004 01:34 AM

find ~ -name 'iads' -exec mv \{\} location \;

CroMagnon,

1. If you run this on the command prompt you don't need to escape {} right? You need to escape {} ONLY if you run this as a script yes?

2. Also, what would happen if you don't escape \? Why is it that you need \; for an -exec to work?

Thanks!

-twantrd

CroMagnon 09-28-2004 02:45 AM

Actually, I escaped {} out of habit. If it works on the command line, it will very likely work in a script. Perhaps it works because the {} is empty (if you don't know what { and } do in the shell, try this: echo {f,c,r}at ). In fact, that must be it, because I've used it unescaped with xargs. Thanks, you've taught me something ;)

The important part is escaping the semicolon, because ; on it's own indicates to the shell that a command line is complete, and it is not passed to find. find requires a literal ; to tell it where the end of parameters for -exec are. It would also work if you put the semicolon in quotes, like ";".

twantrd 09-28-2004 11:37 AM

Ahh, thank you very much for clearing that up!

-twantrd


All times are GMT -5. The time now is 03:45 AM.