How do I write a script to copy certain files from a directory using a partial filename?

Viewed 22

I have a linux directory with thousands of files with a variety of different filenames and types. I want to find all the files where the filenames start a certain way eg "xx_xxxx_xx...", I want to copy/clone those files into the same directory with a new suffix attached to the end of the filename. The filenames all have the same starts and a different end and I don't have a list of them in another file.

I haven't found a good method for loping through a set of files based on a partial filename. Help appreciated.

I've tried to loop through files based on partial filenames but haven't found a way of doing this.

1 Answers

Hopefully this could help you.

#!/bin/sh/
pattern='0_*.sh'
array=($(ls -d $pattern))
echo ${array[@]}
for i in "${array[@]}"
do
   echo "$i"
   #cp $i "your path"
done
Related