Using the command line in linux – part 4



Dealing with text…. we’ve looked at a couple of basics on logging in and starting to use the command line and hopefully not feeling too helpless there… we’ve seen ways to navigate directories and how to find new commands and how to find out more about how to use them. Now it’s time to talk about how to manipulate (work with) text files. Most every important configuration file in linux is text based and editable from the command line. This can be a VERY good thing at times, or very intimidating if you’re not comfortable with a command line environment. It’s worth noting that you CAN edit configuration files with a graphical interface text editor….


So, there are a number of text editors, one that I use quite a bit is pico, but there is also vi and emacs. pico is a nice, easy to use text editor and is highly recommended for beginners. With pico, ctrl-o writes a file and ctrl-x closes, with vi it’s a bit more complicated, esc must be pressed to quit editing and then :wq are typed (and enter) to write, and quit. vi is worth an article or two on it’s own and frankly, I don’t know that it would qualify for “basics”…. Let’s assume we can use pico…

First off, let’s take a look at a text file…
$cat textfile.txt

should give you a stream across the screen of the contents of textfile.txt… nice, but hard to read. OK, try this…
$less textfile.txt
now you can use the up or down arrows to move up or down one line at a time, and page up/page down to go down a page at a time.

$more textfile.txt has the same effect… except “enter” moves you by line and space moves by page…

What if you wanted to find out if there was a certain word in textfile.txt…..

$grep specialword textfile.txt
this line uses specialword which is a keyword that we will later search for

grep allows you to search for a term…. I use it daily….
For instance… (Has the googlebot visited?)

grep googlebot access_log -i
-i tells it to not be case sensitive…

It’s possible too, to chain grep commands: what if I wanted to see if the googlebot had visited pages published in January…. (2006/01 in the path…) Us the | (pipe) to feed output of the first part, to whatever followes the | (the | is usually the “shift” equivalent of \ )

grep googlebot access_log -i | grep 2006/01

I hope you’ve enjoyed the last few articles which give some ideas for how you might get started with the command line if you’ve never tried it before… Fortunately most operating systems respond to the “help” command and many have features to give further information when you follow a command with -h or –help or /? or /h …….

Dealing with text files at the command line can quickly get very difficult and take us out of “basics” territory into lower-intermediate levels, so I’m going to cut this series here before I get you in too deep…

   Send article as PDF   

Similar Posts