Renaming files is one of the most basic operation you often need to do on any computer. So, here's how to rename file linux with command line for beginners.
Certainly using the command line is not the easiest way to rename files in linux, a graphical file manager is much convenient.
But when you've to rename hundreds file or using a graphical file manager is not an option(over SSH), then command line is your best friend.
So, in this quick tip, we'll learn how to easily rename files with the mv
, raname
and other few commands.
Contents
Rename files in Linux with mv command
The mv
command is basically for moving and renaming files/directories with command line, one of the easiest to learn.
Here's the typical way to rename a file with mv
. Use the ls
command to get an over view of the files you want to rename.
mv old_file_name new_file_name
Also don't forget to add the proper file extension, however you always don't have to worry about file extensions in Linux.
Some useful arguments to use with the mv command are,
-n
, don't overwrite an existing file.-i
, prompt before overwrite-f
, force overwrite, no prompt-u
, rename or move only when source file is newer than destination file, or missing.
Note: If you're getting the Permission denied error, then you've to use the sudo
command with mv, example below.
sudo mv old_file_name new_file_name
Here's some more realistic examples, hope this helps you to understand better.
Let's assume you've a file awesome_song_by_a_great_singer.mp3, it's a long name, so you might want to rename it to something shorter.
mv awesome_song_by_a_great_singer.mp3 awesome_song.mp3
You also have to take care of proper path if the source and destination of the file is not same, or not located in your current working directory(pwd).
mv /home/user/Documents/resume.pdf /home/user/Desktop/my_resume.pdf
The above command renames the file resume.pdf located under the Documents folder to my_resume.pdf and moves it to the Desktop at the same time.
Rename file Linux with the rename command
Using the rename
command is a bit complicated, not suitable for absolute beginners. But it's worthy to learn if you've to rename lots of file.
The rename
command is a perl script, supports perl's regular expressions, below the typical way to rename files with it.
rename 's/string1/string2' list_of_files
The string1 and string2 could be any valid strings, any random name, depends on the files you want to rename.
Few useful options to use with rename
command,
-v
, be verbose, prints successfully renamed files.-n
, like a dry run, print names of files to be renamed, but does nothing.-f
, overwrites existing files if used.-h
, prints the help message.
One of the best use of rename command could be managing lots of photos, renaming and shorting them.
Let's assume you've many photos on a specific data, and want to rename something memorable, example below.
cd ~/cam_snaps rename 's/20161225/christmas/i' *.jpg
The above command will rename all jpg files containing 20161225 at anywhere in their name with christmas in place of the date.
The i modifier used to ignore case-insensitive characters, use the -n
argument if you want to have a look before actually renaming the files. You could also use the -v
argument before to see which files are being renamed.
Synopsis - rename file linux
So, that's all about how you can renames files with command line easily. Now the round-up of the comments used to perform our renaming task.
- Rename a specific file in current directory,
mv old_file_name new_file_name
- Use sudo when getting Permission denied,
sudo mv old_file_name new_file_name
- Rename multiple files at once with
rename 's/string1/string2' list_of_files
- Dry run renaming multiple files,
rename -n 's/string1/string2' list_of_files
It's the second installment of the linux for beginners series, stay tuned, read the first one how to remove directory.
If you’ve any question or suggestion, please feel free to ask ! And don’t forget to share this tutorial with your linux loving friends.
Leave a Reply