commandlinefu.com is the place to record those command-line gems that you return to again and again.
You can sign-in using OpenID credentials, or register a traditional username and password.
Subscribe to the feed for:
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")
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.
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... :|
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
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
ssh you@server1 "mkdir .ssh"
scp .ssh/id_rsa.pub you@server1:; .ssh/authorized_keys2
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)
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.
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.
Removes an extra character and space.
Diffs two xml files by formatting them first using xmllint and then invoking diff.
Usage: diffxml XMLFile1 XMLFile2
Maybe very limited in its applicability but could be of use at times.
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
Description is moved to "Sample output" because the html sanitizer for commandlinefu breaks the examples..
Using sed to print newlines as doing it in one line with diff is non-trivial.
Sometimes you need to compare two config files on different servers. Put the file names into the above script and let 'er rip.
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.
M - current revision, N - older revision
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'...
If colordiff utility installed, it is sometimes handy to call this command. Of course, you should create an alias for it. E.g. svndiff.