Finding a File With Specific Permissions - shell

I need to write a command pipeline that will show all non-hidden files that have read permissions for all users.
I dont know why this wouldn't work:
find * -perm a=r -print
I get no output and am not sure where I am going wrong. Please Help.

you should specify this way
find . -type f -perm -a+x or find . -type f -perm -a=x
note that -
or you can use octal also
find . -type f -perm -655
. specify current directory

You need to specify it as:
find * -perm -a+r
Note the dash in front of a.

Related

Why can't I exclude a directory using find

I attempting to run a command on all subdirectories in a directory using find and -exec, however on one of the directories, the user the script runs under does not have adequate permissions and I get an error (permission denied). I am attempting to ignore the directory using either ! -path or using -prune. Neither of these methods work. I have tried both of the commands down below.
I have tried every combination of subDirToExclude— with and without ./ at the beginning, with and without /* at the end. I've tried relative path, full path and every single combination of all of them that you can think of to try and match this path, but it simply does not work. The man page is unhelpful and no suggestions from any related questions on this forum produce any useful results. Why do none of the methods suggested in the man page work? How can this actually be done?
find /path/to/dir -maxdepth 1 -type d ! -path "subDirToExclude" -exec somecommand {} +
find /path/to/dir -maxdepth 1 -type d -path "subDirToExclude" -prune -o -exec somecommand {} +
find: ‘/path/to/dir/subDirToExclude’: Permission denied
The argument to the -path option should be a full pathname, not just the name of the directory. Use -name if you just want to match the name of the directory.
find /path/to/dir -maxdepth 1 -type d ! -name "subDirToExclude" -exec somecommand {} +
You could also do this without using find at all, since you're not recursing into subdirectories because of -maxdepth 1.
shopt -s extglob
somecommand /path/to/dir /path/to/dir/!(subDirToExclude)/
Putting / at the end of the filename makes the wildcard only match directories. Actually, this will also match symbolic links to directories; if that's a problem, you can't use this solution.

How to use find command in unix to search files only in a directory not in subdirectories

I want to list all the files or specific files based on the pattern only from the directory which i give and not in its descend or sub directories. I tried of using -maxdepth 1 / -depth 1 / -prune options. when using -maxdepth option, it says "maxdepth is not a valid option" and when using -depth option, it says "conjunction is missing".
For example,
#1
find /directory/ -maxdepth 1 -type f -name '*.txt' -ls
#2
find /directory/ -depth 1 -type f -name '*.txt' -ls
#3
find . \( -name .dir1 -prune -o -name '*.txt' \) -print
I am not able to use any of these options. Please correct me if anything is wrong with my script/let me know how to get files only from the directory which i give and not from its sub directories. I actually almost tried many options apart from the above examples but still i am stuck up with this. Thanks in advance for your response.

How to find all non-binary text files (with extended attributes) in a directory on OSX bash?

The following command cann't work when the file name contains extended attributes.
cd ~/Library/Containers
find . -type f -name "*.xml"
It returned nothing. But
less com.apple.TextEdit/Data/Music/iTunes/iTunes\ Music\ Library.xmlary.xml
The xml file is there.
In order to follow the symbolic links in the directory hierarchy, you need to use the -L option to find:
find -L . -type f -name "*.xml"

Exclude specified directory when using `find` command

I have a directory which contains a number of files (no subdirectories). I wish to find these files. The following gets me close:
$ find docs
docs
docs/bar.txt
docs/baz.txt
docs/foo.txt
I don't want the directory itself to be listed. I could do this instead:
$ find docs -type f
docs/bar.txt
docs/baz.txt
docs/foo.txt
Using a wildcard seems to do the trick as well:
$ find docs/*
docs/bar.txt
docs/baz.txt
docs/foo.txt
My understanding is that these work in different ways: with -type, we're providing a single path to find, whereas in the latter case we're using wildcard expansion to pass several paths to find. Is there a reason to favour one approach over the other?
You have a UNIX tag, and you example has a *. Some versions of find have a problem with that.
If the directory has no subdirectories.
FYI.
Generally the first parms to find has to be a directory or a list of directories
find /dir1 /dir2 -print
Find is recursive - so it will follow each directory down listing every thing, symlinks, directories, pipes, and regular files. This can be confusing. -type delimits your search
find /dir1 /dir2 -type f -print
You can also have find do extra output example: have it rm files older than 30 days for example:
find /dir1 /dir2 -type f -mtime +30 -exec rm {} \;
Or give complete infomation
find /dir1 /dir2 -type f -mtime +30 -exec ls -l {} \;
find /dir1 /dir2 -type f -mtime +30 -ls # works on some systems
To answer your question: because find can be dangerous ALWAYS fully specify each directory , file type ,etc., when you are using a nasty command like rm. You might have forgotten your favorite directory is also in there. Or the one used to generate your paycheck. Using a wildcard is ok for just looking around.
Using *
find /path/to/files -type f -name 'foo*'
-- tics or quotes around strings with a star in them in some UNIX systems.
find docs -type f
will get you a listing of every non-directory file of every subdirectory of docs
find docs/*
will get you a listing of every file AND every subdirectory of docs

find prune not working

I am trying to find all ruby files in the project. However I want to ignore all the files residing under directory vendor.
find . -name .vendor -prune -o -name '*.rb' -print
Above command is not working. Anyone knows the fix?
Try:
find . -name '*.rb' ! -wholename "./vendor/*" -print
You may have to escape ! (i.e. write \!) character depending on your shell.

Resources