You are not logged in.

#1 2023-05-29 09:10:17

rupeshforu3
Member
Registered: 2023-01-06
Posts: 70

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

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.

$:\ 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.

Offline

#2 2023-05-29 10:02:02

jonno2002
Member
Registered: 2016-11-21
Posts: 702

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

does this get the '128kbs' in the right place, if so then ill try figure out how to do the renaming part. also is it always '128kbs' or does that vary as well ?

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

you need the 'tree' package installed obviously for this to work.

EDIT: here is a script that seems to work, let me know how it goes

#!/bin/bash
tree -fdX music/ | grep -ioP "(?<=name=\").+(?=\"><\/dir)" | while read line
do 
	mv "$line" "${line} 128kbs"
done

Last edited by jonno2002 (2023-05-29 10:36:12)

Offline

#3 2023-05-29 12:34:48

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

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

Ah, you mean just this:

find music/*/*/* -type d -exec mv '{}' '{}'\ 128\ kbps \;

You can test first with the following to see what it would do:

find music/*/*/* -type d -exec echo "mv {} {}\ 128\ kbps" \;

Are you sure you want spaces in the directory names?  Spaces are fine in path names in general, but avoiding them as a rule on any given system can simplify a range of tasks down the road.

Also, I'm not sure what you mean by "recursively" in your title.  The way you've described the goal is explicitly not recursive.  You want to apply this change only to the third level directories under "music", right?

EDIT: note too that my proposed solution targets the third level directories - you did say you wanted to target the "last directory" in each path, but are there "last directories" that are not third?

Last edited by Trilby (2023-05-29 13:25:07)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2023-05-29 12:46:39

seth
Member
Registered: 2012-09-03
Posts: 58,243

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

Offline

#5 2023-05-29 15:14:01

rupeshforu3
Member
Registered: 2023-01-06
Posts: 70

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

Trilby wrote:

Ah, you mean just this:

find music/*/*/* -type d -exec mv '{}' '{}'\ 128\ kbps \;

You can test first with the following to see what it would do:

find music/*/*/* -type d -exec echo "mv {} {}\ 128\ kbps" \;

Are you sure you want spaces in the directory names?  Spaces are fine in path names in general, but avoiding them as a rule on any given system can simplify a range of tasks down the road.

Also, I'm not sure what you mean by "recursively" in your title.  The way you've described the goal is explicitly not recursive.  You want to apply this change only to the third level directories under "music", right?

EDIT: note too that my proposed solution targets the third level directories - you did say you wanted to target the "last directory" in each path, but are there "last directories" that are not third?

My music folder consists of 100s of sub directories and thousands of music mp3 files.

Adding manually text to the desired directory is heavy task and so I am searching for script.

Spaces in path names are acceptable for me because I am going to play these files in Android music player which can understand such paths.

Offline

#6 2023-05-29 15:31:23

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

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

rupeshforu3 wrote:

My music folder consists of 100s of sub directories and thousands of music mp3 files.

Adding manually text to the desired directory is heavy task and so I am searching for script.

Yes, that's what we're all discussing here.  A single find command will readily work on 100s of directories and thousands of files.  Just because there are a lot of items doesn't make a solution recursive - but that may be a semantic issue.

But as to your goal, are the intended targets for renaming always the third level directory, or should be be directories as any depth so long as they don't contain subdirectories?  Either one of these could be acheived with a simple find command - but each one would require a different find command.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2023-05-29 15:55:02

rupeshforu3
Member
Registered: 2023-01-06
Posts: 70

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

Yes, that's what we're all discussing here.  A single find command will readily work on 100s of directories and thousands of files.  Just because there are a lot of items doesn't make a solution recursive - but that may be a semantic issue.

But as to your goal, are the intended targets for renaming always the third level directory, or should be be directories as any depth so long as they don't contain subdirectories?  Either one of these could be acheived with a simple find command - but each one would require a different find command.

Ok I will try with find command on few files and check it's working fine.

Offline

#8 2023-05-29 16:14:25

rupeshforu3
Member
Registered: 2023-01-06
Posts: 70

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

Hi I have tested the script suggested by jonno and it's working fine.

I will use the same script for the remaining work also.

I think that using tree utility is more recommended than using find.

Offline

#9 2023-05-29 19:22:40

seth
Member
Registered: 2012-09-03
Posts: 58,243

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

[qoute]I think that using tree utility is more recommended than using find.[/qoute]
Translation: I'm lazy and went with the pre-fabbed solution.

The stackexchange link explains how to find the directories you're looking for (those without subdirectories and only files inside) and with all repect to jonno2002, grepping around in the visually pimped representation of a directory tree is inefficient and hyper-fragile.

[Edit: A Parrot thankfully reminded me that I'm better than this]

Last edited by seth (2023-05-30 05:55:27)

Offline

#10 2023-05-30 05:22:48

jonno2002
Member
Registered: 2016-11-21
Posts: 702

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

yip i agree, find is the better option, this seems to work:

find music -depth -type d -links 2 -exec mv '{}' '{} 128kbps' \;

i added '-depth' to stop errors, as find would rename the dir and then try look inside it causing a 'no such directory' error, but '-depth' looks inside the directory first preventing the error.

Offline

Board footer

Powered by FluxBB