Instead of # i used * (i.e:###Archive###) here but not in the script.
$DATA_DIR is set to data file path and $ARCHIVE is set to Archive file path. I do echo for the same, its showing the correct path. But the thing is ''. If i use '' for data file its throwing the error.
write a diagnostic message saying that mv can't stat ABC_*
and then you say that:
mv DIR\abc_* ${ARCHIVE}
works on the command line, but not in your script.
Do you not see that abc_* is not the same as ABC_*.dat , / is not the same as \ , and you still haven't told us what the value of ${DATA_DIR} is nor what the value of ${ARCHIVE} is?
I can assure you that you won't get a diagnostic about not being able to stat a file named ABC_* from an attempt to move a file named ABC_*.dat .
What operating system are you using? The confusion between / and \ makes me wonder if you're running on Windows instead of on a UNIX or Linux platform.
2.) If i run the above mv command inside my shellscript, im getting the error like
mv: cannot stat `/u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/data/ABC_*.dat': No such file or directory
But if i run the same command (i.e:
mv ${DATA_DIR}\ABC_*.dat ${ARCHIVE}
) outside the shell script, then it works fine.
3.) Instead of using '*' if i used the complete file name (i.e: ABC_20120103) its working fine inside the shell script itself. But i don't want to do that, since n number of files with different date format is present in the directory. So i want to move all the files from data directory to archive directory.
You have not published your script, only some lines
#!/bin/ksh
# if you like to set value for variable, then
DATA_DIR=/u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/data
# no spaces after variable name and after = char
ARCHIVE=/u01/data/oracle/rms/UPGRMSBATCH/archive
# will give error if
# /u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/data/ABC_*.dat not include any ABC_*.dat files
mv $DATA_DIR/ABC_*.dat $ARCHIVE
# test your command line
echo mv $DATA_DIR/ABC_*.dat $ARCHIVE
# => you will see the parsed command line => shell try to execute
# echo /u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/data/ABC_*.dat
# what is the output ?
# echo $DATA_DIR/ABC_*.dat
# what is the output ?
I think I am clear with your issue. You refuse to see that the backslash character ( \ ) and the slash character ( / ) are different.
You say that the following:
mv ${DATA_DIR}\ABC_*.dat ${ARCHIVE}
works on the command line, but don't understand why the following:
mv ${DATA_DIR}/ABC_*.dat ${ARCHIVE}
doesn't work in your script.
Please execute the following commands exactly and post the results:
uname -a
ls -l /u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/data\ABC_*.dat
ls -l /u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/dataABC_*.dat
ls -l /u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS/data/ABC_*.dat
On UNIX and Linux systems, the / character separates components in pathnames. On UNIX and Linux systems, the shell uses \ as an escape character; not to separate a directory name from the name of a file in that directory. Therefore, the command:
mv ${DATA_DIR}\ABC_*.dat ${ARCHIVE}
and the command:
mv ${DATA_DIR}/ABC_*.dat ${ARCHIVE}
are VERY different. If the first one is working on the command line and the second one is not working in your script; use the first one in your script.
Note that the first one uses the asterisk ( * ) to expand to a list of files that match the pattern dataABC_*.dat in the directory:
/u01/data/oracle/rms/UPGRMSBATCH/RETLforRPAS
while the second one uses it expand to a list of files that match the pattern ABC_*.dat in the directory:
Good point. From the description of the problem, I was under the impression that there would always be at least one match, but it would be better to verify that there is a match. The test can be inside the loop, or an if could be added before the loop that exits if there is no match. I believe some shells also have a set special built-in utility option that causes patterns that contain wildcard characters to be removed from the list if there are no matching files.
In my script, if there is no match, $PWD/*ABC* will be printed (with the asterisks) and mv will print a file not found diagnostic message and terminate with a non-zero exit status causing the script to return a non-zero exit status.