Windows and macOS users often manage their files using the Graphical User Interface (GUI) file manager provided on their systems. Typically Windows File Explorer or macOS Finder, respectively. Linux systems also usually have a GUI file manager, such as Dolphin, Nautilus, or Thunar. However, when managing a remote server, you may not have access to the GUI. It can be incredibly frustrating trying to figure out command line file operations while typing at an SSH, LISH, or other command line prompt. Fortunately, the commands are straightforward once you understand them.
This tutorial primarily discusses how to use the
rename commands to rename one or more files in a terminal session. Creating files and displaying your Linux file system using the
ls commands are also covered.
Before You Begin
For the purposes of this tutorial, a shared instance with 1 CPU and 1 GB of memory running Ubuntu 22.04 LTS works. Pick a region that is close to your location. Create a strong root password and save it for later. Should you ever forget your root password, you can create a new one on your settings page. Don’t bother creating a SSH key for the account unless you’re already familiar with RSA keys.
If you have not already done so, create a Linode account and Compute Instance. See our
Getting Started with Linode and
Creating a Compute Instance guides.
Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.
NoteThe steps in this guide require root privileges. Be sure to run the steps below as
root. For more information on privileges, see our
Users and Groups guide.
When your new Linode is running, click on the Launch LISH Console button.
Once the Weblish side of the display has stopped scrolling, click on the Glish side of the display.
Log in as root with the password you chose in the previous step.
Renaming Files Using mv
Short for “move”, the
mv command moves files from one directory to another, but it also renames single files.
From the root directory, type:
You should have no results, as there are no visible files in the root directory.
However, there are hidden files, to reveal them, type:
You should now see a handful of hidden files:
. .. .bashrc .cache .profile .ssh
Create an empty file:
View the directory again:
Your test file should now be listed:
Rename the file:
mv test.txt test1.txt
View the directory again:
Your test file should now be listed with a different filename:
Rename File(s) Using the rename Command
can be used inside a shell loop to rename multiple files, that requires some advanced text substitution. Instead, you can use a different command,
If your Ubuntu Linode is brand new, it probably doesn’t have the rename command installed.
First, update your package sources:
apt install rename
Once installed, create a second file with touch:
List both of them:
You should now see both files:
renameuses a Perl expression to act on the file names. Run
man renamefor an explanation and several examples.
As an example, let’s rename both text files to backup files:
rename 's/txt/bak/' *.txt
On CentOS Stream 9, use this command instead:
rename .txt .bak *.txt
Now list them:
You should see the same files as before, but with .bak extensions:
When you’re done with this exercise, exit the LISH shell. If you don’t need this Linode anymore, delete it from its settings in the … dropdown menu to avoid incurring future charges.
Renaming a single file on a terminal in Ubuntu Linux is accomplished with the
mv command. Renaming multiple files is accomplished with the
rename command, which you have to install in a new instance of Ubuntu 22.04 LTS.