Have you ever found yourself with a camera full of photos and no easy way to organize them? If so, you're not alone. Many people struggle with managing their photos, especially when they're spread across multiple folders and devices. But fear not, because today we're going to show you how to use a simple BASH script to copy image files recursively from camera folders and rename them with their creation date.
Before we dive into the details, let's first understand what a BASH script is. BASH stands for "Bourne Again SHell," and it is a command-line shell and scripting language used in Unix-based operating systems like Linux and macOS. BASH scripts allow you to automate tasks by writing a series of commands that can be executed in sequence.
Now, let's get started with the script. Open your favorite text editor and create a new file. We'll name it "copy_images.sh" for this example. Begin by adding the following line at the top of your script:
#!/bin/bash
This line tells the system that this file is a BASH script and should be executed using the BASH interpreter.
Next, we'll define a few variables that will be used throughout the script. Add the following lines to your script:
source_dir="/path/to/source/folder"destination_dir="/path/to/destination/folder"
Replace "/path/to/source/folder" with the path to the folder where your camera photos are stored. Similarly, replace "/path/to/destination/folder" with the path to the folder where you want to copy the photos.
Now, let's move on to the main part of the script. Add the following lines:
find "$source_dir" -type f -iname "*.jpg" -o -iname "*.jpeg" | while read -r filedo creation_date=$(date -r "$file" +"%Y-%m-%d") new_filename="$creation_date-$(basename "$file")" cp "$file" "$destination_dir/$new_filename"done
Let's break down what each line does:
- The "find" command is used to search for files in the specified directory and its subdirectories. In our case, we're searching for files with the extensions ".jpg" or ".jpeg".
- The "while read -r file" loop reads each file found by the "find" command and assigns it to the variable "file".
- The "date -r" command retrieves the creation date of the file and formats it as "YYYY-MM-DD".
- The "basename" command extracts the filename from the full file path.
- The "cp" command copies the file to the destination folder using the new filename, which consists of the creation date followed by the original filename.
Once you've added these lines to your script, save the file and exit your text editor. Now, let's make the script executable. Open a terminal and navigate to the directory where you saved the script. Run the following command:
chmod +x copy_images.sh
This command grants execute permissions to the script, allowing you to run it as a program. Now, you're ready to run the script! In the terminal, run the following command:
./copy_images.sh
The script will start copying your image files from the source directory to the destination directory, renaming them with their creation dates along the way. Depending on the number of files and their sizes, this process may take some time.
Once the script finishes, you'll find all your image files neatly organized in the destination folder, with filenames that include their creation dates. This will make it much easier to locate and manage your photos.
That's it! You've successfully used a BASH script to copy image files recursively from camera folders and rename them with their creation dates. Feel free to customize the script to suit your specific needs. Happy organizing!
| Reference | Link |
|---|---|
| BASH Scripting Tutorial | https://www.tutorialspoint.com/unix/shell_scripting.htm |
| BASH Manual | https://www.gnu.org/software/bash/manual/ |
| Linux Command Line Basics | https://ubuntu.com/tutorials/command-line-for-beginners#1-overview |