Moving Files from one folder to another folder

Hi,

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.

LOG_DIR=/logs

DESTINATION_DIR=/tmp/logs

find ${LOG_DIR} -mtime +1 -exec mv {} ${DESTINATION_DIR} \;

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.

Any ideas? or some other way to do this

Regards,
Muhammad Farooq

add these options to your find command:

-maxdepth 0  -type f

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

How about this?

find Messages -type f -mtime +5 | while read file
do
    dest="Logs${file#Messages}"
    [ -d "$dest" ] || mkdir -p "$dest"
    mv "$file" "$dest"
done

Can anyone please explain what the following line is doing :

dest=$(basename "$file" | sed 's/Messages/Logs/')
[ -d "$dest" ] || mkdir -p "$dest"

Actually, I made a mistake. :slight_smile: I edited my post to correct it, but obviously wasn't fast enough. Anyway, my update was also incorrect...

    dest=$(dirname "$file" | sed 's/Messages/Logs/')
    [ -d "$dest" ] || mkdir -p "$dest"

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.

thanks for reply,
what is this s for in 'sed s/Messages/Logs/'

Also, i have a source folder and destination folder in a variable, so how would i replace that in a above command

$SRC_DIR=/var/opt/Messages

probably shomething like below?
dest=$(dirname "$file" | sed 's/${SRC_DIR}/Logs/')

The sed s command is just like in vi. s/water/wine/ would search-and-replace water with wine.

Strings between 'single quotes' are not evaluated for variables. If you use "double quotes" instead it should work:

dest=$(dirname "$file" | sed "s/${SRC_DIR}/Logs/")

thanks for your help. i have a slight issue. my code is below:

CurTime=`date +%Y%m%d%H%M%S`
OVSA_DIR=/var/opt/OV/ServiceActivator/response_messages

logDir=/tmp/OVSA/OVSALogs_${CurTime}
mkdir ${logDir}

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

Thanks in advance

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}%"

thanks all, problem resolved