AngryVolcano
AngryVolcano

Reputation: 17

Finding and copying files with same name but different extensions

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.

Upvotes: 0

Views: 3170

Answers (2)

Jonathan
Jonathan

Reputation: 883

If I'm right: fo each trs file, you have a txt and wav file to copy:

for FILE in *.trs; do 
    BASENAME=${file%.trs}
    cp $FILE $BASENAME.txt  $BASENAME.wav somewhere/
done

Upvotes: 0

konsolebox
konsolebox

Reputation: 75488

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

Upvotes: 4

Related Questions