How to recursively rename all the files in several layers of subdirectories without changing their extensions?
Below's a toned down version (to save room) of what I've got. For argument's sake, I want all the files to have the same title, yet retain their original extension. There's never more than a single file per directory, so there's no chance of doubling up.
For simplicity, we'll just call them all foo
, followed by their current extension.
So just to clarify:
Asset\ 1.pdf
, Asset\ 1.png
, Asset\ 1@4x.png
, Asset\ 1.svg
Will become:
foo.pdf
, foo.png
, foo.png
, foo.svg
And so on in that fashion.
I would typically use parameter expansion and a for
loop, like:
for f in */*; do mv "$f" "${f%/*}/foo.${f##*.}"; done
But it's not recursive. So I would prefer to use something with find
..-exec
or similar.
~/Desktop/Project/Graphics/
├── Huge
│ ├── PDF
│ │ └── Asset\ 1.pdf
│ ├── PNG
│ │ ├── 1x
│ │ │ └── Asset\ 1.png
│ │ └── 4x
│ │ └── Asset\ 1@4x.png
│ └── SVG
│ └── Asset\ 1.svg
├── Large
│ ├── PDF
│ │ └── ProjectAsset\ 2.pdf
│ ├── PNG
│ │ ├── 1x
│ │ │ └── ProjectAsset\ 2.png
│ │ └── 4x
│ │ └── ProjectAsset\ 2@4x.png
│ └── SVG
│ └── ProjectAsset\ 2.svg
├── Medium
│ ├── PDF
│ │ └── ProjectAsset\ 3.pdf
│ ├── PNG
│ │ ├── 1x
│ │ │ └── ProjectAsset\ 3.png
│ │ └── 4x
│ │ └── ProjectAsset\ 3@4x.png
│ └── SVG
│ └── ProjectAsset\ 3.svg
├── Small
│ ├── PDF
│ │ └── ProjectAsset\ 4.pdf
│ ├── PNG
│ │ ├── 1x
│ │ │ └── ProjectAsset\ 4.png
│ │ └── 4x
│ │ └── ProjectAsset\ 4@4x.png
│ └── SVG
│ └── ProjectAsset\ 4.svg
└── Tiny
├── PDF
│ └── Asset\ 5.pdf
├── PNG
│ ├── 1x
│ │ └── Asset\ 5.png
│ └── 4x
│ └── Asset\ 5@4x.png
└── SVG
└── Asset\ 5.svg
30 directories, 20 files
find
and GNUmv
like described in this answer. – Felix Oct 20 '18 at 13:35find x -exec y {} \;
to play nicely with"${parameter%/*}/foo.${expansion##*.}"
, etc. – voices Oct 20 '18 at 16:19