Welcome to the Linux Foundation Forum!

rename a range of files using one-line command

Hi, all

I have a range of files in a certain dir, like this,

$ touch file[123456]

$ ls

file1 file2 file3 file4 file5 file6

now, how could rename them, and add a prefix to each file? for example, add a 'pre-' prefix, as following,

pre-file1

pre-file2

pre-file3

...

pre-file6

Thanks in advance. :cheer:

Regards,

gavin

Comments

  • amnesia
    amnesia Posts: 60
    for files in filename* ; do new="prefix_$files" ; echo "$files $new" ; done

    you don't even need the new="prefix_files";

    you can
    for files in filename* ; do mv $files pre_$files ; done

    and make it even shorter

    root@msu:~/haha# for files in haha* ; do new="prefix_$files" ; echo "$files $new" ; done
    haha1 prefix_haha1
    haha2 prefix_haha2
    haha3 prefix_haha3
    haha4 prefix_haha4
    haha5 prefix_haha5
    haha6 prefix_haha6
    haha7 prefix_haha7
  • gavin2u
    gavin2u Posts: 3
    thanks for ur reply, :laugh: i now find another two solutions as follows,

    solution1:
    ls  | xargs -I{} mv {} pre_{}
    

    solution2:
    rename '' pre- *
    
  • Mtalamu
    Mtalamu Posts: 2
    i would like to help you but not here this is my mail mtalamu8@hotmail.com
  • rename file pre-file *

Categories

Upcoming Training