3

One advised me to post here since this would be more appropriate (I am going to remove the previous one).

On my MacBook, I have the following entry in root's crontab:

0 */4 * * * cd /private/var/root && ./locate.updatedb

I have an alias for locate in ~/.zshrc:

alias locate='locate -d /var/db/locate.database'   

When I do a "locate Table_Synthesis_GREY_" , in all the full paths of files found, the first slash of the path indicating the file system root is missing. Here is a sample of the ouput:

Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_05_AOUT_2020/temp/Table_Synthesis_GREY_IMPORTANT_SAVE_5_AOUT_2020.run.xml
Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_05_AOUT_2020/temp/Table_Synthesis_GREY_IMPORTANT_SAVE_5_AOUT_2020.tex
Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_05_AOUT_2020/temp/Table_Synthesis_GREY_IMPORTANT_SAVE_5_AOUT_2020.zip
Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_05_AOUT_2020/temp/Table_Synthesis_GREY_IMPORTANT_SAVE_5_AOUT_2020_ORIGINAL_INTO_zip_here.pdf
Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_AFTER_05_AOUT_2020/FLAT_Synthesis/Table_Synthesis_GREY_FLAT.docx

How can I have paths output as absolute paths, i.e. starting at /Users? The desired output is

/Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_AFTER_05_AOUT_2020/FLAT_Synthesis/Table_Synthesis_GREY_FLAT.docx

as opposed to

Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_AFTER_05_AOUT_2020/FLAT_Synthesis/Table_Synthesis_GREY_FLAT.docx

UPDATE 1: I have copied /usr/libexec/locate.updatedb into my home root, that is in /var/root/ . I did that since I could not modify the /usr/libexec/locate.updatedb file, even being root or with sudo.

So in this other file /var/root/locate.updatedb, I could do the modification :

if [ "$(id -u)" = "99" ]; then

instead of initially :

if [ "$(id -u)" = "0" ]; then

What do you think about this trick ? and mostly why I have not the first slash displayed when results into my /Users/henry/.

0 */4 * * * cd /private/var/root ; /bin/sh locate.updatedb

but issue remains.

By the way, if results are located into /Applications or other main directories but not systematically, the first slash is well displayed.

The content of script is not very big, here is :

# $FreeBSD: src/usr.bin/locate/locate/updatedb.sh,v 1.20 2005/11/12 12:45:08 grog Exp $

if [ "$(id -u)" = "99" ]; then
    rc=0
    export FCODES=`sudo -u nobody mktemp -t updatedb`
    chown nobody $FCODES
    tmpdb=`su -fm nobody -c "$0"` || rc=1
    if [ $rc = 0 ]; then
        install -m 0444 -o nobody -g wheel $FCODES /var/db/locate.database
    fi
    rm $FCODES
    exit $rc
fi
: ${LOCATE_CONFIG="/etc/locate.rc"}
if [ -f "$LOCATE_CONFIG" -a -r "$LOCATE_CONFIG" ]; then
       . $LOCATE_CONFIG
fi

# The directory containing locate subprograms
: ${LIBEXECDIR:=/usr/libexec}; export LIBEXECDIR
: ${TMPDIR:=/tmp}; export TMPDIR
if ! TMPDIR=`mktemp -d $TMPDIR/locateXXXXXXXXXX`; then
    exit 1
fi

PATH=$LIBEXECDIR:/bin:/usr/bin:$PATH; export PATH

# 6497475
set -o noglob

: ${mklocatedb:=locate.mklocatedb}      # make locate database program
: ${FCODES:=/var/db/locate.database}    # the database
: ${SEARCHPATHS:="/"}                   # directories to be put in the database
: ${PRUNEPATHS:="/private/tmp /private/var/folders /private/var/tmp */Backups.backupdb"} # unwanted directories
: ${FILESYSTEMS:="hfs ufs apfs"}        # allowed filesystems
: ${find:=find}

case X"$SEARCHPATHS" in 
    X) echo "$0: empty variable SEARCHPATHS"; exit 1;; esac
case X"$FILESYSTEMS" in 
    X) echo "$0: empty variable FILESYSTEMS"; exit 1;; esac

# Make a list a paths to exclude in the locate run
excludes="! (" or=""
for fstype in $FILESYSTEMS
do
    excludes="$excludes $or -fstype $fstype"
    or="-or"
done
excludes="$excludes ) -prune"

case X"$PRUNEPATHS" in
    X) ;;
    *) for path in $PRUNEPATHS
    do
        excludes="$excludes -or -path $path -prune"
    done;;
esac

# Ignore the target of firmlinks
while read firmlink; do
    excludes="$excludes -or -path $firmlink -prune"
done <<< "$(awk -F'\t' '{print "/System/Volumes/Data/" $2}' /usr/share/firmlinks)"

tmp=$TMPDIR/_updatedb$$
trap 'rm -f $tmp; rmdir $TMPDIR; exit' 0 1 2 3 5 10 15

# search locally
# echo $find $SEARCHPATHS $excludes -or -print && exit
if $find -s $SEARCHPATHS $excludes -or -print 2>/dev/null |
        $mklocatedb -presort > $tmp
then
    case X"`$find $tmp -size -257c -print`" in
        X) cat $tmp > $FCODES;;
        *) echo "updatedb: locate database $tmp is empty"
           exit 1
    esac
fi

Anyone has a clue ?

UPDATE 2: below the output of sudo ls -Alh /private/var/root :

total 128K
-r--------  1 root wheel    7 Dec  8 23:29 .CFUserTextEncoding
drwx------  3 root wheel   96 Dec  8 23:29 .Trash
-rw-------  1 root wheel 5.6K Oct 21 20:49 .bash_history
drwx------  3 root wheel   96 Dec  8 23:44 .bash_sessions
drwx------  3 root wheel   96 Oct 16 14:02 .config
-rw-------  1 root wheel 2.5K Dec  9 02:55 .dircolors
-r--r--r--  1 root wheel   10 Apr 17  2020 .forward
drwxr-xr-x  4 root wheel  128 Apr 10  2020 .grc
drwxr-xr-x  3 root wheel   96 Oct  6 00:15 .gsutil
drwxr-xr-x  2 root wheel   64 Jan  1 00:40 .nedit
drwxr-xr-x  4 root wheel  128 Oct  5 22:38 .parallel
drwx------  3 root wheel   96 Sep 17 02:49 .ssh
-rw-------  1 root wheel  14K Jan 17 14:21 .viminfo
-rw-r--r--  1 root wheel 2.9K Dec  9 02:57 .vimrc
-rw-r--r--  1 root wheel  47K Dec  9 02:56 .zcompdump
-rw-------  1 root wheel  15K Jan 19 22:55 .zsh_history
-rw-r--r--  1 root wheel  17K Dec 22 17:52 .zshrc
drwx------  2 root wheel   64 Dec  8 23:29 Downloads
drwxr-xr-x 44 root wheel 1.4K Dec 13 15:05 Library
-rwxr-xr-x  1 root wheel 3.6K Jan 11 01:52 locate.updatedb

Moreover, I build the database into :

$ ls -l /var/db/locate.database
-rw-r--r-- 1 root 330M Jan 24 17:01 /var/db/locate.database

UPDATE 3: If I restore with "if [ "$(id -u)" = "0" ]; then", I get by launching from root user:

mktemp: too few X's in template ‘updatedb’
chown: missing operand after ‘nobody’
Try 'chown --help' for more information.
zsh:1: permission denied: ./locate.updatedb
rm: missing operand
Try 'rm --help' for more information.

UPDATE 4: Here the last issue :

If I do :

if [ "$(id -u)" = "0" ]; then
    rc=0
    #export FCODES=`sudo -u nobody mktemp -t updatedb`
    export FCODES=`sudo -u nobody mktemp -t updatedbXXXXXXXXXX`
    chown nobody $FCODES
    tmpdb=`su -fm nobody -c "$0"` || rc=1
    if [ $rc = 0 ]; then
        install -m 0444 -o nobody -g wheel $FCODES /var/db/locate.database
    fi
    rm $FCODES
    exit $rc
fi

and if I launch the locate.updatedb script, I get :

# ./locate.updatedb
zsh:1: permission denied: ./locate.updatedb

Any idea ?

youpilat13
  • 1
  • 1
  • 12
  • @alecxs . Thanks for your quick answer. I show you above the content of the script "`locate.updatedb`", could you tell me please how to introduce `realpath` or `find` in this script (I think that `find` is already used but not `realpath`) ? – youpilat13 Jan 17 '21 at 12:38
  • sorry for misleading, your issue is not related to scripting. wait for some unix geeks – alecxs Jan 17 '21 at 14:26
  • Is `Users` directory directly under `/private/var/root`? – Arkadiusz Drabczyk Jan 20 '21 at 14:23
  • no, there is no `Users` directory into `/private/var/root` – youpilat13 Jan 20 '21 at 14:26
  • @youpilat13: it might be a symlink. Can you show output of `ls -Alh /private/var/root`? – Arkadiusz Drabczyk Jan 24 '21 at 13:47
  • @ArkadiuszDrabczyk above in **UPDATE 2**, you can see the ouput. – youpilat13 Jan 24 '21 at 16:55
  • @youpilat13: indeed, there is no `Users` directory. I cannot reproduce this behavior on my FreeBSD system - even if `/private/var/root/Users` existed `locate Table_Synthesis_GREY_` would show `/private/var/root/Users/Table_Synthesis_GREY_`. Can you do `cd /private/var/root && ./locate.updatedb` in the interactive shell outside of crontab and check if something changes? – Arkadiusz Drabczyk Jan 24 '21 at 17:20
  • @youpilat13: and what happens if you restore `if [ "$(id -u)" = "0" ]; then`? – Arkadiusz Drabczyk Jan 24 '21 at 17:44
  • @ArkadiuszDrabczyk You can see the results into **UPDATE 3**. – youpilat13 Jan 24 '21 at 18:09
  • @youpilat13: replace `mktemp -t updatedb` with `mktemp -t updatedbXXXXXXXXXX` and please post output of `man mktemp | tail -n1` – Arkadiusz Drabczyk Jan 24 '21 at 18:16
  • code snippet : `if [ "$(id -u)" = "0" ]; then rc=0 export FCODES=`sudo -u nobody mktemp -t updatedbXXXXXXXXXX\`` and `man mktemp | tail -n1` gives : `GNU coreutils 8.32 March 2020 MKTEMP(1)` – youpilat13 Jan 24 '21 at 19:45
  • @ArkadiuszDrabczyk but problem remains – youpilat13 Jan 24 '21 at 19:47
  • @youpilat13: funny, you use GNU mktemp on Mac. Above I asked for one more thing: can you do `cd /private/var/root && ./locate.updatedb` in the interactive shell outside of crontab and check if something changes? – Arkadiusz Drabczyk Jan 24 '21 at 19:48
  • @ArkadiuszDrabczyk if I take `if [ "$(id -u)" = "99" ]`, then, `/var/db/locate.database`is well built. If i take the version `if [ "$(id -u)" = "0" ]`, then I get errors. Any suggestions to fix it ? – youpilat13 Jan 24 '21 at 19:59
  • @youpilat13: what does `well built` mean? What errors? You have to be specific. – Arkadiusz Drabczyk Jan 24 '21 at 20:00
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/118872/discussion-between-youpilat13-and-arkadiusz-drabczyk). – youpilat13 Jan 24 '21 at 20:08
  • @ArkadiuszDrabczyk I have the errors of **UPDATE 3**. – youpilat13 Jan 24 '21 at 20:08
  • @youpilat13: `too few X's in template` - we already discussed that. – Arkadiusz Drabczyk Jan 24 '21 at 20:10
  • @youpilat13: `chmod +x ./locate.updatedb` – Arkadiusz Drabczyk Jan 24 '21 at 20:17
  • @ArkadiuszDrabczyk you can see the unique error I get into **UPDATE 4** if I launch the script `locate.updatedb` as root from `/private/var/root` – youpilat13 Jan 24 '21 at 20:17
  • `chmod +x` already done. – youpilat13 Jan 24 '21 at 20:19
  • other results that could give a track : `# sh locate.updatedb shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied find: .: Permission denied` – youpilat13 Jan 24 '21 at 20:22
  • @youpilat13: sorry but this conversation leads nowhere. You mention you have an error A in the question and then post error B in the comment. – Arkadiusz Drabczyk Jan 24 '21 at 20:31
  • I got error B with configuration of UPDATE 4, nothing else. – youpilat13 Jan 24 '21 at 20:56

1 Answers1

1

I would use a custom script and alias it to the locate command. A suggestion:

#!/bin/bash
 
path=$(locate $1)
full_path=$(dirname $path)
echo $full_path
esantix
  • 422
  • 3
  • 15
  • 1
    Thanks for your answer but how coud I do if I want to add option like `-i` or other options ? – youpilat13 Jan 24 '21 at 01:02
  • 1
    Try $@ instead of @1. $@ passes all the arguments. That should work. – esantix Jan 24 '21 at 01:08
  • I tried the following script but not working : `#!/bin/bash path=$(locate $@ -d /var/db/locate.database) full_path=$(dirname $path) echo $full_path` – youpilat13 Jan 24 '21 at 01:24
  • Just change de $1 to $@. The on the terminal run -d /var/... etc. If that doesn't work, I can't offer more. Good luck! – esantix Jan 24 '21 at 02:01
  • Thanks for your patience. Actually, I would like that option "`-d /var/db/locate.database`" to be implicit when I launch on terminal for example `locate -i file.txt` but this doesn't work – youpilat13 Jan 24 '21 at 02:40