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:
cd to the folder containing the wav files, then convert them all to flac. yeah baby!
in ubuntu, to get the flac program just:
sudo apt-get install flac
flac file input formats are wav, aiff, raw, flac, oga and ogg
Find files recursively that were updated in the last hour ignoring SVN files and folders. Incase you do a full svn up on accident.
I've been using linux for almost a decade and only recently discovered that most terminals like putty, xterm, xfree86, vt100, etc., support hundreds of shades of colors, backgrounds and text/terminal effects.
This simply prints out a ton of them, the output is pretty amazing.
If you use non-x terminals all the time like I do, it can really be helpful to know how to tweak colors and terminal capabilities. Like:
echo $'\33[H\33[2J'
Usage example:
newest Desktop/*
Replace "-nt" with "-ot" for oldest.
Run
shopt -s dotglob
first to include dotfiles.
bash2 : for X in $(seq 1 5); do printf "%03g " "$X";done
bash3 : for X in {1..5}; do printf "%03g " "$X";done
bash4 : echo {001..5}
Usage:
mydir=/very/long/path/to/a/dir
cd mydir
I often need to cd where no man wants to go (i.e. long path). by enabling the shell option cdable_vars, I can tell cd to assume the destination is the name of a variable.
Get your server's fingerprints to give to users to verify when they ssh in. Publickey locations may vary by distro. Fingerprints should be provided out-of-band.
Alternative to the ping check if your firewall blocks ping. Uses curl to get the landing page silently, or fail with an error code. You can probably do this with wget as well.
If you omit the function name, the command will display all definitions
If you issue the "set" command, you'll see a list of variables and functions. This command displays just those functions' names.
I often need to extract a function from a bash script and this command will do it.
Really useful way to combine less and grep while browsing log files.
I can't figure out how to make it into a true oneliner so paste it into a script file called lgrep:
Usage:
lgrep searchfor file1 [file2 file3]
Advanced example (grep for an Exception in logfiles that starts with qc):
lgrep Exception $(find . -name "qc*.log")
For example, if you are the type who type ls very often, then
PROMPT_COMMAND=ls
will ls after every command you issue.
Place this in your .bash_profile and you can use it two different ways. If you issue 'h' on its own, then it acts like the history command. If you issue:
h cd
Then it will display all the history with the word 'cd'
Joker wants an email if the Brand X server is down. Set a cron job for every 5 mins with this line and he gets an email when/if a ping takes longer than 3 seconds.
Check out the usage of 'trap', you may not have seen this one much. This command provides a way to schedule commands at certain times by running them after sleep finishes sleeping. In the example 'sleep 2h' sleeps for 2 hours. What is cool about this command is that it uses the 'trap' builtin bash command to remove the SIGHUP trap that normally exits all processes started by the shell upon logout. The 'trap 1' command then restores the normal SIGHUP behaviour.
It also uses the 'nice -n 19' command which causes the sleep process to be run with minimal CPU.
Further, it runs all the commands within the 2nd parentheses in the background. This is sweet cuz you can fire off as many of these as you want. Very helpful for shell scripts.
This will comment out a line, specified by line number, in a given file.
I've used this a number of times troubleshooting user permissions. Instead of just 'su - user' you can throw another hyphen and stay in the original directory.
How often do you make a directory (or series of directories) and then change into it to do whatever? 99% of the time that is what I do.
This BASH function 'md' will make the directory path then immediately change to the new directory. By using the 'mkdir -p' switch, the intermediate directories are created as well if they do not exist.
This is how I typically grep. -R recurse into subdirectories, -n show line numbers of matches, -i ignore case, -s suppress "doesn't exist" and "can't read" messages, -I ignore binary files (technically, process them as having no matches, important for showing inverted results with -v)
I have grep aliased to "grep --color=auto" as well, but that's a matter of formatting not function.
The backtick operator, in general, will execute the text inside the backticks. On OS X, the pbpaste command will put the contents of the OS X clipboard to STDOUT. So if you put backticks around pbpaste, the text from the OS X clipboard is executed.
If you add the pipeline | pbcopy, the output from executing the command on the clipboard is placed back on the clipboard.
Note: make sure the clipboard is text only.
This pipeline will find, sort and display all files based on mtime. This could be done with find | xargs, but the find | xargs pipeline will not produce correct results if the results of find are greater than xargs command line buffer. If the xargs buffer fills, xargs processes the find results in more than one batch which is not compatible with sorting.
Note the "-print0" on find and "-0" switch for perl. This is the equivalent of using xargs. Don't you love perl?
Note that this pipeline can be easily modified to any data produced by perl's stat operator. eg, you could sort on size, hard links, creation time, etc. Look at stat and just change the '9' to what you want. Changing the '9' to a '7' for example will sort by file size. A '3' sorts by number of links....
Use head and tail at the end of the pipeline to get oldest files or most recent. Use awk or perl -wnla for further processing. Since there is a tab between the two fields, it is very easy to process.