I have a folder which contain the log files. The folder may contain sub folders as well. I want to move the contents of the log folder to tmp folder periodically. I have used the command.
but, the problem is that this command moves all the files in the same folder and source folder structure (sub-folders) is not maintained in the destination directory.
No success, as it gives 'bad Option -maxdepth' error.
Actually, my scenerio is that I have a folder name 'Messages' and contains subfolders 'message1', 'message2', 'message3', 'message4' and so on. I want to move the folder Messages to the destination directory say 'Logs' and want that directory structure in the destination remains the same. But, when i use the above find command, all the files in the subfolder are placed in the same folder.
Is there any other way through which i can move files older than, say 5 days, to another location, while maintaining the directory structure of the source
Let's say you have a file, Messages/message3/someoldlogfile.log, dirname would pull out the "Messages/message3" component, and sed converts it to "Logs/message3".
The second line tests whether this directory exists, and if not, creates it.
find ${OVSA_DIR} -type f -mtime +2 | while read file
do
dest=$(dirname "$file" | sed "s/response_messages/OVSALogs_${CurTime}/")
[ -d "$dest" ] || mkdir -p "$dest"
mv "$file" "$dest"
done
files are now copied in their respective folders but not in the OVSALogs_${curTime} folder
instead, they are copied to =/var/opt/OV/ServiceActivator/OVSALogs_${CurTime} folder.
The problem is in sed "s/response_messages/OVSLogs_$curTime}/". What sould i pass in place of resonse_messages in sed command here so that files are copied to /tmp/OVSA/OVSALogs_${curTime} directory
Using a different separator in sed is a good idea when you are using path names, because / is obviously also present in the path names. I'm using the percent sign here, but you can use any character which is not also in the data you are trying to handle.
sed "s%/var/opt/OV/ServiceActivator/response_messages%/tmp/OVSA/OVSALogs_${curTime}%"