Rename the file in linux command

How to Rename Files in Linux

A command-line terminal is an essential tool for administrating Linux servers. It provides Linux users some of the best productivity tools while saving your machine’s resources.

To effectively use the potential of Linux, you will need to have strong knowledge of the fundamentals – simple commands, like renaming existing files and folders. In this tutorial, you’ll learn how to rename folders in Linux.

How to Rename Files in Linux with the mv Command

Shortened from “move,” the mv command is one of the easiest commands to use. It can do two basic but essential tasks when handling files on Linux. One is moving files from one location to another, and the other is renaming one or more files through the terminal.

First, let’s see how renaming files with mv works on Linux.

To begin, we access our server through the command line using SSH. If you are unsure about SSH and would like to learn more, here’s a helpful tutorial.

To access our server, type the following into your terminal:

If we are using a local computer, instead of a server, then we will have to open the terminal from the main menu.

Afterward, it is important to know how the mv command works. To do this, we run the following:

As we can see in the previous image, the basic use of the mv command is as follows:

Here are some of the most popular mv options:

  • -f – shows no message before overwriting a file.
  • -i – displays warning messages before overwriting a file.
  • -u – only move a file if it is new or if it does not exist in the destination.
  • -v – show what the command does.

And the parameters are:

[SOURCE] – the source destination of the file

[DESTINATION] – the destination directory.

Rename File on Linux Using the mv Command

If we want to rename a file, we can do it like this:

Assuming we are located in the directory, and there is a file called file1.txt, and we want to change the name to file2.txt. We will need to type the following:

As simple as that. However, if you are not in the directory, you will need to type a bit more. For example:

Rename Multiple Files With the mv Command

The mv command can only rename one file, but it can be used with other commands to rename multiple files.

Let’s take the commands, find, for, or while loops and renaming multiple files.

For example, when trying to change all files in your current directory from .txt extension to .pdf extension, you will use the following command:

This will create a loop (for) looking through the list of files with the extension .txt. It will then replace each .txt extension with .pdf. Finally, it will end the loop (done).

If you want more advanced features, you’ll need to use the rename command, we’re about to cover.

Rename Files on Linux Using the Rename Command

With the rename command, you will have a bit more control. Many Linux configurations include it by default. But, if you don’t have it installed, you can do it in just a minute with a simple command.

In the case of Debian, Ubuntu, Linux Mint, and derivatives:

On the other hand, if you are using CentOS 7 or RHEL:

And, if you are using Arch Linux:

Now, we can start using the rename command. In general, the basic syntax of the rename command looks like this:

It may seem complex at first, but it’s a lot simpler than it might seem.

In this example, we will create a new folder called filetorename, and using the touch command, we will create 5 files.

With the last ls command, you can view the files that you created.

If we want to rename a single file called file1.txt, the sentence would be like this:

If we wanted to change the extension to all files, for example, to .php. We could do it this way:

We can also specify another directory where the files you want to rename are.

We’d like to mention that rename uses a regular expression of Perl, meaning this command has extensive possibilities.

Finally, it is a good idea to check all the command options. You can view them in the terminal by executing:

Some common examples of how to use the rename command are:

    • Convert filenames to uppercase:
    • Convert filenames to lowercase:
    • Replace spaces in filenames with underscores:

Remove Rename Command

If you no longer wish to have rename installed on your system, remove it using the software manager. Or from the terminal.

Читайте также:  Linux device driver books

For Debian, Ubuntu, Linux Mint and derivatives:

And for CentOS and RHEL:

That’s it, rename is removed from your Linux machine.

Conclusion

Renaming files in Linux using the terminal is a simple and practical task but sometimes very important. Knowing how to do it is something every server manager should know.

As we have seen, there are two commands that can do it. One is simpler than the other, but both accomplish the task.

We encourage you to continue researching these commands and improving the quality of your everyday workflow.

Edward is an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward’s goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Источник

UNIX Command To Rename A File

H ow do I rename a file in UNIX using command line options?

This quick UNIX tutorial explains mv command to rename file, which is a Unix command that renames one or more files or directories. The original filename or directory name is no longer accessible. Write permission is required on all directories and files being modified. Use the mv command to:

Tutorial details
Difficulty level Easy
Root privileges No
Requirements mv command to move files on Unix/macOS/*BSD
Est. reading time 1 minute
  • Moves a file (i.e. gives it a different name).
  • Rename a file.

mv command syntax to rename a file on Unix

You need to use the mv command to rename a file as follows on Unix, macOS and *BSD family of operating systems:

Examples

Open the terminal (bash shell prompt) and type the following command to list file names:

In this example, rename a file called data.txt to letters.txt, enter:

File is renamed so the following command will display an error on screen:

The following would rename a file called foo to bar, while keeping it in the current directory:

  • 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

Rename a directory/folder Unix command

The following would rename a directory (folder) called dir1 to dir2, while keeping it in the current directory:

Move a file/folder to an existing directory

The following would move a file named resume.txt, without changing its name, to an existing subdirectory called /home/nixcraft/Documents/

How to rename a file on Unix and display verbose output

Pass the -v option to the mv command to show files after they are moved. For example:

Moving or renaming files with confirmation on Unix

We need to pass the -i option to the mv. It causes the mv command to write a prompt to screen before moving a file that would overwrite an existing file. If the response from the keyboard input begins with the character ‘y’ or ‘Y’, then move attempted. It is a safety feature:
mv -i old_name new_name
mv -i -v sales old_sales
We can also tell the mv command, not to overwrite an existing file by passing the -n :

You will see message as follows:

Here is a sample session from my FreeBSD Unix server:

A note about renaming multiple files

You need to use additional commands as mv cannot do that for you. Please see the following tutorials:

Summing up

The mv command may change or add an option as per Unix vendor and version. Hence read the man pages. Type the following man command to read mv command man page:
man mv

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

Источник

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 a File in Linux With Simple Command Line Options

Developed in the early 1990s, Linux has remained a popular operating system for both personal use and commercial servers. Today, there are over 600 Linux distributions (versions) available, though only a handful of them remain in common use. Based on Unix, the Linux system is prized for its portability, reliability, and stability. There are many individuals (primarily industry professionals) who prefer using Linux over any other operating system.

That said, Linux does require some advanced knowledge to use. While today most Linux distributions have a GUI (graphical user interface), many don’t. Working with Linux requires prior knowledge because most commands are given through a command-line text.

If you’ve worked in DOS or have programmed in C, you will know how to use a command line. But with Linux, it’s all about knowing individual commands — including the commands to rename and move files.

Today, we’re going to take a look at how to rename a file in Linux. We’ll look at the rename tools that Linux offers, renaming a single file, how to rename files and directories, and renaming multiple files through batch rename.

Last Updated September 2021

Linux Administration (Ubuntu and CentOS) for Beginners. Get the Linux skills to boost your career and get ahead. | By Andrei Dumitrescu, Crystal Mind Academy

How to rename a single file in Linux

If we need to rename a single file in Linux, we have two options: we can create a copy of the file with a new name (and delete the old one) or we can rename the file by moving it (with the MV command).

Renaming a file by copying and deleting it

Linux users copy a file by using the “cp” command. When you copy a file, you give the source files and rename the files.

As an example, if we were to rename “august.png” to “september.png”:

This will create a copy of the file with a new name. But now you’ll have two copies. You’ll resolve this by deleting the old file. You delete with “rm.”

You may be wondering why this is so cumbersome compared to Windows, where you’d just rename a file. Linux has a lot of power, but it manages that power by putting it in the user’s hands. Linux users will perform more operations incrementally, but they have complete control over how those operations are performed.

This gives way to another issue: We could have named that second file september.jpg or september.doc. And then the file would no longer work! Linux provides a lot of power and a lot of freedom.

Renaming a file by moving it

You may be relieved to know that there is an easier method. But it’s a counterintuitive one. You can rename a file by “moving it” to a new file name.

This actually does what you would think of as a “rename” command and in a single step. But it should be noted that it’s also the general “move” command. So, you are moving the file at the same time. If you move it to a different directory, it will be moved as well as renamed.

As with the cp command, you have to be aware of file extensions. For example, say we introduced a typo and typed:

That action would break our file.

File extensions and renaming

For the most part, Windows protects you from changing the extension of a file. When you click a file in Windows to rename it, it only highlights the actual name, not the extension. You need to do a little work to change the extension of a file.

Linux doesn’t protect you. The extension is part of the file name and can be changed at any time. For that reason, you should be very aware of file extensions. .JPEG is not necessarily the same as .JPG, even if they both denote “JPG” files.

If a file is no longer working after you’ve moved, renamed, or copied it, it’s very likely that it’s because of an error with the file extension.

How to rename multiple files in Linux

What if you wanted to rename multiple files in Linux? This is where we start to see the advantages of using a command-line system.

Let’s say we did make a mistake. We changed everything to JPGs, but instead, they need to be PNGs. We can do this like so:

What this does is it uses a wildcard operator (*) to select any file that ends with JPG and changes that to a PNG. We had better know what we’re doing, though, because if there were any actual JPGs, they’re broken now.

Wildcards are one of the simplest forms of regular expressions. If we had wanted to rename the files in Windows, we would have had to click on them one by one. To rename the files in Linux, we can just use an expression to highlight them all. For that reason, power users in Linux can perform operations very quickly.

Renaming a directory in Linux

One of the quirks of Linux and Unix is that not much changes whether you’re using files or directories. A directory is more or less a blank file that contains other files. If we had a directory called “2021” and we wanted to rename it to “2022,” we would do this:

It’s as simple as that. Existing directories are treated like existing files that do not have a file extension by the MV command.

Rename a file while moving it in Linux

Earlier, we noted that we are using the MV command to rename files and that we could also move the file to a different directory if we so chose.

Now let’s say we want to rename august.png to september.png and also move it into the 2022 directory.

As again, both the directory and the file name (and its extension) are just pointers to where data is. This remains consistent throughout Linux.

Using flags (options) for your operations

Linux makes it possible to use something called “flags,” or options, with its commands. This controls how the command is performed.

Two commands you may want to know are -i and -v.

  • -i. This is the interactive option, and it makes it possible for you to “interrupt” the sequence to confirm actions.
  • -v. This is the verbose option, and it makes it possible for you to see everything that happens with the command as it happens.

Both of these option flags can be helpful when using the MV command in Linux.

Using the interactive renaming function in Linux

We’ve noted a couple of times that it’s possible to make large, potentially irrevocable mistakes in Linux. But there’s also a way to get around this. If you add the “-i” option with the MV command, you’ll be prompted before anything is done.

Let’s say we type:

But the problem is, we’ve already created september.png. It already exists! The “interactive” function will let us know we’re about to overwrite an existing file. We can then type “Y” or “N” to either confirm the action or deny it.

If we knew that it was going to be overwritten anyway, we may be fine with it. But otherwise, we might want to know that there’s another file in danger.

Using the verbose option to track what’s happening

In addition to interactively interrupting your commands, you may also want to just know what operations are occurring. With Linux, you can always use the -v or -verbose option.

This will tell you exactly what it has done to the source files and the new files. And you can also:

This will tell you about all the operations and also allow you to interrupt them. Whenever you’re using a new command, consider using -v for verbose. The -v flag should work for nearly any command.

Mass moving and renaming files in Linux

Sometimes you need to move and rename a large number of files in Linux. Consider that you might have an entire directory of files that have to be either changed or moved. This could happen if you installed something into the wrong directory.

For this, Linux has another command altogether: MMV. MMV can be used to move, link, and append files. It’s a non-destructive method of doing so; it will try to identify any potential errors or collisions.

This is particularly valuable because you don’t want to accidentally ruin a lot of files at once.

The above would be another way of changing all PNGs to JPGs, in the event that for instance, a large number of files had the wrong extension. This could happen if you had copied the original file over incorrectly.

But you’ll notice that we could have done this with the MV command, too. The only difference is that MMV is a more careful way of doing this.

If you wanted to mass copy a lot of files, you could even copy over the entire directory:

And that would copy 2021 to 2022 while still leaving 2021. Copying is less dangerous than moving because copying doesn’t inherently delete the original files. Moving does. This is one reason why when renaming a file, we looked at the CP function before the MV function — there is less that can go wrong with the operation because it’s not inherently destructive.

Installing rename in Linux

At this point, you might be thinking “Why can’t I just rename?” Actually, you can. There is a “rename” command in Linux, but you would need to install it.

On your administrative account for Ubuntu or Debian:

For Fedora or CentOS:

(If you have a different distribution, look up your installation syntax.)

This will install the “rename” package. After you install rename, you’ll be able to use complex regular expressions to rename your files.

Complex regular expressions are a topic in and of themselves. We saw that * can be used as a wildcard. But with regular expressions, we can also select things like “any file that starts with letters but ends with numbers” or “any file that has 59 in it.” Regular expressions can be used to identify email addresses or other patterns — they are very powerful but take some practice.

The rename command will not operate much differently from MV, MVV, or even CP. The difference is that you’ll have a dedicated function called “rename” that you can use. There are many function packages for different Linux distributions that you can install. If you’re trying to complete an operation and you can’t find it in vanilla Linux, consider looking for a package.

Getting started with Linux

Linux has an internal manual ($ man) that can be used to look up commands. To look up more information about the move command in Linux, you can also always type in:

Or look up the $ man entries for anything else.

Linux requires a lot of memorization. For the most part, you just need to know what’s possible in the system. Once you know what’s possible, you should be able to reference the manual to figure out how.

But you should always remember that Linux does what it’s told — there are fewer safeguards with the Linux system. While Windows might alert you that you’re overwriting a file, Linux isn’t likely to alert you unless you specifically ask that it does. If you’re just learning Linux now, a great way to do so is through a Linux bootcamp or workshop. There are so many distributions of Linux, you’ll also want to decide which distribution you want to use first. Check out our guide to finding the best Linux distro!

Page Last Updated: September 2021

Источник

Читайте также:  Что будет за использование нелицензионного windows
Оцените статью