Skip to content

TechDirectArchive

Hands-on IT, Cloud, Security & DevOps Insights

  • Home
  • About
  • Advertise With US
  • Reviews
  • Contact
  • Toggle search form
Home » Linux » Rename or Move Files or Directories in Linux with Bash Terminal
  • Delete VM and Storage in Proxmox
    How to delete a VM and Storage in Proxmox Virtualization
  • How to Enable Time Limit to Disconnect Remote Desktop After Inactivity
    How to Enable Time Limit to Disconnect Remote Desktop After Inactivity Windows
  • CI With GitLab 1
    Build Docker Images with GitLab CI Automation
  • Add User to Sudoers
    Add a User to the Sudoers List in Linux Linux
  • mountedimagenotaccessible
    Unable to access the image: Make sure that the image path exist Windows
  • telnet
    Could not open a connection to the host, on the port, connect failed Windows Server
  • VBR upgrade to 12.2.3
    Upgrade Veeam Backup and Replication v12.3.x to 12.3.2 Backup
  • wordpresserrorsiteadmin
    There has been a critical error on this website please check your site admin email inbox for instructions Web Server

Rename or Move Files or Directories in Linux with Bash Terminal

Posted on 29/10/202216/12/2023 Imoh Etuk By Imoh Etuk No Comments on Rename or Move Files or Directories in Linux with Bash Terminal
MV-command-in-Linux

In this article, we shall discuss how to rename or Move Files or Directories in Linux with Bash Terminal. When it comes to working on the Linux OS renaming files and directories is a common daily task for a system administrator. It is a routine operation that can be carried out whether you are using the command line or the GUI. Please see How to install Windows Subsystem for Linux on Windows 11 via the Command line, and Microsoft Store, and how to get Domain information using WHOIS Command in Linux.

The CLI is a particularly potent tool when compared to the GUI (or Graphical User Interface). This is partially because you can rename files in bulk or even schedule the scripts to do it at a specific time.

See the following guides to learn more: How to fix SU authentication failure and Sudo Permission denied Prompt in Ubuntu, Linux Cheat Sheet: The Linux Command Line,

The CLI is a particularly potent tool when compared to the GUI (or Graphical User Interface). This is partly due to the fact that you can rename files in batch and or schedule the scripts to do it at a specific time.

Bash is a command generator that normally operates in a text window where users enter instructions to trigger operations Linux OS. A shell script is a file that contains commands that Bash can read and execute. Apart from having the rename or file moving command (mv), there are thousands of other Bash commands that are worth knowing as a system administrator.

If will like to read about Git, see the following guides: Git command not found: How to fix Git is not recognized as an internal or external command. Git Vulnerability: Git for Windows uninstaller is vulnerable to DLL hijacking when run under the SYSTEM user account.

What is BASH?

Bash stands for Bourne Again Shell. Computers provide two types of interfaces to the user Linux OS; that is, the Graphical User Interface (GUI), and text-based interfaces (through the command line or terminal). These interfaces allow you to navigate through the operating system and file system.

In text-based interfaces, a command line interpreter or shell is used to interpret the given commands by the user. Thus, allowing users to provide commands through which the user can specify what they want to do. Before providing the commands, bash displays a prompt, as you open the command line (in Linux and macOS), such as the one below.

user@localhost:~$

Renaming a File in Linux

In this section, you will learn how you can rename or move files in the Linux command line using the built-in mv command.

In Linux operating systems, the mv command moves and renames files and directories. You can use the built-in Linux command mv to rename files.

The mv command follows this syntax:

mv [options] source_file destination_file

For instance, in the screenshot below I created a directory called folder1 and folder2 with the mkdir command and later renamed folder2 to folder3.

To test if that really took effect, I navigated to folder1 with the cd command but got an error and later cd into folder3 and it worked Linux OS.

Linux operating system

While using the mv command below are some useful options:

-v , --verbose: Explains what is being done.
-i, --interactive: Prompts before renaming the file.

Another example is, let’s say you want to rename index.html to mywebsite_page.html. You use the mv command as shown below:

 $ mv index.html web_page.html
Linux platform

Renaming files extension in bulk using the mv command Linux OS

Having learned about renaming singles, let’s take a look at how to rename bulk files.

In thejsfiles directory, we have a bunch of .js files that we will like to rename in bulk.

Linux distribution

Next, we want to convert them to .json extensions. To do so, run the command below:

for f in *.js; do mv -- "$f" "${f%.js}.json"; done
Linux environment

Now let’s take brief look at this long command to see what it means:

The first part [for f in *.js] tells the for loop to process each “.js” file in the directory.
 The next part [do mv -- "$f" "${f%.js}.json] specifies what the processing will do. It is using mv to rename each file. The new file is going to be named with the original file’s name excluding the .js part. A new extension of .json will be appended instead.
 The last part [done] simply ends the loop once all the files have been processed.

Renaming using regular expressions Linux OS

mv does not interpret regular expressions (regex). If you want to rename many files using a complex or nuanced mapping from old to new file names, you use the rename command instead. rename accepts Perl regular expressions. For example Linux OS:

rename 's/My\ file(..)/newdocs$1/' My*

This command renames files My file.txt and My file 2.txt to newocs.txt and newdocs 2.txt.

Moving Files to a Directory using the mv command

The mv command is a powerful command as it can also be used to move files between directories.

Moving-a-file1

In the above screenshot, we created a file named text1.txt in the Desktop location and moved it to the myfiles directory.

Another example is if we move the file myfile.txt into the directory myfiles. If myfiles is a file, it is overwritten. If the file is marked as read-only but you own the file. You are prompted before overwriting it.

File-replacement

Another instance is if myfiles is a file or directory and myfiles1 is a directory, move myfiles into myfiles1. If myfiles1 does not exist, the file or directory myfiles is renamed myfiles1.

Replacing-files

Now let’s move the directory myfiles to the parent directory of the current directory Linux OS.

Move-files-to-the-parent-directory

In conclusion, renaming and moving files or directories is quite easy using the mv command. You can use it to perform a lot of powerful tasks on Linux.

I hope you found this article useful on how to rename or Move Files or Directories in Linux with Bash Terminal. Please feel free to leave a comment below.

Rate this post

Thank you for reading this post. Kindly share it with others.

  • Share on X (Opens in new window) X
  • Share on Reddit (Opens in new window) Reddit
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Facebook (Opens in new window) Facebook
  • Share on Pinterest (Opens in new window) Pinterest
  • Share on Tumblr (Opens in new window) Tumblr
  • Share on Telegram (Opens in new window) Telegram
  • Share on WhatsApp (Opens in new window) WhatsApp
  • Share on Mastodon (Opens in new window) Mastodon
  • Share on Bluesky (Opens in new window) Bluesky
  • Share on Threads (Opens in new window) Threads
  • Share on Nextdoor (Opens in new window) Nextdoor
Linux

Post navigation

Previous Post: How to enable Adblocker on Microsoft Edge
Next Post: How to Install and Use Minikube on a Linux System

Related Posts

  • centos feature
    How to change the system time zone under RedHat and CentOS Linux
  • feature linux boot process
    Linux Boot Process Explained Step by Step for Beginners Linux
  • squid proxy feature
    How to Set Up and Configure a Squid Proxy Server Linux
  • LAMP
    How to setup and configure a Lamp stack on CentOS Linux
  • Feature image nagios
    How to Install and Configure Nagios on Ubuntu Linux
  • Generate SSH Keys
    Associate SSH Public key with Azure Linux VM AWS/Azure/OpenShift

More Related Articles

centos feature How to change the system time zone under RedHat and CentOS Linux
feature linux boot process Linux Boot Process Explained Step by Step for Beginners Linux
squid proxy feature How to Set Up and Configure a Squid Proxy Server Linux
LAMP How to setup and configure a Lamp stack on CentOS Linux
Feature image nagios How to Install and Configure Nagios on Ubuntu Linux
Generate SSH Keys Associate SSH Public key with Azure Linux VM AWS/Azure/OpenShift

Leave a Reply Cancel reply

You must be logged in to post a comment.

Microsoft MVP

VEEAMLEGEND

vexpert-badge-stars-5

Virtual Background

GoogleNews

Categories

veeaam100

sysadmin top30a

  • Delete VM and Storage in Proxmox
    How to delete a VM and Storage in Proxmox Virtualization
  • How to Enable Time Limit to Disconnect Remote Desktop After Inactivity
    How to Enable Time Limit to Disconnect Remote Desktop After Inactivity Windows
  • CI With GitLab 1
    Build Docker Images with GitLab CI Automation
  • Add User to Sudoers
    Add a User to the Sudoers List in Linux Linux
  • mountedimagenotaccessible
    Unable to access the image: Make sure that the image path exist Windows
  • telnet
    Could not open a connection to the host, on the port, connect failed Windows Server
  • VBR upgrade to 12.2.3
    Upgrade Veeam Backup and Replication v12.3.x to 12.3.2 Backup
  • wordpresserrorsiteadmin
    There has been a critical error on this website please check your site admin email inbox for instructions Web Server

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,825 other subscribers
  • RSS - Posts
  • RSS - Comments
  • About
  • Authors
  • Write for us
  • Advertise with us
  • General Terms and Conditions
  • Privacy policy
  • Feedly
  • Telegram
  • Youtube
  • Facebook
  • Instagram
  • LinkedIn
  • Tumblr
  • Pinterest
  • Twitter
  • mastodon

Tags

AWS Azure Bitlocker Microsoft Windows PowerShell WDS Windows 10 Windows 11 Windows Deployment Services Windows Server 2016

Copyright © 2025 TechDirectArchive

 

Loading Comments...
 

You must be logged in to post a comment.