Score:-2

Rename files in subfolders, move them to parent folder and delete subfolder

ve flag

Currently doing some cleanup work and I'm struggling to rename my files accordingly.

I currently have it akin to this (Names are just placeholders, there are a lot more than alpha and beta):

└── Project/
    ├── alpha/
    │   ├── alpha1/
    │   │   ├── file1
    │   │   └── file2
    │   └── alpha2/
    │       ├── file1
    │       └── file2
    └── beta/
        ├── beta1/
        │   ├── file1
        │   └── file2
        └── beta2/
            ├── file1
            └── file2

And would like it to be like this:

└── Project/
    ├── alpha/
    │   ├── alpha1_file1
    │   ├── alpha1_file2
    │   ├── alpha2_file1
    │   └── alpha2_file2
    └── beta/
        ├── beta1_file1
        ├── beta1_file2
        ├── beta2_file1
        └── beta2_file2

How could I achieve this? Help appreciated and thanks a lot in advance!

Score:1
jp flag

Using bash(The default user shell in Ubuntu) shell parameter expansion, it can be done from within the Project directory with e.g.:

for d in */*/
  do
  for f in "${d}"*
    do
    [[ -f "${f}" ]] && echo mv -nv -- "${f}" "${f%/*}_${f##*/}"
    done
  rmdir -v "${d}"
  done

or as a one-liner:

for d in */*/; do for f in "${d}"*; do [[ -f "${f}" ]] && echo mv -nv -- "${f}" "${f%/*}_${f##*/}"; done; rmdir -v "${d}"; done

echo is for a safe dry-run and rmdir will only remove empty directories and error otherwise ... Please, see demonstration below:

ubuntu@DESKTOP:~/test$ tree Project/
Project/
├── alpha
│   ├── alpha1
│   │   ├── file1
│   │   └── file2
│   └── alpha2
│       ├── file1
│       └── file2
└── beta
    ├── beta1
    │   ├── file1
    │   └── file2
    └── beta2
        ├── file1
        └── file2

6 directories, 8 files
ubuntu@DESKTOP:~/test$ cd Project/
ubuntu@DESKTOP:~/test/Project$ for d in */*/; do for f in "${d}"*; do [[ -f "${f}" ]] && mv -nv -- "${f}" "${f%/*}_${f##*/}"; done; rmdir -v "${d}"; done
renamed 'alpha/alpha1/file1' -> 'alpha/alpha1_file1'
renamed 'alpha/alpha1/file2' -> 'alpha/alpha1_file2'
rmdir: removing directory, 'alpha/alpha1/'
renamed 'alpha/alpha2/file1' -> 'alpha/alpha2_file1'
renamed 'alpha/alpha2/file2' -> 'alpha/alpha2_file2'
rmdir: removing directory, 'alpha/alpha2/'
renamed 'beta/beta1/file1' -> 'beta/beta1_file1'
renamed 'beta/beta1/file2' -> 'beta/beta1_file2'
rmdir: removing directory, 'beta/beta1/'
renamed 'beta/beta2/file1' -> 'beta/beta2_file1'
renamed 'beta/beta2/file2' -> 'beta/beta2_file2'
rmdir: removing directory, 'beta/beta2/'
ubuntu@DESKTOP:~/test/Project$ tree
.
├── alpha
│   ├── alpha1_file1
│   ├── alpha1_file2
│   ├── alpha2_file1
│   └── alpha2_file2
└── beta
    ├── beta1_file1
    ├── beta1_file2
    ├── beta2_file1
    └── beta2_file2

2 directories, 8 files
Jerome Stephan avatar
ve flag
Awesome, thank you!
Score:0
hr flag

Using the contributed zmv module in zsh:

 % autoload -Uz zmv

 % zmv -v -Q 'Project/*/*/*(.N)' '$f:h_$f:t'
mv -- Project/alpha/alpha1/file1 Project/alpha/alpha1_file1
mv -- Project/alpha/alpha1/file2 Project/alpha/alpha1_file2
mv -- Project/alpha/alpha2/file1 Project/alpha/alpha2_file1
mv -- Project/alpha/alpha2/file2 Project/alpha/alpha2_file2
mv -- Project/beta/beta1/file1 Project/beta/beta1_file1
mv -- Project/beta/beta1/file2 Project/beta/beta1_file2
mv -- Project/beta/beta2/file1 Project/beta/beta2_file1
mv -- Project/beta/beta2/file2 Project/beta/beta2_file2

The -Q switch turns on the bare glob qualifier (.N) which matches only plain files, so that it will not attempt to move any subdirectories. The replacement pattern uses history-style head and tail modifiers.

Now we just need to remove the empty directories:

 % rmdir -v Project/*/*(/^F)
rmdir: removing directory, 'Project/alpha/alpha1'
rmdir: removing directory, 'Project/alpha/alpha2'
rmdir: removing directory, 'Project/beta/beta1'
rmdir: removing directory, 'Project/beta/beta2'

leaving

 % tree Project
Project
├── alpha
│   ├── alpha1_file1
│   ├── alpha1_file2
│   ├── alpha2_file1
│   └── alpha2_file2
└── beta
    ├── beta1_file1
    ├── beta1_file2
    ├── beta2_file1
    └── beta2_file2

2 directories, 8 files
Score:0
tn flag

Using Perl's rename:

$ tree Project/
Project/
├── alpha
│   └── alpha1
│       ├── file1
│       └── file2
└── beta
    └── beta1
        ├── file1
        └── file2

4 directories, 4 files
rename -n 's@(Project/[^/]+)/(\w+\d+)/(file\d+)@$1/$2_$3@' ./Project/*/*/*
rmdir ./Project/*/*

Remove -n (aka dry-run) when the output looks satisfactory.

Output

rename(Project/alpha/alpha1/file1, Project/alpha/alpha1_file1)
rename(Project/alpha/alpha1/file2, Project/alpha/alpha1_file2)
rename(Project/beta/beta1/file1, Project/beta/beta1_file1)
rename(Project/beta/beta1/file2, Project/beta/beta1_file2)
Jerome Stephan avatar
ve flag
Thanks, though as I said there are a lot more alpha and betas than just two, and manually renaming them would take way too long
Gilles Quenot avatar
tn flag
Post edited accordingly
Jerome Stephan avatar
ve flag
Thanks! Sadly it doesn't seem to do anything the way you posted it; heres a screenshot of my terminal: https://i.vgy.me/M2MTQR.png
Gilles Quenot avatar
tn flag
Have you tested that rename is the perl version that I explain in my link?
Gilles Quenot avatar
tn flag
rename --version
Jerome Stephan avatar
ve flag
Yes, I installed it according to their instructions, with rename --version I get ```/usr/bin/rename using File::Rename version 1.10```
muru avatar
us flag
@JeromeStephan you need to remove `-n` to actual perform the operations
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.