7

I have a list of files in a folder, which I will like to rename according to a textfile. For example:

These are the 5 files in the folder.

101_T1.nii
107_T1.nii 
109_T1.nii
118_T1.nii
120_T1.nii

I will like to have them rename using a text file containing a list of new filenames in the same order, without the extension .nii:

n01
n02
n03
n04
n05

How may I go about doing so?

  • 2
    ls | paste - /the/text/file | xargs -n2 mv – Stéphane Chazelas Aug 28 '14 at 18:48
7

one liner, this command reads the 'list' txt and parses for each line a file.

for file in *.nii; do read line;  mv -v "${file}" "${line}";  done < list
3

You could do:

paste OLD NEW|while read OLD NEW;do mv ${OLD} ${NEW};done

...where the file named "OLD" contains the old filenames and the file named "NEW" has the new matching (1:1) names.

0

If your shell supports process substitution, try:

paste -d' ' <(ls *.nii) /path/to/textfile | xargs -n2 mv

or you can do it POSIXly:

ls *.nii | paste -d' ' - /path/to/textfile | xargs -n2 mv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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