bash find command with path as requirement - unix

I want to get file names of files in /bin that contain letter 'm' using find command not beeing in /bin.
When /bin is my working directory it works fine but when I add /bin as requirement in path it returns nothing independently of current directory.
Works:
find -type f -name "*m*" -exec basename {} \;
Doesn't:
find -type f -name "*m*" -path "/bin/*" -exec basename {} \;

I suspect you don't want to use -path /bin… but just
find /bin -type f -name "*m*" -exec basename {} \;
The first argument to find is the path to search in. The -path flag is a pattern matching feature that checks if the pattern matches the full path of the found name.
In fact, if you had tried this command on a BSD find such as comes with macOS, it won't even let you try one of your commands, because you didn't include the path.
find -type f … # not ok
find . -type f … # ok
find /bin -type f … # ok

This will work.
find /bin/* -type f -name "*m*" -exec basename {} \;
It is equivalent to going to /bin folder and executing
find -type f -name "*m*" -exec basename {} \;

Related

Searching for particular files in a directory non-recursively using find. AIX

I have a script which has the following command. I am trying to edit this in such a way that it only searches the files in the directory of the path without going in the subdirectories. That is not recursive search
find {Path} -name "cor*" -type f -exec ls -l {} \;
Example: The command should give cor123.log only and not cor456.log. Currently it gives both
<Path>
..cor123.log
<directory>
..cor456.log
I tried using -maxdepth but it's not supported in AIX. -prune and -depth didn't help either.
Will appreciate any help. Thanks
You can use
find . -name . -o -prune
to find files and directories non-recursively.
So in your case this one will work:
find . -name . -o -prune -name 'cor*' -type f -exec ls -l {} \;
Do you need find for selecting files only?
When you know that all files starting with cor are regula files, you can use
ls -l ${Path}/cor*
or
ls -l ${Path}/cor*.log
When you need the -type f, you can try to filter the results.
The filename can not have a /, so remove everything with an / after the Path.
We do not know the last char of ${Path}. It can be /, which will make the grep -Ev "${Path}/.*/" filter useless. After the Path at least one character is needed before looking for the next /.
find "${Path}" -name "cor*" -type f 2>/dev/null| grep -Ev "${Path}..*/" | xargs -ls
Late answer but may save some. In aix
find /some/directory/* -prune -type f -name *.log
For instance make your path have the last forward slash with a wildcard then -prune
/*
find /some/directory/* -prune -name "cor*" -type f -exec ls -l {} \
Tested.

Unix: find files in every subdirectory named upd

I have directory tree structure which looks like this
/app/bad/upd /app/pass/upd /app/bad/upd /app/warn/upd
I want to build a find command which can list all the files in every sub-directory named upd.
Currently I list it individually
find /app/bad/upd -type f -name "*${FILE_NAME}*"
This might be what you look for:
find /app -type d -name upd -exec ls -l {} +
or perhaps:
find /tmp/* -type d -name upd -exec sh -c "ls -l {}/*${FILE_NAME}* 2>/dev/null" sh {} \;
If the upd directory is always in the 2nd directory down, you could simply do:
ls /app/*/upd

Concatenate all files recursively, ignoring one file extension

I want to create a concatenated file which appends all files except those which end in .XYZ from a directory (recursing into subdirectories).
I tried this but it does not work:
find . -type f | grep -v *.XYZ -exec cat {} \; > /tmp/alldata.txt
This works but fails to exclude files ending in ".XYZ":
find . -type f -exec cat {} \; > /tmp/alldata.txt
find . -type f -not -name "*.XYZ" -exec cat {} \; > /tmp/alldata.txt
More recent versions of gnu find include -not which negates the next argument. In this case, you can combine that with the -name argument to get what you want without invoking grep -v.

find command not working from other directory

My dir sturcture that looks like
x
/log
/bin
I am giving this command from dir- x/bin
find ../log -type f -name \*.log -mtime +90 -exec ls -l {} \;
(to find and display list of files older than 90 days.) and it doesn't display anything.
Whereas if i execute same command in dir- x/log
find . -type f -name \*.log -mtime +90 -exec ls -l {} \;
it gives me a list of files older than 90 days.
Can you please help?
Recall that paths are relative.
If you have a dir sturcture that looks like
x
/log
/bin
AND your're in x/bin then you need to give the relative path to x/log, ie
pwd
x/bin
find ../x/log -type f -name \*.log -mtime +90 -exec ls -l {} \;
I hope this helps.
Two suggestions.
First, escape the * using \*. If you have any log files in current dir, they will get expanded before the command is executed.
Second, I think you mean find ../x/log ...?

Find and Tar multiple files while keeping original names

I want to find several directories, and make each one a Tar. My current find command sends the filenames to a file for logging.
find ${SRC_DIR} -name ./* -level 0 -type d -mtime +14 -exec basename {} \; >>${FILE}
This works fine. Now I want to take each of the files that I found and Tar them all, so that they're named OriginalFileName.tar.
Is there a way to do this all in one command, and how do I get the Tar files to have the original filenames?
Does this help your problem:
for i in $(find ${SRC_DIR} -name ./* -level 0 -type d -mtime +14 -exec basename {} \;); do tar -cvf $i.tar $i; done

Resources