Sebastian Zeki
Sebastian Zeki

Reputation: 6874

Create new file with old filename using bash

I have a script that I need to run on every file in a folder. The script creates a new file with the same name as the original file in a new folder.

So the folder contains files with names like:

SLX-8691.ART12.seq
SLX-8690.ART12.seq
SLX-8692.ART12.seq
SLX-8693.ART12.seq

The script I want to run on every file isexample is as follows:

$ ./unique_seq_counts.rb ./qualitymask/SLX-8691.ART12.seq > ./uniquecounts/SLX-8691.ART12.counts.txt

So that SLX-8691.ART12.seq is replaced by each file name.

Is there a way of looping through a replacing each filename automatically?

Thanks

Upvotes: 0

Views: 715

Answers (1)

konsolebox
konsolebox

Reputation: 75488

Using a loop:

for F in ./qualitymask/*.seq; do
   T=${F##*/}  ## Removes directory part. Same as $(basename "$F")
   T=${T%.seq}.counts.txt  ## Removes .seq and adds .counts.txt
   ./unique_seq_counts.rb "$F" > "./uniquecounts/$T"
done

See: Parameter Expansion and Filename Expansion.

Upvotes: 1

Related Questions