Skip to content

Instantly share code, notes, and snippets.

@rasschaert
Last active October 13, 2015 08:37
Show Gist options
  • Save rasschaert/4168753 to your computer and use it in GitHub Desktop.
Save rasschaert/4168753 to your computer and use it in GitHub Desktop.
Bash oneliners
######################################
# Collection of handy bash oneliners #
######################################
# License: CC0 (http://creativecommons.org/publicdomain/zero/1.0/)
# To the extent possible under law, Kenny Rasschaert has waived all copyright and related or neighboring rights to Bash oneliners.
# This work is published from: Belgium.
# fast rsync
rsync -av --compress-level=9 --progress -e 'ssh -C -c arcfour256,arcfour128,arcfour,blowfish-cbc' /home/user/stuff remote:~/
# The usual -z option provides the default level of compression, which is 6. If your network connection
# is more of a bottleneck than your CPU is, you can change the compression factor up to 9.
# The ssh option -C enables further compression and the -c selects a preferred list of ciphers. There is some debate
# about which are the fastest ciphers, depending on your CPU architecture and extensions. There is even more debate
# about whether the fast ciphers are secure enough. I would not recommend using this cipher list for sensitive data.
# The example provided will transfer the directory "stuff" in its entirety over to the remote machine and place it
# in the home directory. A new directory named "stuff" will be created if needed on the destination.
# Add the option --delete to rsync if you have an older copy on the remote site which may have obsolete files that may be deleted.
# Use with caution!
# parallel shell tasks using xargs (for example ssh!)
xargs -a /list/of/servers.txt -I"SERVER" -P0 -n1 sh -c "printf \"\n###### SERVER ######\n\$(ssh SERVER \"$command\" 2>&1)\n\""
# Make use of the awesome parallel processing power of xargs to spawn multiple shell tasks at once.
# The example given here uses SSH to run commands on many servers at once. The list of servers is passed to xargs with the -a option.
# Piping input into xargs instead of using -a works just as well.
# Keeping count with awk
find IRCClient/src -name '*.java' -exec wc '{}' \; | awk '{sum+=$1}END{print sum}'
# This example counts the lines of code in all .java source files.
# Find finds the Java source files and runs wc on each of them to get a line, word, and character count.
# Awk takes the first column of this output (the line count) and adds each occurrence to a variable named SUM, then prints it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment