Skip to content

Instantly share code, notes, and snippets.

@theirishpenguin
Created January 17, 2012 11:45
Show Gist options
  • Save theirishpenguin/1626380 to your computer and use it in GitHub Desktop.
Save theirishpenguin/1626380 to your computer and use it in GitHub Desktop.
Sort the contents of each file in the current directory (non-recursive)
# Usage:
#
# (Note - This command does not recursively traverse subdirectories!)
#
# * Save the script to a directory that is in your PATH variable so it can be executed
# * chmod u+x path/to/sort_dir # so that it can be executed
# * cd into the directory containing the files who's contents you wish to sort
# * Simpy run the command 'sort_dir'
# * This will product an subdirectory called 'sorted' containing all the current
# directory's files with their contents sorted
mkdir sorted
# Explanation
# * The -type f argument picks out files only (ignores dirs)
# * The -print0 and -0 arguments protect spaces in filenames
# * The -I {} allows you to grab each thing that xargs iterates over
# as {}
# * So {} is probably the most important thing
find . -type f -print0|xargs -0 -I {} sort {} -o "sorted/"{}
# If you want to see what's going on by printing out the xargs output
# find . -type f -print0|xargs -0 -I {} echo "sorted/"{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment