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.
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.
@rupeshforu3 Did you read one of the suggested books?
In Unix you can combine simple tools to complex tasks.
Say find . -type d -links 2
shows the deepest path (true if the file system sticks to the Unix file system conventions),
and if you have a GNU/Linux find (true if find --version says something with GNU).
Then there is still some work to be done. find does not like if the currently visited directory is renamed. (GNU find does it but complaints.)
The following uses -exec command {} + that runs the command with the collected filename arguments - it takes time to collect the arguments, so find has already left the directories when it invokes the command.)
Then the command is /bin/sh and takes an embedded script; in this case a script name needs to be given before the {} that holds the actual directory name.
find music/ -type d -links 2 -exec /bin/sh -c '
for fn
do
echo mv "$fn" "${fn}_128_kbps"
done
' mymv.sh {} +
The echo is prefixed for safety: it only prints the mv commands. Remove it, then it runs the mv commands.
A bit less safe (regarding the timing, and regarding newlines in file names) is the following:
find music/ -type d -links 2 |
while IFS= read -r fn
do
echo mv "$fn" "${fn}_128_kbps"
done
BTW "${fn}_128_kbps" is simply adding the string _128_kbps
You can use a modifier "${fn%_128_kbps}" to do the reverse, i.e. remove a trailing _128_kbps
madeingermany, no they didn't and won't read any books. Got banned and deleted from linux.org for stating they'd keep changing the wording on questions until someone answered.
A google for the op's name turns up a ton. Same exact thing every time every where.