As a relative *nix newbie (compared to my years of windows experience) I’m constantly learning about new ways to do things, new commands, new places in the OS. Before about a year ago my only real interaction with *nix was via ssh sessions to web servers, which I mostly just used to edit the odd file and access mysql for database backups, restores, and queries. I started managing a couple of debian based LAMP servers at work which necessitated learning a bit more, and lately I’ve been getting into python scripting and fooling around with the Raspberry Pi, so I’ve been in a *nix environment a lot more.
Unfortunately I’m forgetting commands nearly as fast as I’m learning them, unless they are so useful that I use them on a daily basis. That is why I’m going to start to write little blurbs about some of the commands I’ve been discovering, so I can concentrate them somewhere to help me remember them all! I will start with one of the first commands I learned back when I was doing web development (aside from basic folder navigation and file manipulation, etc).
Todays command is grep. I mostly use it to narrow down standard output of other commands. For instance, if I have a directory full of files and I know “banana” is somewhere in the filename of a file I want to check the permissions of, I can pipe the output of ls -l to the grep command with a string argument such as “banana”:
ls-l | grep “banana”
This will return only the lines in the output of ls -l which contain the string pattern “banana”, which is super useful for a variety of things. It seems you can also omit the quotes in the argument (Ie: banana instead of “banana”) and it still works.
I also have been using it to narrow down the results of ps aux to find the pid of a specific running task so I can kill it with pkill
ps aux | grep “python”
This will return a process ID for tasks associated with the python. I can then pass the id to pkill to stop the task.
Later I realized that there is already a varient of the grep command called pgrep which does basically the same thing.
grep is a very versatile command, and once you start using it you’ll never sift through a page of standard output visually parsing for the item you’re looking for again.
p.s. even though this feature is called “command of the day” there is no way in hell I’m actually going to be doing write-ups daily, so don’t hold your breath :)
- dujoducom posted this