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 diff from sorted by
Terminal - Commands using diff - 98 results
diff -Naur /source/path /target/path --brief | awk '{print "cp " $2 " " $4}' | sh
2012-04-13 12:07:20
Functions: awk diff
0

I use this a lot to sync changes between folders that don't share a SVN or GIT repository. If you want to preview the command before executing, just leave out the last part ("| sh")

cvs -q diff --brief | grep file | cut -d/ -f3- | cut -d',' -f1
git diff --color-words file1 file2
diff -dbByw $COLUMNS FILE1 FILE2
2012-02-28 03:19:20
User: DewiMorgan
Functions: diff
Tags: diff compare
0

The normal output of 'diff' is a wonderful thing. But just sometimes, you want something that is a little more... well... readable.

This is that command.

-d - (optional) find the minimal set of changes

-b - (optional) ignore changes in the amount of whitespace

-B - (optional) ignore changes that just insert or delete blank lines

-y - this is where the magic happens! Use the side-by-side output format.

-w $COLUMNS - more magic! Instead of using 80 columns, use the current width of the terminal.

for i in $(ps -eo pid,pmem,pcpu| sort -k 3 -r|grep -v PID|head -10|awk '{print $1}');do diff -yw <(pidstat -p $i|grep -v Linux) <(ps -o euser,pri,psr,pmem,stat -p $i|tail);done
2012-02-16 20:54:32
Functions: awk diff grep head ps sort
0

It grabs the PID's top resource users with $(ps -eo pid,pmem,pcpu| sort -k 3 -r|grep -v PID|head -10)

The sort -k is sorting by the third field which would be CPU. Change this to 2 and it will sort accordingly.

The rest of the command is just using diff to display the output of 2 commands side-by-side (-y flag) I chose some good ones for ps.

pidstat comes with the sysstat package(sar, mpstat, iostat, pidstat) so if you don't have it, you should.

I might should take off the timestamp... :|

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
3

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
svn diff --diff-cmd='meld' -r 100:BASE FILE
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
diff <(exiftool img_1.jpg) <(exiftool img_2.jpg)
2012-01-30 22:01:17
User: fpunktk
Functions: diff
8

I've been looking for this for a long time. Does anybody know how to do this in dash (POSIX shell)?

An alternative version might be:

exiftool img_1.jpg | diff - <(exiftool img_2.jpg)
svn diff <FILE>
2012-01-30 16:47:48
User: bbbco
Functions: diff
Tags: svn diff
0

If you have ever edited a locally checked out version of a file to tweak it for testing purposes, and came back to it over a weekend, you might have forgotten what you exactly changed. This command helps you see the differences between the the checked in SVN version, and the one you tweaked.

diff -U99999 original.css modified.css | awk '/^-/{next} {f=f"\n"$0} /^\+.*[^ ]/{yes=1} /}/ {if(yes){print f} f="";yes=0}'
2012-01-12 07:57:22
User: unhammer
Functions: awk diff
0

This will extract the differing CSS entries of two files. I've left the initial character (plus or space) in output to show the real differing line, remove the initial character to get a working CSS file. The output CSS file is usable by either adding it in a below the to original.css, or by only using the output but adding @import url("original.css"); in the beginning.

This is very useful for converting Wordpress theme copies into real Wordpress child themes.

Could exclude common lines within entries too, I guess, but that might not be worth the complexity.

for f in $(ls -A ./dir); do echo -n $f && diff original.txt ./dir/$f | wc -l ; done | perl -ne 'my $h={}; while (<>) { chomp; if (/^(\S+?)\s*(\d+?)$/){$h->{$1}=$2;} }; for my $k (sort { $h->{$a} $h->{$b} } keys %$h ){ print "$k\t$h->{$k}\n"}'
diff -ru originDir updateDir > result.patch
diff -r -u originDir updateDir > result.patch
diffxml() { diff -wb <(xmllint --format "$1") <(xmllint --format "$2"); }
2011-10-06 07:36:13
User: sharfah
Functions: diff
Tags: diff xml xmllint
13

Diffs two xml files by formatting them first using xmllint and then invoking diff.

Usage: diffxml XMLFile1 XMLFile2

diff -U 9999 file_a file_b | tail -n +3 | grep -P "^(\ Header|\-|\+)"
2011-09-21 21:33:40
User: nnutter
Functions: diff grep tail
Tags: diff
0

Maybe very limited in its applicability but could be of use at times.

LC_ALL=C diff -q dir1 dir2 | grep differ | awk '{ print $2, $4 }' | xargs -n 2 gvim --nofork -d
2011-09-06 11:50:33
User: brock
Functions: awk diff grep xargs
0

LC_ALL=C is here to always grep on "differ" whatever your language env.

xargs -n 2 to run gvim -d with 2 arguments

gvim --nofork to use only one instance of gvim

diff rpm_output_from_other_computer <(rpm -qa|sort)
2011-06-25 11:45:15
User: xeor
Functions: diff rpm
0

Description is moved to "Sample output" because the html sanitizer for commandlinefu breaks the examples..

diff --changed-group-format='differs from line %dF to line %dL|' --unchanged-line-format='' $FILE1 $FILE2 | sed 's/|/\n/'
2011-06-02 15:04:36
User: dullgiulio
Functions: diff sed
-1

Using sed to print newlines as doing it in one line with diff is non-trivial.

Diff files over SSH: ssh [login]@[host] "cat [remote file]" | diff - "[local file]"
2011-04-28 16:59:56
User: dennisfaust
Functions: diff ssh
3

Sometimes you need to compare two config files on different servers. Put the file names into the above script and let 'er rip.

svn diff -r 1792:HEAD --summarize | awk '{if ($1 != "D") print $2}'| xargs -I {} tar rf incremental_release.tar {}
2011-04-05 15:00:49
User: windfold
Functions: awk diff tar xargs
Tags: bash svn awk xargs tar
0

The result of this command is a tar with all files that have been modified/added since revision 1792 until HEAD. This command is super useful for incremental releases.

svn diff -r M:N file.php | patch -p0
2011-03-29 04:15:02
User: ruslan
Functions: diff patch
1

M - current revision, N - older revision

diff -Naur --strip-trailing-cr
2011-02-10 14:32:42
User: ruslan
Functions: diff
3

This form is used in patches, svn, git etc. And I've created an alias for it:

alias diff='diff -Naur --strip-trailing-cr'

The latter option is especially useful, when somebody in team works in Windows; could be also used in commands like

svn diff --diff-cmd 'diff --strip-trailing-cr'...
svn diff --diff-cmd="colordiff"
2011-02-10 14:27:55
User: ruslan
Functions: diff
1

If colordiff utility installed, it is sometimes handy to call this command. Of course, you should create an alias for it. E.g. svndiff.