Linux Rename 时间格式

7 min read Oct 11, 2024
Linux Rename 时间格式

How to Rename Files with Timestamps in Linux

Renaming files with timestamps in Linux is a common task for organizing files and data. This can be incredibly useful for maintaining a chronological order of your files, especially when working with logs, backups, or any other data that needs to be tracked by date and time.

Let's explore how to achieve this using the power of the Linux command line.

Understanding the date Command

Before diving into renaming, let's familiarize ourselves with the date command. This is the key to generating timestamps in various formats.

Example:

date +%Y-%m-%d_%H-%M-%S 

This command will output a timestamp in the format "YYYY-MM-DD_HH-MM-SS".

Explanation:

  • date: The command to access the system's date and time.

  • +%Y-%m-%d_%H-%M-%S: This is a format string that dictates how the output will be structured.

    • %Y: Year with four digits (e.g., 2023)
    • %m: Month as a two-digit number (e.g., 01 for January)
    • %d: Day of the month as a two-digit number (e.g., 15)
    • _%H: Hour in 24-hour format (e.g., 10 for 10 AM)
    • %M: Minute (e.g., 30)
    • %S: Second (e.g., 55)

Renaming Files with mv and date

Now, let's combine our knowledge of the date command with the mv command (move/rename) to rename files.

Example:

mv my_file.txt my_file_$(date +%Y-%m-%d_%H-%M-%S).txt

Explanation:

  • mv: This is the command to move/rename a file.

  • my_file.txt: The original filename.

  • my_file_$(date +%Y-%m-%d_%H-%M-%S).txt: The new filename, incorporating the timestamp generated by the date command.

    • $(date +%Y-%m-%d_%H-%M-%S): This section uses command substitution to insert the generated timestamp directly into the new filename.

Let's break it down:

  1. mv: We initiate the move/rename command.
  2. my_file.txt: We specify the original filename.
  3. my_file_: This part defines the prefix for the new filename.
  4. $(date +%Y-%m-%d_%H-%M-%S): The generated timestamp is inserted here.
  5. .txt: The file extension remains the same.

Renaming Multiple Files with for Loop

For scenarios where you need to rename multiple files with timestamps, the for loop is your friend.

Example:

for f in *.txt; do
  mv "$f" "${f%.*}_$(date +%Y-%m-%d_%H-%M-%S).${f##*.}"
done

Explanation:

  • for f in *.txt; do ... done: This loop iterates over all files ending with .txt in the current directory.

  • mv "$f" ...: The mv command is used to rename each file.

  • "${f%.*}_$(date +%Y-%m-%d_%H-%M-%S).${f##*.}": This is the new filename construction.

    • "${f%.*}": This extracts the filename without the extension.
    • "_$(date +%Y-%m-%d_%H-%M-%S)": The timestamp is inserted with an underscore as a separator.
    • "${f##*.}": This extracts the original file extension.

Important Note: The for loop will rename all .txt files in the current directory. Be cautious before running this command as it will permanently rename your files.

Advanced Timestamp Formatting

The date command offers a variety of formatting options. Here are some other useful formats for renaming files:

  • ISO 8601 Timestamp: date +%Y-%m-%dT%H:%M:%SZ (e.g., 2023-12-14T15:20:30Z)
  • Short Date: date +%Y-%m-%d (e.g., 2023-12-14)
  • Hour and Minute: date +%H-%M (e.g., 15-20)
  • Day of the Week: date +%A (e.g., Thursday)

Renaming Files with Specific Timestamps

If you need to use a specific timestamp that is not the current time, you can simply replace the date command with the desired timestamp in the desired format.

Example:

mv my_file.txt my_file_2023-12-14_15-20-30.txt

In this case, we are explicitly using the timestamp "2023-12-14_15-20-30".

Additional Tips

  • Backup: Always create a backup of your files before making any changes, especially when using the for loop.
  • Test: Test your commands on a few files before running them on a larger set.
  • Customization: Experiment with different date formats and filenames to suit your needs.

Conclusion

Renaming files with timestamps in Linux is a powerful technique for file organization and management. By combining the date and mv commands, you can easily add timestamps to your files, allowing you to quickly identify and access them based on their creation date and time. Remember to use these commands with caution and always create backups before making significant changes to your files.

Featured Posts