Commands by mpb (44)


  • -4
    tput clear
    mpb · 2019-06-12 17:51:31 34
  • Display a list of the 16 most recently installed RPM packages with newest first. Show Sample Output


    0
    rpm -qa --queryformat '%{INSTALLTIME} %{name}-%{version}-%{release}\n' | sort -k 1,1 -rn | nl | head -16 | awk '{printf("%3d %s %s\n", $1,strftime("%c",$2),$3)}'
    mpb · 2018-09-12 17:47:26 329
  • This function make it easy to compute X/Y as a percentage. The name "wpoxiy" is an acronym of "what percentage of X is Y" Show Sample Output


    1
    function wpoxiy () { # What percentage of X is Y? Where percent/100 = X/Y => percent=100*X/Y # Example: wpoxiy 10 5 # 50.00% # Example: wpoxiy 30 10 # 33.33% echo $(bc <<< "scale=2; y=${1}; x=${2}; percent=x*100/y; percent")"%"; }
    mpb · 2018-06-10 22:43:20 239
  • This command uses the "exiftool" command which is available here: http://www.sno.phy.queensu.ca/~phil/exiftool/ NB, there should be a degree symbol right after the first "%d" NOT a question mark. For some unknown reason, commandlinefu is not able to handle degree symbol correctly ("?")? Show Sample Output


    3
    echo "https://www.google.com/maps/place/$(exiftool -ee -p '$gpslatitude, $gpslongitude' -c "%d?%d'%.2f"\" image.jpg 2> /dev/null | sed -e "s/ //g")"
    mpb · 2016-06-09 13:33:10 12
  • It's quite fun to invert text using "flip.pl" (ref: http://ubuntuforums.org/showthread.php?t=2078323 ). Slightly more challenging is to flip a whole "cowsay". :-) Show Sample Output


    0
    echo Which way up? | flip.pl | cowsay | tac | sed -e "s,/,+,g" -e "s,\\\,/,g" -e "s,+,\\\,g" -e "s,_,-,g" -e "s,\^,v,g"
    mpb · 2016-04-08 11:41:44 28
  • In pre-systemd systems, something like: "# grep sshd /var/log/messages" would display log events in /var/log/messages containing "sshd". # journalctl -u sshd --no-pager The above command displays similar results for systemd systems. (Note that this needs to be run with root permissions to access the log data.)


    2
    # journalctl -u sshd --no-pager # display sshd log entries
    mpb · 2015-10-15 08:48:47 14
  • This will display the system memory size in kb. If you want to see the value in mb, you can type: grep MemTotal: /proc/meminfo | awk '{printf("MemTotal: %d MB\n",$2/1024)}' Show Sample Output


    0
    grep MemTotal: /proc/meminfo # display how much memory installed
    mpb · 2015-05-15 09:19:02 17
  • Working with lists of IP addresses it is sometimes useful to summarize a count of how many times an IP address appears in the file. This example, summarizeIP, uses another function "verifyIP" previously defined in commandlinefu.com to ensure only valid IP addresses get counted. The summary list is presented in count order starting with highest count. Show Sample Output


    1
    function summaryIP() { < $1 awk '{print $1}' | while read ip ; do verifyIP ${ip} && echo ${ip}; done | awk '{ip_array[$1]++} END { for (ip in ip_array) printf("%5d\t%s\n", ip_array[ip], ip)}' | sort -rn; }
    mpb · 2015-05-01 16:45:05 10
  • When processing IP addresses in the shell (or shell script) it is useful to be able to verify that the value of data is an IP address (an not some random string or non-sensible IP address). Show Sample Output


    1
    function verifyIP() { octet="(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])"; ip4="^$octet\.$octet\.$octet\.$octet$"; [[ ${1} =~ $ip4 ]] && return 0 || return 1; }
    mpb · 2015-05-01 12:22:57 16
  • I needed to convert a screen capture when using Gnome's "recordmydesktop" and convert it to a .wmv for playback in Windows.


    0
    ffmpeg -i input.ogv -qscale 0 output.wmv # convert .ogv to .wmv
    mpb · 2014-06-24 16:23:18 7
  • Today, I needed to reboot a Windoze machine on another continent which had no shutdown or restart options via "Start" in the remote desktop (the only options available were: "Logoff, Disconnect, or Lock"). Fortunately, I found how to shutdown and restart from the command line.


    -1
    C:\> shutdown /f /r /t 0
    mpb · 2014-04-02 22:35:00 9
  • By defining a function "gh" as shown here, it saves me typing "history | grep" every time I need to search my shell history because now I only have to type "gh". A nifty time saver :-) You can also add the "gh" function definition to your .bashrc so it is defined each time you login. (updated 2015_01_29: changed from hg to gh to avoid clash with that other hg command. mnemonic: gh = grep history) Show Sample Output


    4
    function gh () { history | grep $* ; } # gh or "grep history"
    mpb · 2014-04-02 15:17:31 11
  • Some information about robots. :-)


    6
    firefox about:robots
    mpb · 2013-07-07 14:12:36 6
  • Need to find a Mageia Linux mirror server providing Mageia 4 via rsync? Modify the "url=" string for the version you want. This shows i586 which is the 32bit version. If you want the 64bit version it is: url=http://mirrors.mageia.org/api/mageia.4.x86_64.list; wget -q ${url} -O - | grep rsync: Show Sample Output


    1
    url=http://mirrors.mageia.org/api/mageia.4.i586.list; wget -q ${url} -O - | grep rsync:
    mpb · 2013-05-20 16:19:05 7
  • Interesting to see which packages are larger than the kernel package. Useful to understand which RPMs might be candidates to remove if drive space is restricted. Show Sample Output


    1
    rpm -qa --queryformat '%{size} %{name}-%{version}-%{release}\n' | sort -k 1,1 -rn | nl | head -16
    mpb · 2013-03-19 21:10:54 6
  • I find it useful, when cleaning up deleting unwanted files to make more space, to list in size order so I can delete the largest first. Note that using "q" shows files with non-printing characters in name. In this sample output (above), I found two copies of the same iso file both of which are immediate "delete candidates" for me. Show Sample Output


    1
    ls -qahlSr # list all files in size order - largest last
    mpb · 2013-03-13 09:52:07 29
  • I find it very handy to be able to quickly see the most recently modified/created files in a directory. Note that the "q" option will reveal any files with non-printable characters in their filename. Show Sample Output


    7
    ls -qaltr # list directory in chronological order, most recent files at end of list
    mpb · 2013-02-25 14:14:44 13
  • Display the machine "hardware name" 32 or 64 bit. "x86_64" is shown on 64 bit machines "i686" is typically shown on 32 bit machines (although, you might also see "i386" or "i586" on older Linuxen). On other "unix-like" systems, other hardware names will be displayed. For example, on AIX, "uname -m" gives the "machine sequence number". For whatever reason, IBM decided that "uname -M" would give the machine type and model. (ref: http://www.ibm.com/developerworks/aix/library/au-aix-systemid.html ) On Sun Solaris, "uname -m" can be used to determine the chip type and "isainfo -v" will reveal if the kernel is 64 or 32 bit. (ref: http://www.ibiblio.org/pub/packages/solaris/sparc/html/32.and.64.bit.packages.html ) A more reliable way to determine "64-bit ness" across different Unix type systems is to compile the following simple C program: cat <<eeooff > bits.c /* * program bits.c * purpose Display "32" or "64" according to machine type * written January 2013 * reference http://www.unix.org/whitepapers/64bit.html */ /* hmm, curious that angle-brackets removed by commandlinefu.com data input processing? */ #include "/usr/include/stdio.h" long lv = 0xFFFFFFFF; main ( ) { printf("%2d\n",(lv < 0)?32:64); } eeooff Compile and run thusly: cc -o bits bits.c; ./bits Show Sample Output


    -4
    uname -m # display machine "hardware name"
    mpb · 2013-01-04 11:46:43 15
  • Display the holidays in December and January for UK/England (2012/2013). Most Linux distros have "gcal" in their package manager system. If not, it is available here: http://www.gnu.org/software/gcal Show Sample Output


    0
    gcal -K -q GB_EN December/2012-January/2013 # Holidays for Dec/2012 and Jan/2013 with week numbers
    mpb · 2012-11-07 18:01:31 4
  • Generate a table of random 10 character passwords Show Sample Output


    1
    pwgen 10 # generate a table of 10 character random passwords
    mpb · 2012-10-26 08:57:47 4
  • The vi key sequence "!}" will feed the block of lines from the current position to the next blank line to the command provided: in this case "sort -nut. -k 1,1 -k 2,2 -k 3,3 -k 4,4". The sort is ascending, numeric (-n), removing duplicates (-u), using "." as key delimiter (-t ."). The "-nut." is a memorable mnemonic :-). The same command (less the "!}") can, of course, be used from command line to sort a file of IP addresses in a text file. In the command line version, I found it also useful to remove blank lines and comment lines thusly: < IPaddresses.txt sed -e "/^#/d" -e "/^$/d" | sort -nut. -k 1,1 -k 2,2 -k 3,3 -k 4,4 # sort IP addresses


    0
    !}sort -nut. -k 1,1 -k 2,2 -k 3,3 -k 4,4
    mpb · 2012-08-12 16:16:24 7
  • When searching in vi, the search string gets highlighted but the highlighting can become a nuisance. By searching for the very unlikely pattern "^~" the highlighting is effectively switched off. Show Sample Output


    -5
    /^~
    mpb · 2012-08-02 21:10:23 10
  • I was surprised to find that with RedHat bash, I could not find any comment lines (begining with #) in my bash shell history. Surprised because in Mageia Linux this works. It turns out that RedHat's bash will keep comment lines if in my .bashrc, I define: export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?' Why have comment lines in shell history? It's a handy and convenient way to make proto-commands (to be completed later) and for storing brief text data that is searchable in shell history. Show Sample Output


    1
    export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?'
    mpb · 2011-10-18 19:58:39 869
  • [continued]...with "bin:" and line starting with "lp:". This specific example with /etc/passwd shows the power of sed to extract data from text files. Here we see an extract from /etc/passwd beginning with the line starting with "bin:" and ending with the line starting with "lp:". Note also, placing the STDIN redirection at the start of the command makes it easy to recall and modify the command parameters line in shell history. Show Sample Output


    0
    < /etc/passwd sed -n "/^bin:/,/^lp:/p"
    mpb · 2011-10-18 13:33:12 6
  • This command adds a urpmi media source called "google-talkplugin" to the urpmi configuration on Mandriva or Mageia. Needs to be run as root. We specify the option "--update" so that when Google provides a newer version of Google Talk plugin in their download system then running a system update (eg: "urpmi --auto-update") will result in our copy of Google Talk plugin getting updated (along with any other Mandriva/Mageia pending updates). To install Google Talk plugin from this source, use: urpmi google-talkplugin # install plugin used for voice and video Google chat via gmail Show Sample Output


    0
    urpmi.addmedia --update google-talkplugin http://dl.google.com/linux/talkplugin/rpm/stable/$(uname -m | sed -e "s/i.86/i386/")
    mpb · 2011-04-30 23:01:36 3
  •  1 2 > 

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

Generate soothing noise
Substitute 'brown' with 'pink' or 'white' according to your taste. I put this on my headphones when I'm working in an "open concept" office, where there are always three to five conversations going in earshot, or if I'm working somewhere it is "rude" of me to tell a person to turn off their cubicle radio.

Create a large test file (taking no space).

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"

Simple Video Surveillance by email
This takes a picture (with the web cam) every 5 minutes, and send the picture to your e-mail. Some systems support mail -a "References: " so that all video surveillance emails are grouped in a single email thread. To keep your inbox clean, it is still possible to filter and move to trash video surveillance emails (and restore these emails only if you really get robbed!) For instance with Gmail, emails sent to me+trash@gmail.com can be filtered with "Matches: DeliveredTo:me+trash@gmail.com"

Create a new file

Stream system sounds over rtmp
sox (SOund eXchange) can capture the system audio be it a browser playing youtube or from hardware mic and can pipe it to ffmpeg which encodes it into flv and send it over rtmp. Tested using Red5 rtmp server.

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

Fastest segmented parallel sync of a remote directory over ssh
Mirror a remote directory using some tricks to maximize network speed. lftp:: coolest file transfer tool ever -u: username and password (pwd is merely a placeholder if you have ~/.ssh/id_rsa) -e: execute internal lftp commands set sftp:connect-program: use some specific command instead of plain ssh ssh:: -a -x -T: disable useless things -c arcfour: use the most efficient cipher specification -o Compression=no: disable compression to save CPU mirror: copy remote dir subtree to local dir -v: be verbose (cool progress bar and speed meter, one for each file in parallel) -c: continue interrupted file transfers if possible --loop: repeat mirror until no differences found --use-pget-n=3: transfer each file with 3 independent parallel TCP connections -P 2: transfer 2 files in parallel (totalling 6 TCP connections) sftp://remotehost:22: use sftp protocol on port 22 (you can give any other port if appropriate) You can play with values for --use-pget-n and/or -P to achieve maximum speed depending on the particular network. If the files are compressible removing "-o Compression=n" can be beneficial. Better create an alias for the command.

Easy to extend one-liner for cron scripts that automate filesystem checking
This one-liner is for cron jobs that need to provide some basic information about a filesystem and the time it takes to complete the operation. You can swap out the di command for df or du if that's your thing. The |& redirections the stderr and stdout to the mail command. How to configure the variables. TOFSCK=/path/to/mount FSCKDEV=/dev/path/device or FSCKDEV=`grep $TOFSCK /proc/mounts | cut -f1 -d" "` MAILSUB="weekly file system check $TOFSCK "

Speed up upgrades for a debian/ubuntu based system.
Please install aria2c before you try the above command. On ubuntu the command to install aria2c would be: $sudo aptitude install aria2


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: