Shell – List any file ending with .in and corresponding .out with shell script

lsscriptingshell

I have a directory full of files ending with different extensions, how would I list/select only the files ending with .in and corresponding .out that share the same basename?

e.g.

file1.txt
file1.in
file2.in
file3.in
file2.out
file3.out

What I want to select from these files are:

file2.in
file2.out
file3.in
file3.out

Best Answer

Since you want .in to be paired with .out, loop through only *.in and check if there is a corresponding .out file, if so, print out both:

for f in *.in; do
  if [[ -f ${f%.in}.out ]]; then
    echo $f
    echo ${f%.in}.out
  fi
done
Related Question