Daniel
Daniel

Reputation: 1822

How to output the non-readable contents of a directory in bash

In bash how would one output the non-readable contents of a directory?

For example, let's say the directory is ~/foo, and there's a non-readable folder ~/foo/folder with a file ~/foo/folder/file1.txt, and another non-readable file ~/foo/file2.txt. I want to output:

~/foo/folder cannot be read.
~/foo/folder/file1.txt cannot be read.
~/foo/file2.txt cannot be read.

Upvotes: 0

Views: 471

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753665

Directory permissions are interesting. If you don't have read permission on a directory, you cannot find out which files it contains by a system call such as readdir() (which is how commands such as find, ls, and even the shell generate lists of file names).

If you have read permission on a directory, you can find a basic list of the files in the directory, but you need 'execute' permission to access the files, even to find out the file permissions.

If you have execute permission without read permission but you know the name of a file in the directory, you can both list the file and access it (if the file permissions give you permission to do so).

So, if the directory is not readable but you have execute permission on the directory, you can investigate any files you know about in the directory. You can't find out which files are there, though.

Upvotes: 1

Olaf Dietsche
Olaf Dietsche

Reputation: 74018

If you cannot read folder ~/foo/folder, there's no way for you to detect any files in it, be they readable or not.

If you can read ~/foo, you can go over all files and directories and test, wether they are readable or not:

find ~/foo | while read file; do
    if test \! -r "$file"; then
        echo "$file cannot be read"
    fi
done

Upvotes: 1

Related Questions