Technology
Renaming Files with Last Modified Timestamp in Unix
How to Rename a Batch of Files with Last Modified Timestamp in Unix
Renaming files in a Unix-like system and appending the last modified timestamp to the end of each filename can be achieved through a combination of find, stat, and mv commands. This guide will walk you through a shell script example that demonstrates how to automate this process.
Using the following steps, you can efficiently rename multiple files with their respective timestamps at the end of the filename.
Example Script: Automating File Renaming
1. Example Script
The following Bash script demonstrates how to rename a batch of files in a directory, appending their last modified date to the end of the filename:
#!/bin/bash # Change to the directory containing the files cd /path/to/your/files # Loop through each file in the directory for file in * do # Check if it is a file if [ -f "$file" ]; then # Get the last modified timestamp in YYYYMMDD format timestamp$(stat -c %Y "$file" | date -r -u "%Y%m%d") # Get the file extension extension"${file##*.}" # Get the filename without the extension filename"${file%.*}" # Construct the new filename new_filename"${filename}_$timestamp.${extension}" # Rename the file mv "$file" "$new_filename" fi done
Explanation of the Script:
Change Directory: Change the working directory to where the files are located.
Loop Through Files: Loop through each file in the directory.
Check if File: Check if the current item is a file.
Get Timestamp: Use stat -c %Y to get the last modified timestamp in Epoch time, then convert it to YYYYMMDD format using date -r -u "%Y%m%d".
Extract Filename and Extension: Extract the filename and extension.
Construct New Filename: Construct the new filename by appending the timestamp and the original extension.
Rename the File: Rename the file using mv.
Running the Script
Save the script to a file, for example, rename_
# Make the script executable chmod x rename_ # Run the script ./rename_
Note: Always test the script with a few files first to ensure it works as expected. Backup your files before running batch renaming operations to prevent data loss.
Utilizing xargs for Enhanced Performance
xargs is often used when dealing with numerous file names. It can run faster and safer than bash loops. Here's an example using xargs with find and mv:
find /path/to/your/files -type f -printf '%p0' | xargs -0 -I{} -n2 mv {} {}.date_timestamp_suffix
Note: This command will add a suffix to the filenames, and the -n2 option ensures it processes two arguments at a time. Be sure to replace date_timestamp_suffix with an appropriate format, such as _YYYYMMDD.
Handling Pathological File Names
It's important to handle filenames that contain spaces or special characters. Using -print0 with find and -0 with xargs can help:
find /path/to/your/files -type f -print0 | xargs -0 -I{} -n2 mv {} {}.date_timestamp_suffix
This ensures that filenames with spaces or special characters are correctly handled.
Filtering Existing Renamed Files
If you want to run the script multiple times without reprocessing already renamed files, you can add a filter to the find command:
find /path/to/your/files -type f -regextype posix-egrep -not -regex '.*[0-9]{8}$' -print0 | xargs -0 -I{} -n2 mv {} {}.date_timestamp_suffix
In this command, -regextype posix-egrep -not -regex '.*[0-9]{8}$' ensures that only files not ending with a 8-digit timestamp are processed.