From: Sven Mascheck on
srikanth wrote:

> I have a problem while executing multiple command with in a Find
> command.

> find Desktop/Test ... -exec cmd-1 \; -exec cmd-2 \;

By the way. How portable is calling several -exec?

V7 accepted that, and I also succeeded on SysIII, Ultrix, SunOS4,
OpenServer 5, IRIX 6, SunOS 5.6 ff, the free BSDs, UnixWare,
Minix 3, heirloom, sfind, AST and GNU.

However, a saved find executable from SunOS 5.1 executed on SunOS
5.9 ignored all but the first -exec. Otherwise it works flawlessly.
Different expressions, like -exec and -print (except -ok, which is
handled like -exec) are executed correctly. Thus I wonder if vanilla
SVR4 had a problem here. Currently I can only try SunOS 5.1.
From: srikanth on
On Apr 20, 6:12 pm, pk <p...(a)pk.invalid> wrote:
> srikanth wrote:
> >> > find Desktop/Programs -name test.sh -exec sh -c 'rm -rf "$1";touch
> >> > "$1"/test.sh' sh {} \;
> >> > The above command ran successfully with out any errors but file was
> >> > not created.
>
> >> You need to do at least
>
> >> -exec sh -c 'rm -rf "$1"; mkdir "$1"; touch "$1"/test.sh' sh {} \;
>
> >> but much better is
>
> >> sh -c 'rm -rf -- "$1" && mkdir -- "$1" && touch -- "$1"/test.sh' sh {} \;
>
> > I want to create a file under the same directory where I am doing
> > search.
>
> The above does (also) that. Did you try it?
> If you simply want to create a file, just do touch "$1"/yourfile. Why are
> you removing the directory?
>
> > Is there an alternative way to create a file.
>
> You should explain what is your real problem. The big picture.

I have tried the above commands but it was not creating file under
directory after removing. Did that worked for you? The above command
which I was mentioned in my first mail did the same, file was getting
removed but file created by touch is not showing under the dir.
From: pk on
srikanth wrote:

> I have tried the above commands but it was not creating file under
> directory after removing. Did that worked for you? The above command
> which I was mentioned in my first mail did the same, file was getting
> removed but file created by touch is not showing under the dir.

Yes it works for me.

# mkdir temp tomp
# find . -maxdepth 1 -mindepth 1 -type d -exec sh -c 'rm -rf -- "$1" &&
mkdir -- "$1" && touch -- "$1"/test.sh' sh {} \;
# ls temp
test.sh
# ls tomp
test.sh

I still maintain that you are either doing something wrong, or attempting to
solve your problem in a wrong way, or both. But without more information
it's impossible to tell.
From: srikanth on
On Apr 20, 6:45 pm, pk <p...(a)pk.invalid> wrote:
> srikanth wrote:
> > I have tried the above commands but it was not creating file under
> > directory after removing. Did that worked for you? The above command
> > which I was mentioned in my first mail did the same, file was getting
> > removed but file created by touch is not showing under the dir.
>
> Yes it works for me.
>
> # mkdir temp tomp
> # find . -maxdepth 1 -mindepth 1 -type d -exec sh -c 'rm -rf -- "$1" &&
> mkdir -- "$1" && touch -- "$1"/test.sh' sh {} \;
> # ls temp
> test.sh
> # ls tomp
> test.sh
>
> I still maintain that you are either doing something wrong, or attempting to
> solve your problem in a wrong way, or both. But without more information
> it's impossible to tell.

See I will tell you my problem...
I have a dir which contains lots of files. I want to search one file
and delete it. Then I need to create a file with same name.
So first I am trying to search for that file in my dir and removing.
This is the command I have tried and got success "find Desktop/Test -
name test -exec rm -rf {} \;"
Then I am trying to create a file with same name. So I have tried find
command to do both like
find Desktop/Test -name test -exec rm -rf {} \; -exec touch {}/test
\;

The thing is it was deleting the file from the specified dir but not
creating the file. I have told the same in the above as well. If it's
not clear please let me know so that I will try to give much more info.
From: pk on
srikanth wrote:

> See I will tell you my problem...
> I have a dir which contains lots of files. I want to search one file
> and delete it. Then I need to create a file with same name.
> So first I am trying to search for that file in my dir and removing.
> This is the command I have tried and got success "find Desktop/Test -
> name test -exec rm -rf {} \;"
> Then I am trying to create a file with same name. So I have tried find
> command to do both like
> find Desktop/Test -name test -exec rm -rf {} \; -exec touch {}/test
> \;
>
> The thing is it was deleting the file from the specified dir but not
> creating the file. I have told the same in the above as well. If it's
> not clear please let me know so that I will try to give much more info.

Your "find" command is quite dangerous written that way. If there is a
directory (anywhere) with the same name as the file you're looking for, it
will be deleted.

If you just want to remove and recreate that file, you can do

find Desktop/Test -type f -name test -exec sh -c '> "$1"' sh {} \;

that will truncate to 0 bytes /all/ files named "test" under the hierarchy
rooted at Desktop/Test. If you are looking for a specific file called "test"
(among many), you need to add more checks like for example grepping for the
content or others.

Again, that's surely not your outer problem, but good luck.