Menu

Handy Linux Commands

Searching for a file

The find command in linux is super powerful, and comes with a bunch of options that can help you find a file that you're looking for.

$ find <folder> -type <f - file / d - directory> -name <filename>

So to find a file called config.yml that exists somewhere in your file system (but you're not sure where), you'd do the following command:

$ find / -type f -name "*config.yml"
/home/jordan/my-config.yml
$

Searching found files for specific text

Often, after performing a search for a group of files (e.g. log files or config files) you will want to search the files for a particular piece of text. The find command can also be used in conjunction with grep to perform this search.

$ find . -type f -name "*config.yml" -exec grep -iHn "test" {} \;
/home/jordan/my-config.yml:1:test=123

The -exec operation will execute the supplied command with each found file, the grep -iHn will search the found file for the supplied text, and the {} replaces the subject of the grep with the filename output from the find command. The \; tells the interpreter to end the grep command.

I've read that this can be performed much faster using xargs instead of find/grep but this is the easiest for me to remember and has come in handy many times.

Finding out which ports are listening

The netstat command can tell us which ports are actively listening when combined with a grep.

$ netstat -ant | grep -i "listen"
tcp        0      0 127.0.0.1:43211         0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN
tcp        0      0 10.0.0.200:8080         0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:41585         0.0.0.0:*               LISTEN
tcp        0      0 10.0.0.200:41329        0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:5050            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8989            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN
tcp6       0      0 :::139                  :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:631                 :::*                    LISTEN
tcp6       0      0 ::1:6010                :::*                    LISTEN
tcp6       0      0 :::445                  :::*                    LISTEN

This doesn't tell you the processes listening on these ports, but it will tell you whether a port is actually listening, which is sometimes useful enough.

More commands to come as I remember them.