11

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?

Anthon
  • 79,293

3 Answers3

16

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
Sebastian
  • 8,817
  • 4
  • 40
  • 49
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.

JRFerguson
  • 14,740
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
cuonglm
  • 153,898