简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM