Skip to content

Instantly share code, notes, and snippets.

@RobertAudi
Created October 16, 2013 18:15
Show Gist options
  • Save RobertAudi/7012299 to your computer and use it in GitHub Desktop.
Save RobertAudi/7012299 to your computer and use it in GitHub Desktop.
# Touch n files with random names
randtouch() {
# A number of files to create must be specified
if [[ -n "$1" ]] && [[ "$1" = "${1%%[!0-9]*}" ]]; then
# Check if an extension was specified
if [[ -n "$2" ]]; then
# Extensions can only contain 2-10 letters
if [[ "$2" =~ ^\\.?[a-z]{2,10}$ ]]; then
# Check if extension has a dot in it...
if [[ "$2" =~ ^\\. ]]; then
local extension="$2"
else
# ...or add one
local extension=".$2"
fi
fi
else
# If no extension was passed, use a default one
local extension=".randtouch"
fi
# Ask the user if he is sure!!
read "answer?Are you sure you want to create $1 random files in $PWD? "
if [[ "$answer" =~ ^[Yy] ]]; then
for (( i = 0; i < $1; i++ )); do
local rand=$(hexdump -n 16 -v -e '/1 "%02X"' /dev/urandom)
touch "$rand$extension"
echo -ne "."
done
echo -ne "Done!\n"
fi
else
echo "$fg[red]You need to supply a number ro randtouch!$fg[default]"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment