2

I'm trying to rename a bunch of audio files. They are named "1 Audio Track.aiff" and so on. I have a .txt file with the old filenames and the new filenames separated by a tab. For example, the .txt file says: 1 Audio Track.aiff Blues in Orbit.

I'm running this code in the directory in which the files are:

 for oldname newname in $(cat filenames.txt); do
 mv "$oldname" "$newname"
 done

The .txt file is also in the same directory. However, when the code is ran, I get: No Such File or Directory. If I change the filename to 1, and then make a .txt file with the old filename, 1, and the new filename, let's say, Artur, it works. The problem appears to by the syntax in the .txt file, but I cannot get it to work with spaces; Terminal interprets 1 Audio Track Blues in Orbit as changing the file 1 to Audio, etc. I've tried encasing them in single & double quotes, as well as brackets; I even added backslashes to simulate the spaces; nothing works. Any ideas? I appreciate the help.

3
  • 2
    Never do for var in $(cat file).....
    – terdon
    Aug 12, 2023 at 16:33
  • FYI, if your .aiff files have all the appropriate metadata (in particular, the title), you can avoid the need for your tab-separated text file - you could write a script to extract the song title for each file using, say, exiftool or mediainfo, and then rename the file appropriately. See, for example: Shell script to read metadata of audio file
    – cas
    Aug 13, 2023 at 1:40
  • The example script there (using mediainfo) doesn't do any error checking, it needs to at least check that the title is non-empty and that the mv target filename doesn't already exist. I'd treat is a demo of the idea, not something safe to use. Also, there are existing programs that can batch rename audio/video/image files based on their metadata (e.g. exfalso for .mp3,.ogg,.flac, etc or rename-flac for .flac files) but I don't know if any work with .aiff files (which is my ignorance, not proof that they dont exist). Might be worth searching for such a tool before writing your own.
    – cas
    Aug 13, 2023 at 1:50

1 Answer 1

4

Maybe you can try something like:

while IFS=$'\t' read -r oldname newname 
do
 mv -- "$oldname" "$newname"
done < filenames.txt

In this way, you set a delimiter (IFS) to read from the file and ensure that bash does not split the line on spaces.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .