Jan Carlo Viray React · Angular · Node · SQL · Cloud · Entrepreneurship

Portfolio ·  LinkedIn ·  Twitter ·  Stack Overflow ·  Github ·  Contact Me

Find Files, Texts, Processes in Linux

I’m building a “cheat sheet” on finding files, monitoring and everything related to that in Linux. Here’s some of them.

Recursively Find Files Containing a Specific Text

grep -rn "pattern" /path/to/dir

# include only certain files
grep -rn "pattern" --include="*.js" --exclude="*node_modules/*" --exclude="*.min.js*" /path/to/dir
  • add -w to match whole words instead of partial
  • add -i for case insensitive search

How to Find Files in Linux

find /path/to/dir -name "*.js"

# execute a command on those files
# "{} \;" just means the command ends
find /path/to/dir -name "*.js" -exec rm -f {} \;
find /path/to/dir -name "*.js" -exec chmod 700 {} \;

The find command is very powerful and can do additional things like:

  • finding files with certain permissions find . -type f -perm 0664
  • finding files belonging to a specific user find . -user joe
  • finding files modified N days back find . -mtime 50
  • finding files of given size find . -size 50M

Find If Your Process Is Running

ps aux | grep postgres

Find Open Ports and the Processes That Owns Them

lsof -i
lsof -i | grep apache

# list processes using tcp port 80
lsof -i tcp:80

lsof is a command that lists open files. Running it by itself will list all open files in active processes. Adding -i without option lists all that has network files. Adding an option

Find Your Internal IP Address

If you have any questions or comments, please post them below. If you liked this post, you can share it with your followers or follow me on Twitter!