titiyoyo
titiyoyo

Reputation: 942

linux diff to only output filename and nothing else

I'm trying to get diff to output only the name of the modified files. I tried to use the -q option but it's still giving me too much output :

What i get now is this :

files path/to/file1/file1 and path/to/file2/file2 are different

And i would like this :

path/to/file1/file1

Any ideas on how to do this ?

Upvotes: 2

Views: 4669

Answers (5)

mikhail
mikhail

Reputation: 5179

You could do: diff -q file1 file2 | cut -f2 -d' '

Upvotes: 3

kyoto
kyoto

Reputation: 125

Another option is to use cmp --silent instead of diff -q. This sends nothing to stdout, so you only need to deal with the exit status.

For example:

file1=path/to/file1/file1; cmp --silent $file1 path/to/file2/file2 || echo $file1

The exit status of cmp is the same as diff (0 if inputs are the same and 1 if different).

Upvotes: 1

jon
jon

Reputation: 6246

Similar to Wes's answer:

echo "files path/to/file1/file1 and path/to/file2/file2 are different" | sed -e "s/^files //" -e "s/ and .*$//"

.

//output 
path/to/file1/file1

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96266

Use the exit status of diff:

if ! diff -q file1 file2 >/dev/null; then echo file1; fi

Upvotes: 3

Wes Hardaker
Wes Hardaker

Reputation: 22262

You can always pipe it to something else, such as:

# diff ... | sed 's/^files //;s/ and .*//;'

However, note that if you have a file with a literal " and " in it, then the above will cause a problem. Generally I encourage people not to use spaces in file names anyway. Yes you can do it, but yes it still causes problems.

Upvotes: 1

Related Questions