I have a list of files on a Linux machine which are differ by some date, so I have to search for unique files and need to place them in some other directory. "Unique" here refers to the name of the file until the second _, so 100001_ABC and 100001_XYZ in the example below.
100001_ABC_25Sep2020_1200-25Sep2020_1300.csv
100001_XYZ_30Sep2020_1300-30Sep2020_1400.csv
100001_XYZ_30Sep2020_1400-30Sep2020_1500.csv
I want the uniquely named to be placed under this directory:
/home/vikrant_singh_rana/uniquefiles/
The script should only copy the files below:
100001_ABC_25Sep2020_1200-25Sep2020_1300.csv
100001_XYZ_30Sep2020_1300-30Sep2020_1400.csv
Here's my shell script
#!/bin/bash
set +o posix
#reading file names into file_array
readarray -t file_array < <(
cd "/home/vikrant_singh_rana/unzipfiles"
printf "%s\n" * | cut -d"_" -f2 | cut -d"-" -f1 | sort -u )
#print items of array
printf '%s\n' "${file_array[@]}"
for i in "${file_array[@]}"; do
#echo $i
find /home/vikrant_singh_rana/unzipfiles/ -type f -name "*$i*.csv" -exec awk '!seen[$0]++' {} +
done
The script can find the unique names correctly, but I can't find how to move them to the other directory.
25Sep2020? Do you want always to move the first alphabetically from the uniques?