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

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

Related

Combining a few " find "commands in linux

find /home/imk-prac/ \( -type f -size -13c -name '*\?plik\?*' \) -o\( -type d -name '\[Kolo1\]*' \)2> /dev/nul;
This command counts normal files which has less than 13 symbols and contains a sequence of symbols ?plik?.
I want to add looking for files which were modified less than 30 days and I wrote this command:
find /home/imk-prac/ -type f -mtime -30 -exec ls -l {} \; > /dev/null
I don't know how to combine this two commands in to one.
I wanted to add looking for files with specified quantity of symbols and I found this command:
grep -Po '(^|\s)\S{64}(\s|$)' file
But there is the same problem or even worse, because of grep command.
Thanks for your time and I hope you will help me to figure it out ;)

How do I find the number of all .txt files in a directory and all sub directories using specifically the find command and the wc command?

So far I have this:
find -name ".txt"
I'm not quite sure how to use wc to find out the exact number of files. When using the command above, all the .txt files show up, but I need the exact number of files with the .txt extension. Please don't suggest using other commands as I'd like to specifically use find and wc. Thanks
Try:
find . -name '*.txt' | wc -l
The -l option to wc tells it to return just the number of lines.
Improvement (requires GNU find)
The above will give the wrong number if any .txt file name contains a newline character. This will work correctly with any file names:
find . -iname '*.txt' -printf '1\n' | wc -l
-printf '1\n tells find to print just the line 1 for each file name found. This avoids problems with file names having difficult characters.
Example
Let's create two .txt files, one with a newline in its name:
$ touch dir1/dir2/a.txt $'dir1/dir2/b\nc.txt'
Now, let's find the find command:
$ find . -name '*.txt'
./dir1/dir2/b?c.txt
./dir1/dir2/a.txt
To count the files:
$ find . -name '*.txt' | wc -l
3
As you can see, the answer is off by one. The improved version, however, works correctly:
$ find . -iname '*.txt' -printf '1\n' | wc -l
2
find -type f -name "*.h" -mtime +10 -print | wc -l
This worked out.

How to find the latest executable file in a directory

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" "

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

How do I find all the files that were created today in Unix/Linux?

How do I find all the files that were create only today and not in 24 hour period in unix/linux
On my Fedora 10 system, with findutils-4.4.0-1.fc10.i386:
find <path> -daystart -ctime 0 -print
The -daystart flag tells it to calculate from the start of today instead of from 24 hours ago.
Note however that this will actually list files created or modified in the last day. find has no options that look at the true creation date of the file.
find . -mtime -1 -type f -print
To find all files that are modified today only (since start of day only, i.e. 12 am), in current directory and its sub-directories:
touch -t `date +%m%d0000` /tmp/$$
find . -type f -newer /tmp/$$
rm /tmp/$$
Source
I use this with some frequency:
$ ls -altrh --time-style=+%D | grep $(date +%D)
After going through many posts I found the best one that really works
find $file_path -type f -name "*.txt" -mtime -1 -printf "%f\n"
This prints only the file name like
abc.txt not the /path/tofolder/abc.txt
Also also play around or customize with -mtime -1
This worked for me. Lists the files created on May 30 in the current directory.
ls -lt | grep 'May 30'
Use ls or find to have all the files that were created today.
Using ls : ls -ltr | grep "$(date '+%b %e')"
Using find : cd $YOUR_DIRECTORY; find . -ls 2>/dev/null| grep "$(date '+%b %e')"
find ./ -maxdepth 1 -type f -execdir basename '{}' ';' | grep `date +'%Y%m%d'`
You can use find and ls to accomplish with this:
find . -type f -exec ls -l {} \; | egrep "Aug 26";
It will find all files in this directory, display useful informations (-l) and filter the lines with some date you want... It may be a little bit slow, but still useful in some cases.
Just keep in mind there are 2 spaces between Aug and 26. Other wise your find command will not work.
find . -type f -exec ls -l {} \; | egrep "Aug 26";
If you're did something like accidentally rsync'd to the wrong directory, the above suggestions work to find new files, but for me, the easiest was connecting with an SFTP client like Transmit then ordering by date and deleting.
To get file before 24 hours execute below command:
find . -type f -mtime 1 -exec ls -l {} \;
To get files created today execute below command:
find . -type f -mtime -1 -exec ls -l {} \;
To Get files created before n days before, where +2 is before 2 days files in below command:
find . -type f -mtime +2 -exec ls -l {} \;

Resources