Skip to content

Instantly share code, notes, and snippets.

@lavoiesl
Created April 17, 2015 00:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lavoiesl/b2270522ad1f2283c813 to your computer and use it in GitHub Desktop.
Save lavoiesl/b2270522ad1f2283c813 to your computer and use it in GitHub Desktop.
BASH sort by filename
##
# Sort all files by their filename, independently of their folder
# Example: ls -1 /a/* /b/* | sort_by_filename
#
# @input One path per line
# @outputs One path per line
function sort_by_filename() {
while read filename; do
echo "${filename}" | sed -E 's/^(.+)\/([^/]+)$/\2#\1\/\2/';
done | sort -n | cut -d '#' -f 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment