I'm trying to write a script to do the following: There´s a directory "x". In this directory there are several folders, some of these folders begin with NZ or NC (e.g NZ_FGT10000, NC_HGU987000) (folders containing fasta files). Inside these folders, there are files that begin with the word "cluster" (these are the fasta files) and other files. Inside the directory "x" i have a folder called "y". I want to access every folder in the directory "x" that begins with NZ or NC in their names, and copy all the files that begins with "cluster" to the folder "y".

Im new to the world of linux and shell, and i have no experience in bash, so i would appreciate the help. Thank you.

1 Answer 1

cp x/N[CZ]*/cluster* x/y/

The […] is a range expression and matches any of the enclosed characters and * matches any string (including the null string). So N[CZ]*/cluster* matches any directory names beginning with NC and NZ and filenames beginning with cluster.

Reference: Pattern Matching (Bash manual)

  • 1
    This is correct. There will only be an issue 1) if multiple files have the exactly the same name at the destination (use cp -i for interactively asking for confirmation to over-write), and 2) if the pattern expands to so many names that the expanded command becomes too long (use a loop in this case).
    – Kusalananda
    Nov 7, 2019 at 6:52
  • It worked wonderfully
    – tucanco
    Nov 18, 2019 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .