commandlinefu.com is the place to record those command-line gems that you return to again and again.
Delete that bloated snippets file you've been using and share your personal repository with the world. 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.
If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/
You can sign-in using OpenID credentials, or register a traditional username and password.
First-time OpenID users will be automatically assigned a username which can be changed after signing in.
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
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:
When you have different digital cameras, different people, friends and you want to merge all those pictures together, then you get files with same names or files with 3 and 4 digit numbers etc. The result is a mess if you copy it together into one directory.
But if you can add an offset to the picture number and set the number of leading zeros in the file name's number then you can manage.
OFFS != 0 and LZ the same as the files currently have is not supported. Or left as an exercise, hoho ;)
I love NF="${NF/#+(0)/}",it looks like a magic bash spell.
usage examples
ls largedir |rd
lynx -dump largewebsite.com |rd
rd < largelogfile
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)
first off, if you just want a random UUID, here's the actual command to use:
uuidgen
Your chances of finding a duplicate after running this nonstop for a year are about the same as being hit by a meteorite before finishing this sentence
The reason for the command I have is that it's more provably unique than the one that uuidgen creates. uuidgen creates a random one by default, or an unencrypted one based on time and network address if you give it the -t option.
Mine uses the mac address of the ethernet interface, the process id of the caller, and the system time down to nanosecond resolution, which is provably unique over all computers past, present, and future, subject to collisions in the cryptographic hash used, and the uniqueness of your mac address.
Warning: feel free to experiment, but be warned that the stdin of the hash is binary data at that point, which may mess up your terminal if you don't pipe it into something. If it does mess up though, just type
reset
I have this as a file called deletekey in my ~/bin.
Makes life a little easier.
This will show the amount of physical RAM that is left unused by the system.
One of the first functions programmers learn is how to print a line. This is my 100% bash builtin function to do it, which makes it as optimal as a function can be. The COLUMNS environment variable is also set by bash (including bash resetting its value when you resize your term) so its very efficient. I like pretty-output in my shells and have experimented with several ways to output a line the width of the screen using a minimal amount of code. This is like version 9,000 lol.
This function is what I use, though when using colors or other terminal features I create separate functions that call this one, since this is the lowest level type of function. It might be better named printl(), but since I use it so much it's more optimal to have the name contain less chars (both for my programming and for the internal workings).
If you do use terminal escapes this will reset to default.
tput sgr0
For implementation ideas, check my
http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html
This one liner; combines all sequentially numbered files; in this example IMG_0001.png to IMG_1121.png by generating the shell script, making the shell script executable and then running the shell script to combine the 1121 png into a single png file named _final.png
tested on Mac OS X 10.6.3 with ImageMagick 6.5.8-0 2009-11-22 Q16 http://www.imagemagick.org
This provides a way to sort output based on the length of the line, so that shorter lines appear before longer lines. It's an addon to the sort that I've wanted for years, sometimes it's very useful. Taken from my http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html
this command prints itself out. it doesn't need to be stored in a file and it isn't as easy as
echo $BASH_COMMAND
for information on quines see http://en.wikipedia.org/wiki/Quine_(computing)
A cronjob command line to email someone when a webpages homepage is updated.
I created this command to give me a quick overview of how many file types a directory, and all its subdirectories, contains. It works based off file extension, rather than file(1)'s magic output, because it ended up being more accurate and less confusing.
Files that don't have an ext (README) are generally not important for me to want to count, but you're free to customize this fit your needs.
Pure Bash
This will print a row of characters the width of the screen without using any external executables. In some cases, COLUMNS may not be set. Here is an alternative that uses tput to generate a default if that's the case. And it still avoids using tr.
printf -v row "%${COLUMNS:-$(tput cols)}s"; echo ${row// /#}
The only disadvantage to either one is that they create a variable.
Add this to a fiend's .bashrc.
PROMPT_COMMAND will run just before a prompt is drawn.
RANDOM will be between 0 and 32768; in this case, it'll run about 1/10th of the time.
\033 is the escape character. I'll call it \e for short.
\e7 -- save cursor position.
\e[%d;%dH -- move cursor to absolute position
\e[4%dm \e[m -- draw a random color at that point
\e8 -- restore position.
The function 'box' takes either one or two arguments. The first argument is a line of text to be boxed, the second argument (optional) is a character to use to draw the box. By default, the drawing character will be '='.
The function 'n()' is a helper function used to draw the upper and lower lines of the box, its arguments are a length, and an character to print. (I used 'n' because 'line', 'ln' and 'l' are all commonly used)
underline() will print $1, followed by a series of '=' characters the width of $1. An optional second argument can be used to replace '=' with a given character.
This function is useful for breaking lots of data emitted in a for loop into sections which are easier to parse visually. Let's say that 'xxxx' is a very common pattern occurring in a group of CSV files.
You could run
grep xxxx *.csv
This would print the name of each csv file before each matching line, but the output would be hard to parse visually.
for i in *.csv; do printf "\n"; underline $i; grep "xxxx" $i; done
Will break the output into sections separated by the name of the file, underlined.
This one uses hex conversion to do the converting and is in shell/sed only (should probably still use the python/perl version).
Prompts the user for username and password, that are then exported to http_proxy for use by wget, yum etc
Default user, webproxy and port are used.
Using this script prevent the cleartext user and pass being in your bash_history and on-screen