Ask Your Question
0

How can strings be deleted from multiple filenames using Linux or Python?

asked 2022-03-16 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2022-06-05 19:00:00 +0000

ladyg gravatar image

In Linux, you can use the rename command to delete strings from multiple filenames. Here’s an example:

If you want to remove the string “_old” from all the filenames in a directory, you can use the following command:

rename 's/_old//' *

This command will replace “_old” with nothing in all filenames in the current directory.

In Python, you can use the os module to rename files. Here’s an example:

import os

directory = "/your/directory/path/"

for filename in os.listdir(directory):
    if "_old" in filename:
        new_filename = filename.replace("_old", "")
        os.rename(directory + filename, directory + new_filename)

This Python code will check all the filenames in the directory specified by directory and remove “_old” from filenames that contain it. The modified filename is then used to rename the file using the os.rename function.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-03-16 11:00:00 +0000

Seen: 12 times

Last updated: Jun 05 '22