# 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 <"$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.

Use sort's -u and -o switches:

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


Just maybe rsync for remote synchronisation might be attractive.

rsync --dryrun ...

• Code in the question is part of a backup script which actually executes rsync at some point. But prior to any rsync call, some commands have to be run like the one above which append entries from ~/foo/bar to the file not yet inserted. – Stphane Sep 2 '19 at 13:47
• Okay, so I did not need to mention rsync. (-n or.--dryrun has its uses.) – Joop Eggen Sep 2 '19 at 13:51
• I am only looking for a way to avoid the use of a tempory file involved in the supplied few lines. (That said, improvements of any kind are welcome) – Stphane Sep 2 '19 at 13:55

I don't think you need the temporary file, if you open for append (i.e. >>), and use comm to find the non-duplicate lines (after sorting).