Last active
September 24, 2020 06:05
-
-
Save dale-c-anderson/e77979c8ddb45811a2bd74a6e6258c3f to your computer and use it in GitHub Desktop.
Flatten a directory structure by one level, preserving the full path of file names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eu -o pipefail | |
# Turn: | |
# foo/ | |
# ├── bar/ | |
# │ ├── one.txt | |
# │ ├── two.txt | |
# ├── baz/ | |
# ├── three.txt | |
# Into: | |
# foo-bar/ | |
# ├── one.txt | |
# ├── two.txt | |
# foo-baz/ | |
# foo-baz-three.txt | |
DIR=$1 | |
if [ ! -d "$DIR" ]; then | |
>&2 echo "ERR: Not a directory: $DIR" | |
exit 1 | |
fi | |
find "${DIR}" -maxdepth 1 -mindepth 1 -type d -exec sh -c 'new=$(echo "{}" | tr "/" "-" | tr " " "_"); mv -iv -- "{}" "$new"' \; | |
find "${DIR}" -maxdepth 1 -mindepth 1 -type f -exec sh -c 'new=$(echo "{}" | tr "/" "-" | tr " " "_"); mv -iv -- "{}" "$new"' \; | |
rmdir -v "${DIR}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment