You need to use the mv command. It is used to rename and move files and directories.
To rename multiple files use rename command or script provided by MySQL/MariaDB package as per regex.
How to rename a file Linux?
The syntax is as follows:
mv old-file-name new-file-name
mv [options] old-file-name new-file-name
mv file1 file2
Examples: Renaming files with mv Command
In this example, rename a file called resumezzz.pdf to resume.pdf. Open a command-line terminal (select Applications > Accessories > Terminal), and then type:
mv resumezzz.pdf resume.pdf
If resumezzz.pdf is located in /home/vivek/docs/files directory, type:
cd /home/vivek/docs/files
mv resumezzz.pdf resume.pdf
OR
mv /home/vivek/docs/files/resumezzz.pdf /home/vivek/docs/files/resume.pdf
Use the ls command to view files:
ls -l file1
ls -l file1 file2
ls -l /home/vivek/docs/files/*.pdf
ls -l *.pdf
Linux rename a file syntax
In short, to rename a file:
mv file1 file2
You can get verbose output i.e. mv command can explain what is being done using the following syntax:
mv -v file1 file2
Sample outputs:
`file1' -> `file2'
To make mv interactive pass the -i option. This option will prompt before overwriting file and recommended for new users:
mv -i file1 file2
Sample outputs:
mv: overwrite `file2'? y
How to use rename command to renames multiple files
The syntax is:
rename 's/old/new/' files
rename [options] 's/old/new/' files
For example, rename all perl files (*.perl) to *.pl, enter:
rename 's/perl/pl/' *.perl
OR
rename -v 's/perl/pl/' *.perl
Sample outputs:
'bar.perl' renamed to 'bar.pl' 'foo.perl' renamed to 'foo.pl' 'input.perl' renamed to 'input.pl' 'output.perl' renamed to 'output.pl' 'test.perl' renamed to 'test.pl'
The above command changed all files with the extension .perl to .pl. See “Howto Rename Multiple Files At a Shell Prompt In Linux or Unix” for more info.
Detailed information about mv command
You can also view the manual page on mv using the following command:
man mv
OR
info mv
OR
man rename
🐧 15 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Thank you very much. It worked
While the move command will work, there is actually a rename command:
rename
Which in this case would be:
rename resumezzz resume resumezzz.pdf
For those of us arriving because we googled the syntax for the “rename” command in linux, this is annoying.
thank you sir… :)
“ls -l ” is this commend applicable for listout all..
Thanks it’s working
rename texttoreplace textreplacement listoffiles
If I want to remove some text I use just use “”
rename texttoremove “” *
“rename texttoreplace textreplacement listoffiles
If I want to remove some text I use just use “”
Thank you!!! First post that clearly showed what each argument represented. It was not as obvious in the “foo foo0 foo?” example in the man pages. I think it would have been better to give an example like yours, or something like “foo bar foo?”.
Just to randomly add a mv command I like and find handy…
mv test{,.OLD}
Can do some fun little combo’s with that.
Thank you for this guide. I’m a windows user and so I’m not too familiar with linux commands, but I’m using git for windows which allows for unix commands, so I wanted to learn. Thanks you.
Can i move 2files together into one file ?
@ashish, you can use the cat command for that:
cat file1 file2 > file1and2
thank you for sharing, vivek.
This just deleted my file.
Hello,
I am trying to rename all files in a directory using mv but it isn’t working!
My code is:
id= ’29 10 15′
for zzvar in ${id} ; do
mv /media/storage/DTI/vgcross_sj${var}*
/media/storage/DTI/vgcross_SJ${var}*
done
even when I take the loop out and just try the command with one subject, I get the error: mv: target:/media/storage/DTI/vgcross_SJ is not a directory.
Obviously it’s not a directory, because that’s what I’m trying to rename the file as!
Do you have any ideas as to why it’s not working?
Thank you very much!