updatedb-local 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/sh
  2. HOME_DB_FOLDER="$HOME/.cache"
  3. DB_FILE="locate.db"
  4. if [ "$1" = "-h" ]; then
  5. cat <<EOF>&2
  6. Usage: ${0##*/} [FOLDERS]
  7. Update the 'locate' databases for the home folder, external drives and the FOLDERS arguments.
  8. For external drives, databases are only updated if found, they are not
  9. automatically created (unless explicitly specified in the FOLDERS arguments).
  10. - The database for the home folder is stored in '$HOME_DB_FOLDER/$DB_FILE'.
  11. - Other databases are stored in '$DB_FILE' at the target root.
  12. EOF
  13. exit
  14. fi
  15. update() {
  16. [ $# -ne 2 ] && set -- "$1" "$1/locate.db"
  17. echo >&2 "Updating '$2' database for '$1'..."
  18. ## From https://git.archlinux.org/svntogit/packages.git/tree/trunk/updatedb.conf?h=packages/mlocate.
  19. updatedb -l 0 -o "$2" -U "$1" \
  20. --prune-bind-mounts=1 \
  21. --prunefs="9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset cramfs debugfs devpts devtmpfs ecryptfs exofs ftpfs fuse fuse.encfs fuse.sshfs fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs shfs smbfs sockfs sshfs sysfs tmpfs ubifs udf usbfs vboxsf" \
  22. --prunepaths="/gnu/store /afs /mnt /net /sfs /tmp /udev /var/cache /var/lib/pacman/local /var/lock /var/run /var/spool /var/tmp" \
  23. --prunenames=".git .hg .svn .cache Trash .Trash-$(id -u) .snapshots"
  24. }
  25. update "$HOME" "$HOME_DB_FOLDER/$DB_FILE"
  26. # Lookup $1's root and direct subfolders (useful when it's a Btrfs partition with subvolumes).
  27. find_db() {
  28. if [ -f "$1/$DB_FILE" ]; then
  29. update "$1"
  30. else
  31. for i in "$1"/*; do
  32. if [ -f "$i/$DB_FILE" ]; then
  33. update "$i"
  34. fi
  35. done
  36. fi
  37. }
  38. ## Only update external media databases:
  39. for i in "/run/media/$USER"/* "/media/$USER"/*; do
  40. find_db "$i"
  41. done
  42. ## Only update non-external media:
  43. for i in "/media"/*; do
  44. [ "$i" = "/media/$USER" ] && continue
  45. find_db "$i"
  46. done
  47. ## Create database for the specified folders.
  48. for i; do
  49. [ -d "$i" ] && update "$i"
  50. done