How To Rename A Directory In Linux

Linux is a great operating system – but it’s also fair to say that it tends to do things a little differently sometimes. Of course, this is both a weakness and a strength! 

How to Rename A Directory In Linux

The sheer versatility of Linux makes it viable for so many different applications. However, its idiosyncrasies can make transitioning to Linux daunting at first!

One of the important things to learn about Linux is how to do simple manipulation of files and folders – moving, copying, renaming, and so on. 

It’s not very hard at all to learn these things – but if you’re coming from a Windows or Mac environment, then you’ll have to learn the way that Linux likes to do things!

At some point, you’re likely to want to rename a directory on Linux. There are a few different ways to do this  – so let’s have a look at a few of them!

Renaming Directories Using The Command Line

The easiest way to rename a directory is using the command line. This is probably the most common way to manipulate directories on Linux (see also ‘How To Update Kali Linux‘). To rename a directory, simply type:

mv old_directory new_directory

The command “mv”, as you may or may not already know, is short for “move” – and indeed, moving files and folders is of course one of the primary uses for this command.

However, it’s also the way that we rename directories in the Linux command line!

If you think about it, it makes sense – although, at first, it might seem like a strange way to do things.

You’re telling the computer to move one folder entirely to another path. To a computer, this might well be the same thing as telling it to rename the directory – after all, the data and files in the directory are all the same, they’ve just been “moved” to a directory with a different name.

If you use the “man” command on mv, as follows:

man mv

You’ll come up with the manual page for mv, which shows the following at the top:

NAME

       mv – move (rename) files

This clearly shows that this isn’t unintended behavior for the mv command – but is one of the intended uses of it!

So, why does the Linux command line behave this way? 

Well, it’s because Linux doesn’t care what you call your files and folders. In other words, when you tell the computer to move a file from one location to another, it doesn’t care what the actual names of those locations are.

It will happily move any file or folder into any other directory without question. And since there’s no real difference between calling a directory “old_directory” and “new_directory”, the computer sees no reason to complain!

Of course, you could always manually change the directory names yourself – but that would defeat the purpose of having a powerful OS like Linux (see also ‘How To Update Pi-hole‘)!

Rename Directories Using Find

Another way to rename a directory on the Linux command line is by using the “find” command.

How to Rename A Directory In Linux (1)

To use the “find” command to rename a directory, simply run the following:

find. -name old_directory -execdir mv {} new_directory \;

Now, this is a far more convoluted thing to type than the previous way of renaming a directory! So, you might be wondering, what’s the point of doing it this way?

Well, sometimes, you might know the name of a directory, but not exactly where it is on your system. Using this command will find the directory that matches the name, and rename it for you too!

Of course, as this is a very powerful tool – and something of a blunt instrument – be very careful if you’re using it! 

As with all things in the Linux command line, it will do exactly what you ask of it – even if you’ve made a mistake!

Therefore, before running this command, it might be best to run the find command on its own first, as follows:

find old_directory

This way, you’re giving yourself a chance to double-check that you’ve got the correct directory before you rename it.

Renaming Directories Using The UI

However, sometimes you just don’t want to use the command line, for whatever reason. If you’re coming from another computer OS, then you might well find yourself being more comfortable with using a visual file manager to manipulate directories instead. 

This is no problem – there are plenty of great file managers available for Linux, no matter what distro or desktop environment you’re using! And, for the most part, they’ll work in almost the same way you’re used to on a Windows or Mac environment.

To rename a directory using the GUI, simply open whichever file manager you use, and navigate to the place where the directory you want to rename resides. Right-click on the directory (i.e. click using the right button on your mouse), and you should notice a menu pops up.

Select the rename option, and then type in the new name for the directory! This will change the directory to the name you’ve chosen.

File Permissions And Sudo

You might need to change the name of a directory that’s in a place outside of your normal user directory. 

This isn’t common, and should be done only with care – generally, you’ll find all of your files, and things that it’s safe for you to manipulate, in your /home/username directory. 

Anything inside this is fair game – the worst that can happen is you could lose some important files. Nothing, as long as you’ve got backups!

However, meddling with directories outside of /home/ can have a bad effect on your system, and should never be done unless you’re sure of what you’re doing. One simple mistyping of a command can break your operating system completely!

If you understand the risks, you can use the command sudo to give yourself temporary elevated permissions – as long as your account settings and permissions allow for it, of course.

For example, the following command:

sudo mv old_directory new_directory

This essentially tells the computer to ask you for a password, proving you’re the account owner. If your account permissions allow for it, it’ll run the mv command with elevated permissions. 

Again, exercise caution – and if you’re not sure if something is a good idea, then better to leave well alone.

The sudo command can be used before any other command or program to give it elevated privileges. Therefore, this will work to launch your file manager with these permissions too!

Conclusion

Renaming directories on Linux isn’t a difficult task – and hopefully, this article has helped you to learn just how to do it!

Erik D