How to check if a file has been modified in Bash?

How to check if a file has been modified in Bash?

It is suitable for waiting for changes to files from shell scripts. It can either exit once an event occurs, or continually execute and output events as they occur. $ inotifywait -m -e modify /tmp/testfile Setting up watches.

How to check whether a file was created more than X?

I want to check in linux bash whether a file was created more than x time ago. let’s say the file is called text.txt and the time is 2 hours. You can use -cmin for change or -amin for access time.

How to check the size of a file using Bash?

For getting the file size in both Linux and Mac OS X (and presumably other BSDs), there are not many options, and most of the ones suggested here will only work on one system. cut -d’ ‘ -f1 doesn’t work because on Mac, the number may be right-aligned, padded with spaces in front.

How to check the date of a file in Bash?

I use bash; modify as necessary. The +%s tells date to output a UNIX time (the important bit is that it’s an integer in seconds). You can also use stat to get this information – the command stat -c %Y is equivalent. Make sure to use %Y not %y, so that you get a usable time in seconds.

How to find the last modified date of a file?

I solved the problem this way: get the current date and last modified date of the file (both in unix timestamp format). Subtract the modified date from the current date and divide the result by 60 (to convert it to minutes). expr $(expr $(date +%s) – $(stat mail1.txt -c %Y)) / 60 Maybe this is not the cleanest solution, but it works great.

How to get time since file was last modified in Linux?

You could also compute and print the elapsed seconds directly without temporary variables: Unfortunately the BSD implementation of date (for example in Mac OS X) doesn’t support the -r flag. To get the last modification seconds, you can use the stat command instead, as other answers suggested.