Thursday, June 9, 2011

Linux Commands - Before you get started

Before we get into the actual Linux commands, let me introduce you to how the whole thing works. Because there is an effectively infinite number of commands, you can never remember all of them. It would be futile to even try as even the most experienced Linux system administrators do not neccessarily remember everything.  However there IS a certain structure - a pattern - that all commands have in common. A good understanding of these patterns will help you remember commands and even predict guess commands that you don't actually know.

Commands are files

Of course in Linux, everything is a file! When we enter a command on the CLI (or invoke it using the GUI for that matter) we are effectively calling on a certain script, which is stored inside a file to do its work.

For example the command to view the network configuration is "ifconfig":

ifconfig

If you navigate to the /sbin directory and list the available files, ( ls -l ) you will see one called "ifconfig". So when you enter "ifconfig" and press enter, you are actually summoning the file that's inside /sbin. If you look at the other files inside /sbin, you will probably notice more files with familiar names.

That solves another mystery. That is how the terminal knows how to auto-complete your commands when you press TAB. It simply looks for files that match the string you have typed, inside the /sbin directory, /bin directory and any other location specified in the PATH environment variable. 
The command:

/sbin/ifconfig

would therefore have the exact same effect as using just "ifconfig".

So, what would a "command not found" error message mean? In all likelihood, you've made a typo, but if your sure you've typed it correctly, "command not found" probably means that there is no such file to execute. Maybe you need to install a package to get that file or maybe the file is not inside one of the locations in the PATH environment variable.

In case you need to find out the location of a particular command you can use the following commmand:

whereis <command/file>

Doesn't get any more straightforward than that!

Parameters

Okay. Now for all those bits and pieces trailing the commands. Parameters or arguments you may call them, allow you to tell the kernel exactly how you want a command to work. A command is ALWAYS a single word (no spaces). An argument ALWAYS comes after a SPACE. For example:

tar -xvf myarchive.tar

is the correct command used to untar the archive "myarchive.tar". It invokes the command "tar" and passes the arguments "xvf" to it.

If you type it this way:

tar-xvf myarchive.tar (no space between tar and -x)
the terminal will look for a command called "tar-xvf" instead of "tar" and will of course give you a "command not found" error. "myarchive.tar" serves as simply another argument.

0 comments:

Post a Comment