Check if user has permission to execute file - bash

as in title: how to check if user(script parameter) have permission to execute file using find in bash?
I think is something with -perm but i tottaly dont now how to start with that.

Use find as in the following example, given user as script parameter $1:
$ find /path/to/dir/ -user $1 -type f -perm -u+x

Related

how to use find result as part of regex in bash shell

I want to find out which directory doesn't have *.dsc file in it, so I had a try:
find . -type d -exec ls {}/*.dsc
the output is as below:
ls: connot access './abc/*.dsc': No such file or directory
I'm sure that there is a dsc file in abc/ directory.
Seems bash shell will treat "{}/*.dsc" as a string but not a regex, so I had another try:
find . -type d|xargs -I {} ls {}/*.dsc
but the result is the same.
How could I get the command work as I need?
Can you try this one out :
find . ! -iname "*.dsc" -type d
!: This is used to negate the match. It will list everything but files with .dsc extension.
-type d: This will fetch all the directories.
I wasn't able to use wildcards in find + ls, but the command below works.
find . -type d -not -path . | while read -r dir; do [ -f $dir/*\.dsc ] || echo $dir; done
It test separately whether a file *.dsc exists and echoes the directory otherwise.

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.

Find files that has write permission for current user

I need to find files in a directory which has "write" permissions for the current user.
I tried using find like below:
find $DIR -type f -user $(whoami) -perm -u+w
But the above find looks for file owned by $(whoami).
The files that i am looking for may not be owned by $(whoami) but still has permissions (for eg., 666 permission)
I also tried below (sorry if it looks stupid):
find $DIR -type f -exec test -w {} \;
Your second approach seems to be on the right track; you just forgot to print the filename.
You could make a script
#!/bin/sh
test -w "$1" && echo "$1"
and then use -exec on this script.
UPDATE: If you don't like the idea of having a separate script, you can put it also into one line:
find $DIR -type f -exec bash -c 'test -w "$1" && echo "$1"' x {} \;
The lone x just serves as a placeholder, because this is what bash sees as $0, and you want to assign the current file to $1. Another possibility would be
find $DIR -type f -exec bash -c 'test -w "$0" && echo "$0"' {} \;
but for me, this looks less clear and like a misuse of $0.
Your second command is correct, you just have to print the paths. Usually -print doesn't have to be mentioned, but with a few options like -exec you have to explicitly specify that you want to print the found paths.
find "$DIR" -type f -exec test -w {} \; -print
You may wonder: »Why does this print only writeable files?«
find uses short-circuit evaluation – the next option is only evaluated if the preceding option succeeded.
Example: In the command find -type f -user USER the check -user USER will only be performed on files, not on directories as -type f fails for directories.
The -exec cmd option also acts as a check – the exit status of cmd will be used to determine whether the check passed or not.
Example: find -exec false \; -user USER won't ever perform the check -user USER since the program false never succeeds.
In your case that means that -print will only be executed if test -w succeeded.

Shell script for coppying files from one directory to another

I am trying to write a shell script to copy files with some specific name and creation/modification date from one folder to another. I am finding it hard that how I can do this ?
However i have tried this till now.
srcdir="/media/ubuntu/CA52057F5205720D/Users/st4r8_000/Desktop/26 nov"
dstdir="/media/ubuntu/ubuntu"
find ./ -type f -name 'test*.csv' -mtime -1
Now my question is, is it possible to put that find command into a if condition to get the files found by find.
I am very new to shell script. any help would be really appreciated.
What I found useful for this is the following code. I am sharing this here so that some one who is new like me can take some help from it:
#!/bin/bash
srcdir="/media/ubuntu/CA52057F5205720D/Users/st4r8_000/Desktop/office work/26 nov"
dstdir="/media/ubuntu/ubuntu"
find "$srcdir" -type f -name 'test*.csv' -mtime -1 -exec cp -v {} "$dstdir" \;

Finding a File With Specific Permissions

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.

Resources