Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Linux – How to diff all files which have same name but different extensions in a directory

Say in the current directory there are some files called:

test1.out, test2.out, test3.out ...

test1.expect, test.expect, test3.expect ...

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would like to compare each test.out with its corresponding test.expect, like:

diff test1.out test1.expect
diff test2.out test2.expect

and so on. I would like to know how could I achieve this more efficiently. Thank you!

I am not very familiar with linux shell. I have tried things like:

for i in *.out; do diff "$i" "$i.expect"; done

but I realize that this is not correct because $i includes the .out extension.

>Solution :

With Bash, you have a lot of powerful variations on parameter expansion. For example, in this variation on your attempt:

for i in *.out; do diff "$i" "${i/%out/expect}"; done

The "${i/%out/expect}" expands to the value of shell variable i, with the trailing out (if any) replaced by expect.

The bash man page and the bash manual are both good references for bash features and capabilities.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading