“locate”返回相对路径而不是绝对路径

“locate”返回相对路径而不是绝对路径

有人建议我在这里发帖,因为这会更合适(我将删除上一篇)。

在我的 MacBook 上,我的 root 中有以下条目crontab

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

我有一个别名locatein ~/.zshrc

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

当我执行“ locate Table_Synthesis_GREY_”时,在找到的所有文件的完整路径中,指示文件系统根目录的路径的第一个斜杠丢失了。以下是输出示例:

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

如何将路径输出为绝对路径,即从 开始/Users?期望的输出是

/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

相对于

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

更新1:我已复制/usr/libexec/locate.updatedb到我的主根目录中,即/var/root/.我这样做是因为我无法修改该/usr/libexec/locate.updatedb文件,即使being root或使用sudo.

所以在另一个文件中/var/root/locate.updatedb,我可以进行修改:

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

而不是最初:

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

你觉得这个技巧怎么样?主要是为什么当结果进入我的/Users/henry/.

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

但问题仍然存在。

顺便说一句,如果结果位于 /Applications 或其他主目录中但没有系统地,第一个斜杠显示得很好。

脚本的内容不是很大,这里是:

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

有人有线索吗?

更新2:下面的输出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

此外,我将数据库构建为:

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

更新3:如果我用“”恢复if [ "$(id -u)" = "0" ]; then,我可以从 root 用户启动:

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.

更新4:这是最后一期:

如果我做 :

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

如果我启动locate.updatedb脚本,我会得到:

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

任何想法 ?

答案1

我将使用自定义脚本并将其别名为locate命令。一条建议:

#!/bin/bash
 
path=$(locate $1)
full_path=$(dirname $path)
echo $full_path

相关内容