Bash selection of files with similar name

Hi all,
This is my first day on Linux shell!!!

So, I am trying to write a script that that will pick up pairs of files with the same name (not the same content) but that are different in one character (one is *R1 the other is *R2)...

Something like: look ate the files, whenever they are the same except in the R1/R2, than run ...

The if that I tried is I think is going into the the file content rather than the name....

Did this make any sense?

Thanks a lot.

This is what I was able to do so far....

#!/bin/bash

file1_path="~/All_files/"
var1=S*R1.fastq.gz
echo $var1 

file2_path="~/All_files/"
var2=S*R2.fastq.gz  
echo $var2


if ["${VAR1:0:18}" = "${VAR2:0:18}"] ; then

Welcome to the forum, firstly some housekeeping
...
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
...

The variables var1 and var2 actually contain the literal strings you have assigned to them (i.e S*R1.fastq.gz and S*R2.fastq.gz ) these are expanded by the shell when they are build into the echo command. To demonstrate:

$ var1=S*R1.fastq.gz
$ touch SecondR1.fastq.gz
$ echo "$var1"
S*R1.fastq.gz
$ echo $var1
SecondR1.fastq.gz
$ echo "${var1:1:1}"
*

See how using double quotes protects the string from being expanded by the shell. You could do something like this to have the shell expand the wildcard:

$ var1=S*R1.fastq.gz
$ touch SecondR1.fastq.gz
$ var1=$(echo $var1)
$ echo "$var1"
SecondR1.fastq.gz
$ echo ${var1:6:2}
R1

--- Post updated at 08:13 AM ---

One approach to this problem is to populate an array with the expanded filenames and then step thru it comparing with the next entry like this:

REST=.fastq.gz
file=( S*R[12]$REST )

for((i=0;i<${#file[@]};i++))
do
    if [ "${file/%R1$REST/R2$REST}" = "${file[i+1]}" ]
    then
        echo ${file} and ${file[i+1]} are similar
        ((i++))
    fi
done
2 Likes

Thank you very much for the help.
I am sorry about missing out on the rules of posting. I just focused on the "no smoking"one!!! :wink:
I am missing out on a lot!!!!! I have found a few but do you have any recommendation on a website that can take me step by step on programming on unix?

Thanks once again.

You may find this on-line version of "The Are of Unix Programming" useful:

[The Art of Unix Programming](http://www.catb.org/~esr/writings/taoup/html/)

A quick google search for "Bash tips and tricks" turned up this fairly good article on bash:

101 Bash Commands and Tips for Beginners to Experts - DEV Community

I pretty much learnt awk programming from this site . Initially I was amazed how easily awk could be used to solve problems posted here. I worked thru the solutions proposed by others and used google and other on-line resources to nut out what was going on. I did start with a lot of programming experience in other languages, which gave me a head start. Start by understanding the supplied solutions and work up to figuring out your own answers without looking at how others have done it first. Compare what you have come up with to the supplied answer. It's fairly important that you have access to a system you can trial and debug solutions on.