Find all .py files across directory and subdirectories, fetch the last modified date of all and return the max date in a specific format - linux

I have been struggling to do this. I have multiple .py files located in my directory and subdirectories. I want to find and list all of them with their date and time and return the date and time of the last updated .py file in a variable in the format "+%m-%d-%Y %H:%M:%S".
How can I achieve this?
As of now, I'm able to reach only at this stage where it shows me the stats of the last modified date in terminal. Below is the command I'm using:
find . -path ./ABC -prune -false -o -name '*.py' -exec ls -lat {} + | head -n1
Here I have specifically mentioned not to search in the ./ABC folder.
The output of the above command is:
-rw-rw----+ 1 owner server 8263 Jul 8 09:09 ./apps/Test.py
I'm looking for a way where I can return the date and time of the last updated file (./apps/Test.py in this case) in a variable with the format like 07-08-2021 09:09:30.

Just find ... -printf. See man find.
find . -printf "%Tm-%Td-%TY %TH:%TM:%TS\t%p\n"
I see find output prints seconds with fractional part. You can filter them with sed.

Related

How to sort all the files with a particular extension inside a directory in Cent OS 7 according to modification time?

What command should I use to sort all the files in a project directory according to the date of modification?
Tried ls -t but it does not find the file and used find to find all the files of the specified type, but could not sort them.
You can use this to find all the files of the type you are looking for and sorted by the last modified date.
find $directory -type f -name "*.type" -print0 | xargs -0 stat -c "%y %n" | sort
The above looks for all the files of type "type" and then sorts it according to the last modified timestamp.

Find file according to the access time using "grep" and "find" commands

My goal is to find all text files with extension .log, which have the last access more than 24 hours ago and contain the required text.
Here is what I have already tried:
find / *.log -mtime +1 -print | grep "next" *.log
but this doesn't work.
Question is: how can I reach the goal I have described above?Maybe some ways to modify my find expression?
The problem with your command is that you are running the grep on the output of the find command - which means you are running it on the file names, not content (actually, since you have the *.log at the end, you run it on all *.log files, completely ignoring what your find command found). also, you need -name in order to filter only the .log files.
you can use the -exec flag of find to execute a command on each of the files that matches your find criteria:
find / -name "*.log" -mtime +1 -exec grep 'next' \{};
Try with xargs:
find / -name "*.log" -mtime +1 | xargs grep "next"
But also, note what the find manual says about the arg to -atime which also applies to -mtime. That is, your mtime as specified probably doesn't get the time period you want.
When find figures out how many 24-hour periods ago the file was last
accessed, any fractional part is ignored, so to match -atime +1, a
file has to have been accessed at least two days ago.

Find files created 30 minutes before AND after a given file - UNIX

I am trying to figure out the command to display all files created 30 minutes (as an example) before and after another file was created. So far I managed to find files newer than that file
but I cannot work out how to look for both before and after given time.
A command I have used:
find -type f -newer file.txt -cmin -30
This works fine but only does half of what I am trying to do.
Also, I need to modify that to search for setuid files only, which I THINK I can do by adding the -perm -4000 in that command.
Any suggestions?
As far as I know there is no way to find file creation time.
You can try by modification time (this will get all files last-modified between 5th and 8th)
find . -type f -newermt 2012-10-05 ! -newermt 2012-10-08
(or access time replace newermt with newerat)
newerXY is flag to compare timestamps of current file with reference (see man find for more info).
According to man find (on my debian) there are 4 flags (aside from t to interpret directly as time)
a The access time of the file reference
B The birth time of the file reference
c The inode status change time of reference
m The modification time of the file reference
You can also try with 'B' birth time but it does not work for me, gives me error. I don't know why it is included in the man page
compare to another file
find / -newer file
find / ! -newer file
You can create temp file (one with modification time 30 min before the target file, another 30 mins after)
touch -d `stat -c %y test.txt` - 30 temp/before_temp
touch -d `stat -c %y test.txt` + 30 temp/after_temp
find / -newer temp/before_temp ! -newer temp/after_temp
touch -d takes a date option, so if you add and subtract correctly, this should work.

Is there any way to find out changed file after some date in whole project code?

see i am working in one BIG project source code Now i want to know which files are modified after some date.
Is there any command or any way to get that..
i have tried
# ls -R -l
but here it shows all file with last modified data but i want to filter this output by some data ...
so is there any way to do this in linux? is there any tool available for this?
#set timestamp for file
touch --date "2011-12-31" /tmp/foo
# Find files newer than 2011/Dec/31, in /some/files
find /some/files -newer /tmp/foo
Use find command with mtime arguments: Some examples are here or here
For example, list files changed in last 7 days...
find / -type f -mtime -7
For fine grained search you may try -mmin argument. See an example discussed in another SE site: Find All files older than x minutes
You should use find with -newerXY option.
m – modification time of the file reference
t – reference is interpreted directly as a time
All files modified after 2022-12-01 (inclusive):
find . -type f -newermt 2022-12-01

How to recursive list files with size and last modified time?

Given a directory i'm looking for a bash one-liner to get a recursive list of all files with their size and modified time tab separated for easy parsing. Something like:
cows/betsy 145700 2011-03-02 08:27
horses/silver 109895 2011-06-04 17:43
You can use stat(1) to get the information you want, if you don't want the full ls -l output, and you can use find(1) to get a recursive directory listing. Combining them into one line, you could do this:
# Find all regular files under the current directory and print out their
# filenames, sizes, and last modified times
find . -type f -exec stat -f '%N %z %Sm' '{}' +
If you want to make the output more parseable, you can use %m instead of %Sm to get the last modified time as a time_t instead of as a human-readable date.
find is perfect for recursively searching through directories. The -ls action tells it to output its results in ls -l format:
find /dir/ -ls
On Linux machines you can print customized output using the -printf action:
find /dir/ -printf '%p\t%s\t%t\n'
See man find for full details on the format specifiers available with -printf. (This is not POSIX-compatible and may not be available on other UNIX flavors.)
find * -type f -printf '%p\t%s\t%TY-%Tm-%Td %Tk:%TM\n'
If you prefer fixed-width fields rather than tabs, you can do things like changing %s to %10s.
I used find * ... to avoid the leading "./" on each file name. If you don't mind that, use . rather than * (which also shows files whose names start with .). You can also pipe the output through sed 's/^\.\///'.
Note that the output order will be arbitrary. Pipe through sort if you want an ordered listing.
You could try this for recursive listing from current folder called "/from_dir"
find /from_dir/* -print0 | xargs -0 stat -c “%n|%A|%a|%U|%G” > permissions_list.txt

Lists files and directories passes through to stat command and puts all the info into a file called permissions_list.txt
“%n|%A|%a|%U|%G” will give you the following result in the file:
from_
 dir|drwxr-sr-x|2755|root|root
from_dir/filename|-rw-r–r–|644|root|root

Cheers!


Resources