We can rename files in Linux terminal using different approaches, one of the approaches is using Python which is discussed in this article. We cannot write Python code directly on the terminal, we create a separate file in a text editor. The “rename” is one of the operating system functions so we will use a Python OS module for this purpose.

To rename file using Python OS module follow the procedure given below:

Rename file in Python Using OS module on Ubuntu

Requirements:

  • File should exist previously
  • Any Python version should be installed on your Linux System

The Python OS module allows you to perform various operations related to the Operating System. Renaming a file is also one of the operations of the Operating System which can be performed using Python OS module function os.rename().

Now follow the procedure below to change the name of file using Python OS module:

Creating Python File : First you need to create a Python file using nano, for instance I am creating Python_file.py file by below mentioned command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/1-1.jpg" data-lazy- height="203" src="data:image/svg xml,” width=”1166″>

You can set the filename of your choice.

Using “os.rename” Function to Rename File

To rename file in Current directory: Below mentioned “os.rename()” function syntax will be used to rename file in current directory:

$ os.rename(“source file”, “destination file”)

  • source file: old filename
  • destination file: new filename

Now write the below mentioned code into the Python_file to rename the “old_linuxhint.txt” to “new_linuxhint.txt” in current directory:

The “import os” is used to import the OS module in the program.

import os

os.rename(“old_linuxhint.txt”,“new_linuxhint.txt”)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/2-1.jpg" data-lazy- height="748" src="data:image/svg xml,” width=”1171″>

Press “ctrl s” to save the file and “ctrl x” to exit the file.

Now to execute the file run the below mentioned command, I am using Python3 to execute Python_file.py as Python3 is pre installed in Ubuntu system:

And ls command is used to list the files of directory to check whether the file to be renamed exists or not:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/3-1.jpg" data-lazy- height="306" src="data:image/svg xml,” width=”1169″>

Now to verify that file is renamed or not, again list the files of current working directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/4-1.jpg" data-lazy- height="301" src="data:image/svg xml,” width=”1164″>

To rename file in Directory apart from Current Directory: Below mentioned “os.rename()” function syntax will be used to rename file:

$ os.rename(“source file”, “destination file”)

  • source file: path of the file old filename
  • destination file: path to file modified/new filename

Now write the below mentioned code into the Python_file.py to rename the “old_linuxhint.txt” to “new_linuxhint.txt” on Desktop directory:

import os

os.rename(“https://linuxhint.com/home/alishba/Desktop/old_linuxhint.txt”,“/home/alishba/Desktop/new_lin

uxhint.txt”

)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/5-1.jpg" data-lazy- height="748" src="data:image/svg xml,” width=”1260″>

Press “ctrl s” to save the file and “ctrl x” to exit the file.

Use ls command to list the files of Desktop directory to check whether the file to be renamed exists or not:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/6-1.jpg" data-lazy- height="280" src="data:image/svg xml,” width=”1266″>

Now to verify that file is renamed or not, again list the files of Desktop directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/7.jpg" data-lazy- height="272" src="data:image/svg xml,” width=”1266″>

To rename multiple files: We can also change the name of multiple files at a time. For instance, I have three files in my Desktop directory as file1, file2, file3 and I want to add the prefix of “linuxhint_” to all of the three filenames, then run the below mentioned code in “Python_file.py”.

import os

for textfile in os.listdir(“https://linuxhint.com/home/alishba/Desktop”):

os.rename(textfile,f“https://linuxhint.com/home/alishba/Desktop/linuxhint_{textfile}”)

You can modify the command according to your requirements; you can choose the filenames and directory in which they exist of your choice:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/8.jpg" data-lazy- height="466" src="data:image/svg xml,” width=”650″>

We will use the “listdir()” function in “for loop” to get files one by one and then rename them using the os.rename function. The f string function of Python in above given code is used to change the name of each file in the desktop to “/home/alishba/linuxhint_{old filename}”. Here we are using “textfile” as loop variable for old filename. which will get each file of directory. Press “Ctrl s” to save the file and “Ctrl x” to exit the file.

Firstly, list the files of Desktop to check existence of files and then execute the file to rename files:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/9.jpg" data-lazy- height="164" src="data:image/svg xml,” width=”619″>

Now again list the files of Desktop to verify that files are renamed properly:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/10.jpg" data-lazy- height="180" src="data:image/svg xml,” width=”622″>

In the above picture we can clearly see that all files of the Desktop directory are renamed.

Conclusion:

Python OS module function called os.rename() is used to rename files using Python. We can rename files using different techniques but in this article we discussed how to rename files in the current directory, apart from the current directory and to rename multiple files using the Python OS module. If you are a Python programmer and want to rename file/files using Python then this article will surely help you.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/7c99efcc024949e17b176845c757687e?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Alishba Iftikhar

I am currently an undergraduate student in my 1st year. I am an internee author with Linuxhint and loved learning the art of technical content writing from senior authors. I am looking forward to opting my career as a full time Linux writer after I graduate.