Find command in Shell Script

hi

I am a newbee in Shell scripting (hardly 7 days)
I have to execute a shell script which looks like this

#!/bin/sh
var1=`date +"%Y%m%d"`
echo $var1
find . -name "$var1*" -exec mv {} Delete/ \;

the find command in the script is running independently but when kept in this script it is giving following error

20101123
find: 0652-018 An expression term lacks a required parameter.

Please help

Sweet

[[ -z "${var1}" ]] && echo "var1 is empty, script abort" && exit 1
find . -name "${var1}*" -exec mv {} ./Delete/ \;

Hi

Thanks for the quick reply, your code has not thrown an error but has not moved any files in the /Delete folder either.

It has created a file {} of 0kb

I need to move all files to the folder Delete/ where the name contains todays timestamp

Thanks
Sweet

??
Are you sure they were some file to move ?
Did you check you are granted to manipulate those files ?
make sure you are granted to write in the target directory

if the following command list the file you are looking for

find . -name "${var1}*" 

then give a try to

find . -name "${var1}*" | xargs -i cp {} ./Delete/

the -i option may depends on your OS

( cp is more secure for testing, if ok, then replace it with the mv command)

Hi

Again this statement is not working inside the script but working in the command prompt

please help

Sweet

---------- Post updated at 02:29 PM ---------- Previous update was at 01:19 PM ----------

Hi
Can anyone tell me if there is a difference in the way a "find" statement is executed inside a shell script and normally on the command prompt

Sweet

the dot in find . means "the current dir you are in" so if your script does some cd <somewhere_else> , then your find command may be launched in a directory that does not contain any files you are looking for.

Please show us the full code of your script, and your operating system version (use command uname -a )

Please provide the full PATH of the directory you want to search in.
Please provide an example of absolute filename you have and want to be found.

find . -name "*${var1}*" | xargs -i cp {} ./Delete/

If you have error message when running your script, please provide them

Hi

The current script looks like this

#!/bin/sh
var1=`date +"%Y%m%d"`
echo $var1
find . -name "${var1}*" -exec mv {} ./Delete/ \;

OS: AIX mfkrv259 1 6

Path I want to search in and where the script is currently residing: /var/prodtmse/RIGS

filename looks like 20101124-c9ha430.xml

The code you have provided is throwing following error

 find . -name "*${var1}*" -i cp {} ./Delete/ \; 

find: 0652-086 Specify a decimal integer for -i

 Usage: find [-H | -L] Path-list [Expression-list]

Yes, as i said, the -i option may depends on your OS : you are on AIX, it seems you do
not have the same option than me for the xargs command.

so you need to do man xargs and see which option is suitable

(maybe it is in uppercape : -I ?)

That's not the code that ctsgnb posted! The -i is an option to xargs - which mysteriously disappeared - not to find.

find . -name "*${var1}*" | xargs -I cp {} ./Delete/

I have tried the above code inependently on the shell and it is throwing following error:

xargs: {}: A file or directory in the path name does not exist.

Hi.

-I requires a replacement string ({} by convention), and the replacement string must be used in the command:

find . -name "*${var1}*" | xargs -I{} cp {} ./Delete/

hi

I have made the correction and this command works fine
But when i keep it inside my shell script it is throwing following error

-----

20101124
new.sh[5]: ^M:  not found.
new.sh[6]: ^M:  not found.
new.sh[7]: ^M:  not found.
new.sh[8]: ^M:  not found.

-----

the script is

#!/bin/sh
var1=`date +"%Y%m%d"`
echo $var1
find . -name "*${var1}*" | xargs -I{} cp {} ./Delete/

Do this :

dos2unix new.sh new.sh

and give another try to run the script

Getting error:

dos2unix: not found.

dos2unix isn't on AIX (at least, as I can see by default).

You can maybe use tr to remove the ^M.

cp new.sh new.sh.1 && tr -d '\r' < new.sh.1 > new.sh && rm new.sh.1

Did you write your script directly in a unix editor, or did you wrote it into a Windows editor and then did a copy/paste ?
It looks as if you would have ^M stuff to remove from your file (such as it happens when things come from Windows.

---------- Post updated at 08:20 PM ---------- Previous update was at 08:10 PM ----------

Hi Scott

Yep i just noticed that :wink:
unfortunately i don't have an AIX machine around (linux Solaris and FreeBSD only) :o

But i found an AIX man xargs
I feel angry at myself not to be able to help ppl running a simple

find ...| xargs ...

but i still go on, i am sure we will get it working ! :D:D:D

No need to feel that way - you do a fine job here. And most people who don't work with AIX won't have access to it.

But there's nothing special about xargs on AIX. Perhaps sweetnsourabh should have read the man page for -I instead of using it blindly :slight_smile:

Thx ! :slight_smile:

Yeah! true... it's just that when sweetnsourabh stated it wasn't working , it made me doubts :stuck_out_tongue:

Hi Scot and ctsgnb

Thanks for the guidance at my early stage of shell learning.

As I mentioned erlier after using tr the ^M error is gone but the find command in the script is still not executing inside the script but independently it is working...

Can you let me know the reason and solution to this

Hi.

What do you mean by "is not executing"?

I would start by replacing cp with echo in the xargs command for debug.

What output is there? Is it running in a cronjob, or from the command line? What shell are you using? If possible post the entire script - or at least everything up to and including the find command.