Ubuntu – How to remove lines from output containing a particular string

command line

I am trying to find the latest modified files in my system say in the last 2 hours or so. The command that I am using is:

sudo find / -mmin -120 -printf '%T@ %p\n' | sort -n | cut -f2- -d" " | more

But the above command outputs a lot of entries that contain the string /proc. So, are there any better ways by which I can remove those lines from the output?

Also, if you have any better methods to deal with the above situation then please suggest. The only criteria is that the output should contain files as well as directories.


I modified the above command to also include names with newline, then added Arronical's answer and αғsнιη's answer to it. The final outcome is:

sudo find / ! -path '*/proc*' -type f -mmin -120 -exec ls -al {} \; | grep -v anystring | less

Best Answer

You can use grep -v to remove lines containing a string, such as '/proc', you could add this as a pipe to your command to make:

sudo find / -mmin -120 -printf '%T@ %p\n' | grep -v '^/proc' | sort -n | cut -f2- -d" " | more

However, as files in /proc/ are transitory in nature, you'll be likely to get some errors like

find: â?~/proc/22232/fdinfo/5â?T: No such file or directory

You can avoid these by redirecting the STDERR of the find command to /dev/null using, 2>/dev/null like so:

sudo find / -mmin -120 -printf '%T@ %p\n' 2>/dev/null | grep -v '^/proc' | sort -n | cut -f2- -d" " | more

A Better Method

Rather than filtering the results, and getting rid of your error messages, you can alter your find command so that it never looks in /proc:

sudo find / -not -path '/proc/*' -mmin -120 -printf '%T@ %p\n' | sort -n | cut -f2- -d" " | more