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 by knoppix5 from sorted by
Terminal - Commands by knoppix5 - 29 results
find . -type d| while read i; do echo $(ls -1 "$i"|wc -m) $(du -s "$i"); done|sort -s -n -k1,1 -k2,2 |awk -F'[ \t]+' '{ idx=$1$2; if (array[idx] == 1) {print} else if (array[idx]) {print array[idx]; print; array[idx]=1} else {array[idx]=$0}}'
2014-02-25 22:50:09
User: knoppix5
Functions: awk du echo find ls read sort wc
0

Very quick! Based only on the content sizes and the character counts of filenames. If both numbers are equal then two (or more) directories seem to be most likely identical.

if in doubt apply:

diff -rq path_to_dir1 path_to_dir2

AWK function taken from here:

http://stackoverflow.com/questions/2912224/find-duplicates-lines-based-on-some-delimited-fileds-on-line

(sed 's/#.*//g'|sed '/^ *$/d'|tr '\n' ';'|xargs echo) < script.sh
2013-10-26 23:23:51
User: knoppix5
Functions: sed tr xargs
5

Opposite:

Convert an one-liner to script:

foo() { <one-liner> ; }

...

typeset -f foo

...

unset -f foo
/foo
2013-10-09 08:40:06
User: knoppix5
Tags: man search
-3

Will highlight search term i.e. "foo" across entire man page (scroll up or down to see)

/term_to_search_for+Enter

That is how to find things ;)

/Enter

(slash+empty string+Enter) will jump to the next occurence of previous searched item.

rsync -v -r --size-only --compare-dest=../A/ B/ C/
2013-09-10 21:41:16
User: knoppix5
Functions: rsync
7

Assumed dir A, B, C are subdirs of the current dir

Exact syntax of the command is:

rsync -v -r --size-only --compare-dest=/path_to_A/A/ /path_to_B/B/ /path_to_C/C/

(do not omit end-slashes, since that would copy only the names and not the contents of subdirs of dir B to dir C)

You can replace --size-only with --checksum for more thorough file differences validation

Useful switch:

-n, --dry-run perform a trial run with no changes made

echo "template file: ";read tpl;echo "new file(s separated w. space):"; read fl;touch $fl;find $fl -exec cp -ap $tpl "{}" \;
2013-03-08 10:00:36
User: knoppix5
Functions: cp echo find read touch
0

make a bunch of files with the same permissions, owner, group, and content as a template file

(handy if you have much to do w. .php, .html files or alike)

sed -i -n '/%/p' *.php
hexdump -v -e '"%u"' </dev/urandom|fold -60|head -n 30|sed 's/\(.\{2\}\)/\1 /g'
2013-02-16 12:56:24
User: knoppix5
Functions: fold head hexdump sed
6

or

od /dev/urandom -w60 -An|sed 's/ ..../ /g'|head -n 30

(this one lacks digits 8 and 9)

for count in $(seq 2 1001); do espeak "$count sheeps";sleep 2;done
tr -s ' \t' <1.txt >2.txt
2012-12-19 07:36:00
User: knoppix5
Functions: tr
2

Remove empty lines additionally:

tr -s ' \t\n' <1.txt >2.txt

identical with:

tr -s '[:space:]' <1.txt >2.txt

To "clean perfectly" a text or code file, You can combine this command with

another one:

while read l; do echo -e "$l"; done <1.txt >2.txt

(= remove all leading and trailing spaces or tabs from all lines of a text file)

a=$(stty -a -F /dev/tty1| awk -F'[ ;]' '/columns/ { print $9 }'); fold -w$a /dev/vcs1;echo
source ~/.bashrc
2012-10-01 08:30:19
User: knoppix5
4

Any changes to BASH shell made in .bashrc will be active in the current terminal window from the moment you execute this command, ie. aliases, prompt settings etc. No need to restart terminal.

(In BASH 'source' simile to 'eval' lets you generally execute any bunch of commands stacked in a text file).

tree -ifs --noreport .|sort -n -k2
2012-05-04 09:18:39
User: knoppix5
Functions: sort
1

or

tree -ifsF --noreport .|sort -n -k2|grep -v '/$'

(rows presenting directory names become hidden)

for a in $(find . -xdev -type f -printf '%i\n'|sort|uniq -d);do find . -xdev -inum $a -printf '%s %i %m %n %U %G %AD %Ar %p\n';done|sort -n|awk '{if(x!=$2){print "---"};x=$2;print $0}'
2012-04-09 12:52:07
User: knoppix5
Functions: awk find sort uniq
0

The listing will be nice separated with dashes in chunks of identical files.

Output format:

Size Inode Mode Count_of_identical_files UID GID Date Time Path/Filename

hardlink -vpot .
2012-04-09 12:40:38
User: knoppix5
1

Meaning of switches (see man page too):

v verbose

p ignore mode (permissions)

o ignore owner, group

t ignore time of modification

Disadvantage: If you modify any linked file, this will propagate to all other files which occupy the same space.

for a in $(cat sample.txt); do echo "$(wc -m<<<$a) $a";done|sort -n
2012-03-15 08:51:42
User: knoppix5
Functions: cat echo sort
0

optionally you can add

|cut -d' ' -f2|uniq

to the end of the command line.

strings -f sample.txt
while read l; do echo -e "$l"; done <1.txt >2.txt
2012-03-13 14:27:49
User: knoppix5
Functions: echo read
Tags: bash read
5

Bash only, no sed, no awk. Multiple spaces/tabs if exists INSIDE the line will be preserved. Empty lines stay intact, except they will be cleaned from spaces and tabs if any available.

GET www.example.com
GET checkip.dyndns.org
2012-03-07 07:55:42
User: knoppix5
2

Clean output, if used in scripts:

GET checkip.dyndns.org|grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'

or

GET icanhazip.com
fuser -km /media/sdb1
2012-02-27 13:41:05
User: knoppix5
Functions: fuser
Tags: kill umount
1

Alternative if "Lazy unmount" (umount -l) doesn't obey.

Alternative for NFS:

umount -f /media/sdb1

Use with caution: forcing to unmount a busy partition can cause data loss!

grep $'\t' sample.txt
diff --suppress-common-lines -y <(cd path_to_dir1; find .|sort) <(cd path_to_dir2; find .|sort)
2012-02-13 12:49:33
User: knoppix5
Functions: cd diff find
2

Output of this command is the difference of recursive file lists in two directories (very quick!).

To view differences in content of files too, use the command submitted by mariusbutuc (very slow!):

diff -rq path_to_dir1 path_to_dir2
10,30,50 * * * * ping -q -c1 -w3 192.168.0.14 | grep '1 received' - || mail -ne -s'Host 192.168.0.14 not reachable' admin@example.com
2012-02-06 10:42:46
User: knoppix5
Functions: grep mail ping
-3

Every 20 minutes test if host with IP 192.168.0.14 is 'dead' or not reachable.

The line should be put in your crontab file.

10,30,50 * * * * ping -q -c1 -w3 192.168.0.14 | grep '1 received' - || env DISPLAY=:0 xeyes
2012-02-06 09:40:11
User: knoppix5
Functions: env grep ping
Tags: ping host crontab
-2

Every 20 minutes ping host with IP address 192.168.0.14. If it's not 'alive' or not reachable, then display something eye-catching (here xeyes) on the desktop.

Hint for newbies: edit crontab with

crontab -e
diff -y <(ssh user@host find /boot|sort) <(find /boot|sort)
2012-01-31 15:04:30
User: knoppix5
Functions: diff find ssh
3

You can compare directories on two different remote hosts as well:

diff -y <(ssh user1@host1 find /boot|sort) <(ssh user2@host2 find /boot|sort)

To avoid password-prompt on remote host just generate the rsa key locally and copy it to remote host:

ssh-keygen -t rsa

then

ssh you@server1 "mkdir .ssh"

then

scp .ssh/id_rsa.pub you@server1:; .ssh/authorized_keys2