In my directories I have multiple files named content. I need to rename them using the exact name of the parent directory, move it outside and delete the parent directory.
The tricky part is the parent directory name formats are a.txt , b.pdf , c.mp3 so the file content needs to be renamed content.txt , content.pdf , content.mp3 afterwards moved outside and the directory should be deleted.
ex;
INPUT
a.txt/content
b.pdf/content
c.mp3/content
OUTPUT
a.txt
b.pdf
c.mp3
I used this find command and gives the desired echo output out I'm not sure where to go from there.
find . -type d -not -empty -exec echo mv \{\}/content \{\} \;
Saving the files with an .bak
extension in an intermediate step and then renaming to desired name:
find . -mindepth 2 -type f -exec bash -c 'mv "$1" "${1%/*}".bak' _ {} \; \
-exec bash -c 'rm -r "${1%/*}"' _ {} \; && rename 's/\.bak$//' *.bak
Example:
% tree
.
├── a.txt
│ └── content
├── b.pdf
│ └── content
└── c.mp3
└── content
% find . -mindepth 2 -type f -exec bash -c 'mv "$1" "${1%/*}".bak' _ {} \; -exec bash -c 'rm -r "${1%/*}"' _ {} \; && rename 's/\.bak$//' *.bak
% tree
.
├── a.txt
├── b.pdf
└── c.mp3