Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to diff two sets of mod_rewrite rules. The set of lines are about 90% identical, but the order is so different that diff basically says they are completely different.

How can I see which lines are truly different between two files, regardless of their line number?

share|improve this question
Pass them both through sort fist. – Shawn J. Goff Oct 25 '11 at 21:45
@Shawn Can I do this without creating (and deleting afterwards) two one-off files? – user394 Oct 25 '11 at 21:52
up vote 20 down vote accepted

sort can be used to get the files into the same order so diff can compare them and identify the differences. If you have process substitution, you can use that and avoid creating new sorted files.

diff <(sort file1) <(sort file2)
share|improve this answer

Made a script for this which keeps the line sequence intact. Here's an annotated version of the important lines:

# Strip all context lines
diff_lines="$(grep '^[><+-] ' | sed 's/^+/>/;s/^-/</')" || exit 0

# For each line, count the number of lines with the same content in the
# "left" and "right" diffs. If the numbers are not the same, then the line
# was either not moved or it's not obvious where it was moved, so the line
# is printed.
while IFS= read -r line
    count_removes="$(grep -cFxe "< $contents" <<< "$diff_lines" || true)"
    count_adds="$(grep -cFxe "> $contents" <<< "$diff_lines" || true)"
    if [[ "$count_removes" -eq "$count_adds" ]]
        # Line has been moved; skip it.

    echo "$line"
done <<< "$diff_lines"

if [ "${line+defined}" = defined ]
    printf "$line"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.