Commands by jgc (13)

  • Print a row of characters across the terminal. Uses tput to establish the current terminal width, and generates a line of characters just long enough to cross it. In the example '#' is used. It's possible to use a repeating sequence by dividing the columns by the number of characters in the sequence like this: seq -s'~-' 0 $(( $(tput cols) /2 )) | tr -d '[:digit:]' or seq -s'-~?' 0 $(( $(tput cols) /3 )) | tr -d '[:digit:]' You will lose chararacters at the end if the length isn't cleanly divisible. Show Sample Output

    seq -s'#' 0 $(tput cols) | tr -d '[:digit:]'
    jgc · 2010-04-01 09:06:44 0
  • This command will play back each keystroke in a session log recorded using the script command. You'll need to replace the ^[ ^G and ^M characters with CTRL-[, CTRL-G and CTRL-M. To do this you need to press CTRL-V CTRL-[ or CTRL-V CTRL-G or CTRL-V CTRL-M. You can adjust the playback typing speed by modifying the sleep. If you're not bothered about seeing each keypress then you could just use: cat session.log Show Sample Output

    (IFS=; sed 's/^[]0;[^^G]*^G/^M/g' <SessionLog> | while read -n 1 ITEM; do [ "$ITEM" = "^M" ] && ITEM=$'\n'; echo -ne "$ITEM"; sleep 0.05; done; echo)
    jgc · 2010-01-20 16:11:32 2
  • Print out contents of file with line numbers. This version will print a number for every line, and separates the numbering from the line with a tab. Show Sample Output

    sed = <file> | sed 'N;s/\n/\t/'
    jgc · 2009-12-11 14:39:14 0
  • The -i option in sed allows in-place editing of the input file. Replace myexpression with any regular expression. /expr/d syntax means if the expression matches then delete the line. You can reverse the functionality to keep matching lines only by using: sed -i -n '/myexpression/p' /path/to/file.txt

    sed -i '/myexpression/d' /path/to/file.txt
    jgc · 2009-11-09 11:40:45 2
  • Alternative command to retrieve the CPU model name and strip off the "model name : " labels. Show Sample Output

    sed -n 's/^model name[ \t]*: *//p' /proc/cpuinfo
    jgc · 2009-11-05 10:59:31 1
  • This command uses the top voted "Get your external IP" command from to get your external IP address. Use this and you will always be using the communities favourite command. This is a tongue-in-cheek entry and not recommended for actual usage.

    eval $(curl -s|sed -n '/^# Get your external IP address$/{n;p;q}')
    jgc · 2009-11-04 16:58:31 0
  • There's been so many ways submitted to get your external IP address that I decided we all need a command that will just go pick a random one from the list and run it. This gets a list of "Get your external IP" commands from and selects a random one to run. It will run the command and print out which command it used. This is not a serious entry, but it was a learning exercise for me writing it. My personal favourite is "curl". I really don't think we need any other ways to do this, but if more come you can make use of them with this command ;o). Here's a more useful command that always gets the top voted "External IP" command, but it's not so much fun: eval $(curl -s|sed -n '/^# Get your external IP address$/{n;p;q}') Show Sample Output

    IFS=$'\n';cl=($(curl -s|sed -n '/^# Get your external IP address$/{n;p}'));c=${cl[$(( $RANDOM % ${#cl[@]} ))]};eval $c;echo "Command used: $c"
    jgc · 2009-11-04 16:55:44 3
  • Another way of doing it that's a bit clearer. I'm a fan of readable code.

    script_path=$(cd $(dirname $0);pwd)
    jgc · 2009-10-14 16:04:03 3
  • In this simple example the command will add a comma to the end of every line except the last. I found this really useful when programatically constructing sql scripts. See sample output for example. Show Sample Output

    sed -e "$ ! s/$/,/"
    jgc · 2009-10-13 10:13:52 1
  • Makes use of $RANDOM environment variable.

    head -c10 <(echo $RANDOM$RANDOM$RANDOM)
    jgc · 2009-10-09 15:09:02 2
  • Using the standard numeric comparison but suppressing the STDERR output acts as the simplest way to check a value is numeric. See sample output for some examples. Show Sample Output

    if [ "$testnum" -eq "$testnum" 2>/dev/null ]; then echo It is numeric; fi
    jgc · 2009-10-09 14:57:27 4
  • Very useful for interactive scripts where you would like to return the terminal contents to its original state before the script was run. This would be similar to how vi exits and returns you to your original terminal screen. Save and clear the terminal contents with: tput smcup Execute some commands, then restore the saved terminal contents with: tput rmcup

    tput smcup; echo "Doing some things..."; sleep 2; tput rmcup
    jgc · 2009-10-08 16:48:04 2
  • In this example the command "somecommand" will be executed and sent a SIGALARM signal if it runs for more than 10 seconds. It uses the perl alarm function. It's not 100% accurate on timing, but close enough. I found this really useful when executing scripts and commands that I knew might hang E.g. ones that connect to services that might not be running. Importantly this can be used within a sequential script. The command will not release control until either the command completes or the timeout is hit. Show Sample Output

    perl -e "alarm 10; exec @ARGV" "somecommand"
    jgc · 2009-09-23 12:03:55 4

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Check These Out

geoip lookup

Compare copies of a file with md5
I had the problem that the Md5 Sum of a file changed after copying it to my external disk. This unhandy command helped me to fix the problem.

Terminal redirection
can display the commands and their output to another user who is connected to another terminal, by example pts/3

Alert on Mac when server is up
When run on a mac, this command will bring up a dialog box in the Terminal when server HOSTNAME first responds to a ping.

List every file that has ever existed in a git repository
What was the name of that module we wrote and deleted about 3 months ago? windowing-something? $ git log --all --pretty=format:" " --name-only | sort -u | grep -i window

Shows users and 'virtual users' on your a unix-type system
Shows a list of users that currently running processes are executing as. YMMV regarding ps and it's many variants. For example, you might need: $ ps -axgu | cut -f1 -d' ' | sort -u

FizzBuzz in one line of Bash
The (in)famous "FizzBuzz" programming challenge, answered in a single line of Bash code. The "|column" part at the end merely formats the output a bit, so if "column" is not installed on your machine you can simply omit that part. Without "|column", the solution only uses 75 characters. The version below is expanded to multiple lines, with comments added. for i in {1..100} # Use i to loop from "1" to "100", inclusive. do ((i % 3)) && # If i is not divisible by 3... x= || # ...blank out x (yes, "x= " does that). Otherwise,... x=Fizz # ...set x to the string "Fizz". ((i % 5)) || # If i is not divisible by 5, skip (there's no "&&")... x+=Buzz # ...Otherwise, append (not set) the string "Buzz" to x. echo ${x:-$i} # Print x unless it is blanked out. Otherwise, print i. done | column # Wrap output into columns (not part of the test).

Git fetch all remote branches

prints line numbers
the sed way to print line numbers

Show File System Hierarchy
Curious about differences between /bin, /usr/bin, and /usr/local/bin? What should be in the /sbin dir? Try this command to find out. Tested against Red Hat & OS X

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: