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


    6
    seq -s'#' 0 $(tput cols) | tr -d '[:digit:]'
    jgc · 2010-04-01 09:06:44 5
  • 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


    0
    (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 5
  • 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


    -1
    sed = <file> | sed 'N;s/\n/\t/'
    jgc · 2009-12-11 14:39:14 3
  • 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


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


    0
    sed -n 's/^model name[ \t]*: *//p' /proc/cpuinfo
    jgc · 2009-11-05 10:59:31 4
  • This command uses the top voted "Get your external IP" command from commandlinefu.com 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.


    -1
    eval $(curl -s http://www.commandlinefu.com/commands/matching/external/ZXh0ZXJuYWw=/sort-by-votes/plaintext|sed -n '/^# Get your external IP address$/{n;p;q}')
    jgc · 2009-11-04 16:58:31 6
  • 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 commanlinefu.com 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 icanhazip.com". 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 http://www.commandlinefu.com/commands/matching/external/ZXh0ZXJuYWw=/sort-by-votes/plaintext|sed -n '/^# Get your external IP address$/{n;p;q}') Show Sample Output


    3
    IFS=$'\n';cl=($(curl -s http://www.commandlinefu.com/commands/matching/external/ZXh0ZXJuYWw=/sort-by-votes/plaintext|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 9
  • Another way of doing it that's a bit clearer. I'm a fan of readable code.


    7
    script_path=$(cd $(dirname $0);pwd)
    jgc · 2009-10-14 16:04:03 8
  • 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


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


    0
    head -c10 <(echo $RANDOM$RANDOM$RANDOM)
    jgc · 2009-10-09 15:09:02 6
  • 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


    0
    if [ "$testnum" -eq "$testnum" 2>/dev/null ]; then echo It is numeric; fi
    jgc · 2009-10-09 14:57:27 9
  • 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


    8
    tput smcup; echo "Doing some things..."; sleep 2; tput rmcup
    jgc · 2009-10-08 16:48:04 24
  • 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


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

What's this?

commandlinefu.com 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

tail -f a log file over ssh into growl

dump database from postgresql to a file

Lists all usernames in alphabetical order

Keep a copy of the raw Youtube FLV,MP4,etc stored in /tmp/
Certain Flash video players (e.g. Youtube) write their video streams to disk in /tmp/ , but the files are unlinked. i.e. the player creates the file and then immediately deletes the filename (unlinking files in this way makes it hard to find them, and/or ensures their cleanup if the browser or plugin should crash etc.) But as long as the flash plugin's process runs, a file descriptor remains in its /proc/ hierarchy, from which we (and the player) still have access to the file. The method above worked nicely for me when I had 50 tabs open with Youtube videos and didn't want to have to re-download them all with some tool.

Convert seconds to [DD:][HH:]MM:SS
Converts any number of seconds into days, hours, minutes and seconds. sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }

Change the homepage of Firefox
Pros: Works in all Windows computers, most updated and compatible command. Cons: 3 liner Replace fcisolutions.com with your site name.

Copy without overwriting

Fast, built-in pipe-based data sink
This is shorter and actually much faster than >/dev/null (see sample output for timings) Plus, it looks like a disappointed face emoticon.

bash screensaver off

return the latest kernel version from a Satellite / Spacewalk server software channel


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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: