4

I am trying to figure out how to use find to show a list of all files in my current directory and lower that have been modified since my current terminal session was started.

Obviously a recursive find is to be used, but how do i delineate the results to just show files that are modified since i logged in? do i check them against some other file that is always modified on login? Is there a built in way?

say some thing like: find . -newer 'xxxx' where xxxx is some file that gets modified at the start of a terminal session. But what file would work for that?

2 Answers 2

2

There's no specific file you can use for this, but it's easy to add your own.

In your .profile or .bash_profile or whatever you could do something like

TIMEFILE=$HOME/.lastlogin
[[ ! -f $TIMEFILE ]] && touch $TIMEFILE
find $HOME -newer $TIMEFILE
touch $TIMEFILE

The [[ line is there to prevent find from complaining if the file doesn't exist.

Edit: Ah, sorry, I may have slightly misunderstood your question. You may want to run the command at any time, so in this case you could just have this in your .bash_profile

touch $HOME/.lastlogin

And now from the command line

find $HOME -newer $HOME/.lastlogin

You can reset the timer at any time by touching the file again.

4
  • ok so each time my shell is invoked it makes a new file and then uses it as the time baseline?
    – DanMan3395
    Jun 28, 2018 at 2:13
  • 1
    It's important to do the find before the touch. So you use the previous timestamp file (ie your previous login) for the comparison, then update the timestamp when it completes. Or if you just want to run the command at any time, the touch command in your .bash_profile is sufficient, and you can run the find command at any time. Jun 28, 2018 at 2:16
  • 1
    Ah, I may have slightly misunderstood your question, so I added some additional discussion to the answer. Jun 28, 2018 at 2:24
  • This makes sense thank you. Just curious, is there some other, built in way to search for files modified since login? My original assumption was find but i want to make sure i am not overthinking something simple.
    – DanMan3395
    Jun 28, 2018 at 2:28
1

In Bash you could calculate the time that has passed since you logged in, with last.

TIMEDIFF=$(( ( $(date --date="$(last -1 | head -n 1 | tr -s " " | cut -d" " -f3-6)" +%s) - $(date +%s) ) / 60 ))

Note that this will yield a negative number. (That's useful for the next step.)

Then you add the time difference with the -mmin parameter to the find command:

find . -mmin $TIMEDIFF

Of course you can also make it a one-liner.

2
  • This doesn't work for me as the find just shows everything that exists. I also get weird results from the code in that variable: TIMEDIFF=$(( ( $(date --date="$(last -1 | head -n 1 | tr -s " " | cut -d" " -f3-6)" +%s) - $(date +%s) ) / 60 )) date: invalid date ‘134.29.149.192 Wed Jun 27’
    – DanMan3395
    Jun 28, 2018 at 2:11
  • Fixed the oneliner, but requires GNU grep: TIMEDIFF=$(( ( $(date --date="$(last -1 $(whoami) | head -n 1 | grep -oP '\w{3} \w{3} \d{2} \d{2}:\d{2}')" +%s) - $(date +%s) ) / 60 )). Alternatively, if you're using bash, the $SECONDS variable might help you out more: TIMEDIFF="-$(( $SECONDS / 60 ))"
    – Flourid
    Aug 19, 2022 at 10:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .