Commands tagged redirection (17)

  • Useful when you've produced a large file of numbers, and want to quickly see the distribution. The value of y halfway along the x axis is the median. Simple! Just create the listOfNumbers.txt file with a number on each line to try it out.

    gnuplot -persist <(echo "plot '<(sort -n listOfNumbers.txt)' with lines")
    penthief · 2009-05-02 13:46:02 6
  • Though without infinite time and knowledge of how the site will be designed in the future this may stop working, it still will serve as a simple straight forward starting point. This uses the observation that the only item marked as strong on the page is the single logical line that includes the italicized fact. If future revisions of the page show failure, or intermittent failure, one may simply alter the above to read. wget -O - 2>/dev/null | tee lastfact | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;" The file lastfact, can then be examined whenever the command fails.

    wget -O - 2>/dev/null | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;"
    tali713 · 2010-03-30 23:49:30 56
  • Define a function vert () { echo $1 | grep -o '.'; } Use it to print some column headers paste <(vert several) <(vert parallel) <(vert vertical) <(vert "lines of") <(vert "text can") <(vert "be used") <(vert "for labels") <(vert "for columns") <(vert "of numbers") Show Sample Output

    echo "vertical text" | grep -o '.'
    dennisw · 2009-09-11 03:45:04 5
  • You have a script where =ALL= STDERR should be redirected to STDIN and you don't want to add "2>&1" at the end of each command... E.G.: ls -al /foo/bar 2>&1 Than just add this piece of code at the beginning of your script! I hope this can help someone. :)

    exec 2>&1
    morgents · 2010-08-05 08:24:18 5
  • Bash has a great history system of its commands accessed by the ! built-in history expansion operator (documented elsewhere on this site or on the web). You can combine the ! operator inside the process redirection Very handy. Show Sample Output

    drewk · 2010-02-06 18:35:10 3
  • A bitcoin "brainwallet" is a secret passphrase you carry in your brain. The Bitcoin Brainwallet Private Key Base58 Encoder is the third of three functions needed to calculate a bitcoin PRIVATE key from your "brainwallet" passphrase. This base58 encoder uses the obase parameter of the amazing bc utility to convert from ASCII-hex to base58. Tech note: bc inserts line continuation backslashes, but the "read s" command automatically strips them out. I hope that one day base58 will, like base64, be added to the amazing openssl utility. Show Sample Output

    function b58encode () { local b58_lookup_table=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z}); bc<<<"obase=58;ibase=16;${1^^}"|(read -a s; for b58_index in "${s[@]}" ; do printf %s ${b58_lookup_table[ 10#"$b58_index" ]}; done); }
    nixnax · 2014-02-18 02:29:30 7
  • the tee command does fine with file names, but not so much with file descriptors, such as &2 (stderr). This uses process redirection to tee to the specified descriptor. In the sample output, it's being used to tee to stderr, which is connected with the terminal, and to wc -l, which is also outputting to the terminal. The result is the output of bash --version followed by the linecount Show Sample Output

    tee >(cat - >&2)
    camocrazed · 2010-07-20 17:22:31 5
  • A bitcoin "brainwallet" is a secret passphrase you carry in your brain. The Bitcoin Brainwallet Exponent Calculator is one of three functions needed to calculate the bitcoin PRIVATE key. Roughly, the formula is exponent = sha256 (passphrase) Note that this is a bash function, which means you have to type its name to invoke it. You can check the accuracy of the results here Show Sample Output

    function brainwallet_exponent () { printf %s "$1"|sha256sum|head -c 64; }
    nixnax · 2014-02-18 01:49:09 7
  • A bitcoin "brainwallet" is a secret passphrase you carry in your brain. The Bitcoin Brainwallet Exponent Calculator is the second of three functions needed to calculate a bitcoin PRIVATE key. Roughly, checksum is the first 8 hex digits of sha256(sha256(0x80+sha256(passphrase))) Note that this is a bash function, which means you have to type its name to invoke it Show Sample Output

    function brainwallet_checksum () { (o='openssl sha256 -binary'; p='printf';($p %b "\x80";$p %s "$1"|$o)|$o|sha256sum|cut -b1-8); }
    nixnax · 2014-02-18 02:07:02 18

  • 3
    echo "vertical text" | fold -1
    zude · 2009-10-05 23:20:14 3
  • This works even if there are spaces in any word in the command line. Show Sample Output

    quietly() { "$@" > /dev/null 2>&1; }
    wipu · 2011-10-04 06:45:42 4
  • This command will sort the contents of FILENAME by redirecting the output to individual .txt files in which 3rd column will be used for sorting. If FILENAME contents are as follows: foo foo A foo bar bar B bar lorem ipsum A lorem Then two files called A.txt and B.txt will be created and their contents will be: A.txt foo foo A foo lorem ipsum A lorem and B.txt will be bar bar B bar

    awk '{print > $3".txt"}' FILENAME
    alperyilmaz · 2009-03-31 15:14:13 3
  • A common mistake in Bash is to write command-line where there's command a reading a file and whose result is redirected to that file. It can be easily avoided because of : 1) warnings "-bash: file.txt: cannot overwrite existing file" 2) options (often "-i") that let the command directly modify the file but I like to have that small function that does the trick by waiting for the first command to end before trying to write into the file. Lots of things could probably done in a better way, if you know one... Show Sample Output

    buffer () { tty -s && return; tmp=$(mktemp); cat > "${tmp}"; if [ -n "$1" ] && ( ( [ -f "$1" ] && [ -w "$1" ] ) || ( ! [ -a "$1" ] && [ -w "$(dirname "$1")" ] ) ); then mv -f "${tmp}" "$1"; else echo "Can't write in \"$1\""; rm -f "${tmp}"; fi }
    Josay · 2009-07-27 20:21:15 5
  • extension to tali713's random fact generator. It takes the output & sends it to notify-osd. Display time is proportional to the lengh of the fact.

    wget -O - 2>/dev/null | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;" | while read FUNFACT; do notify-send -t $((1000+300*`echo -n $FUNFACT | wc -w`)) -i gtk-dialog-info "RandomFunFact" "$FUNFACT"; done
    mtron · 2010-04-02 09:43:32 3
  • This is a working version, though probably clumsy, of the script submitted by felix001. This works on ubuntu and CygWin. This would be great as a bash function, defined in .bashrc. Additionally it would work as a script put in the path. Show Sample Output

    lynx -dump | grep -A 3 U | sed 1D
    xizdaqrian · 2009-05-05 07:52:10 10
  • Use tee -a to append.

    command foo bar | sudo tee /etc/write-protected > /dev/null
    adeverteuil · 2015-02-08 03:58:35 4
  • Adds the stdout (standard output) to the beginning of logfile.txt. Change "command" to whatever command you like, such as 'ls' or 'date', etc. It does this by adding the output to a temporary file, then adding the previous contents of logfile.txt to the temp file, then copying the new contents back to the logfile.txt and removing the temp file.

    command > tmp && cat logfile.txt >> tmp && tmp > logfile.txt && rm tmp
    akoumjian · 2009-04-05 22:00:32 10

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

Export unpushed files list

Continue a current job in the background
Continue a current job in the background and detach it from current terminal

delete all trailing whitespace from each line in file

Pick a random line from a file

List programs with open ports and connections
I prefer to use this and not the -n variety, so I get DNS-resolved hostnames. Nice when I'm trying to figure out who's got that port open.

Rename files in batch

Safe Russian Roulette (only echo, don't delete files)
Shows "Bang!" in a chance of 1 out of 6, like in the original game with the gun (spin every round). Otherwise, echoes "Click...". If feeling brave you can also do: $[ $[ $RANDOM % 6 ] == 0 ] && echo 'Bang!' && a really killer command || echo 'Click...'

check open ports without netstat or lsof

shush MOTD
I'm annoyed by the boilerplate "don't login unless you are supposed messages in our environment" - this shuts them up.

Alias TAIL for automatic smart output
Run the alias command, then issue $ps aux | tail and resize your terminal window (putty/console/hyperterm/xterm/etc) then issue the same command and you'll understand. $ ${LINES:-`tput lines 2>/dev/null||echo -n 12`} Insructs the shell that if LINES is not set or null to use the output from `tput lines` ( ncurses based terminal access ) to get the number of lines in your terminal. But furthermore, in case that doesn't work either, it will default to using the default of 80. The default for TAIL is to output the last 10 lines, this alias changes the default to output the last x lines instead, where x is the number of lines currently displayed on your terminal - 7. The -7 is there so that the top line displayed is the command you ran that used TAIL, ie the prompt. Depending on whether your PS1 and/or PROMPT_COMMAND output more than 1 line (mine is 3) you will want to increase from -2. So with my prompt being the following, I need -7, or - 5 if I only want to display the commandline at the top. ( ) 275MB/748MB [7995:7993 - 0:186] 06:26:49 Thu Apr 08 [askapache@n1-backbone5:/dev/pts/0 +1] ~ $ In most shells the LINES variable is created automatically at login and updated when the terminal is resized (28 linux, 23/20 others for SIGWINCH) to contain the number of vertical lines that can fit in your terminal window. Because the alias doesn't hard-code the current LINES but relys on the $LINES variable, this is a dynamic alias that will always work on a tty device.

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: