Find a File By Owner In Linux - linux

I need to find a file belonging to a specific user.
I have tried using -O "userName" but, it prints: The -O option must be immediately followed by a decimal integer. I tried it with small 'o' and still, it wouldn't work

For searches based on the file owner, you should use the -user option.
find /path/to/some/folder -user exampleuser
The -O option is used for search optimization and is followed by an integer (1 to 3 levels).

Related

Script not running properly

Here is what I have to do
“ Make a script to find all old, unused files, larger than 1 megabyte, starting at a directory supplied by the user (default: /tmp). Make “old” be more than a month by default, but allow the user to change it. Similarly make the size be one megabyte by default, but allow the user to change it.”
I have attempted several scripts but can’t seem to get this right. Here is what I wrote:
#!/bin/bash
find /home/usr/local/temp -atime +31 –size +1M
# Then to allow permission for everyone
chmod u+x Filename
In –size you seem to be using the Unicode en dash instead of an ASCII hyphen. Otherwise your command is fine.
Consider adding type -f to find only files, not directories.
find /home/usr/local/temp -atime +31 -size +1M -type f
Also, by "allow the user to change it", I think it means allow the user to change the parameters, like 10 days and 5 MB, not giving the user write permission.

What is/how does the "search" command work?

Put briefly, can I use the 'search' command to search through file contents? If so, how?
I am trying to find an easier way to search through file contents in Linux Mint than having to type
grep -rnw . -e 'my search text'
I just noticed there is a 'search' command
$ which search
/usr/local/bin/search
However, when I look at its help text; I am presented with this:
usage: search [arguments] [options]
arguments:
for text
in directory
I am unable to make sense of this, any arguments I try passing in seem to inevitably lead me to the same text.
There is also no man page entry, I tried that as well.
I have tried e.g.
$ search "my search text" .
$ search . "test"
$ search . . . .
Lacking a way to get this to work, I may opt for an alias. Sadly though, 'search' is already taken..
I just did a clean install, and went to have a look at this 'search' executable. Turns out, it is in fact a shell command with the following resulting command:
find $directory -type f -exec grep -$case$verbose "$text" --color=auto -n {} \;
The four $variables above are respectively populated with the search directory, case-sensitivity, verbosity and ultimately the single word search text.
I have no idea why this facility exists, but as the manual suggests, you can only use it to search for a single word (no spaces), and in the specified following directory.
An example looking for (case insensitive) and without verbosity would thus be:
search for bar in ~/home/foo
The command was apparently added in Linux mint in 2008, as indicated in this post that also details its use.
Holy smokes. I never thought you may need to interpret a manual so literally;
$ search for text in directory
find: ‘directory’: No such file or directory
It still doesn't completely make me sure how to use it exactly, though; I am puzzled on whether it supports regex/wildcards, etc.
I do get good results for simple case-insensitive text with
~/mytargetfolder/ $ search for singlewordspacesdontwork in .
However, it breaks as soon as there is a space, escaping it with ", \ or ' is not working.
Given my experienced, I'd recommend steering clear of this command.
I'd also like to recommend not giving answers on StackOverflow, or asking questions like these, since I seem to be having a case of the downvotes as of just now?

Using find with -L option

I have this:
mylink -> myfile
When I do:
find -L . -name 'mylink'
I get:
./mylink
I don't understand why this is so, given this from the man page:
-L : Follow symbolic links. When find examines or prints information about files, the information used shall be taken from the properties of the file to which the link points, not from the link itself (unless it is a bro- ken symbolic link or find is unable to examine the file to which the link points).
Based on the above I was expecting the following behavior for my example case: find starts the search. It encounters mylink. Since -L is in effect it dereferences it and gets the name of the pointed to file 'myfile'. The file name does not match the pattern 'mylink' and nothing is reported. Whats happening?
The name is not a property of the file, it's a property of the directory containing it.
If you want to match the contents of a symlink, use -lname.
The 'following of links' applies when there is a link to a directory.
A link to a file is just a file itself, to find. Without -L a link to a directory is also just a file.
Only when -L is added, will find recurse into the linked-to directory

How to find all files executable by specific user (not current)

How to find all files which can be executed by specific user (not current!)
For current I can do it like
find /some/start/dir -executable
But now I want to do something like: find all files which user 'josh' can execute (by 'other' permissions, 'user' permissions and 'group' permissions). Of course, I do not know users's 'josh' password so I cannot su'ing.
Look up the user id of "josh" in /etc/passwd.
Then run: find /some/start/dir -type "f" -uid <ID> -perm 111.
I know this is an older thread, but I had to do this recently and it is still relevant.
Since we're talking about *nix permissions, one tedious yet thorough way to approach this is by looking at the membership that the ID has on the system:
ie:
# assuming josh is a member of group "grpname"
find / -user josh -perm -100 # gets files owned by josh & are executable
find / -group grpname -perm -010 # gets files with grp ownership and executable
# via group
# Must be repeated for each group josh is in
find / -perm -001 # gets files executable by any user
Note there could be some overlap for files that josh owns but are also owned by group "grpname". A sort|uniq would filter those out pretty easily.

linux find command is not working properly

I am using Linux(Ubuntu), I am trying to find the files, but it is not working properly.
I have created some files in my directory structure, for example: World/India/Maharashtra/Pune/filename.xml
When I use the find command like:
find /home/lokesh/Desktop/Testing_India2/Test/World/India/Maharashtra/ -name filename*.xml -mmin -3000
It is giving the result perfectly.
But, when I am using the same command at "World" or "India" level:
find /home/lokesh/Desktop/Testing_India2/Test/World/ -name filename*.xml -mmin -3000
it does not give any result.
I have lots of directories at "India" level as well as at "Maharashtra" level and may be some directories within "Maharashtra's" inner directories. I have to find each file created in all directories.
And I have mounted all folders from different machine.(I mean some state from different and some from different machine.)
If someone knows how to solve this problem please reply me as soon as possible.
Double quote your search string and -L to make it follow symbolic links:
find -L /home/lokesh/Desktop/Testing_India2/Test/World/ -name "filename*.xml" -mmin -30000
This is something I ran into earlier today actually when using the '*' wildcard. I couldn't get it to continually traverse the subdirectories unless I escaped the * with a .
Give this a try:
find -L /home/lokesh/Desktop/Testing_India2/Test/World/ -name filename\*.xml -mmin -30000
Yes, as mentioned you have to double qoute your -name argument or use a backslash prior to the *. The reason for it not working from one directory, but working fine in other directories, is that the * character is used for filename generation by your shell. This of course happens before the find command is executed. Therefore, if you have a file that match the filename*.xml pattern in your current directory it will be substituted before find is executed, which is not what you want. On the other hand, if there is no pattern match in the current directory, the * character is passed on to the find command unmodified. By qouting you protect the string from shell filename generation.
Regards

Resources