Linux find permission denied

Linux find permission denied

Когда использую команду find, то часто получаю сообщения Permission denied (Отказано в доступе) об отсутствии доступа к некоторым директориям и файлам. Особенно это мешает, когда ищу по всему диску:
find / -name myfile

В итоге получаю много лишних сообщений среди которых нужно еще выискивать найденные файлы:
find / -name "apache*"
find: `/etc/ssl/private’: Отказано в доступе
/etc/apparmor.d/abstractions/apache2-common
find: `/etc/polkit-1/localauthority’: Отказано в доступе
find: `/etc/cups/ssl’: Отказано в доступе
find: `/sys/kernel/debug’: Отказано в доступе
find: `/run/udisks2′: Отказано в доступе

Данные сообщения появляются, когда алгоритм поиска натыкается на файлы, к которым у пользователя нет доступа.

Есть несколько способов убрать сообщения Permission denied. Наверное, самый простой — это перенаправлять все ошибки в /dev/null. Для этого в конце вашей команды просто дописываем: 2>/dev/null. Например:

У этого способа есть и недостаток, так как вы не получите на экран сообщения об ошибках. Но для простого поиска данный метод подходит.

Linux and Unix tutorials for new and seasoned sysadmin

Linux and Unix tutorials for new and seasoned sysadmin

Adblock detected 😱

Find command basic syntax

The syntax is:
find where-to-look criteria action
find /dir/to/search -name filetosearch
find /dir/to/search -name "*.c"
find /home/nixcraft/project/ -name "*.py" -print
In this example, find will search the /tmp directory for any files named “data*.txt” and display their pathnames:

find /path/to/dir -name "pattern" -print find /tmp -iname "data*.txt"

cd /tmp find . -iname "data*.txt" -print

Sample outputs:
Fig. 01: Find will show an error message for each directory on which you don’t have read permission.

How to fix find command permission denied messages

In this above example, I do not have read permission for vmware-root and orbit-Debian-gdm directories. To to avoid this problem try the following syntax:

Читайте также:  Libwand so библиотека не обнаружена

## redirect error spam message to /dev/null ## find where-to-look criteria action 2>/dev/null find . -iname "data*.txt" -print 2>/dev/null

Sample outputs without permission denied spam from find command:

How does it works?

The 2>/dev/null at the end of the find command tells your shell to redirect the error messages (FD #2) to /dev/null, so you don’t have to see them on screen. Use /dev/null to to send any unwanted output from program/command. All data written on a /dev/null special file is discarded by the system. To redirect standard error to /dev/null and store file list to output.txt, type:

## redirect error spam to /dev/null ## find . -iname "data*.txt" -print 2>/dev/null > output.txt cat output.txt

Exclude all “permission denied” messages from “find” command on Linux

There is one problem with the following command. It would filter out all error messages created by find command, not just the permission denied ones:

find / -name foo 2>/dev/null find / -type d -name bar 2>/dev/null

To avoid that try the following find command along with grep command on Linux or Unix-like systems:

find / -name foo 2>&1 | grep -v "Permission denied" find / -type d -name bar 2>&1 | grep -v "Permission denied"

In short you should use following syntax to skip “permission denied” errors messages when running find in Linux or Unix-based systems:

find /path/to/dir -name "search-patter" 2>&1 | grep -v "Permission denied" find /etc -name "x*.conf" 2>&1 | grep -v "Permission denied"

To store output to a file run:

find /path/to/dir -name "search-patter" 2>&1 | grep -v "Permission denied" > output-file find /etc -name "x*.conf" 2>&1 | grep -v "Permission denied" > output.txt

Читайте также:  Hp designjet 510 ошибка 79 04

Display output.txt using cat command:
cat output.txt

In the above example, we used find command along with grep command to filter out permission denied error messages.

See also
  • Man page for bash or ksh shell.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Your support makes a big difference:

I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft takes a lot of my time and hard work to produce. If everyone who reads nixCraft, who likes it, helps fund it, my future would be more secure. You can donate as little as $1 to support nixCraft:

When I run this command in Linux (SuSE):

I get many error messages of the form:

Does find take an argument to skip showing these errors and only try files that I have permission to access?

marked as duplicate by Gilles ‘SO- stop being evil’, George M, Mat, Michael Mrozek ♦ Jul 13 ’12 at 20:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

you can filter out messages to stderr . I prefer to redirect them to stdout like this.

Читайте также:  Php sql html css javascript

In short, all regular output goes to standard output ( stdout ). All error messages to standard error ( stderr ).

grep usually finds/prints the specified string, the -v inverts this, so it finds/prints every string that doesn’t contain "Permission denied". All of your output from the find command, including error messages usually sent to stderr (file descriptor 2) go now to stdout (file descriptor 1) and then get filtered by the grep command.

This assumes you are using the bash/sh shell.

Ссылка на основную публикацию
Adblock detector