Ubuntu – Rename files by adding their parent folder name

command linerename

I have a parent directory composed of a set of sub-directories. Each sub-directory is composed of a list of files.

Here is the scenario:

v_1:
filenames.txt

v_2:
labels.txt

v_3:
coding.txt

What is my expected output?

v_1:
v_1_filenames.txt

v_2:
v_2_labels.txt

v_3:
v_3_coding.txt

What l have tried to solve that?

rename -n 's/(.*)\//$1\/$1_/' */*

which prints

rename(v_1/filenames.txt, v_1/v_1_filenames.txt)
rename(v_2/labels.txt, v_2/v_2_labels.txt)
rename(v_3/coding.txt, v_3/v_3_coding.txt)

However it doesn't work. To confirm that I did:

ls *

and I get:

v_1:
filenames.txt

v_2:
labels.txt

v_3:
coding.txt

Any cue? Thank you

Best Answer

  • Simply remove the option -n. From man prename*:

    -n, --no-act
            No Action: show what files would have been renamed.
    

    * There are multiple commands called rename; I assume you're using prename on Ubuntu 16.04 or earlier, though option -n does the same thing for most other versions. For details see What's the difference between the different "rename" commands?

  • Related Question