How to find the latest executable file in a directory - linux

I'm on linux an I want to know how to find the latest executable file in a directory?
I already know how to find the latest with:
ls -rt1 | tail -1
but how to filter out executable files?
EDIT:
I found a solution:
find path/to/dir/myfile* -perm /u=x,g=x,o=x -mtime 0 | tail -1
is this save? or is there a better solution??

Given the basic find command to look for files starting on current directory:
find . -type f
Let's add functionalities:
To find executables you can use the -executable option:
find . -type f -executable
To just find on one level of depth, that is, not within subdirectories, use the -maxdepth 1 option:
find . -maxdepth 1 -type f
To find last modified file in a directory, you can use How to recursively find the latest modified file in a directory?:
find . -type f -printf '%T# %p\n' | sort -n | tail -1 | cut -f2- -d" "
All together, this looks for last modified executable file in one level depth:
find . -maxdepth 1 -type f -executable -printf '%T# %p\n' | sort -n | tail -1 | cut -f2- -d" "

Related

UNIX: Use a single find command to search files larger than 4 MiB, then pipe the output to a sort command

I currently have a question I am trying to answer below. Below is what I have come up with, but doesn't appear to be working:
find /usr/bin -type f -size +4194304c | sort -n
Am I on the right track with the above?
Question:
Use a single find command to search for all files larger than 4 MiB in
/usr/bin, printing the listing in a long format. Pipe this output to a sort command
which will sort the list from largest to smallest
I'd fiddle with for -printf command line switch, sth like this:
find YOUR_CONDITION_HERE -printf '%s %p\n' | sort -n: %s stands for size in bytes, %p for file name.
You can trim the sizes later, e.g. using cut, e.g.:
find -type f -size +4194304c -printf '%s %p\n' | sort -n | cut -f 2 -d ' '
But given the fact you need the long list format, I guess you'll be adding more fields to printf's argument.
Related topic: https://superuser.com/questions/294161/unix-linux-find-and-sort-by-date-modified
You are on the right track, but the find command will only output the name of the file, not it's size. This is why sort will sort them alphabetically.
To sort by size, you can output the file list and then pass it to ls with xargs like this:
find /usr/bin -type f -size +4194304c | xargs ls -S
If you want ls to output the file list on a single column, you can replace the -S with -S1. The command would become:
find /usr/bin -type f -size +4194304c | xargs ls -S1
To make your command resistant to all filenames, I would suggest using -print0 (it will separate paths with the null character which is the only one that cannot appear in a filename in Linux). The command would become:
find /usr/bin -type f -size +4194304c -print0 | xargs -0 ls -S1
You could also try
find /usr/bin -type f -size +4194304c -ls | sort -n -k7
and if you want the results reversed then try
find /usr/bin -type f -size +4194304c -ls | sort -r -n -k7
Or another option
find /usr/bin -type f -size +4194304c -exec ls -lSd {} +

How to use find command with sorting by creation date&time recursively?

I have many time tried for this issue.
find command results do not care listing order.
How to use directory listing based on creation date&time from find recursive command result ?
ls -lc
This works good , but not recursively way .
find . -type f -iname "*.txt" -exec ls -lc {} \;
This doesn’t work.
find . -type f -iname "*.txt" | sort -n
This also only name based.
solution from https://superuser.com/q/294161/992527 combined with the command from the question:
find . -type f -iname "*.txt" -printf "%T# %Tc %p\n" | sort -n
See the different answers for variations of the command.
Explanation cited from referenced question:
printf arguments from man find:
%Tk: File's last modification time in the format specified by k.
#: seconds since Jan. 1, 1970, 00:00 GMT, with fractional part.
c: locale's date and time (Sat Nov 04 12:02:33 EST 1989).
%p: File's name.
Solution for MacOS copied from #cooljobs' comment to the question
find . -type f -iname "*.txt" -exec stat -f '%B %m %N' {} \; | rev | cut -d '/' -f 1 | rev | sort -n

Count only visible files in directory

I'm having problem with hidden file in my directory. If I use $(find . -type f | wc -l) it shows 8 files, which counts hidden file too, there should be only 7 files.
Is there anything that could count only visible files?
Ignore the names that start with . by saying:
find . ! -name '.*' -type f | wc -l
From the man page:
! expression
-not expression
This is the unary NOT operator. It evaluates to true if the
expression is false.
If you have filenames with newlines, then you can do using gnu find (as suggested by gniourf gniourf in comments):
find . ! -name '.*' -type f -maxdepth 1 -printf 'x' | wc -c
find . -type f -not -path '*/\.*' | wc -l
-not -path allows you to ignore files with name starting with . (hidden files)
Exclude all files starting with ( . )
find ./ ! -name '\.*' -type f | wc -l
! simply negates the search
If that doesnt work then try this dirty looking solution:
ls -lR | egrep '^(-|l|c|b|p|P|D|C|M|n|s)' | wc -l
Listed all types of files there excluding directories.
You can find the type of files in linux here
without -R of you want to look only in same dir.

grep command to find files

I'm looking for a command that use grep to search in /usr/bin for all the files who have 2 links and sort them in ascending.
The second command I'm looking for must use the first one and display just the files that contain the "x"
Thanks you
You can do this direct from grep, eg:
grep -r --include=*.py "HOSTS" .
will search recursively ('-r') under the current directory ('.') in all python files ('*.py') for the string "HOSTS".
This would do
find /usr/bin -links 2 -print0 | xargs -0 ls -adltr
modify the ls to do the sorting you require
find /usr/bin -links 2 -print0 | xargs -0 grep -l "x"
Files containing the "x" :)
If you meant: 'contain the x' as 'are executable (x appears in ls -l output), use
find /usr/bin -links 2 -executable -print0 | ls -adltr
To see only dirs:
find /usr/bin -links 2 -type d -executable -print0 | ls -adltr
To see only files:
find /usr/bin -links 2 -type f -executable -print0 | ls -adltr
Note: directories get 2 links by default (. is a link) so you might want to look for -links 3 with directories

Find files older than X and Count them

Using Linux. What I need to do is determine the number of files in a directory(recursively) that are older than DATE and echo that number.
I have:
find /u1/database/prod/arch -type f -mtime +10 -exec ls -laR | wc -l \;
That lists the files fine.
And then I have:
ls -laR | wc -l
Which lets me count the files recursively.
But I can't seem to put them together. I think I need a script to do this but don't know how to do that.
Would love some help
find /u1/database/prod/arch -type f -mtime +10 | wc -l
works here.
You dont need the exec. use -print (or nothing) and find will print a line per file (and handle the recursion)
find /u1/database/prod/arch -type f -mtime +10 -print | wc -l

Resources