Find command in Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find command in Shell Script
# 1  
Old 11-23-2010
Java 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
Code:
#!/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
Code:
20101123
find: 0652-018 An expression term lacks a required parameter.

Please help

Sweet

Last edited by Franklin52; 11-23-2010 at 12:56 PM.. Reason: Please use code tags
# 2  
Old 11-23-2010
Code:
[[ -z "${var1}" ]] && echo "var1 is empty, script abort" && exit 1
find . -name "${var1}*" -exec mv {} ./Delete/ \;


Last edited by ctsgnb; 11-23-2010 at 01:51 PM..
# 3  
Old 11-23-2010
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
# 4  
Old 11-23-2010
??
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
Code:
find . -name "${var1}*"

then give a try to
Code:
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)

Last edited by ctsgnb; 11-23-2010 at 01:56 PM..
# 5  
Old 11-23-2010
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
# 6  
Old 11-24-2010
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.
Code:
find . -name "*${var1}*" | xargs -i cp {} ./Delete/

If you have error message when running your script, please provide them
# 7  
Old 11-24-2010
Hi

The current script looks like this
Code:
#!/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
Code:
 find . -name "*${var1}*" -i cp {} ./Delete/ \;

find: 0652-086 Specify a decimal integer for -i
Code:
 Usage: find [-H | -L] Path-list [Expression-list]


Last edited by Scott; 11-24-2010 at 06:30 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Find command doesn't return in shell script.

Hi All, I am using below snippet to search for a string (read from a file 'searchstring.out') in all locations (/) and then iterate over the files found to write the locations and the respective owner to an output file. However, this doesn't work as I believe the find command doesn't exit's... (11 Replies)
Discussion started by: Vipin Batra
11 Replies

2. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

3. Shell Programming and Scripting

find command in shell script

Hi all, Please i need an explanation for the following statements ref_file=/tmp/cleanfiles export ref_file touch `TZ=WAT+2 date "+%Y%m%d%H%M"` $ref_file find . ! -name . -prune -type f ! -newer $ref_file -exec store_file.sh {} \; (1 Reply)
Discussion started by: anish_1982
1 Replies

4. UNIX for Dummies Questions & Answers

find command in shell script doesn't work

Hello all, Something strange going on with a shell script I'm writing. It's trying to write a list of files that it finds in a given directory to another file. But I also have a skip list so matching files that are in that skip list should be, well uhm, skipped :) Here's the code of my... (2 Replies)
Discussion started by: StijnV
2 Replies

5. Shell Programming and Scripting

find command in shell script

Hi, dirs.conf fine contains below data /a/b/c/dir1|50 /a/b/c/dir2|50 /a/b/c/dir3|50 In a shell script I do as below while read file_rec do dir_name=`echo "${file_rec}" | cut -d "|" -f 1` purge_days=`echo "${file_rec}" | cut -d "|" -f 2` if then... (3 Replies)
Discussion started by: icefish
3 Replies

6. Shell Programming and Scripting

Find command in Korn Shell

Hi, I am trying to execute the below in Ksh (telnet) find ./request.txt -mmin -30 It says find: bad option -mmin What i am trying to do is by using find command i am checking wheather the file request.txt is there for 30 minutes or not Please help (1 Reply)
Discussion started by: chinniforu2003
1 Replies

7. Shell Programming and Scripting

script to find whether shell command is available

I am running shell scripts on windows using Cygwin tool. In my shell scripts, i want to add an error check, that verify whether a certain command is available or not. For example if SED comamnd is not available in Cygwin, then it should exit with error message. How do i write such shell... (2 Replies)
Discussion started by: mmunir
2 Replies

8. UNIX for Dummies Questions & Answers

Shell script 'find' command

I have a script that has the following command: find /home/user -name test.dat The script works as desired when running normally. However, when I run the script preceding it with 'sh', it fails. Is there something I need to account for when preceding the execution of the script with 'sh'? (1 Reply)
Discussion started by: bsavitch
1 Replies

9. UNIX for Advanced & Expert Users

find command from a shell script

Hi experts, I have a shell script (korn shell on aix) where I am giving find command with file options which are read from a configuration file. For some reason I am getting an error find: 0652-017. I have put set -x in the shell script and the command looks okay. If I cut it and paste it in the... (6 Replies)
Discussion started by: kodermanna
6 Replies

10. UNIX for Advanced & Expert Users

Problem with find command in C-shell

when i use the following command find / -name '*.*' -exec grep -il 'text' {} \; I can redirect the errors to /dev/null. This happens only in ksh but not in csh. the 2>/dev/null is not working in csh. Can you some one suggest an alternative for this in csh ? (3 Replies)
Discussion started by: dhanamurthy
3 Replies
Login or Register to Ask a Question