Home / Linux/Unix/BSD / Rename File Linux

Rename File Linux

Are you planning to rename files? Linux allows you to do that!

If you’re using a Linux system, you’ve probably already tried renaming most, or at least some, of your files. After all, it’s one of the primary tasks you can do!

You can either rename a file in Linux using its command-line terminal or a GUI file manager. It might sound complicated, but it’s not!

Today, we’ll teach you how renaming files on Linux is easy! We’ll teach you how to use the “mv” and “rename” commands.

In no time, you’ll be an expert on how to rename files in Linux and even directories!

How to Rename File in Linux using the “mv” Command

The "mv" command is short for the move command line.

It’s a command that allows you to rename files or move Linux files from one location to another. The syntax for using the “mv” command looks something like this:

mv [OPTIONS] source destination

The source can include one or more files, depending on what you want to rename or move. Directories and destination, on the other hand, refers to either a single file or directory.

We can sum it up as follows, so you’ll remember when to use the command to rename:

  • If you use the “source” command to rename multiple files, ensure that the “destination” command is a directory. When you do this, the multiple files you put on your “source” will be moved to the directory you indicated.
  • If you only placed a single file on your “source” command and the “destination” command is for an already existing directory, it will be moved there.
  • To quickly rename a file, you need to specify which file uses the “source” command and use the “destination” command for its target.

If you feel this concept feels particularly challenging, let’s take an example! First off, the syntax should be like this:

mv old-file-name new-file-name

mv [options] old-file-name new-file-name

mv file1.txt mv file2.txt

You want to rename the file name “file1.txt” as “file2.txt”. Start by opening your command-line terminal. You can access this by selecting Applications> Accessories> Terminal. Once you’ve opened the terminal, type:

mv file1.txt file2.txt

If the original file1.txt is located in /home/apple/docs/files directory, you can type it on the terminal as:

cd /home/apple/docs/files

mv file1.txt file2.txt

In the alternative, you can also rename by typing the command as:

mv /home/apple/docs/files/file1.txt

/home/apple/docs/files/file2.txt

Similarly, you can use the ls command to view your files:

ls -l file 1

ls -1 file 1 file 2

ls -1 /home/apple/docs/files/*.txt

ls -1 *.txt

In sum, the syntax to rename a file would be:

mv file 1 file 2

Rename multiple files using the “mv” command

Awesome! You’ve learned the “mv” command. Did you know that you can also use the same rename command in conjunction with other commands?

You can use “mv” together with “find” or inside bash “for,” and even the command “while” so that you can rename multiple files.

Let’s take an example using the “for” command.

for f in *.html; do

    mv -- "$f" "${f%.html}.php"

done

On the example, we used the bash command to rename all “.html” files to “.php” Let’s analyze the example line by line.

  • The first line uses the command “for.” It looks through the files which end in “.html.”
  • The second line refers to the files which currently use the “.html” extension and rename files that use it too. “PHP”
  • The third line says “done,” which marks the end of the segment.

Let’s make another example using the “find” command combined with the “mv” command.

find . -depth - name "*.html" - exexc sh - c 'f="{}"; mv -- "$f" "${f%.html}.php"' \;

On the example, the “find” command will go through all the files in your directory that end with “.html” and “mv” then one by one using the “-exec” command.

“{}” indicates which files are being processed.

As you might have noticed, renaming multiple files in Linux isn’t as easy as renaming single files.

Besides using the “mv” command, you’ll also need to have a good knowledge of how to use Bash script.

Renaming Files using the “rename” Command

To rename more than one file in Linux, you’ll need to use the “rename” command.

This is more advanced than the “mv” command because you’ll need some basic knowledge about using regular expressions.

You can also use the “rename ” command to rename files in Linux files, regardless of the number! The syntax to rename files in Linux using the “rename” command should look like this:

rename 's/old/new/' files

rename [options] 's/old/new/' files

So, if you want to rename all PDF files (*.pdf) to word file (*.docx), the syntax should be like this:

rename 's/pdf/docx/' *.pdf

In the alternative, the rename command syntax could also be typed as:

rename  -v 's/pdf/docx/' *.pdf

Conclusion

You can now rename files in no time! As long as you remember and master the code, you’ll be good, so make good use of your Linux.

Rename files, remember the commands, and you’re set. If you have extra time, you can even put in some effort to learn how to do Bash script.

We hope you enjoyed our article about Linux! Feel free to share this article with your friends!

Let us know what you think in the comments below. What are you waiting for? Master Linux, rename file one, two, and more!