by Peter Kelly (critter)
Windows command line vs Linux command line
In recent years, the tools available to Windows users have improved considerably and many commands previously found only in Unix/Linux systems have found their way onto the MS command line. Many of the commands have similar options to those available under Linux, but there are differences.
For those users that are familiar with the MS command line I will show the MS command with the Linux command, where appropriate, like this ls (MS = dir). Actually, under PCLinuxOS, the command dir is is an acceptable alternative to ls but not all distributions support this.
How the commands work
Commands entered on the command line in Linux, such as ls, tend to have short names to reduce and to speed up typing. They are normally designed to perform a very basic function but usually will accept many options that enhance the basic function and permit the user to control, often to a very high degree, the effect that the function has upon the data passed to it and/or the output that is produced.
To better understand the command line we need to know how Linux commands operate. Although Linux commands are plentiful, and diverse in their operation, the basic operation is mostly the same.
A command does one thing and one thing only. The command ls lists, cd (MS = cd) changes directories, sort (MS = sort) sorts and the rpm command (redhat package manager) controls the system used by PCLinuxOS to handle software installation, removal and upgrade.
Each command accepts zero or more options that control the output from the command and the way in which the command operates upon the data that it is required to process. There are sometimes modifiers to these options which fine tune the action of the command and then, finally, there are the arguments or data that is supplied to the function for processing.
Don't be alarmed if all of this sounds too complex. The basic command will usually suffice -- everything after that is usually optional but available if you want tailor-made results. Some commands have an overwhelming list of options and modifiers, more than any sane person could possibly remember, but you can use only what you want or need. To find out what is available, type the command name followed by --help or -- h. This usually produces a brief summary of the options available for the command.
By now you may be wondering what commands are available. Type a single letter, any letter, and press the tab key (you may be prompted to type y or n), then this will list all of the available commands that begin with that letter.
To demonstrate the use of commands, I will use only the simple command ls, and its options, modifiers and arguments. This command will not change or remove any files so you can safely experiment with it.
Permissions and groups
Before we can go any further with this introduction, there are some things that need to be explained as they are different in Linux from the way that you are used to working in Windows. Although it is possible to use permissions and groups under modern versions of MS Windows using access control lists, most Windows users are blissfully unaware of their existence or of the use of MS commands such as CACLS. Linux, on the other hand, makes strenuous use of them. Fortunately they are not difficult to understand.
Look again at the output from the command ls -al.
That is only part of the output, but I think that we need an explanation here as this is entirely different from Windows. The first letter on each line describes the type of file - d means a directory and -- means an ordinary file. There are others too, such as l for links to other files which are similar in nature to shortcuts in Windows. The next nine (actually three sets of three characters) are permissions. This is fundamental to the Linux file system and often a source of confusion to new users who can't understand why they are refused access to a file. Let's get this out of the way now.
In the introduction, I said that Linux was a multi-user system and that each user was given his own home directory. This would not be a very convenient situation for you if other users could see, edit or delete your files. But sometimes you will want to share some files with other people, but not with everybody, and this is where permissions and groups help. Didn't I mention groups? Sorry, but I'm afraid that you have to also learn about those, but there is nothing that is difficult.
Every file or directory is given a set of permissions when created that allow users to read, write or execute them. Read and write are pretty obvious. Execute, when applied to an ordinary file, allows the system to run programs and scripts. For a directory, it means that you may make changes to it or list the contents. If a permission is granted then a r ,w or x appears at the appropriate position. If a hyphen - is present, then that permission has not been granted. Why three sets of permissions? These are the permissions for
- The owner, usually the person who created the file or directory (the first set of three characters)
- Members of the group to which the file belongs (the second set of three characters)
- Everybody else (the final set of three characters).
Obviously you would want read and write permission for your own files and execute permission for your scripts, and for any files you own, that are, well, executable. Everyone else you would want to deny. Well, perhaps not everybody. Some things are meant to be shared and this is where groups come in.
When a user account is created, that user is given a group of their own and is added to various other groups as decided by the system administrator or the distribution development team. To be able to use the printer, you need to be a member of the lp group. You can see which groups you belong to with the command groups.
So a file with the nine letters set as rw- r-- --- would mean that the file may be read and written to but not executed by the owner of the file, a member of the group to which the file belongs may only read the file, but everybody else has no access whatsoever (except, of course, the omnipotent root who sees all).
In the above listing, the directory 'Projects' belongs to the group 'project-team' and the group permissions have been set to allow members of that group to enter the directory, read the contents of the directory and to write files to the directory. However, unless the files in the directory have read/write permissions for this group, the group members will not be able to read or modify them. The Projects directory has no permissions set in the last three positions, so users who do not belong to the group can't even look inside the directory.
After the nine letters is a number, which need not concern us right now. This is followed by the name of the owner of the file and then by the group of users who have access to the file. After this we get the file’s size, timestamp and the name of the file.
Options, modifiers and arguments
You can find out what options are available for the ls command with the command ls - -help.
Some of the options may be meaningless to you and you can just ignore them. If you don't understand them, then you probably don't need them. A useful one for the ls command is -h which gives the size of a file in human readable form so that a file of 2048558360 bytes displays as 2.0G.
If you type ls -lS
then you are issuing the command ls and passing the options l and S which tells the command to generate a long listing which is Sorted by file size. But what if you want to have the listing sorted by some other criteria such as filename extension, as you might in a folder full of picture files with various .bmp .jpg and .png extensions? In this case you might use the option
--sort and then add the modifier 'extension' like this
ls -l - -sort=extension
The --sort option allows the following modifier words: none, extension, size, time and version. In the time honoured tradition of Linux/Unix users being lazy typists, this can be abbreviated to the first letter of the modifier like this
ls -l - -sort=e
Note that Linux does not use filename extensions to determine the file type as Windows does but extensions are often used to aid human users quickly recognize a file type. However, this can be misleading as a text file named mytext.bmp is still a text file. To determine correctly the type of a file under Linux you should use the file command.
myfile.bmp: ASCII text
A graphical image file would produce output like this:
/usr/share/icons/pclinuxos.png: PNG image data, 32 x 32, 8-bit/color RGBA, non-interlacedfile
That leaves arguments to be de-mystified. Arguments are the information that you pass to the command. The default argument for the ls command is 'the current working directory' which is what it will list out if you pass no other arguments to the command. You may pass more than one argument to the command.
ls -l ~/Pictures ~/Documents
This command will list first the contents of your Pictures directory (there's that squiggle again) and then the contents of your Documents directory. Remember, the squiggly character or tilde is used as a shorthand way of saying “my home directory”.
In practice you will use very few of the options available with the ls command unless you start writing shell scripts or spend a lot of time on the command line but if you understand the basic usage demonstrated here then you can apply this to almost all of the Linux command line tools. Now it is time that we looked further than the simple ls command and considered some of the other commands that are available.
As an ordinary user you have permissions to enter and list the contents of most directories but not to change or delete anything except for those sub-directories in your home directory. To change to another directory use the command
for example cd /etc would put you in the /etc directory. cd on its own will take you back to your home directory. To change to your Documents directory you could type cd /home/linux_user/Documents or use the shorthand cd ~/Documents. Remember the squiggle?
To rename a file we use the mv (MS = move) command
mv old-file-name new-file-name command. The new file name may optionally include the address of a directory to which we have write permissions.
Deleting a file or files is accomplished with the rm (MS = del or delete) command
rm file-name Be careful of this one as there is no undelete command. The PCLinuxOS version of this command will prompt you for confirmation before sending the file to oblivion but this is not always the case.
To make a new directory you need to have write permission for the directory where you want to place it and you use the command mkdir (MS = md or mkdir).
You can remove an empty directory with the command rmdir (MS = rd or rmdir)
To remove a directory and all the files that it contains you will need to use the rm command with the option -r (MS = deltree). If there are many files in the directory then the prompting can become quite tedious but you can turn it off with the -f (force) option.
Important Be very, very careful with this. It is ruthless, quick and permanent. A little trick that I often use is to replace rm -rf with ls -R to list out which files would be deleted. If that is what I intended then I issue the rm -rf death warrant. The -R option tells the ls command to recurse down through any sub-directories to show everything that would be deleted (make sure that you use upper case -R as lower case -r will only reverse the listing).
To look at the contents of a file we have the cat (MS = type)command (short for concatenate which just means to join together) Try
This will display the contents of the file that tells the system where to mount your file systems. You can also use the cat command to create a simple file without having to open an editor by using the redirection method which we previously discussed.
cat > newfile
This is telling the cat command to redirect its output to the file newfile which it will create if necessary. As we haven't passed to the cat command any file as an argument for it to process it looks for input from stdin, the keyboard, and it will just sit there waiting for somebody to start typing. Type whatever you need and press return whenever you want to start a new line. When you have finished typing, hold down the control key and press 'd'. This sends the shell an invisible character known as an end-of-file character or EOF. The file is then written and closed.
You can create a new, empty file with the touch command
This is useful where you are using a command that doesn't automatically create a file when needed. You can create an empty container ready to accept the output from the command Often you need to refer to a file that is buried deep in a sub-directory and an easy way to do that is, as in Windows, to create a short-cut to it although the manner in which Windows and Linux accomplish this are entirely incompatible. In Linux there are two types of short-cut and they are known as links. There are hard links and soft or symbolic links. For now we will concentrate only on soft links. The command to create them is ln or ln -s for a soft link.
ln -s /home/linux_user/Documents/invoices/November/Acme_inc.txt acme_11 will create a shortcut named acme_11 which will point to the buried file making it easier to load into a text editor for example. This is very useful for editing things like system scripts without having to remember where they are.
When you receive some output from a command, it invariably is not in the order that you would like it. To sort it into the required order we use a command called, naturally, sort
There are many options for sort to produce data sorted alphabetically, numerically, reversed, by date etc. The sort command is one of many Linux tools known as a filter. Data is passed to a filter and the output is the altered data. This is one of the basics of command line operation and part of the real power of Linux. The usual way of using a filter is to separate the command producing the data from the filter with the vertical bar character known as a pipe. Like this
That should give you a start in command line use under Linux. In the next article we shall look at some more of the most commonly used Linux commands.