Rename files and directories in Linux

Overview

Hello there Linux aficionados!

Today’s chapter is all about how to rename files and directories in Linux using many different tools in cli and applying various diferrent techniques. We’ll also cover some example how to rename multiple files and directories in linux, aka - batch renaming.

Last time we discussed how to copy files and directories and I mentioned ‘mv’ command. Like its name suggests, it’s used primarily for moving files or directories from one destination to another, practically doing cut & paste operations. But appart from that it’s also capable of renaming the files and directories as well and we’re going to start of this post with introducing the rename files and directories in linux with ‘mv’.

Rename files with mv command

The basic syntax for mv command looks like this:

mv \[option\] \[source\] \[destination\]

Or in our particular case, it was:

mv index.php ~/test2

rename files with mv

Esentially all we achieved is - we moved the ‘index.php’ file to the ‘test2’ dir.

Now let’s enhance the command with the -v option to get the verbose output and now we’ll get something that looks like this:

mv –v index.php ~/test2

rename files in linux

Even though, its name is ‘move’ it also double as a rename command as well.

It’s worth mentioning that ‘mv’ command can’t move/rename files (and directories for that matter) to a remote location. You’ll have to use other methods and tools for that.

Rename directories with mv command

It comes with no surprise that syntax for renaming directories is identical to syntax we used above for renaming files:

mv \[option\] \[source\] \[destination\]

or in our case:

mv –v ~/scptest ~/mvtest

rename directories with mv

Since there was no files with a same name, I could omit ‘ ~/ ’ part.

Rename multiple files using mv command with for loop

Now you’re probably asking what to do if occasion arises that you’ll need to rename multiple files at once? Could you use mv command with some option? Sadly, no. There is a way to rename multiple files using mv command but you’ll have to add just a pinch of magic called - programming.

Let’s say, hipotethically, you have a significant number of index.html files and you want to turn them to php all at once. No, even better – you have a certain number of .txt files and you decided to turn them to them into .pdf. Like I said, you just need a pinch of programming “magic” and we get magic words that would look like this:

for i in \*txt; do mv – “$i” “${i%.txt}.pdf”;
done

rename multiple files in linux

Joking asided this is just a simple script that executes a ‘for’ loop which runs the ‘mv’ command until it processes all the files that matches the requirements we specicifed. And with this, we achieved what is known as - Batch renaming.

Rename multiple directories using mv command with for loop

In almost similar manner we can use for loop to rename multiple directories. I created 10 test directories inside test directory and now I want to add ‘directory’ to their name. I’ll use the next command:

for d in \*; do mv “$d” “${d}\_directory”; done

*** C A U T I O N ! ***

I implore you not to use loops until you’ll have a solid knowledge of Linux system and how to set a proper loop. And test it all first in virtual machine enviroment just to be sure. Trust me, I know what I’m talking about.

Rename files using find command

There is another way of renaming files using commands that come with the system. That command is find. Yes, the command for searching files can also rename files too.

But, nevertheless, it’s doable and quite powerful. Let me show you. That 10 files named sometext we changed from .txt to .pdf, we’ll now reverse the process:

find . -type f -name "*.pdf" -exec sh -c 'f="{}"; mv -- "$f" "${f%.pdf}.txt"' \;

rename files with find

If you look closely, you’ll see mv command nested inside find. This is possible because ‘find’ has option ’exec’ which allows to nest another command after the search criteria is matched. This is not strictly binded to a single command of course, and you nest any shell command within ‘find’.

Rename directories using find command

Back in directory test we left 10 directories. Now I’ll demonstrate find and rename combo to rename those directories:

find –depth –type d –name “test*” –exec rename ‘s/test/new/’ {} \;

I know that these last few examples can be quite challenging for beginners, so I advise you once again to use them after you get a solid grasp of Linux knowledge. That said, let’s explore some easier ways to do the task. Since we used command that comes with a system, it’s time to turn to third party tools and I’ll just name three of them:

  • mmv

  • rename

  • prename.

mmv

One of the tools to rename files and directories is mmv. Depending on your distro, you can install it either with:

  • apt install mmv (Debian based),

  • yum install mmv (Ret Hat based),

  • dnf install mmv (Fedora) or

  • yaourt –S mmv (Arch).

Rename files using mmv

Synthax is not hard and I advise you to consult manual on how to use it properly. It doesn’t have too many options so it will be super fast to read it. Let me show you a simple example. I’ve made a mmv_test directory and created 10 tstfiles inside. It’s a typo, obviously. So to rename it to testfiles I’ll use next command:

mmv “\*tst\*” “#1test#2”

To rename extansion would be even easier:

mmv “\*.txt” “#1.pdf”

Rename directories using mmv

Renaming directories with mmv isn’t much different than renaming files. directory test_mmv I”ll rename to mmv_directory with this syntax:

mmv –r “test\_\*” “#1\_directory”

As you see, quite simple. Oh, and just for your info, –r is mandatory when it comes to renaming directories.

Rename

Just a quick info that rename can be installed with:

  • apt install rename (Debian based distro),

  • yum install rename (Red hat/Fedora based) or

  • pacman –S rename (Arch based)

Rename files using rename

To show you syntax of rename command i’ve created a new directory and a few files that’ll be renamed in process.

rename ‘s/files\*/renamed\_files/’ \*.txt

As you can see, it’s a bit different than usual syntaxes. It would take you some time to learn it. But, a word of advice. Don’t get too attached to this tool. It has very limited possibilities and you’ll soon learn that for some complexed jobs it’s better to avoid it or you’ll have a case of serious headache. The problem is that this tool replaces only first occurrence of a string, so it can’t handle complex swaps/renames.

Rename directories using rename

directory rename_directory we’ll try to rename to renamed_directory. So syntax for that job would be:

rename –d ‘s/rename\_directory/renamed\_directory/’\*

Prename

Shorter for Perl-rename. Supports Perl Compatible Regular Expressions (PCRE) which makes it more powerful than rename command and its syntax is quite similar to rename’s. Depending on your distro, to install it you’’ll have to use one of this commands:

  • apt-get install rename (Debian based)

  • yum install prename (Red Hat based)

  • pacman –Syu perl-rename (Arch based)

Rename files using prename

To show you how powerful this tool is, I’ve created a few files. Intentionally, I’ve made typos, making some letters uppercased. So, instead renaming them one at the time, we can do that in one go, using lower case function in Perl (hence lc).

prename ‘s/.\*/lc(“$&”)/e’ \*

Rename directories using prename

For directories, I’ll just show you how to uppercase all directory I’ve created for tools showcase:

prename ‘s/a-z/A-Z/’ \*\_directory

Well, I’m a bit rusty, so I’ll have to see how to avoid that notification. But, the result is here and that’s what matters right now.

***BONUS (Honorable mention)***

Linux wouldn’t be a land of opportunities if it hadn’t multiple options to do the same job. And in several cases that would be achievable with non-job-specific tools. In this particular case, that would be awk command (abbreviated from its creators - Aho, Weinberger, and Kernighan) which is not a renaming tool. Let me just show you an example what it would look like to uppercase files in test2 directory

find . –type f | awk –v mvCmd=’mv “%s” “%s”\n’ \\ ‘{ new=toupper($0); printf mvCmd,$0,new; }’ | sh

As you probably noticed, there were find and mv commands in this syntax. Linux is something like Lego – you get different pieces and it’s up to you what will you build with them.

Useful resource - explain shell tool

Before I conclude this article, let me give you one valuable resource. It’s https://explainshell.com

On this site you can paste various commands you come across, it will break them in pieces and explain them to you chunk by chunk. You can start with commands from this very article. Explore the vast universe of Linux commands.

Summary

And with that we came to an end of this lesson. We covered here multiple different tools and methods on how to rename files and directories in linux and also how to rename multiple files in linux at once and how to rename multiple directories using the same tools and techniques. This article came up a bit lenghty and i do hope you found it interesting and i thank you for your time.