Some backup Bash script on my machine copies a specific directory entries filenames into a text file. Entries that no longer exist have to be kept (entries added during a previous run).
The script makes sure that entries which still exist will not be inserted twice in the backup file using sort and uniq.
local file=~/bar-entries.txt
touch "$file" "${file}_"
find ~/foo/bar -maxdepth 1 -type d \! -name bar | cat <"$file" - | sort | uniq > "${file}_"
mv "${file}_" "$file"
Can this code be optimized? Like can this scenario be achieved without relying on a temporary file? Any other improvement is welcome.