仅复制 *.log 文件

问题描述 投票:0回答:1

我有以下 .ksh 文件。我只想将 *.log 文件从parent_dir 复制到target_dir,同时维护文件夹结构和时间戳。 下面的 ksh 复制了 source/parent_dir 中的所有内容。

我只想复制 *.log 文件。

#!/bin/ksh
# Input arguments
PARENT_DIR=$1
TARGET_DIR="/output/ndc_grid/target/folder/log_dump"

# Check if parent directory exists
if [ ! -d "$PARENT_DIR" ]; then
  echo "Parent directory $PARENT_DIR does not exist."
  exit 1
fi

# Check if target directory exists, if not create it
if [ ! -d "$TARGET_DIR" ]; then
  echo "Target directory $TARGET_DIR does not exist. Creating it..."
  mkdir -p "$TARGET_DIR"
fi

# Copy parent directory and its subdirectories to the target directory
echo "Copying parent directory and its subdirectories..."
#cp -r "$PARENT_DIR" "$TARGET_DIR"
find "$PARENT_DIR" -type f -name *.log -exec cp -pr {} "$PARENT_DIR" "$TARGET_DIR" \;
#find "$PARENT_DIR" -type f -name "*.log" -exec cp -pr {} "$TARGET_DIR/{}" \;

echo "Parent directory and its subdirectories have been copied to $TARGET_DIR"
shell unix ksh
1个回答
0
投票

使用

cpio
的可能解决方案,假设您没有名称中包含特殊字符的文件。

cd "$PARENT_DIR" && find . -type f -name "*.log" | cpio -pdmuv "$TARGET_DIR"

如果您不想看到已处理文件的列表,请忽略

cpio
的选项
v

有关选项的说明,请参阅

man cpio

© www.soinside.com 2019 - 2024. All rights reserved.