Reputation: 449
I have been looking all over for how to use the {} feature in unix filenames. For example, I have a directory that has subdirectories named as an ID#. Each subdirectory has a recorded audio file named as a specific test question ID#. The following command:
for file in */1-01*; do echo "$file"; done
will list the names of the audio files within each subdirectory of the current directory. Doing so gives me a list:
809043250/1-01-20131.wav
813777079/1-01-20131.wav
817786199/1-01-20131.wav
827832538/1-01-20131.wav
834820477/1-01-20131.wav
I want to rename each of the above .wav files as a different ID#, so it should end up like this:
809043250/5001.wav
813777079/5001.wav
817786199/5001.wav
827832538/5001.wav
834820477/5001.wav
So how do I use the ${file/firstOccurance/replaceWith}
notation if I want to use the mv
command to keep the person's ID# (809043250, 813777079, etc) and the first /
but strip off the 1-01-20131.wav
and tack on 5001.wav
?
I don't know how to search for this on google. Is it called regular expressions (i don't think so...)? Brace notation? filename patterns? Does anyone know of a good explanation of these?
Thanks!
Upvotes: 0
Views: 1052
Reputation: 753475
In bash
, one variation on the notation is called Brace Expansion. This generates file names.
The other variation on the notation is called Shell Parameter Expansion. Here, bash
provides a regex-ish substitute notation ${var/match/replace}
.
For your purposes, you need the paremeter expansion:
$ x="809043250/1-01-20131.wav
> 813777079/1-01-20131.wav
> 817786199/1-01-20131.wav
> 827832538/1-01-20131.wav
> 834820477/1-01-20131.wav"
$ for file in $x; do echo $file ${file/1-01-20131/5001}; done
809043250/1-01-20131.wav 809043250/5001.wav
813777079/1-01-20131.wav 813777079/5001.wav
817786199/1-01-20131.wav 817786199/5001.wav
827832538/1-01-20131.wav 827832538/5001.wav
834820477/1-01-20131.wav 834820477/5001.wav
$
Upvotes: 1