#!/bin/bash function help() { echo "Flatten the directory structure from the current folder downwards" echo "Files in subfolders will be renamed / -> _" echo "Empty folders are removed" echo " -f to force action " } function helpexit() { help exit } [[ -z "$1" ]] && { help echo "Are you sure? Break with ctrl-c" read i } [[ "$1" = "-h" ]] && helpexit IFS=$'\n' for f in $( find . -mindepth 2 -type f -path '*.*' -printf %P'\n' ); do mv -iv "$f" "$( echo $f | sed s,[/],_,g )" done find . -depth -type d -empty -delete