cancel
Showing results for 
Search instead for 
Did you mean: 
jeesshnasree
Starfighter Starfighter
Starfighter
  • 472 Views

move files all paths from source to destination path

Jump to solution

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

Labels (1)
1 Solution

Accepted Solutions
OwenV
Cadet
Cadet
  • 451 Views

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"

View solution in original post

2 Replies
OwenV
Cadet
Cadet
  • 452 Views

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"

jeesshnasree
Starfighter Starfighter
Starfighter
  • 425 Views

Hi @OwenV ,

 

 

Thank you so much , script is working . Please find attached screenshot .

 

file-move.png

Join the discussion
You must log in to join this conversation.