Hi All,
I have one query is file move.
in this path "/tmp/my-test/testing/logs/mars" many directories & files need to move files more than 2 days to "/opt/mars/logs-backup" path .
condition is "source-path" direcory not remove while moving files & simultaneously destination path "source path directories" create then logs need to move destination path .
Note : no need create folders manually in destination path , as per source path directories need to create in destination automatically while move more than 2 days files .
please find below paths for sample reports logs path .
if possible any command for below task then please share .
Explanation :
/tmp/my-test/testing/logs/mars/water/result.log.txt-27-Aug-2024
/tmp/my-test/testing/logs/mars/water/result.log.txt-28-Aug-2024
/tmp/my-test/testing/logs/mars/water/result.log.txt-29-Aug-2024
/tmp/my-test/testing/logs/mars/plants/result.log.txt-27-Aug-2024
/tmp/my-test/testing/logs/mars/plants/result.log.txt-28-Aug-2024
/tmp/my-test/testing/logs/mars/temperature-report.log.txt-26-Aug-2024
/tmp/my-test/testing/logs/mars/temperature-report.log.txt-27-Aug-2024
/tmp/my-test/testing/logs/mars/temperature-report.log.txt-28-Aug-2024
/tmp/my-test/testing/logs/mars/rain/data/report.log.txt-26-Aug-2024
/tmp/my-test/testing/logs/mars/rain/data/report.log.txt-27-Aug-2024
/tmp/my-test/testing/logs/mars/rain/data/report.log.txt-28-Aug-2024
/tmp/my-test/testing/logs/mars/rain/data/report.log.txt-29-Aug-2024
------------------------
I am waiting for your valuable information .
I hope explained query clearly .
Regards,
Jeesshnasree
Hi, you can try the following shell script to achieve this:
#!/bin/bash
# Define source and destination paths
SOURCE_PATH="/tmp/my-test/testing/logs/mars"
DEST_PATH="/opt/mars/logs-backup"
# Find files older than 2 days
find "$SOURCE_PATH" -type f -mtime +2 | while read FILE
do
# Get the relative path of the file by stripping the source path from the file path
RELATIVE_PATH="${FILE#$SOURCE_PATH/}"
# Get the directory path for the file in the destination
DEST_DIR="$DEST_PATH/$(dirname "$RELATIVE_PATH")"
# Create the directory structure in the destination path if it doesn't exist
mkdir -p "$DEST_DIR"
# Move the file to the destination path
mv "$FILE" "$DEST_DIR"
done
echo "Files older than 2 days have been moved to $DEST_PATH"
Hi, you can try the following shell script to achieve this:
#!/bin/bash
# Define source and destination paths
SOURCE_PATH="/tmp/my-test/testing/logs/mars"
DEST_PATH="/opt/mars/logs-backup"
# Find files older than 2 days
find "$SOURCE_PATH" -type f -mtime +2 | while read FILE
do
# Get the relative path of the file by stripping the source path from the file path
RELATIVE_PATH="${FILE#$SOURCE_PATH/}"
# Get the directory path for the file in the destination
DEST_DIR="$DEST_PATH/$(dirname "$RELATIVE_PATH")"
# Create the directory structure in the destination path if it doesn't exist
mkdir -p "$DEST_DIR"
# Move the file to the destination path
mv "$FILE" "$DEST_DIR"
done
echo "Files older than 2 days have been moved to $DEST_PATH"
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.