bash script to Add text to last directory ie., end of path recursively

rupeshforu3

Disciple
Hi I am Rupesh from India and I have a pc with Intel i3 10th gen processor and I have installed windows 11 and Arch Linux. I have 10 gb of MP3 files in various sub directories and I want to add text like 128 kbps or 250 kbps to the last directory in the path.

I have mp3 files in the following pattern

music/dir1/dir2/dir3/song1.mp3
music/dir1/dir2/song2.mp3

My requirement is I want to move mp3 files as following

music/dir1/dir2/dir3 128 kbps/song1.mp3
music/dir1/dir2/song2.mp3

Every root directory consists of various sub directories and mp3 files. I just want to add text 128 kbps to the name of last directory in the path.

Previously I have experimented with directories in bash to obtain the last directory in the following way.

Code:
$:\ rev source_names.txt > rev.txt
$:\ cut -f1 -d'/' rev.txt > temp.txt 
$:\ rev temp.txt > extracted_names.txt

I think that it may be useful in our task.

Here another requirement is dir2 consists of one mp3 with name song2.mp3 and another sub directory and so any text must not be added to the name of dir2 I mean dir2 must not be renamed as dir2 128 kbps.

I have tried bulk rename utility like advanced renamer in windows 11 by adding music folder in it but unfortunately it is renaming all directories present in path ie., dir1 to dir1 128 kbps, dir2 to dir2 128 kbps, dir3 to dir3 128 kbps.

At present I am learning slowly unix and linux concepts and it may take atleast one year for to write a script on my own.

Kindly try to suggest a bash script to accomplish this task.

Regards,
Rupesh.
 
Hey,

Option 1:
Instead of creating a list while traversing each directory, you may want to create a tree.
For example,

1685352769569.png


Each 'leaf' node will be the one you'd have to rename.
Edit: as per your example, you don't want to rename dir2. So you can calculate max depth of tree and only rename directories on that depth.

While thinking about the above, came up with
Option 2 (edit: may not work in your case):
You just have to rename the folders that don't have folders inside them. This will be easier. Will try a bash script in the evening on my home PC and attach here.
 
Last edited:
Hi someone from arch linux forums suggested one command and one script. I will check if those works for few files.

Code:
tree -fdX music/ | grep -ioP "(?<=name=\").+(?=\"><\/dir)" | sed "s/$/ 128kbs/g"

Code:
#!/bin/bash

tree -fdX music/ | grep -ioP "(?<=name=\").+(?=\"><\/dir)" | while read line
do  
mv "$line" "${line} 128kbs"
done
Hi the script above is working fine and I have tested two times.
 
Last edited:
I kept reading that ChatGPT writes scarily good code. Decided to check using this requirement.
Here is the code to just list the lowest level sub-directories.
Quite good. <insert impressed emoji here>

Code:
@echo off
setlocal enabledelayedexpansion

set "root_directory=F:\__temp"

for /r "%root_directory%" %%A in (.) do (
    set "subdir=%%~fA"
    set "is_subdir=1"
    for /d %%B in ("!subdir!\*") do (
        set "is_subdir=0"
    )
    if !is_subdir! equ 1 (
        echo !subdir!
    )
)

endlocal

pause
exit


Can easily be modified to rename the lowest level directory (after checking if it has mp3 files).

edit : the last two lines of code added by me
 
Last edited:
Back
Top