How To Rename Files In Linux

rename files linux

One of the most basic things that any user does in Linux is rename files. You can rename files in Linux using a file manager, but it isn’t very interesting. In this article, you will learn renaming a file or rename multiple files in Linux through the terminal.

Moving Files

I have a file called “file” in my directory, and I am going to change its name to “archivo”, using the command ‘mv’:
mv command rename files

The ‘mv’ command is used to move the files, but you can move a file to the same location using a different name.
$ mv file new_name
Also, you can move a file to a different location and different name.
rename files in linux

Also, you can rename directories using the same syntax.
rename directory in linux

You can get verbose output using the ‘v’ option.
verbose rename linux files

If you rename a file using the name of an existing file, the ‘mv’ command will overwrite the existing file, but if you don’t want it, just pass the ‘i’ option, and it will prompt before overwriting file.
overwrite files in linux

If you don’t want to use the ‘i’ option, you should make a backup of the existing file.
i option linux rename files

The backup is calledfile1~

You can add a suffix to the backup name, just type the following syntax.

$ mv -b -S “suffix” file_name existing_file_name
rename files suffix

Options List

–backup
make a backup of each existing destination file
-f –force
do not prompt before overwriting
i –interactive
prompt before overwrite
-n
do not overwrite an existing file
-u
move only when the SOURCE file is newer than the destination file or when the destination file is missing

–backup options
none, off
never make backups (even if –backup is given)
numbered, t
make numbered backups
​existing, nil
numbered if numbered backups exist, simple otherwise
simple, never
always make simple backups

If you want to know more detailed information about ‘mv’ just type: $ man mv  at your terminal.

Rename multiple files in Linux using ‘rename’

Synopsis
rename [ -h|-m|-V ] [ -v ] [ -n ] [ -f ] [ -e|-E perlexpr]*|perlexpr [ files ]
This command is slightly more advanced than mv because it requires the knowledge of, or at least a basic familiarity with regular expressions, “rename” renames the filenames (multiple files) supplied according to the rule specified as the first argument.  The perlexpr argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input.

Options

-v -verbose
Verbose: print names of files successfully renamed.
-n -nono
No action: print names of files to be renamed, but don’t rename.
-f -force
Overwrite: allow existing files to be over-written.
-h -help
Help: print SYNOPSIS and OPTIONS.
-V -version
Version: show version number.
-e
Expression: code to act on files name.
May be repeated to build up code (like “perl -e”).  If no -e, the first argument is used as code.
E
​Statement: code to act on files name, as -e but terminated by ‘;’.

Examples:

I have two files: file1.c and file2.c, and I want to change the extension of file1 to .txt, so I’m going to use the following command:

$ rename ‘s/\.c/\.txt/’ file1.c
rename multiple files in linux

I made a new file named file3.c, and I want to change the extension of files 2 and 3, so I’m going to use the following command:
$ rename ‘s/\.c/\.txt/’ *
rename multiple files linux

Now I have four files:awesomefile, greatfile, bigfile, linuxfile; and I want to change a specific part of their names, i.e. change “file” to “document”, the final result of this should be:

awesomedocument, greatdocument, bigdocument and linuxdocument

So I’m going to use the following command:

$ rename ‘s/file/document/’ *
touch create new file linux

Also, I want to translate lowercase to uppercase, so I’m going to use the following command:
$ rename ‘y/a-z/A-Z/’ *
rename files to uppercase linux

If you want to know more detailed information about ‘mv’ just type: $ man rename  at your terminal.

Conclusion

You can rename files in Linux using a file manager like dolphin or Nautilus but I think renaming files in Linux using the terminal is more fun, also I think that the ‘rename’ command is more powerful than the file manager. You can choose your own way, but I prefer always the terminal.

Leave A Reply

Your email address will not be published.

2 Comments
  1. Vijay Kumar says

    You should provide full path

  2. Veeresh says

    Hi ,
    In Linux env.
    I have files in one folder.
    i want to add GMI at the start of each filename.
    i want to do for all files at one time in Linux.
    Please suggest me command.
    its for end of file
    find . -type f -name ‘file*’ -exec mv {} {}_renamed \;
    i want for start of file.
    when i use
    Test]$ touch dummy
    Test]$ find -type f
    ./dummy
    it gives file with full path.
    find . -type f -name ‘file*’ -exec mv {} GMI_{} \;
    i want like this, but it wont work. please assist
    Regards,
    Veeresh

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More