Im trying to run mv command using shell script but it gives me
mv: cannot stat `/opt/logs/merchantportal/logger.log.20160501.*': No such file or directory
mv: cannot stat `/opt/logs/merchantapi/logger.log.20160501.*': No such file or directory
// THIS IS MY SHELL SCRIPT
#!/bin/bash
now="$(date +'%Y%m%d')"
merchantPortalLogsPath="/opt/logs/merchantportal"
merchantApiLogsPath="/opt/logs/merchantapi"
currentDate="$(date +%Y%m%d)"
olderDate="$(date "+%Y%m%d" -d "1 days ago")"
merchantPortalLogsPathBackup=$merchantPortalLogsPath"."$olderDate
merchantApiLogsPathBackup=$merchantApiLogsPath"."$olderDate
mkdir $merchantPortalLogsPathBackup
mkdir $merchantApiLogsPathBackup
echo $merchantPortalLogsPath"/logger.log."$olderDate".*" $merchantPortalLogsPathBackup"/"
echo $merchantApiLogsPath"/logger.log."$olderDate".*" $merchantApiLogsPathBackup"/"
mv $merchantPortalLogsPath"/logger.log."$olderDate".*" $merchantPortalLogsPathBackup"/"
mv $merchantApiLogsPath"/logger.log."$olderDate".*" $merchantApiLogsPathBackup"/"
// BUT DIRECTORY IS CREATED SUCCESSFULLY
".*"
Putting the *
inside double quotes will prevent the shell from treating that as a wildcard and will instead take it as a literal *
character. Instead, change your script so that it does not double quote the *
. For example:
mv ${merchantPortalLogsPath}/logger.log.${olderDate}.* ${merchantPortalLogsPathBackup}/
mv ${merchantApiLogsPath}/logger.log.${olderDate}.* ${merchantApiLogsPathBackup}/
Note: Technically should actually double quote the variable expansions to handle paths with spaces and other special characters in them. But I have not shown that to focus just on the problem at hand.
The log directories exist, but the log files within those directories did not. Since mv
cannot move from data a log file that doesn't first exist, it complains, rather vaguely:
No such file or directory
Note: IMHO vague error messages are documentation/interface bugs -- if the error had said only:
No such file
And not left the user wondering if there was a missing directory it would have seemed less puzzling, since that message would clearly imply that the directory where the file was supposed to exist did in fact exist.
But consider this egregious GNU mv
example, where a directory /tmp/a/
does not exist:
mv /tmp/a/b/c/d /tmp/foo
Output to STDERR:
mv: cannot stat '/tmp/a/b/c/d': No such file or directory
Now, the directory /tmp/a/
doesn't exist, and also the directories /tmp/b/
and /tmp/a/b/c/
, and the file /tmp/a/b/c/d
, none of them exist. The user is given no indication of which of those is the problem, and it's even possible (unusual, but possible) that /tmp/
doesn't exist. Where as writing just a few lines of code could output an error message that said something so much more useful, like:
mv: cannot stat '/tmp/a/b/c/d': `/tmp/` exists, but not directory `/tmp/a/`
...which collectively would probably save years of user-time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.