Find and copy files with the same name but different extensions

advertisements

I currently have a large folder with thousands of .txt files, and a corresponding .wav file for each, like so:

1.txt
1.wav
2.txt
2.wav

Each file name is a random number.

In this folder there are also multiple .trs files for some of these .txt and .wav file pairs.

What I need to do is to find all triplets of data and copy them to another folder, leaving all .txt and .wav files that don't have a corresponding .trs file behind. So in the end only triplets of data will be in this new folder, like so:

4.txt
4.trs
4.wav
5.txt
5.trs
5.wav

All I've managed to do is to copy all .trs files to a separate folder, and I don't know how to continue and accomplish this.

I appreciate all help or tips you can give me.


for FILE in *.txt; do
    BASE=${FILE%.txt}
    [[ -e $BASE.trs && -e $BASE.wav ]] && cp "$FILE" "$BASE.trs" "$BASE.wav" /some/dir
done

To be specific with numerical filenames, use extended globbing:

shopt -s extglob
for FILE in +([[:digit:]]).txt; do
    BASE=${FILE%.txt}
    [[ -e $BASE.trs && -e $BASE.wav ]] && cp "$FILE" "$BASE.trs" "$BASE.wav" /some/dir
done