Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Lets say I have two folders:

ls /A/
    01 - IncorrectName.flac
    02 - otherincorrect.flac

ls /B/
    01 - CorectName.flac
    02 - Othercorrect.flac

How would I move the files from dir A to B, but using the filenames from dir B?

Could I be doing this automatically with $ mv /A/01 - IncorrectName.flac /B/01 - CorrectName.flac ?

The first 5 characters of all filenames are always "NN - "

share|improve this question
up vote 4 down vote accepted

I am not sure I understand what you want, but this could be it in bash:

#! /bin/bash
for file in "$1"/[0-9][0-9]*.flac ; do
    mv "$file" "$newname"*

Update: should work with command line arguments. The trick was the length of the string was not constant anymore, hence ${#2}.

share|improve this answer
I want to make use of the existing filenames from directory B while renaming/moving the files in directory A, matching a file in A to the name in B by means of the first 5 characters of the filenames. That is, the track number of these flac files. – Alasdair Dec 3 '11 at 0:16
Bash complains of "Bad substitution", my folder names have spaces, but moving them to just A and B has not had any effect, the problem remains. – Alasdair Dec 3 '11 at 0:28
Hi even with the typo replaced I get "Bad substitution" – Alasdair Dec 3 '11 at 0:38
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu) – Alasdair Dec 3 '11 at 0:38
Strange. Are there any other files in the directories? In such a case, you should use some safer wildcard instead of *, e.g. [0-9][0-9]*.flac. – choroba Dec 3 '11 at 0:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.