DEV Community

hexfloor
hexfloor

Posted on

More storage for media : organize files using ChatGPT : part 1, data preparation

Age of AI

Given the rise of ChatGPT and other Gen AI tools I have to reinvent my posts to make them relevant, some introduction here.
Hence since now my posts will be less technical and more functional. From my conversation with ChatGPT it (from now on I will use pronouns it) granted me the permission to use our conversation in this blog and to reference ChatGPT as it.

Introduction

Let's consider a common problem : we wish to optimize storage of media files.
Now, let's imagine we have made a dump of data with photos and videos by year in different formats and a magic tools that may convert them to the format of our choice.

However, we need to do some preparation work : group the files of each type instead of having the grouping by year. How to achieve this using ChatGPT ?

Do the work

Hers is our dump of media :
Image description
Let's get some input from ChatGPT :

windows using git bash and linux commands, I wish to rename the folders in the current dir having name 'Photos from ' simply to year

Image description

for dir in Photos\ from\ *; do
    if [[ -d "$dir" ]]; then
        year="${dir##* }"  # Extract the year from the folder name
        mv "$dir" "$year"  # Rename the folder
    fi
done
Enter fullscreen mode Exit fullscreen mode

Image description

Result :
Image description

Great, let's move on!

Thank you, now I wish to prepend the , which is now a folder name, to each file within each of the directories to avoid the name collisions

Image description

for dir in [0-9]*; do
    if [[ -d "$dir" ]]; then
        year="$dir"  # Use the directory name as the year
        for file in "$dir"/*; do
            if [[ -f "$file" ]]; then
                mv "$file" "$dir/${year}_$(basename "$file")"  # Prepend the year to the file name
            fi
        done
    fi
done
Enter fullscreen mode Exit fullscreen mode

Image description

One more step closer!

Thank you, now I wish to go through all the folder and instead of grouping by year to group by the file extension, the year folders should disappear and at the end of the procedure I wish to have only extension name folders with all the files with the same extension from all the folders.

Image description

for dir in [0-9]*; do
    if [[ -d "$dir" ]]; then
        for file in "$dir"/*; do
            if [[ -f "$file" ]]; then
                ext="${file##*.}"  # Extract the file extension
                mkdir -p "$ext"    # Create a directory for the extension if it doesn't exist
                mv "$file" "$ext/" # Move the file to the extension directory
            fi
        done
        rmdir "$dir"  # Remove the now-empty year directory
    fi
done
Enter fullscreen mode Exit fullscreen mode

Image description

Here is it :

Image description

I will rename the folders to the lower case, you may learn about the case sensitivity.

Image description

Okay cool, we have done the data preparation. Now let's do some optimization.

Top comments (0)