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

如何在Unix(Redhat Linux 6.9)创建目录并复制指定日期前的.log文件用于测试

Hey there, let's walk through exactly what you need to do on your RHEL 6.9 (64-bit) Dev instance—creating temp directories/folders and copying old .log files for testing:

1. Create a Temporary Directory or Custom Folder

Option 1: System-Managed Temporary Directory

If you need a temporary directory that the system will automatically clean up (perfect for short-term testing), use the mktemp command. It generates a unique, secure temp directory in /tmp:

mktemp -d

When you run this, it'll output the path of your new temp dir (e.g., /tmp/tmp.abc123). This directory will typically be deleted on system reboot or by the system's temp file cleaner.

Option 2: Custom Fixed Folder

If you want a persistent folder in a specific location, use mkdir. The -p flag ensures parent directories are created if they don't exist, so you won't get errors if the path doesn't fully exist yet:

# Example: Create a folder named log_test_backup in /home/dev
mkdir -p /home/dev/log_test_backup

2. Copy .log Files Older Than a Specific Date

To copy only .log files modified before a certain date, use the find command combined with cp—this is the most reliable way to filter files by timestamp on RHEL 6.9.

Example 1: Copy files older than a specific calendar date

Let's say you want all .log files from /var/app/logs that were modified on or before 2024-03-01:

find /var/app/logs -type f -name "*.log" -newermt "2024-03-01" ! -newermt "2024-03-02" -exec cp {} /home/dev/log_test_backup/ \;

Breakdown of the key flags:

  • -type f: Only target files (skip directories to avoid copying folder structures)
  • -name "*.log": Filter for files ending in .log
  • -newermt "2024-03-01" ! -newermt "2024-03-02": Select files modified on or before 2024-03-01 (this avoids edge cases with exact time of day)
  • -exec cp {} /path/to/target/ \;: Copy each matching file ({}) to your target folder

Example 2: Copy files older than X days

If you prefer to use relative time (e.g., files older than 14 days), use -mtime +14:

find /var/app/logs -type f -name "*.log" -mtime +14 -exec cp -n {} /home/dev/log_test_backup/ \;

The -n flag added to cp prevents overwriting existing files in the target folder—handy if you run this command multiple times for incremental backups.


内容的提问来源于stack exchange,提问作者Vishal mohan dube

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:46:03