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:
Uses find, plutil and xpath.
Note: Some applications don't have proper information. system_profiler might be better to use.
It's a bit slow query.
Due to command length limit, I removed -name "*.app" and CFBundleName.
Avoids the nested 'find' commands but doesn't seem to run any faster than syssyphus's solution.
This version combines the best of the other suggestions and adds these features:
1. It scans a /16 subnet
2. It is very fast by running the ping commands in the background, running them in parallel.
3. Does not use the "-W" option as that's not available in older ping versions (I needed this for OS X 10.5)
Handles spaces in file names and directories. Optionally change directories as well by pipe to tr from dirname.
This command explains how to manage some asynchronous PID in a global process.
The command uses 4 processes in a global process. The asynchronous scripts are simulated by a time.sh script
more infos :
http://code-esperluette.blogspot.fr/2012/03/bash-gestion-de-processus-asynchrones.html
On Debian/Ubuntu the pygments script is called pygmentize and can be found in the python-pygments package.
For an overview of all available lexers, formatters, styles and filters use
pygmentize -L
Here is an example using more options
pygmentize -f 256 -l sh -F whitespace:spaces=True,tabs=True -O style=borland ~/.bashrc | less -R
or
echo '127.0.0.1 facebook.com' | sudo tee -a /etc/hosts
Do not execute this command if you don't know what you are doing.
this requires the use of a throwaway file.
it outputs a shell function.
assuming the throwaway file is f.tmp
usage: >f.tmp;lso f.tmp > f.tmp; . f.tmp;rm f.tmp;lso -l ...
notes:
credit epons.org for the idea. however his version did not account for the sticky bit and other special cases.
many of the 4096 permutations of file permissions make no practical sense. but chmod will still create them.
one can achieve the same sort of octal output with stat(1), if that utility is available.
here's another version to account for systems with seq(1) instead of jot(1):
lso(){
case $# in
1)
{ case $(uname) in
FreeBSD)
jot -w '%04d' 7778 0000 7777 ;;
*)
seq -w 0000 7777 ;;
esac; } \
|sed '
/[89]/d
s,.*,printf '"'"'& '"'"';chmod & '"$1"';ls -l '"$1"'|sed s/-/./,' \
|sh \
|{
echo "lso(){";
echo "ls \$@ \\";
echo " |sed '";
sed '
s, ,@,2;
s,@.*,,;
s,\(.* \)\(.*\),s/\2/\1/,;
s, ,,';
echo \';
echo };
};
;;
*)
echo "usage: lso tmp-file";
;;
esac;
}
this won't print out types[1]. but its purpose is not to examine types. its focus is on mode and its purpose is to make mode easier to read (assuming one finds octal easier to read).
1. one could of course argue "everything is a file", but not always a "regular" one. e.g., a "directory" is really just a file comprising a list.
Extracting .gz files and placing the output in another directory in one command line is convenient thing.
I just followed some how-to to install Nagios on Ubuntu Lucid Lynx but they give the method to install from archives. I wished to install from the repository. If you do so some files are missing. I've not tested yet but this is an example command line I did to extract
sudo sh -c 'gunzip -c /usr/share/doc/nagios3-common/examples/template-object/templates.cfg.gz > /etc/nagios3/objects/templates.cfg'
We need privilege to write the destination file.
-exec sh -c 'var={}; do something with var' lets you do things in a sub-shell
while it's faster to type, I'm not sure if dozens of subshells execute quicker than the while loops.
One of my friends committed his code in the encoding of GB2312, which broke the build job. I have to find his code and convert.
This is useful if you'd like to see the output of a script while you edit it. Each time you save the file the command is executed. I thought for sure something like this already exists - and it probably does. I'm on an older system and tend to be missing some useful things.
Examples:
ontouchdo yourscript 'clear; yourscript somefiletoparse'
Edit yourscript in a separate window and see new results each time you save.
ontouchdo crufty.html 'clear; xmllint --noout crufty.html 2>&1 | head'
Keep editing krufty.html until the xmllint window is empty.
Note: Mac/bsd users should use stat -f%m. If you don't have stat, you can use perl -e '$f=shift; @s=stat($f); print "$s[9]\n";' $1
Grabs the cmdline used to execute the process, and the environment that the process is being run under. This is much different than the 'env' command, which only lists the environment for the shell. This is very useful (to me at least) to debug various processes on my server. For example, this lets me see the environment that my apache, mysqld, bind, and other server processes have.
Here's a function I use:
aa_ps_all () { ( cd /proc && command ps -A -opid= | xargs -I'{}' sh -c 'test $PPID -ne {}&&test -r {}/cmdline&&echo -e "\n[{}]"&&tr -s "\000" " "<{}/cmdline&&echo&&tr -s "\000\033" "\nE"<{}/environ|sort&&cat {}/limits' ); }
From my .bash_profile at http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html
Do the same as pssh, just in shell syntax.
Put your hosts in hostlist, one per line.
Command outputs are gathered in output and error directories.
Gets you the latest of everything, and removes any remaining junk. The "sh -c" part is so that you'll only run a single sh command, so you won't get asked more than once for the password.
This command is suitable to use as application launching command for a desktop shortcut. It checks if the application is already running by pgrepping its process ID, and offer user to kill the old process before starting a new one.
It is useful for a few x11 application that, if re-run, is more likely a mistake. In my example, x2vnc is an x11 app that does not quit when its connection is broken, and would not work well when a second process establish a second connection after the first broken one.
The LC_ALL=C for xmesseng is necessary for OpenSUSE systems to avoid a bug. If you don't find needing it, remove the "env LC_ALL=C" part
USAGE: $ sudor your command
This command uses a dirty hack with history, so be sure you not turned it off.
WARNING!
This command behavior differ from other commands. It more like text macro, so you shouldn't use it in subshells, non-interactive sessions, other functions/aliases and so on. You shouldn't pipe into sudor (any string that prefixes sudor will be removed), but if you really want, use this commands:
proceed_sudo () { sudor_command="`HISTTIMEFORMAT=\"\" history 1 | sed -r -e 's/^.*?sudor//' -e 's/\"/\\\"/g'`" ; pre_sudor_command="`history 1 | cut -d ' ' -f 5- | sed -r -e 's/sudor.*$//' -e 's/\"/\\\"/g'`"; if [ -n "${pre_sudor_command/ */}" ] ; then eval "${pre_sudor_command%| *}" | sudo sh -c "$sudor_command"; else sudo sh -c "$sudor_command" ;fi ;}; alias sudor="proceed_sudo # "