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.

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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags



Commands tagged script from sorted by
Terminal - Commands tagged script - 29 results
scriptName="reorder_files.sh"; echo -e '#!/bin/sh\n' > "${scriptName}"; cat files.txt | while read file; do echo "touch ${file}; sleep 0.5;" >> "${scriptName}"; done; chmod +x "${scriptName}";
2016-04-19 11:52:00
User: programmer
Functions: cat chmod echo read

Create a bash script to change the modification time for each file in 'files.txt' such that they are in the same order as in 'files.txt'

File name for bash script specified by variable, 'scriptName'. It is made an executable once writing into it has been completed.

ssh(){ L="\$HOME/logs/$(date +%F_%H:%M)-$USER";/usr/bin/ssh -t "$@" "mkdir -p \"${L%/*}\";screen -xRRS $USER script -f \"$L\"";}
2015-10-14 13:14:29
User: flatcap
Functions: ssh

A wrapper around ssh to automatically provide logging and session handling.

This function runs ssh, which runs screen, which runs script.


The logs and the screen session are stored on the server.

This means you can leave a session running and re-attach to it later, or from another machine.




* Log sessions on a remote server

* Transparent - nothing extra to type

* No installation - nothing to copy to the server beforehand



* Function wrapper delegating to ssh

- so nothing to remember

- uses .ssh/config as expected

- passes your command line option to ssh

* Self-contained: no scripts to install on the server

* Uses screen(1), so is:

- detachable

- re-attachable

- shareable

* Records session using script(1)

* Configurable log file location, which may contain variables or whitespace

L="$HOME" # local variable

L="\$HOME" # server variable

L="some space"



* Log dir/file may not contain '~' (which would require eval on the server)



The sessions are named by the local user connecting to the server.

Therefore if you detach and re-run the same command you will reconnect to your original session.

If you want to connect/share another's session simply run:

USER=bob ssh [email protected]


The command above is stripped down to an absolute minimum.

A fully expanded and annotated version is available as a Gist (git pastebin):



If you want to add timing info to script, change the command to:

ssh(){ L="\$HOME/logs/$(date +%F_%H:%M)-$USER";/usr/bin/ssh -t "$@" "mkdir -p \"${L%/*}\";screen -xRRS $USER script --timing=\"$L-timing\" -f \"$L\"";}
alias m='screen -S $$ -m script'
2015-10-01 18:07:18
Functions: alias
Tags: screen script

A quick alias I use right before logging into a server so that I have a log of the transactions as well as the ability to re-connect from another computer. Useful for when your boss says "what commands did you run again on that server?" and you had already closed the terminal ;)

I wrapped it in a script now, with more features, but this is the heart of it.

Never leave home without it.

quickscript () { filename="$1"; history | cut -c 8- | sed -e '/^###/{h;d};H;$!d;x' | sed '$d' > ${filename:?No filename given} }
2014-02-09 12:19:29
User: joedhon
Functions: cut sed

In order to write bash-scripts, I often do the task manually to see how it works. I type ### at the start of my session.

The function fetches the commands from the last occurrence of '###', excluding the function call. You could prefix this with a here-document to have a proper script-header.

Delete some lines, add a few variables and a loop, and you're ready to go.

This function could probably be much shorter...

script -qf | tee >(nc -l -p 5000)
tb send xmpp:[email protected]
2013-04-08 00:29:43

After installing Termbeamer (see termbeamer.com) you can use it to share a terminal session with one or more others even from behind a firewall or NAT.

sudo apt-get <apt-get command and options> --print-uris -qq | sed -n "s/'\([^ ]\+\)' \([^ ]\+\) \([^ ]\+\) MD5Sum:\([^ ]\+\)/wget -c \1/p" > dowload_deb_list.txt
shelr record
while read ; do python <script> ; done
2012-02-23 22:29:09
User: Zulu
Functions: python read

Very useful for test a script. After launch this command, you only have to press ENTER for launch your script again. I work with screen and tape ENTER instead of '!!'+ENTER

If you break your script with CTRL-C, it will wait for press ENTER and will re-launch

You can write like it : while read -p "Press ENTER" ; do python ; done

[ $1 ] && my_dir=$1
2011-11-30 15:02:20

If Argument $1 is supplied, assign it to variable. Otherwise continue on.

script -f /tmp/foo; tail -f /tmp/foo
2011-11-22 15:16:08
Functions: script tail
Tags: script tail -f

script -f /tmp/foo will place all output of the terminal, including carriage returns, to a file. This file can be tail dash-eff'ed by one or more other terminals to display the information of the main terminal. Good way to share one's screen on short notice.

Note: This produces a very accurate output, but that includes depending on the size of your terminal to be the same. You can clear screens or even resize the terminal for others using this function; I use it in conjunction with the "mid" command in my list.

param=${param:-$(read -p "Enter parameter: "; echo "$REPLY")}
2011-09-08 20:48:31
User: frans
Functions: echo read

Can be used for command line parameters too.

If you have a more complicated way of entering values (validation, GUI, ...), then write a function i.e. EnterValue() that echoes the value and then you can write:

mkfifo foo; script -f foo
2011-09-08 02:51:44
User: realist
Functions: mkfifo script

One person does `mkfifo foo; script -f foo' and another can supervise real-time what is being done using `cat foo'.

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

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].

history | tail -(n+1) | head -(n) | sed 's/^[0-9 ]\{7\}//' >> ~/script.sh
2011-06-08 13:40:58
Functions: head sed tail

Uses history to get the last n+1 commands (since this command will appear as the most recent), then strips out the line number and this command using sed, and appends the commands to a file.

mydir=$(cd $(dirname ${BASH_SOURCE:-$0});pwd)
2011-04-27 16:33:38
User: xeor
Functions: cd dirname
Tags: cd script pwd

I submitted a command like this without $0 if $BASH_SOURCE is unset. Therefor, it did only work when using ./script, not using 'sh script'. This version handles both, and will set $mydir in a script to the current working directory. It also works on linux, osx and probably bsd.

script -qf >(nc -ub 5000)
ssh -T [email protected] < script.sh
2011-01-10 20:09:55
User: dlebauer
Functions: ssh

Run local scripts on remote server. "-T Disable pseudo-tty allocation"

script -qf | tee >(nc -kl 5000) >(nc -kl 5001) >(nc -kl 5002)
2010-10-11 07:55:30
User: clvv
Functions: script tee
Tags: tee pipe script nc

run 'nc yourip 5000', 'nc yourip 5001' or 'nc yourip 5002' elsewhere will produce an exact same mirror of your shell. This is handy when you want to show someone else some amazing stuff in your shell without giving them control over it.

find "$1" -iname "*$2*"
find . -iname \*${MYVAR}\* -print
2010-08-04 05:43:51
User: Buzzcp
Functions: find

You define your variable MYVAR with the desired search pattern:


...which can then be searched with the find command.

This is useful if you in a script, where you want the arguments to be fed into the find command.

The provided search is case insensitive (-iname) and will find all files and directories with the pattern MYVAR (not exact matches). This may go without saying, but if you want exact matches remove the \* and if you want case sensitive, use the -name argument.

curl --cookie name=<cookie_value> --data-urlencode name=my_post_key=<post_key>\&delete=1\&submit=Delete+Now\&action=deletepost\&pid=$c --user-agent Firefox\ 3.5 --url http://url/editpost.php?my_post_key=<post_key>\&delete=1\&submit=Delete+Now\&action=dele
2010-07-14 01:50:48
User: mrlockfs

As a user, deletes all your posts from a MyBB board (provided you have the search page listings of all your posts saved into the same directory this command is run from). Full command:

for i in *; do cat $i | grep pid | sed -e 's/;/\ /g' -e 's/#/\ /g' -e 's/pid=/\ /g' | awk -F ' ' '{print $2}' >> posts.txt; done; for c in `cat posts.txt`; do curl --cookie name= --data-urlencode name=my_post_key=\&delete=1\&submit=Delete+Now\&action=deletepost\&pid=$c --user-agent Firefox\ 3.5 --url http://url/editpost.php?my_post_key=\&delete=1\&submit=Delete+Now\&action=deletepost\&pid=$c; sleep 2s; done; echo

(IFS=; sed 's/^[]0;[^^G]*^G/^M/g' <SessionLog> | while read -n 1 ITEM; do [ "$ITEM" = "^M" ] && ITEM=$'\n'; echo -ne "$ITEM"; sleep 0.05; done; echo)
2010-01-20 16:11:32
User: jgc
Functions: echo read sed sleep
Tags: read script

This command will play back each keystroke in a session log recorded using the script command. You'll need to replace the ^[ ^G and ^M characters with CTRL-[, CTRL-G and CTRL-M. To do this you need to press CTRL-V CTRL-[ or CTRL-V CTRL-G or CTRL-V CTRL-M.

You can adjust the playback typing speed by modifying the sleep.

If you're not bothered about seeing each keypress then you could just use:

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

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

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.)