Skip to content

Instantly share code, notes, and snippets.

@vpnry
Created November 26, 2020 15:33
Show Gist options
  • Select an option

  • Save vpnry/d75f1b1ed11270bbcc9f19ca7d7a248e to your computer and use it in GitHub Desktop.

Select an option

Save vpnry/d75f1b1ed11270bbcc9f19ca7d7a248e to your computer and use it in GitHub Desktop.
Move files with specific exts while preserving their directory structures
# Move files while preserving its directory structures
# From https://unix.stackexchange.com/a/59159
FILEext=".*\.\(avi\|mp3\|mp4\)"
SOURCE=/home/d/dev/dev/D/A/
DEST=/home/d/dev/dev/D/BB/
mkdir -p "$DEST"
destination=$(cd -- "$DEST" && pwd) # make it an absolute path
cd -- "$SOURCE" &&
find . -type f -iregex "$FILEext" -exec sh -c '
for x do
mkdir -p "$0/${x%/*}"
mv "$x" "$0/$x"
done
' "$destination" {} +
# FILEext=(mpg avi mp3 mp4)
# for f in ${FILEext[@]}; do
# echo $f
# done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment