Friday, May 3, 2024
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 418  / 1 Year ago, sun, may 14, 2023, 10:34:01

Suppose I have folder structure like below


01. folder1
|- 1. video1
|- 2. video2
|- 3. video3

02. folder2
|- 1. photo1
|- 2. photo2

03. folder3
|- 1. doc1
|- 2. doc2

Now I want to rename those files as below


01 folder1
|- 01 1. video1
|- 01 2. video2
|- 01 3. video3

02 folder2
|- 02 1. photo1
|- 02 2. photo2

03 folder3
|- 03 1. doc1
|- 03 2. doc2

Which means I want to add first 3 character (or any character) of folder name before its own filename (like batch rename).


So what is the command/script to rename it automatically like above?


More From » command-line

 Answers
4

I have found a python script that is nearly similar to this problem.
So I modified the script as per my need.


This solve the exact problem which I am looking for.


python3 rename.py "/parent/dir/path"

# filename is rename.py(save the file)
import shutil
import os
import sys

dr = sys.argv[1]

for root, dirs, files in os.walk(dr):
for f in files:
shutil.move(root+"/"+f, root+"/"+root.split("/")[-1][0:3]+f)

If it shows permission problem, then run


sudo chmod +x rename.py

[#308] Tuesday, May 16, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
needstar

Total Points: 268
Total Questions: 108
Total Answers: 117

Location: Seychelles
Member since Mon, Jun 28, 2021
3 Years ago
needstar questions
;