Welcome to the new 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

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

    [email protected]:~/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
  • gavin2ugavin2u Posts: 3
    thanks for ur reply, :laugh: i now find another two solutions as follows,

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

    solution2:
    rename '' pre- *
    
  • MtalamuMtalamu Posts: 2
    i would like to help you but not here this is my mail [email protected]
  • rename file pre-file *
Sign In or Register to comment.