Change file name linux

How To: Rename A File In Bash

Rename a file in bash using mv

We need to give SOURCE file to DESTINATION file using the following mv command syntax:

Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY

Task: Rename A File Called /tmp/foo To /tmp/bar

Type the following command (open terminal and issue the following commands):

Task: Rename A Directory Called offfer To offers

Type the following command:

Task: Prompt Before Overwrite

The -i option is interactively file processing option. You get an error message before moving a file that would overwrite an existing file. If the response from the user begins with the character y or Y, the move / rename is attempted.

The -u Option

The -u option move only when the SOURCE file is newer than the destination file or when the destination file is missing:

The -v Option

The -v option explain what is being done:

Task: Rename multiple files

Use rename command renames multiple files. For example rename all *.perl file as *.pl, enter:

See howto rename multiple files at a shell prompt for further details.

Summary of all mv command options

To rename a file in bash we use mv command:

  • -v : Verbose option. In other words, display the progress of the files as they are being moved or renamed in a bash shell
  • -i : Prompt before overwriting files
  • -u : Move only when the SOURCE file is newer than the destination file or when the destination file is missing in a bash shell
  • -f : Do not prompt before overwriting files

To see list of all mv command options, type:
man mv
mv —help
Sample outputs:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Conclusion

To rename a file or directory in bash, use the mv command. The third word on the mv command line must end in the new filename. Hence, the syntax is a follows to renames the file cakeday.png to birthday.png:
mv cakeday.png birthday.png

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • 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 • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
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

Comments on this entry are closed.

typo here:
ename .perl .pl *.perl
instead of
rename

I do not see any typo here.

Ah, never mind. I see last updated date at the top of the page. You can delete this comment.

Источник

Linux Rename File Command

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:

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:

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:

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.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to Rename Files in Linux

Renaming files in Linux is one of the easiest tasks that you can accomplish straight from the command line.

There are quite a few ways you can achieve this and in this guide, I will take you through how you can rename files in Linux.

1) Rename files using mv Command

The mv command is one of the easiest commands you can use to rename files in Linux. The syntax is quite straightforward.

Syntax

Example

I have created a new folder in my home directory called files. In the folder, I have a document called file1.txt.

To rename the ‘file1.txt’ to ‘file2.txt’, the command will be

You can verify the results using the ls command

To get verbose output, append the -v flag

Output

If the file is located in a file path e.g. / home/winnie/file1.txt, the syntax will be

NOTE:
If you specify a different file path, the mv command will simply move it to a different location without renaming it.

2) Rename multiple files using the rename command

The rename command can also be used to rename multiple files from one format to another. The syntax is

For example, I have 5 files- file1.txt, file2.txt, file3.txt, file4.txt, and file5.txt

To rename them all files (*.txt) to pdf (*.pdf) run

The wildcard symbol means that all files will be affected.

To change filenames from lowercase to uppercase run

In the above case, all lowercase png files will be converted to Uppercase

To view the man page of rename run

3) Using Metamorphose2 GUI tool

For GUI lovers, Metamorphose is a handy and powerful tool for renaming operations. Metamorphose is a free and open source cross-platform file and folder renamer, available for both Linux and Windows.

First off, head out to Metamorphose website and select your distribution’s installer. A couple of installer options are available e.g. Debian files for Ubuntu and Debian and RPM for RedHat based distros such as CentOS.

Once downloaded use the dpkg command to install

After successful installation of Metamorphose2, launch it on a terminal, by running

Choose your preferred language and click ‘Ok’ button

Click on the renamer tab. A Window like the one below will be displayed. On the left pane is a list of actions that you can take when renaming. in the middle section, these actions are further explained in detail.

Next, click on ‘Picker‘ tab and click on the folder containing the files you want to rename. Highlight the files.

Thereafter, click on the ‘Renamer‘ tab again and this time define the renaming rules. In the example below, I’m replacing ‘file’ with ‘doc’.

The bottom section gives you a preview of how your files will be renamed.

Once satisfied, click on the ‘Go‘ button

3) Using Pyrenamer GUI tool

Pyrenamer is yet another handy tool you can use to rename a batch of files simultaneously in Linux. To install in Ubuntu run

To launch Pyrenamer run

Once launched, scroll down to your directory and select the files you want to rename. Next, choose the actions you wish to apply. In my case, I will replace the extension txt with pdf.

To effect the changes, I will finally click on ‘Rename

Conclusion

In this tutorial, we have touched on various ways you can rename files (both individual files and batches of files) on the terminal and using GUI tools. Found nomino a batch rename utility for developers.

Give it a try and let us know how it went! Thanks and keep it locked for more informative tutorials!

Источник

How to Easily Rename Files in Linux

Renaming files is not a particularly advanced operation; as long as it’s done on a small number of files, it usually doesn’t require special tools. However, when there’s an entire folder of photos from last year’s vacation waiting to be renamed, it may be wise to consider some timesaving tricks or apps.

There are two general approaches when you rename files in Linux: via the command-line interface or a standalone application. Linux users already know how powerful the CLI can be, so it shouldn’t be surprising that there are several commands for file renaming.

Also read: How to Copy and Paste Text, Files and Folders in Linux Terminal

Renaming Files with “mv” Command

A simple way to rename files in Linux is with the mv command (shortened from “move”). Its primary purpose is moving files and folders, but it can also rename them, since the act of renaming a file is interpreted by the filesystem as moving it from one name to another.

The following syntax is used to rename files with mv:

“filename1.ext” is the original name of the file, and “filename2.ext” is the new name.

The same pattern works for renaming folders. If the files are not located in the currently active folder, their full path has to be specified:

Note that the mv command requires write permission for the folder containing the files. In the case of system files and folders, the user needs to obtain root permissions to rename files by prepending mv with sudo . An extra layer of protection is provided by the -i (interactive) option, which asks the user to confirm the file rename before it’s actually applied.

There’s also the -v (verbose) option, which lists all changes that have been made by mv . Options are written after mv but before the filenames.

Using the “rename” Command

This command to rename files in Linux is slightly more advanced than mv because it requires the knowledge of, or at least a basic familiarity with, regular expressions. That may sound scary, but don’t give up on rename just yet – it can be used for plain batch renaming simply by following tutorials like this one.

In many distributions of Linux, the rename command may not be available by default. If you are unable to use it in yours, you will have to install it.

In Debian-based Linux, type this in your terminal:

In Arch-based Linux:

The rename syntax looks like this:

The letter “s” stands for “substitute,” and it’s the main part of the regular expression. Single quotes around it are obligatory. Available options are:

  • -v (verbose: prints the list of renamed files along with their new names)
  • -n (“no action:” a test mode or simulation which only shows the files that will be changed without touching them)
  • -f (a forced overwrite of the original files)

The rename command also accepts wildcards to rename multiple files of the same type, and it works on file extensions as well. For example, this would change all files with the extension .jpeg to .jpg:

The wildcard symbol (*) means that all files in the folder will be affected.

The regular expression also has its own options (modifiers): “g” (global: affects all occurrences of the expression) and “i” (performs case-insensitive substitution). They are written at the end of the expression just before the closing single quote and can be combined:

This would apply to all .jpg files that contain “DSC,” “dSC,” and “dsc” – change that part of the filename to “photo.” However, because of the “-n” option, the command wouldn’t actually rename the files but just print them in the console window.

Substitution is not the only thing this regular expression can do. There’s also translation – marked by the letter “y” – which can transform the filenames on a more complex level. It is most often used to change the filename case:

This would change the names of all .jpg files from lowercase to uppercase. To do it in the reverse, just switch the “oldname” and “newname” parts of the regular expression.

Using the rename command boils down to mixing a few basic patterns to achieve the desired result. Thanks to the -n option, the users will never have to put their files at risk (or their nerves at stake) since it offers a safe and useful preview of what the renamed files will look like.

Also read: How to Fix the “No Space Left on Device” Error on Linux

Métamorphose2

Métamorphose is a cross-platform file and folder mass renamer. For those who prefer a GUI tool for carrying out renaming operations, Métamorphose is quite a powerful tool to use. It is available for both Windows and Linux.

To get started, go to its website and download the installer file for your distro. It provides a deb file for Debian-based distro and RPM package for Fedora, Mandriva and SUSE.

Once you run the app, it will be the first “Picker” tab. This is where you select the directory that contains the files you want to batch rename. Do note that it only supports one directory per renaming operation, though it can recurse through child folders within the selected directory.

After selecting the directory, click on the “Renamer” tab. From here you can decide on the renaming rules. On the left pane you can select the action you want to perform. For example, “insert” will insert terms to the name, while “length” allows you to trim the file name to a number of characters. There are also the “move text,” “replace” and “modify” options you can choose. As you can see, it provides extensive options for you to rename your files.

Lastly, just click the “Go” button at the top of the bottom pane to run the renaming action. If you find any error after renaming, there is an Undo option to revert all the changes.

Also read: How to Use the lp Command in Linux to Print Files From Terminal

pyRenamer – The Easy Way Out

Finally, the solution that all the anti-console users have been waiting for: a desktop application where everything can be done with a simple click of the mouse. pyRenamer is a crazy-powerful file renaming tool written in Python, which can be found here. Users of Ubuntu and its derivatives can install pyRenamer from the repositories using the command:

The interface consists of four parts:

  1. Tree-view file browser for selecting files and folders
  2. Central preview pane showing the filenames before and after renaming
  3. Tabbed control area for choosing the renaming criteria
  4. Options sidebar

pyRenamer can remove accents and duplicate symbols from filenames; replace any string of text with another; 20names to uppercase, lowercase or sentence case; and automatically insert or remove spaces and underscores. Beginners will love it because all of this can simply be selected in the tabbed area, previewed in the main area and confirmed by clicking Rename. If they wish to experiment with patterns, pyRenamer provides a cheatsheet to make it easier.

Advanced users will appreciate pyRenamer’s ability to rename multimedia files by reading from their metadata. It’s also possible to manually rename a single file when a batch rename is unnecessary.

In essence, pyRenamer wraps the functionality of the mv and remove commands into a user-friendly GUI. It’s a great choice for those who aren’t confident in their CLI skills.

Now that you know how to rename files in Linux, you may want to learn how to recursively change file permissions. Read on to learn how to diagnose a slow-running Linux computer or perhaps make use of the Hosts file to block/access websites.

Miguel has been a business growth and technology expert for more than a decade and has written software for even longer. From his little castle in Romania, he presents cold and analytical perspectives to things that affect the tech world.

Источник

Читайте также:  Fira code font mac os
Оцените статью