Assuming you're currently located in the directory where Folder1
is located:
#!/bin/bash
Don't even attempt to do something
if we're in the wrong place.
cd Folder1/Folder2 || exit 1
Make the shell remove patterns that aren't matched,
rather than leaving them as they are.
shopt -s nullglob
for dirpath in output////; do
if [[ $dirpath =~ output/(.)/[^/]*(..)/ ]]; then
# "${BASH_REMATCH[1]}" is something like "0653/3457"
# "${BASH_REMATCH[2]}" is the 2-character suffix, like "01"
newdir=output${BASH_REMATCH[2]}/${BASH_REMATCH[1]}
mkdir -p "$newdir" &&
mv "$dirpath" "$newdir"
fi
done
This uses the regular expression matching feature of bash
to pick out the numerical suffix from the end of the directory pathname and constructs a new output
directory name using this.
This would take the directory structure
.
`-- Folder1/
`-- Folder2/
`-- output/
`-- 0653/
`-- 3547/
|-- 0112945601/
`-- 0112945602/
and turn it into
.
`-- Folder1/
`-- Folder2/
|-- output/
| `-- 0653/
| `-- 3547/
|-- output01/
| `-- 0653/
| `-- 3547/
| `-- 0112945601/
`-- output02/
`-- 0653/
`-- 3547/
`-- 0112945602/
Empty directories in Folder1/Folder2/output
could then be removed using
find Folder1/Folder2/output -type d -empty -delete
or, using standard find
and ignoring errors from rmdir
when it tries to remove a non-empty directory,
find Folder1/Folder2/output -depth -type d -exec rmdir {} \; 2>/dev/null
which leaves
.
`-- Folder1/
`-- Folder2/
|-- output01/
| `-- 0653/
| `-- 3547/
| `-- 0112945601/
`-- output02/
`-- 0653/
`-- 3547/
`-- 0112945602/
You would obviously run this on a copy of your data first.