Commands using expr (16)

  • expr will give you a quick way to do basic math from the CLI. Make sure you escape things like * and leave a space between operators and digits. Show Sample Output


    4
    expr 512 \* 7
    chuckr · 2009-09-23 19:11:38 20
  • watch the seconds of your life tick away - replace YYYY-mm-dd HH:MM:ss w/ your birthtime.


    2
    while [ 0 ]; do expr 2365200000 \- `date +%s` \- `date --date "YYYY-mm-dd HH:MM:ss" +%s`; sleep 1; clear; done
    wwest4 · 2009-02-13 20:02:37 22
  • Same as original, but works in bash


    2
    while [ 1 -lt 2 ]; do i=0; COL=$((RANDOM%$(tput cols)));ROW=$((RANDOM%$(tput cols)));while [ $i -lt $COL ]; do tput cup $i $ROW;echo -e "\033[1;34m" $(cat /dev/urandom | head -1 | cut -c1-1) 2>/dev/null ; i=$(expr $i + 1); done; done
    dave1010 · 2010-05-28 16:07:56 5
  • EXAMPLES jb "next sun 12pm" "/bin/sh ~you/1.sh" & jb "2010-08-29 12:00:00" "~you/1.sh" & jb "29aug2010 gmt" ". ~you/1.sh" & jb 12:00p.m. "nohup ./1.sh" & jb 1min "echo stop!" & SEE ALSO parsedate(3) strftime(3)


    2
    jb() { if [ -z $1 ];then printf 'usage:\njb <"date and/or time"> <"commandline"> &\nsee parsedate(3) strftime(3)\n';else t1=$(date +%s); t2=$(date -d "$1" +%s) ;sleep $(expr $t2 - $t1);$2 ;fi ;}
    argv · 2010-08-26 23:50:42 3
  • Generates labyrinth-like pattern on UTF-8 terminal in bash. For fun ;) Show Sample Output


    2
    while ( true ) ; do if [ $(expr $RANDOM % 2 ) -eq 0 ] ; then echo -ne "\xE2\x95\xB1" ; else echo -ne "\xE2\x95\xB2" ; fi ; done
    tobi · 2015-01-17 12:46:37 10

  • 1
    expr `find . -type f -printf "%s + "0`
    sraeder · 2011-02-13 00:03:31 4
  • Take a screenshot every 2 seconds and save it as a png file


    1
    i=0;while :; do i=$(expr "$i" + 1); scrot "$i".png; sleep 2; done;
    scripteles · 2011-05-27 00:25:28 3
  • command was too long... this is the complete command: fname=$1; f=$( ls -la $fname ); if [ -n "$f" ]; then fsz=$( echo $f | awk '{ print $5 }' ); if [ "$fsz" -ne "0" ]; then nrrec=$( wc -l $fname | awk '{ print $1 }' ); recsz=$( expr $fsz / $nrrec ); echo "$recsz"; else echo "0"; fi else echo "file $fname does not exist" >&2; fi First the input is stored in var $fname The file is checked for existance using "ls -lart". If the output of "ls -lart" is empty, the error message is given on stderr Otherwise the filelength is taken from the output of "ls -lart" (5th field) With "wc -l" the number of records (or lines) is taken. The record size is filelength devided by the number of records. please note: this method does not take into account any headers, variable length records and only works on ascii files where the records are sperated by 0x0A (or 0x0A/0x0D on MS-DOS/Windows). Show Sample Output


    0
    fname=$1;f=$(ls -la $fname);fsz=$(echo $f|awk '{ print $5 }');nrrec=$(wc -l $fname|awk '{ print $1 }');recsz=$(expr $fsz / $nrrec);echo "$recsz"
    vuurst · 2010-09-14 08:40:22 3

  • 0
    expr `echo "123671" | sed -e 's/[0-9]/ + &/g' -e 's/^ +//g'` 20
    maher · 2012-06-24 23:12:30 3
  • Should work with sh, bash, etc. Show Sample Output


    0
    port=32768; while netstat -atn | grep -q :$port; do port=$(expr $port + 1); done; echo $port
    presto8 · 2013-08-29 17:55:55 8
  • I have this in my .bash_aliases and call it before running apt-get install or apt-get upgrade Example: alias apt-install='apt-update; apt-get install' alias apt-upgrade='apt-update; apt-get upgrade' function apt-update () { if [[ $(expr $(date +%s) - $(stat -c %X /var/lib/apt/periodic/update-success-stamp)) -gt 86400 ]]; then sudo apt-get update else echo apt is up to date fi }


    0
    if [[ $(expr $(date +%s) - $(stat -c %X /var/lib/apt/periodic/update-success-stamp)) -gt 86400 ]]; then sudo apt-get update fi
    gargolito · 2015-05-12 14:45:11 10

  • -1
    expr $(fdisk -s ` grep ' / ' /etc/mtab |cut -d " " -f1`) / 1024
    ncaio · 2009-05-21 17:25:38 6

  • -1
    dd of=output.txt if=input.txt ibs=1 skip=$(expr `stat -c%s input.txt` / 2)
    kev · 2011-07-10 12:04:48 7
  • Change HH:MM with your target time. This is for a Debian/Ubuntu GNU system. You need bash (package bash), date (package coreutils) and toilet (package toilet). Install with: # apt-get install bash coreutils toilet toilet-fonts


    -2
    watch -tn1 'date -u +%T -d @$(expr $(date -d HH:MM +%s) - $(date +%s)) | toilet -f bigmono12'
    prayer · 2010-06-26 11:56:11 3
  • plays with bash arrays. instead of storing the list of files in a temp file, this stores the list in ram, retrieves the last element in the array (the last html file), then removes it.


    -3
    a=($(ls *html)) && a=${a[$(expr ${#a[@]} - 1)]} && rm $a
    linuxrawkstar · 2009-10-12 16:40:06 5
  • Matrix Screen HPUX


    -4
    while :; do integer i=0; COL=$((RANDOM%$(tput cols))); ROW=$((RANDOM%$(tput cols))); while (( i <= COL)) do tput cup $i $ROW; echo "\033[1;34m" $(cat /dev/urandom | head -1 | cut -c1-1) 2>/dev/null; i=$(expr $i + 1); done done
    mfrancime · 2010-05-28 11:17:43 3

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

scping files with streamlines compression (tar gzip)
it compresses the files and folders to stdout, secure copies it to the server's stdin and runs tar there to extract the input and output to whatever destination using -C. if you emit "-C /destination", it will extract it to the home folder of the user, much like `scp file user@server:`. the "v" in the tar command can be removed for no verbosity.

pipe output to notify-send
Route output to notify-send to show nice messages on the desktop, e.g. title and interpreter of the current radio stream

bash: display disks by id, UUID and HW path
Shows a tree of the disks. Requires "tree"

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

A command's package details
In Debian based distros, this command will list 'binutils' package details which contains 'nm' command. You can replace 'nm' to any other command.

locate bin, src, and man file for a command

Get max number of arguments
Get max number of arguments that can be accepted by the exec() system call.

Get AWS temporary credentials ready to export based on a MFA virtual appliance
You might want to secure your AWS operations requiring to use a MFA token. But then to use API or tools, you need to pass credentials generated with a MFA token. This commands asks you for the MFA code and retrieves these credentials using AWS Cli. To print the exports, you can use: `awk '{ print "export AWS_ACCESS_KEY_ID=\"" $1 "\"\n" "export AWS_SECRET_ACCESS_KEY=\"" $2 "\"\n" "export AWS_SESSION_TOKEN=\"" $3 "\"" }'` You must adapt the command line to include: * $MFA_IDis ARN of the virtual MFA or serial number of the physical one * TTL for the credentials

Arch Linux sort installed packages by size
This one-liner will output installed packages sorted by size in Kilobytes.

Get a list of the top 10 heaviest tables in your mysql database - useful for performance diagnostics


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: