Skip to content

Instantly share code, notes, and snippets.

@twolfson
Created April 14, 2018 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twolfson/d33dd0686797b658b02b4196761d0aaf to your computer and use it in GitHub Desktop.
Save twolfson/d33dd0686797b658b02b4196761d0aaf to your computer and use it in GitHub Desktop.
Unnest folders into 1 flat folder of symlinks
#!/usr/bin/env bash
# Do you have a bunch of nested folders of images that you want to scroll through easily?
# Well this script is for you, it will take nested folders and symlink their files so they're easily scrollable
# It retains the full path so files which should be next to each other still are :+1:
# Exit under sane conditions
set -euo pipefail
# Allow spaces in path names
# https://stackoverflow.com/a/7039579
IFS=$'\n'
# Define our constants
target_dirname="consolidation"
# Create our directory
# DEV: `-p` is to tolerate the folder's existence
mkdir -p "${target_dirname}"
# Wipe our folder's contents to avoid bloating files when there's a conflict
# DEV: `-f` is to tolerate no files
rm -f "${target_dirname}"/*
# Find all of our non-symlink files
for src_filepath in $(find . -type f); do
# Swap our filepath so it becomes a flat path
# foo/bar/hi-there.jpg -> foo--bar--hi-there.jpg
target_filename="$(echo -n "${src_filepath}" | sed "s/\//--/g")"
# Symlink our file
ln -s "../${src_filepath}" "${target_dirname}/${target_filename}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment