Commands by bbbco (13)

  • Plain old `unzip` won't unzip output coming from STDOUT the ZIP file format includes a directory (index) at the end of the archive. This directory says where, within the archive each file is located and thus allows for quick, random access, without reading the entire archive. This would appear to pose a problem when attempting to read a ZIP archive through a pipe, in that the index is not accessed until the very end and so individual members cannot be correctly extracted until after the file has been entirely read and is no longer available. As such it appears unsurprising that most ZIP decompressors simply fail when the archive is supplied through a pipe. The directory at the end of the archive is not the only location where file meta information is stored in the archive. In addition, individual entries also include this information in a local file header, for redundancy purposes. From the `jar` manpage: > The jar command is a general-purpose archiving and compression tool, based on ZIP and the ZLIB compression format. JAR is smart enough to know how to handle these local file headers when the index is unavailable when reading through the pipe. (Most of the explanation in this description is taken from https://serverfault.com/a/589528/314226 , though they recommend using `bsdtar`, but that is not always available on systems) Show Sample Output


    0
    cat foo.zip | jar xv
    bbbco · 2019-01-14 22:08:19 33
  • Use dots to cd down directories instead of having to remember all of the pesky back slashes! Better yet, works on even and odd number of dots! Now, just estimate how far down you want to traverse. Show Sample Output


    1
    for i in {1..6};do c=;d=;for u in `eval echo {1..$i}`;do c="$c../";d="$d..";eval "$d(){ cd $c;}"; eval "$d.(){ cd $c;}";done;done
    bbbco · 2013-09-04 20:12:45 9

  • 12
    sudo dmidecode | grep Product
    bbbco · 2012-02-07 16:26:23 16
  • If you have ever edited a locally checked out version of a file to tweak it for testing purposes, and came back to it over a weekend, you might have forgotten what you exactly changed. This command helps you see the differences between the the checked in SVN version, and the one you tweaked. Show Sample Output


    0
    svn diff <FILE>
    bbbco · 2012-01-30 16:47:48 14
  • Ever need to get some text that is a specific number of characters long? Use this function to easily generate it! Doesn't look pretty, but sure does work for testing purposes! Show Sample Output


    0
    genRandomText() { a=( a b c d e f g h i j k l m n o p q r s t u v w x y z );f=0;for i in $(seq 1 $(($1-1))); do r=$(($RANDOM%26)); if [ "$f" -eq 1 -a $(($r%$i)) -eq 0 ]; then echo -n " ";f=0;continue; else f=1;fi;echo -n ${a[$r]};done;echo"";}
    bbbco · 2012-01-20 21:18:16 3
  • Prints a string indicating whether a command is an alias, keyword, function, builtin, or file. I have used this in my BASH scripts to allow an external parameter to define which function to run, and ensure that it is a valid function that can indeed be run. Show Sample Output


    1
    type -t $1
    bbbco · 2012-01-10 21:57:29 5
  • Use the -a flag to display all files, including hidden files. If you just want to display regular files, use a -1 (yes, that is the number one). Got this by RTFM and adding some sed magic. [bbbco@bbbco-dt ~]$ ls -a | sed "s#^#${PWD}/#" /home/bbbco/. /home/bbbco/.. /home/bbbco/2011-09-01-00-33-02.073-VirtualBox-2934.log /home/bbbco/2011-09-10-09-49-57.004-VirtualBox-2716.log /home/bbbco/.adobe /home/bbbco/.bash_history /home/bbbco/.bash_logout /home/bbbco/.bash_profile /home/bbbco/.bashrc ... [bbbco@bbbco-dt ~]$ ls -1 | sed "s#^#${PWD}/#" /home/bbbco/2011-09-01-00-33-02.073-VirtualBox-2934.log /home/bbbco/2011-09-10-09-49-57.004-VirtualBox-2716.log /home/bbbco/cookies.txt /home/bbbco/Desktop /home/bbbco/Documents /home/bbbco/Downloads ... Show Sample Output


    -9
    ls -a | sed "s#^#${PWD}/#"
    bbbco · 2011-12-16 22:19:06 6
  • Sometimes you need the full path to your script, regardless of how it was executed (which starting directory) in order to maintain other relative paths in the script. If you attempt to just use something simple like: STARTING_DIR="${0%/*}" you will only get the relative path depending on where you first executed the script from. You can get the relative path to the script (from your starting point) by using dirname, but you actually have to change directories and print the working directory to get the absolute full path. Show Sample Output


    0
    STARTING_DIR=$(cd $(dirname $0) && pwd)
    bbbco · 2011-11-30 17:35:15 5
  • Get a listing of all of your databases in Postgres and their sizes, ordering by the largest size first. Requires that you give the -d parameter a valid database name that you can connect to. Show Sample Output


    6
    psql -c "SELECT pg_database.datname, pg_database_size(pg_database.datname), pg_size_pretty(pg_database_size(pg_database.datname)) FROM pg_database ORDER BY pg_database_size DESC;" -d <ANYDBNAME>
    bbbco · 2011-11-30 15:22:48 5
  • Even simpler! Use du ... the -s and -c flags summarize and print a grand total of all files recursively. The -b flag prints in byte format. You can use the -h flag instead to print in human readable format. Show Sample Output


    2
    du -scb
    bbbco · 2011-06-27 14:20:11 116
  • Ever need to erase the contents of a file and start over from scratch? This easy command allows you to do so. Be warned! This will immediately erase all the contents of your file and start you over from scratch (i.e. your file will be at 0 bytes, like if you touch a file). Show Sample Output


    -3
    > [filename]
    bbbco · 2011-05-18 14:59:02 6
  • This is just a slight alternative that wraps all of #7917 in a function that can be executed Show Sample Output


    2
    anagram(){ s(){ sed 's/./\n\0/g'<<<$1|sort;};cmp -s <(s $1) <(s $2)||echo -n "not ";echo anagram; }; anagram foobar farboo;
    bbbco · 2011-02-17 15:10:43 5
  • Sets an alias to remote desktop to the specified console, along with options to ensure the RDP session takes up the whole screen, includes a home directory mapping, and clipboard mappings. Show Sample Output


    0
    alias rdp='rdesktop -u <user> -g 1600x1200 -D -r disk:home=/home -r clipboard:PRIMARYCLIPBOARD'
    bbbco · 2011-02-04 16:22:49 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

force unsupported i386 commands to work on amd64
The above was done using the i386 flashplayer plugin, and was installed on a AMD64 machine running an AMD64 kernel and AMD64 programs. the resulting plugin install ultimately didn't work for swiftfox (but worked for iceweasel) without also covering it with a nspluginwrapper which took a bit of fenangaling to get to work (lots of apt-getting) but it is a nice feature to be able to trick installers that think you need i386 into running on a amd64, or at least attempting to run on amd64. Enjoy

Gets the english pronunciation of a phrase
Usage examples: say hello say "hello world" say hello+world

Generat a Random MAC address
Generate a random MAC address with capital letters

Extract tarball from internet without local saving

Check whether laptop is running on battery or cable
The original proc file doesn't exist on my system.

Install pip with Proxy
Installs pip packages defining a proxy

Track X Window events in chosen window
After executing this, click on a window you want to track X Window events in. Explaination: "xev will track events in the window with the following -id, which we get by greping window information obtained by xwininfo"

dont execute command just add it to history as a comment, handy if your command is not "complete" yet

list files recursively by size

Get the IP address
gives u each configured IP in a seperate line.


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: