Write directory new entries to file periodically, keeping the ones that existed in the past 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 &lt;"$file" - | sort | uniq &gt; "${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.
Use sort's -u and -o switches:

find … | sort -u - $file -o $file