2
\$\begingroup\$

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.

\$\endgroup\$
1

3 Answers 3

2
\$\begingroup\$

Use sort's -u and -o switches:

find … | sort -u - $file -o $file
\$\endgroup\$
1
\$\begingroup\$

Just maybe rsync for remote synchronisation might be attractive.

rsync --dryrun ...
\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – Stphane
    Commented Sep 2, 2019 at 13:47
  • \$\begingroup\$ Okay, so I did not need to mention rsync. (-n or.--dryrun has its uses.) \$\endgroup\$
    – Joop Eggen
    Commented Sep 2, 2019 at 13:51
  • \$\begingroup\$ 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) \$\endgroup\$
    – Stphane
    Commented Sep 2, 2019 at 13:55
0
\$\begingroup\$

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).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.