You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用scp备份/远程复制时如何保留文件mtime、ctime?含find清理需求

Solutions for Your File Cleanup & Backup Workflow

Hey there, let's walk through your questions step by step since you're building a file cleanup workflow tied to file creation/modification times—super common for maintenance tasks, so I get where you're coming from.

1. Keeping Source File's ctime/mtime Unchanged During scp Backup

First off, you don't have to stress about scp altering your source files' ctime or mtime. scp only reads the source file to create a copy for transfer; it never modifies the original. That means your source file's modification time (mtime) and inode change time (ctime) will stay exactly as they were before the backup operation. No hidden changes here!

2. Preserving File & Directory mtime When Using scp -r

If you want the copied files (and directories, where possible) to retain their original mtime from the remote server, here's what to do:

Use scp with the -p Flag

The -p (short for "preserve") flag tells scp to keep the original file's modification time, access time, and permissions. When combined with -r for recursive copying, this applies to all files in the directory structure:

scp -rp your-user@remote-server:/path/to/source/dir/ /local/destination/path/

A quick heads-up: For directories, some scp versions might not preserve the original mtime. That's because creating a directory on the target system automatically updates its mtime to the current time. If strict preservation of directory timestamps is critical for your workflow, rsync is a better tool for the job—it handles attribute retention far more reliably:

rsync -avz your-user@remote-server:/path/to/source/dir/ /local/destination/path/

The -a (archive) mode defaults to preserving permissions, mtime, and other metadata, while -v gives you verbose output so you can track progress, and -z compresses data during transfer to save bandwidth.

3. Breaking Down Your find Command for File Filtering

You mentioned planning to use this command in your shell script:

find $V_Filepath -mmin +7200 -name "*.ack"

Let's break down what it does, plus a few best practices to avoid headaches:

What This Command Does

  • $V_Filepath: The root directory where find will start searching
  • -mmin +7200: Filters files whose modification time is more than 7200 minutes old (that's 120 hours, or 5 days—perfect for older files you want to clean up)
  • -name "*.ack": Limits results to only files ending with the .ack extension

Script Best Practices

To avoid issues if $V_Filepath contains spaces or special characters, always wrap the variable in double quotes:

find "$V_Filepath" -mmin +7200 -name "*.ack"

Before you delete any files, it's smart to first print out the matching files to make sure you're targeting the right ones:

find "$V_Filepath" -mmin +7200 -name "*.ack" -print

Once you've confirmed the list is correct, you can delete them directly with GNU find's -delete flag:

find "$V_Filepath" -mmin +7200 -name "*.ack" -delete

If you need compatibility with non-GNU systems, use xargs (with -print0 and -0 to handle spaces safely):

find "$V_Filepath" -mmin +7200 -name "*.ack" -print0 | xargs -0 rm -f

内容的提问来源于stack exchange,提问作者Balakumaran Muralidharan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:15:06