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?

1
  • 3
    ls | paste - /the/text/file | xargs -n2 mv Aug 28, 2014 at 18:48

3 Answers 3

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
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

You must log in to answer this question.

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