Using find command to count certain type of executables - unix

I'm trying to use Unix's find command to count the number of executable files in a directory of a certain type, namely Solaris(MSB) executable's. I know I can get the count of all executable files in this directory fairly easy with
find . -type f -perm -u+rx | wc -l
however this doesn't count Solaris(MSB) executable files exclusively. I thought to remedy this I would just throw in a -name flag, something like this.
find . -name "sparc*" -type f -perm -u+rx | wc -l
This will correctly return that there are 6 only if I remove the part of the command that specify's that they need to be executable, if I keep this part of the command it returns a count of 0 which is "wrong". When I look at the ls -l command below I can see that these files are executable I think? or that they are pointing to an executable? This might be the root of the problem.
ls -l
lrwxrwxrwx 1 root other 57 Jul 15 2005 sparc-sun-solaris2.9-c++ -> /usr/local/gnu/pkg/gcc-3.3.6/bin/sparc-sun-solaris2.9-c++*
Any insight is appreciated.

Try
find -L . -type f -perm -u+rx | wc -l
or
find -L . -name "sparc*" -type f -perm -u+rx | wc -l
or whatever conditions you need.
Option -L instructs find to follow symbolic links instead of processing the link itself. (see e.g. https://www.unix.com/man-page/posix/1p/find/)
For example with the symbolic link
sparc-sun-solaris2.9-c++ -> /usr/local/gnu/pkg/gcc-3.3.6/bin/sparc-sun-solaris2.9-c++*
find should behave as if the file /usr/local/gnu/pkg/gcc-3.3.6/bin/sparc-sun-solaris2.9-c++ would be directly located at sparc-sun-solaris2.9-c++
If your find doesn't support option -L you can try -follow like this:
find . -follow -name "sparc*" -type f -perm -u+rx | wc -l

Related

Count number of lines in each directory

I have a directory structure as below
output/a/1/multipleFiles
output/a/2/multipleFiles
output/a/3/multipleFiles
output/b/1/multipleFiles
output/b/2/multipleFiles
output/b/3/multipleFiles
I want to know number of lines each directory has. So basically, number of lines at each inner most directory level instead of file level. The innermost directories 1, 2, 3 are different kinds of output we generate for our analytics which contains multiple hadoop part-xxxx files.
I moved to output directory and tried the below command.
find . -maxdepth 2 -type d -name '*' | awk -F "/" 'NF==3' | awk '{print $0"/*"}' | xargs wc -l
But I am getting an error as
wc: ./a/1/*: No such file or directory
wc: ./a/2/*: No such file or directory
wc: ./a/3/*: No such file or directory
but if I try
wc -l ./a/1/*
I am getting correct output for that specific folder.
What am I missing here.
EDIT:
I updated my command as below to remove unnecessary awk commands.
find . -mindepth 2 -maxdepth 2 -type d -name '*' | xargs wc -l
This again results in error as
wc: ./a/1: Is a directory
wc: ./a/2: Is a directory
wc: ./a/2: Is a directory
Give a try to execdir, for example:
find . -maxdepth 2 -type f -execdir wc -l {} \;
This will run the command wc -l {} only within the directory that the file has been found, from the man:
-execdir The -execdir primary is identical to the -exec primary with
the exception that utility will be executed from the
directory that holds the current file.

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: count number of jpeg files recursively except for one subfolder in every folder?

I think the code to count all the jpeg files recursively in a folder is,
find . -type f -name "*.jpeg" | wc -l
but I now realize I need to exclude some subfolders...
for instance, my folder consists of 5 subfolders and in each subfolder there is a subsubfolder named "meh" consisting of jpeg files I wish not to include in my count... Could anyone let me know how to do that?
Thanks so much for your guidance.
You can do this with find's option -prune or -regex.
find . -name meh -prune -o -name '*.jpeg' -print | wc -l
find . -not -regex '.*/meh/.*' -a -name '*.jpeg' -print | wc -l
Weird that #Prune didn't answer that.
Since find includes the relative path of each file, you could do this:
find . -type f -name "*.jpeg" | grep -vc /meh/
Use any grep variant to filter the output of find.
While you're doing that, use the count option from grep, -c.
-v is reverse logic: list only those that do not match the given pattern.
find . -type f -name "*.jpeg" | egrep -c -v "/meh/"

Unix find and move command

I need a Unix command to find all the text files older than 3 days and move them to some other location in single command.
In that case you want find and use its -exec and -mtime flags.
It should be something like:
find . -mtime -3 -type f -print0 | xargs -0 -I file mv file /new/dir/.
man find
man xargs

how can i find the modified files in ubuntu

I want to find the details of the modified files such as size, permission and modified time in ubuntu and set the cron jobs in cpanel,i have used like this
find /home1/sitename/public_html/ -type f -ctime -1 -exec ls -ls {}
and also like this
find /home1/sitename/public_html/-type f -ctime -1 -exec ls -ls {} \;
but it is not working .
Now I want all the details of the files modified .
try this :
find /home1/sitename/public_html/-type f -ctime -1 | xargs ls -ls

Resources