change filename to directory name

revelstone

New Member
Joined
Dec 24, 2024
Messages
9
Reaction score
2
Credits
95
solved



So here's the deal. i have file_name.* in each directory. what i want to do is rename file_name to the directory name as in the first listing.
So file_name would become Death is a Lonely Business.mp3 or Death is a Lonely Business.mp4
i need to do this in actual hundreds (up to 300) directories. i dont want to do them individually, because i wont be alive that long. really.
so renaming in a bash script would be helpful. any ideas?


./bradbury/Ray Bradbury - Death is a Lonely Business - Unb:
file_name.m4a
file_name.mp3
Ray Bradbury - Death is a Lonely Business (001).mp3
Ray Bradbury - Death is a Lonely Business (002).mp3
Ray Bradbury - Death is a Lonely Business (003).mp3
Ray Bradbury - Death is a Lonely Business (004).mp3
Ray Bradbury - Death is a Lonely Business (005).mp3
Ray Bradbury - Death is a Lonely Business (006).mp3

./bradbury/Ray Bradbury - Fahrenheit 451 (BBC):
file_name.m4a
file_name.mp3
Ray Bradbury - Fahrenheit 451 (BBC).mp3

./bradbury/Ray Bradbury - The Martian Chronicles:
01 - The Martian Chronicles - Preface.mp3
02 - January 1999 - Rocket Summer.mp3
03 - February 1999 - Ylla.mp3
04 - August 1999 - The Summer Night.mp3
05 - August 1999 - The Earth Men.mp3
06 - March 2000 - The Taxpayer.mp3
07 - April 2000 - The Third Expedition.mp3
08 - June 2001 - And The Moon Be Still As Bright.mp3
09 - August 2001 - The Settlers.mp3
10 - December 2001 - The Green Morning.mp3
11 - February 2002 - The Locusts.mp3
12 - August 2002 - Night Meeting.mp3
13 - October 2002 - The Shore.mp3
14 - November 2002 - The Fire Balloons.mp3
15 - February 2003 - Interim.mp3
16 - April 2003 - The Musicians.mp3
17 - June 2003 - Way Up In The Middle Of The Air.mp3
18 - 2004-2005 - The Naming Of Names.mp3
19 - August 2005 - The Old Ones.mp3
20 - September 2005 - The Martian.mp3
21 - November 2005 - The Luggage Store.mp3
22 - November 2005 - The Off Season.mp3
23 - November 2005 - The Watchers.mp3
24 - December 2005 - The Silent Town.mp3
25 - April 2026 - The Long Years.mp3
26 - August 2026 - There Will Come Soft Rains.mp3
27 - October 2026 - The Million-year Picnic.mp3
file_name.m4a
file_name.mp3
Ray Bradbury - The Martian Chronicles cover.JPG
Ray Bradbury - The Martian Chronicles.nfo
Ray Bradbury - The Martian Chronicles.sfv

./bradbury/Ray Bradbury - The Martian Chronicles (Stephen Hoye):
file_name.m4a
file_name.mp3
Ray Bradbury - The Martian Chronicles.mp3
The Martian Chronicles-Cover.jpg

./bradbury/Stories Of Ray Bradbury - AudioBooks - MP3:
file_name.m4a
file_name.mp3
Stories Of Ray Bradbury - S01 - E01 - Golden Apples Of The Sun & The One Who Waits.mp3
Stories Of Ray Bradbury - S01 - E02 - Tomorrow's Child.mp3
Stories Of Ray Bradbury - S01 - E03 - All Summer In A Day & The Fog Horn.mp3
Stories Of Ray Bradbury - S01 - E04 - A Sound Of Thunder.mp3
Stories Of Ray Bradbury - S01 - E05 - Dark They Were & Golden Eyed.mp3

./bradbury/Tales of the Bizarre - Ray Bradbury - Series 1 - BBC Audio Drama:
file_name.m4a
file_name.mp3
Tales of the Bizarre - Ray Bradbury - S01E01 - Night Call Collect.mp3
Tales of the Bizarre - Ray Bradbury - S01E02 - Have I Got a Chocolate Bar for You.mp3
Tales of the Bizarre - Ray Bradbury - S01E03 - The Jar.mp3
Tales of the Bizarre - Ray Bradbury - S01E04 - The Fruit at the Bottom of the Bowl.mp3
Tales of the Bizarre - Ray Bradbury - S01E05 - I Sing the Body Electric.mp3
Tales of the Bizarre - Ray Bradbury - S01E06 - Skeleton.mp3

./bradbury/The Halloween Tree (Dramatized):
file_name.m4a
file_name.mp3
RB29_ The Halloween Tree (Dramatized).jpg
The Halloween Tree Dramatized.mp3

./bradbury/The Martian Chronicles (A Radio Dramatization):
file_name.m4a
file_name.mp3
RB01_ The Martian Chronicles_ A Radio Dramatization 1.jpg
The Martian Chronicles A Radio Dramatization.mp3
 
Last edited:


You could use /usr/bin/cut in a bash script to find the file name before the first . using the . as the delimiter.

Signed,

Matthew Campbell
 
Maybe something like this.

Code:
#!/bin/bash

# Loop through each directory
for dir in ./bradbury/*/; do
  # Get the directory name without the trailing slash
  dirname=$(basename "$dir")
  
  # Loop through each file_name.* in the directory
  for file in "$dir"file_name.*; do
    # Get the file extension
    extension="${file##*.}"
    
    # Rename the file to the directory name with the same extension
    mv "$file" "$dir$dirname.$extension"
  done
done
 
dear dos2unix,
thank you. that did exactly the right thing, on the first try. guess i better read my linux book cover to cover.
 
Others have helped and you can edit your first post to mark this as solved if you don't mind - assuming it is solved.

Off-topic:

While it's a collection of stories, dnn't forget Illustrated Man. It's a great collection, in my opinion.
 
If I were going to use a script to fix filenames, I'd purge the damned spaces from the file names, too. I know it's perfectly legit to have spaces in filenames, but I can't think of any good reason to have a standard delimiter (space) included in a file name. Grrrr!

In the mean time, I'd sure like to browse your sci fi library. :)
 
mikerocor if you tell me how, i can send you a list.
in the meantime, i discovered that if i have multiple directories with multiple authors, it quits working as soon as i get out of bradbury directory.
i tried replacing for dir in ./bradbury//; with ~//; to no avail. also tried //
in the words of mike Grrrrr

i also tried
CURRENT=pwd
BASENAME=basename "$CURRENT"

echo "$BASENAME"
cp file_name.* "$BASENAME.*"

and it tells it's not a directory, which i already knew.
anyway the fun is in the challenge and i continue.
just so it's been said, i never play computer games. making the computer do what i say IS the game.
 
Others have helped and you can edit your first post to mark this as solved if you don't mind - assuming it is solved.

Off-topic:

While it's a collection of stories, don't forget Illustrated Man. It's a great collection, in my opinion.
i've read the illustrated man, been reading daily since the fifth grade. read most of Bradbury and so many books i could fill a library. oh wait, i already have the library. but the illustrated man is good. also Methuselah's children and the spin offs of that. i having my wife bury me under the library of congress so i'll have something to do for eternity.
 
@revelstone
If you wish to change spaces in file names to underscores, prior to the other changes you wish to make, you can run the command in the following example from within the directory in which the files reside:
Code:
$ ls
'file 1'  'file 2'

$ for f in *\ *; do mv "$f" "${f// /_}"; done

$ ls
file_1  file_2
 
so maybe i'm on to something. i typed pwd > list and then i typed cat list which gave me /home/me on the screen. so i least stored the filename i want in list, now i just have to get it out and change file_name to /home/me. once i do that i can repeat the process in a for loop.
 
some one hit me with a brick!!!!!!

dos2unix wrote the following: (see above)

# Loop through each directory
for dir in ./bradbury/*/; do

after repeated brain farts, i finally decided to change

for dir in ./bradbury/*/; do

to

for dir in .///; do

and it worked perfectly through all the directories.

so i'm slow, but i got there and thank you so much dos2unix, whoever you are!!
 


Members online


Top