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

All commands from sorted by
Terminal - All commands - 11,951 results
file =top
2009-07-15 21:29:23
User: libdave
Functions: file
4

Say you want to execute 'file' on the command 'top' (to determine what type of file it is); but you don't know where 'top' resides: preface the argument with = and zsh will implicitly prepend the path.

7z x -so testfile.tar.7z | tar tvf -
2009-07-15 21:00:58
User: slashdot
Functions: tar
2

Sometimes it is handy to be able to list contents of a tar file within a compressed archive, such as 7Zip in this instance, without having to extract the archive first. This is especially helpful when dealing with larger sized files.

print -s "PATH='$PATH'"
2009-07-15 16:38:23
User: libdave
0

say you want to edit your PATH variable using bash/zsh commandline editing, this will put something like this in history so you can edit it:

PATH=/bin:/usr/bin:/usr/sbin

to make this a shell function such that:

eev HOME

will put /home/dave in the last history event:

eev()

{

print -s "$1='$(eval echo \$$1)'"

}

export HISTCONTROL=ignoreboth
2009-07-15 16:05:03
User: ioggstream
Functions: export
Tags: history
4

Don't track in history commands starting with whitespace.

Moreover ignore duplicates from history.

To be set in .bashrc

ex.

$ export HISTCONTROL=ignoreboth

$   echo antani

$   history|grep -c antani

vi `!!`
2009-07-15 15:20:58
User: libdave
Functions: vi
0

say you've just found all the config files with this command

find . -name '*.config'

and you need to edit them all

vi `!!`

will re-execute the command and present them to vi in the argument list

don't use if the list is really long as it may overflow the command buffer

grep -rc logged_in app/ | cut -d : -f 2 | awk '{sum+=$1} END {print sum}'
2009-07-15 14:16:44
User: terceiro
Functions: awk cut grep
-2

grep's -c outputs how may matches there are for a given file as "file:N", cut takes the N's and awk does the sum.

dos2unix <filenames>
vim $(grep test *)
2009-07-15 10:15:04
User: goatboy
Functions: grep test vim
Tags: vim grep
4

I often use "vim -p" to open in tabs rather than buffers.

diff <(echo "$a") <(echo "$b")
2009-07-15 07:26:23
User: olorin
Functions: diff echo
25

You got some results in two variables within your shell script and would like to find the differences? Changes in process lists, reworked file contents, ... . No need to write to temporary files. You can use all the diff parameters you'll need. Maybe anything like $ grep "^>"

is helpful afterwards.

time dd if=/dev/zero of=blah.out oflag=direct bs=256M count=1
2009-07-15 07:17:32
User: olorin
Functions: dd time
0

Let dd use direct I/O to write directly to the disk without any caching. You'll encounter very different results with different block sizes (try with 1k, 4k, 1M, ... and appropriate count values).

for i in `rpm -qva | sort ` ; do ; echo "===== $i =====" ; rpm -qvl $i ; done > /tmp/pkgdetails
2009-07-14 20:34:55
User: tkunz
Functions: echo rpm sort
0

This will create the file /tmp/pkgdetails, which will contain a listing of all the files installed on your RPM-based system (RedHat, Fedora, CentOS, etc). Useful should the RPM system/database become corrupted to find which package installed which files.

dd if=/dev/zero bs=256M count=1 | nc [remoteIP] [remotePort] and on the other host nc -l port >/dev/null
2009-07-14 20:30:52
User: tkunz
Functions: dd host
0

Note, the [remotePort] should be opened in the firewall first. First, start the destination box listening, then fire off the sending box. Data from the /dev/zero device in memory of the source machine is read out using dd, sent over the network with nc, and read back in from the other side of the network with nc, going to the /dev/null device. Essentially, it is a memory-network-memory copy operation, the output of dd will tell you how fast your network really is performing.

dpkg -S `which nm` | cut -d':' -f1 | (read PACKAGE; echo "[${PACKAGE}]"; dpkg -s "${PACKAGE}"; dpkg -L "${PACKAGE}") | less
2009-07-14 20:21:48
User: mohan43u
Functions: cut echo read
Tags: which dpkg
2

In Debian based distros, this command will list 'binutils' package details which contains 'nm' command. You can replace 'nm' to any other command.

time (dd if=/dev/zero of=blah.out bs=256M count=1 ; sync )
2009-07-14 20:19:23
User: tkunz
Functions: dd sync time
2

Depending on the speed of you system, amount of RAM, and amount of free disk space, you can find out practically how fast your disks really are. When it completes, take the number of MB copied, and divide by the line showing the "real" number of seconds. In the sample output, the cached value shows a write speed of 178MB/s, which is unrealistic, while the calculated value using the output and the number of seconds shows it to be more like 35MB/s, which is feasible.

(cd /source/dir ; tar cvf - .)|(cd /dest/dir ; tar xvpf -)
/opt/psa/bin/pleskbackup server -v --output-file=plesk_server.bak
sudo nmap -sS 192.168.0.10 -D 192.168.0.2
2009-07-14 17:37:34
User: sedcommand
Functions: sudo
7

Scan for open ports on the target device/computer (192.168.0.10) while setting up a decoy address (192.168.0.2). This will show the decoy ip address instead of your ip in targets security logs. Decoy address needs to be alive. Check the targets security log at /var/log/secure to make sure it worked.

ps ax -o "%p %U %u %x %c %n"
2009-07-14 17:21:59
Functions: ps
4

ps command gives the possibility to display information with custom formatting with the -o options followed by the format specifier list.

pbpaste | ssh user@hostname 'cat > ~/my_new_file.txt'
2009-07-14 16:32:03
User: mikedamage
Functions: ssh
Tags: ssh osx clipboard
3

Redirects the contents of your clipboard through a pipe, to a remote machine via SSH.

tar cf - /path/to/data | 7z a -si archivename.tar.7z
2009-07-14 14:21:30
User: slashdot
Functions: tar
8

Using 7z to create archives is OK, but when you use tar, you preserve all file-specific information such as ownership, perms, etc. If that's important to you, this is a better way to do it.

dvd+rw-format -force /dev/dvd1
export LANG=C; grep string longBigFile.log
2009-07-14 12:48:02
User: ioggstream
Functions: export grep
Tags: grep LANG
0

greps using only ascii, skipping the overhead of matching UTF chars.

Some stats:

$ export LANG=C; time grep -c Quit /var/log/mysqld.log

7432

real 0m0.191s

user 0m0.112s

sys 0m0.079s

$ export LANG=en_US.UTF-8; time grep -c Quit /var/log/mysqld.log

7432

real 0m13.462s

user 0m9.485s

sys 0m3.977s

Try strace-ing grep with and without LANG=C

git ls-files -z --deleted | xargs -0 git rm
2009-07-14 08:29:38
User: blindgaenger
Functions: xargs
Tags: git
5

I've used technicalpickles command a lot, but this one handles whitespaces in filenames. I'm sure you want to create an alias for it :)

find . -type f ! -name "*html"
find . -name '*.html' -print0| xargs -0 -L1 cat |sed "s/[\"\<\>' \t\(\);]/\n/g" |grep "http://" |sort -u
2009-07-14 07:00:15
User: jamespitt
Functions: cat find grep sed sort xargs
4

Just a handy way to get all the unique links from inside all the html files inside a directory. Can be handy on scripts etc.