Issue
I am looking for a shell command (I am on MacOS bash) that can do this job for me:
So I have a folder with this structure:
.
├── Erie
│ └── archive
│ ├── 001
│ ├── 002
│ ├── 003
│ └── 004
│ ├── 0041
│ ├── 0042
│ └── 0043
└── SB
└── archive
├── 002
├── 003
└── 004
Essentially I would like to get rid of the level "archive", and reorganize the folder to
.
├── Erie
│ ├── 001
│ ├── 002
│ ├── 003
│ └── 004
│ ├── 0041
│ ├── 0042
│ └── 0043
└── SB
├── 001
├── 002
└── 003
What I have tried:
cd Erie
find . -depth 2 -type d
# outputs:
#./archive/001
#./archive/003
#./archive/004
#./archive/002
Then I want to attach commands to move these directories one level up. I tried either find -exec
or xargs
function, but I couldn't work this out:
Method 1:
find . -depth 2 -type d | xargs mv .
# gives me an error:
# mv: rename . to ./archive/002/.: Invalid argument
# this also generates a weird folder structure:
.
└── archive
└── 002
├── 001
├── 003
└── 004
├── 0041
├── 0042
└── 0043
Method 2:
find . -depth 2 -type d -exec mv {} . ;
# This give me an error as well:
# find: -exec: no terminating ";" or "+"
Solution
Here's a simple suggestion:
for Dir in Erie SB
do find "$Dir"/archive -mindepth 1 -maxdepth 1 -exec mv -v '{}' "$Dir" ';'
rmdir -v "$Dir"/archive
done
Hope that helps.
Answered By - Grobu Answer Checked By - Gilberto Lyons (WPSolving Admin)