Command-Line

為什麼要為已排除的路徑找到給出“權限被拒絕”錯誤的命令?

  • January 10, 2022

如果我執行(使用 sudo)

sudo find / -xdev -type f ! -path '/home/blueray/*' ! -path '/timeshift/*' > log.txt

它可以正常工作,沒有任何問題。

但是,如果我在沒有 sudo 的情況下執行相同的命令,我會在控制台中收到一些錯誤。搜尋結果log.txt是正確的(沒有問題)。但我不理解錯誤消息。

% find / -xdev -type f ! -path '/home/blueray/*' ! -path '/timeshift/*' > log.txt
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/etc/polkit-1/localauthority’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/etc/ssl/private’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/etc/cups/ssl’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/log/private’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/log/speech-dispatcher’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/spool/rsyslog’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/spool/cups’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/spool/cron/crontabs’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/lib/apt/lists/partial’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/lib/colord/.cache’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/lib/AccountsService/users’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/lib/bluetooth/5C:F3:70:93:BD:85’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/lib/lightdm-data/lightdm’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/lib/polkit-1’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/lib/udisks2’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/lib/NetworkManager’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/lib/private’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/lib/lightdm’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/lib/tor’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/cache/private’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/cache/apparmor/26b63962.0’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/cache/lightdm/dmrc’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/cache/cups’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/cache/ldconfig’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/tmp/systemd-private-84c1e6e852b747ea9d34d17ca664b32b-tor@default.service-frAbZe’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/tmp/systemd-private-84c1e6e852b747ea9d34d17ca664b32b-systemd-logind.service-EWXwaj’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/tmp/systemd-private-84c1e6e852b747ea9d34d17ca664b32b-ModemManager.service-0wY2Hh’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/tmp/systemd-private-84c1e6e852b747ea9d34d17ca664b32b-colord.service-HLWVQf’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/tmp/systemd-private-84c1e6e852b747ea9d34d17ca664b32b-upower.service-ecD4ii’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/tmp/systemd-private-84c1e6e852b747ea9d34d17ca664b32b-systemd-resolved.service-Qxjpij’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/var/tmp/systemd-private-84c1e6e852b747ea9d34d17ca664b32b-systemd-timesyncd.service-Kf61Zi’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/root’: Permission denied
find: ‘/timeshift/snapshots/2022-01-09_22-00-46/localhost/boot/efi’: Permission denied
find: ‘/etc/libvirt/secrets’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/ssl/private’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/var/log/private’: Permission denied
find: ‘/var/log/speech-dispatcher’: Permission denied
find: ‘/var/spool/rsyslog’: Permission denied
find: ‘/var/spool/cups’: Permission denied
find: ‘/var/spool/cron/crontabs’: Permission denied
find: ‘/var/lib/apt/lists/partial’: Permission denied
find: ‘/var/lib/colord/.cache’: Permission denied
find: ‘/var/lib/AccountsService/users’: Permission denied
find: ‘/var/lib/portables’: Permission denied
find: ‘/var/lib/libvirt/qemu’: Permission denied
find: ‘/var/lib/machines’: Permission denied
find: ‘/var/lib/bluetooth/5C:F3:70:93:BD:85’: Permission denied
find: ‘/var/lib/lightdm-data/lightdm’: Permission denied
find: ‘/var/lib/polkit-1’: Permission denied
find: ‘/var/lib/udisks2’: Permission denied
find: ‘/var/lib/NetworkManager’: Permission denied
find: ‘/var/lib/private’: Permission denied
find: ‘/var/lib/lightdm’: Permission denied
find: ‘/var/lib/tor’: Permission denied
find: ‘/var/lib/fwupd/gnupg’: Permission denied
find: ‘/var/cache/apt/archives/partial’: Permission denied
find: ‘/var/cache/private’: Permission denied
find: ‘/var/cache/apparmor/26b63962.0’: Permission denied
find: ‘/var/cache/lightdm/dmrc’: Permission denied
find: ‘/var/cache/cups’: Permission denied
find: ‘/var/cache/ldconfig’: Permission denied
find: ‘/var/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-ModemManager.service-G72K7i’: Permission denied
find: ‘/var/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-colord.service-2B8Ofg’: Permission denied
find: ‘/var/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-tor@default.service-TaP1xi’: Permission denied
find: ‘/var/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-systemd-logind.service-rnYjPi’: Permission denied
find: ‘/var/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-systemd-timesyncd.service-U7G6Lf’: Permission denied
find: ‘/var/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-systemd-resolved.service-ABf5Xe’: Permission denied
find: ‘/var/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-upower.service-WW9yYi’: Permission denied
find: ‘/root’: Permission denied
find: ‘/lost+found’: Permission denied
find: ‘/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-upower.service-F7Rtjf’: Permission denied
find: ‘/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-tor@default.service-nYCXFi’: Permission denied
find: ‘/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-colord.service-qGi0sf’: Permission denied
find: ‘/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-systemd-logind.service-FhyyUh’: Permission denied
find: ‘/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-systemd-resolved.service-luOB7f’: Permission denied
find: ‘/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-ModemManager.service-d1MZ7g’: Permission denied
find: ‘/tmp/systemd-private-c9d7a83d6f844d998e8423d94d48ce3f-systemd-timesyncd.service-79KkRe’: Permission denied

為什麼find: ‘/timeshift/snapshots/....’: Permission denied即使我! -path '/timeshift/*'在我的 find 命令中使用過它也會這樣說。按照我的常識,find 命令不需要關心已從搜尋中排除的目錄的權限。

那麼,到底發生了什麼以及如何在不使用 sudo 的情況下擺脫這些錯誤消息。我的意思是,在 find 命令中是否有任何位置參數說“先排除然後搜尋”或類似的東西。

使用-path,您可以測試目前路徑名是否與模式匹配,然後將其排除或繼續處理。它不會停止find進入子目錄。

要修剪搜尋樹(從搜尋中刪除目錄),請使用-prune.

find / -xdev \
   \( -path '/home/blueray' -o -path '/timeshift' \) -prune -o \
   -type f -print

這會修剪與兩個-path測試中的任何一個匹配的路徑,並輸出與正常文件對應的所有其他路徑名。修剪後的路徑不會被 輸入find,因此在上面的命令中,沒有任何內容/home/blueray/timeshift什至不會被 考慮find

引用自:https://unix.stackexchange.com/questions/685738