*nix Command Line Tips -n- Tricks
NetworkNewz | For Networking Professionals
Editor's Note - 01.21.02



Microsoft has stooped to another all time low. It seems that this time the monolith was caught stuffing ballots in an online poll to see what readers preferred as a web development platform, Java or .NET. It is exactly this type of tactic that M$ is famous for and is losing face over (much to their confusion...). I have said it before and I will say it again, Microsoft has *no* class. Read more about it here.

On to more good news about Microsoft. Apparently Microsoft has hired an "independent" analytical group to "prove" that Microsoft products are more cost effective than Linux. In fact, Brian Valentine (VP in charge of Windows division for Microsoft) knows what the results of this analysis will be, even thought they have yet to publish their findings. Makes one wonder. Anyway, read more about it here.

Today's article is not directly related to networking; however, it covers an important topic for networkers using *.nix systems. With tools such as secure shell, the power of the command line is truly evident and remote system administration can be made simple, with the right tools.


Sincerely,

Jay Fougere
The NetworkNewz Team


*nix Command Line Tips -n- Tricks

Administering a *nix (Unix or a Unix clone such as Linux or any of the *BSDs) server remotely is usually pretty easy. As long as the network is up, you can access the machine from anywhere that you have a network connection (so long as you don't have any IP filters, etc...). Simply secure shell (SSh) in to the machine, fix whatever needs to be repaired, and log off. Usually, everything that you will need to do is from the command line. (At least I cannot think of an administrative function that cannot be handled from the command line.)

That brings us to today's article. Before we can really discuss using tools like SSh for remote administration, you will need an intro to the *nix command line. Let's look at the most used tools for navigating from the command line on *nix systems.

The first (and probably most used command) is 'cd', which is an acronym for "change directory". Microsoft based systems, for the most part (i.e., if you are not using Distributed File System), will have a separate root directory for each storage device on the machine. In other words you might have a root directory, C:, for your hard drive; you may have another root directory, D:, for your CD drive; etc...

*nix systems have one root directory, '/'. Any additional drives or partitions will be mounted under the '/' directory. Knowing this, you can get to the root directory of any *nix system by entering 'cd /' at the command line (assuming, of course, that you have the proper permissions to change to that directory).

What about typos? Do you have to reenter the command that you just tried to run, but "fat fingered" it? No. Use your up and down arrow keys to scroll through your "BASH history". This will even work after a reboot. Cool, huh?

Now, here is a neat trick. Let's suppose that you are doing some work in a directory /usr/src/project1. Now, let's suppose that there is a file in /usr/src that you need to modify. Simply enter 'cd ..' from inside of /usr/src/project1 to change to the /usr/src directory, rather than entering 'cd /usr/src'. This will save you *tons* of time if you get used to using it. 'cd ..' will move your working directory up to the next level directory in all directories except '/', of course.

Now, let's assume the working directory is /usr/src. Let's also suppose that you want to go back into your /usr/src/project1 directory. Rather than change directories with the full path name (i.e., 'cd /usr/src/project1), simply enter 'cd project1'. As long as you are trying to change to a directory that is in the directory that you are in, you can 'cd' to that directory name directly, rather than having to enter a full path name.

The next command we will take a look at is 'ls'. 'ls' is similar to the old DOS command 'dir', but much more powerful.

Simply entering ls at the command line will list all non-hidden files and folders in the directory that you are in. In *nix systems, to hide a file or folder, all that you have to do is precede the name of the file or directory with a '.'. For instance, if you had a directory named 'mydirectory', but you don't want this directory to show up doing a normal 'ls', simply rename the directory '.mydirectory'.

If you want to be able to see these hidden files and directories, simply use the '-a' switch (which stands for "all").

Another very useful switch to be used with 'ls' is the '-l' switch. This lists the contents of the directory in a list form with much more information than a simple 'ls' will provide. Using the '-l' switch will not only list the files and directories, but will also show the permissions on the entry, the owners (group and user) of the entry, the date the entry was last modified, and the size of the entry if it is a file. You can also use '-l' in conjunction with '-a'.

Usually much of your private user settings are in hidden files and directories. 'cd' (i.e. 'cd /home/user1' if your username is user1) to your home directory and try 'ls', 'ls -a', 'ls -l' and finally 'ls -la'. This will give a very good idea of what each will tell you about the directory that you are in.

In addition to 'ls' you can "pipe" the results through another little program known as 'grep'. For instance, this what you would see (see below) if you were logged into my Linux box as root and ran 'ls -l|grep se'. Any instance of 'se' found will be displayed.

The character used between 'ls -l' and 'grep' is the "pipe" character and can be found above the '\' on most keyboards. This character is used frequently in *nix systems to "pipe" the output of one command into another (just as we are doing with 'ls' and 'grep').

[root@tuxbox etc]# ls -l|grep se
-rw-r--r-- 1 root root 1048 Aug 31 08:29 aliases
-rw-r--r-- 1 root root 12288 Jan 7 10:54 aliases.db
-rw------- 1 root root 150 Nov 21 12:54 ftpusers
-rw-r--r-- 1 root root 2252 Aug 29 13:13 libuser.conf
-rw-r--r-- 1 root root 1048 Aug 28 15:09 minicom.users
-rw-r--r-- 1 root root 35 Sep 9 20:52 redhat-release
-rw------- 1 root root 114 Jun 13 2001 securetty
drwxr-xr-x 3 root root 4096 Sep 9 17:46 security
-rw-r--r-- 1 root root 46302 Aug 31 08:29 sendmail.cf
-rw-r--r-- 1 root root 20195 Aug 22 13:01 sensors.conf
-rw-r--r-- 1 root root 17884 Jul 19 16:13 services
-rw-r--r-- 1 root root 793 Aug 22 16:53 wine.userreg
-rw-r--r-- 1 root root 1398 Aug 28 01:35 ypserv.conf
[root@tuxbox etc]#

Now, if I decide that the results of 'ls -l|grep se' are too broad, I could refine my search by making the 'grep' string a little more specific, as in the example that follows:

[root@tuxbox etc]# ls -l|grep sec
-rw------- 1 root root 114 Jun 13 2001 securetty
drwxr-xr-x 3 root root 4096 Sep 9 17:46 security
[root@tuxbox etc]#

One last thing to get used to using is the 'Tab' key. Tab will autocomplete file and directory names on the command line. For instance, cd into '/etc'. If you do an ls, you will see a *bunch* of files in /etc (some of which you will become intimately familiar with as your experience with *nix grows).

Now, I know that the directory that I want to change to is 'samba'. If I enter 'cd sam' and then press Tab (before I hit 'Enter', of course!) what appears at the command prompt is 'cd samba'. Now, I can just hit 'Enter' and I will be in the /etc/samba directory.

You can try the same thing, except that this time enter 'cd s' instead of 'cd sam' and press Tab. The first time you press Tab, nothing will happen because there are several files/directories that begin with 's'. Now press Tab again, and a list of all the files in the directory beginning with 's' will appear, and at the same time, you will be returned to your command line exactly where you left off. Now all that you need to do is to enter enough characters to make your string unique, and then press Tab again. Your directory name will be autocompleted on the command line. Go ahead, give it a shot!

I realize that the whole autocomplete thing sounds like it may be more work than it is worth and sometimes it is. However, when you are working with files and directories that have long names, and many of them do, you will quickly learn to appreciate this little trick.

I am going to very quickly cover two more commands, 'cp' and 'rm'. 'cp', as you have probably guessed, is the copy command in *nix systems. There are many options that can be used with cp, I am only going to cover the very basics.

'cp' is usually used something like this:
# cp <sourcefile> /destination_path/

This works for an individual file or directory. If you want to copy a directory that has child directories (and you want to copy them and their contents as well), you will have to tell 'cp' to be recursive. All that you have to do to do this is to use a '-R' switch after the 'cp' command, such as:
# cp -R <sourcedirectory> /destination_path/

There are times that you will simply want to rename a file. This can be done with the 'mv' (move) command. 'mv' is used something like this: 'mv <sourcefile> <destinationfile>'. For instance, if you have a file named 'foo' and you want to rename it to 'foo.bar' simply enter the following at the command prompt:
# mv foo foo.bar

You may also want to remove files. This is done with the 'rm' command. Simply enter the directory that houses the file(s) that you want to remove and enter 'rm <filename>'.

'rm' works similarly to 'cp' in that if you want it to act recursively, you will need to use a switch. The switch that you will use with 'rm' is '-r'. If you don't want to be prompted to confirm file/directory deletion, you can use '-f', the 'force' switch. Be *very* careful with this one, especially if you use '-r' with it. If you are logged in as root, you have the ability to erase every file on your machine; you have been warned.

Before I go, let me make one real quick suggestion. Almost all administration on a *nix box is handled through text configuration files. That being said, if you are relatively new to *nix systems, you may want to install 'pico' (or ask your system administrator to do it for you) rather than try to learn 'vi' right off the bat. Now, I can hear a bunch of 'vi' gurus screaming that vi is much more powerful than pico. They are right. However, especially for the new user, pico is infinitely more intuitive and much easier to use than vi.

Many distributions include pico in their recent releases. If you are unsure of whether or not pico is installed, simply enter 'pico -h'. If you are presented with a list of arguments for starting pico, you are all set, pico is installed. If, on the other hand, you receive a command not found error, you will need to install pico. Pico can be found at your distribution's FTP site (most likely).

Here is a nice how-to on pico.

- Jay Fougere, MCSE

Hands-Free Reading! Listen to a great book now!Click Here!


Get Free Email Newsletters

Freeware
WebMasterFree
LinuxWMF
MacWMF
FLADownloads
FreewareToday

Web Developers / Programmers
DesignNewz
DevNewz
DevWebPro
WebProNewsAU
WebProNewsCA
WebProNewsUK
FlashNewz
JavaProNews
MacProNews
TheDevWeb
ThePerlJournal
UnixProNews
WebProASP
XMLProNews

IT Managers/ Decision Makers
WebProNews
DevWebProCA
DevWebProAU
DevWebProUK
HiTechEdge
LinuxProNews
NetworkNewz
SysAdminNews
WinXPDigest

Small Business Owners
ActivePro
ClicksToday
InsideOffice
NetDummy
PromoteNewz
SearchNewz
SohoDay
WebProWire

eBusiness Management
AdvertisingDay
CareerNewz
CRMNewz
EcommNewz
InvestNewz
ManagerNewz
MarketingNewz
SalesNewz
WebSiteNotes

Enter Email
Enter Postal Code



News Headlines




-- NetworkNewz is an iEntry, Inc. publication --
© 2002 iEntry, Inc. All Rights Reserved Privacy Policy Legal
Send This eZine to a Friend... NetworkNewz Forum... Give Us Your Feedback... iEntry Sitemap... WebProWire News Headlines... Manage Your Subscription... Print Version... NetworkNewz Archives... More Free Email Newsletters... Related News Headlines... Advertising Info... counter