The Linux Command Line For Beginners Part 2
In Part 1 of this series we looked at the wide range of commands available in the Linux Terminal. Now its time to use some of them. Of course we will not be exploring their complete capabilities. For that you will need to consult the relevant 'man' pages but we will be looking at a small subset of commands and exploring basic usage. Let us suppose that we are embarking upon a project that involves the creation of a new sub-directory in the home folder. Let's suppose further that we want to use this folder to store a number of files pertaining to our project that we will need to edit from time to time in a text editor. How would we set this up?
First lets examine the contents of our home directory. To do this simply type 'ls'. The output of the 'ls' command will reveal something like the following on a pristine install of Ubuntu 7.10:-
Desktop Documents Music Pictures Public Templates Videos
Since we are essentially looking for a place to store documents the 'Documents' folder seems like a good place to start. To enter this folder simply type 'cd Documents'. This will take us to the 'Documents' folder where we can once again type 'ls' if we wish to see a list of the contents.
Now we make our sub-directory. We use the 'mkdir' command for this. Simply type 'mkdir project' and a quick 'ls' will reveal that we now have a sub-directory with that name in the 'Documents' folder. Supposing that we need four files to work on, the following commands will create them in the 'project' folder:-
1. cd project
2. touch proj1 proj2 proj3 proj4
If we run the 'ls' command now in the 'project' folder we see that the four files have been created. In order to open 'proj1' for editing, type 'gedit proj1'. The excellent 'gedit' text editor will appear on screen and we can start entering text.
Having entered text we save the file and close 'gedit'. If we wish to read the file in the terminal all we have to do is type 'cat proj1' and the text is displayed.
Of course we have covered a lot of ground in a short time. The above is intended as nothing more than a basic and somewhat superficial introduction to the cd, ls, mkdir, touch and cat commands. A detailed and comprehensive account of their capabilities is to be found in the relevant man pages. It might also be argued that the creation of a sub-directory containing a small number of text files can equally well be accomplished using the graphical file browser. This is true! But some tasks cannot be accomplished so easily, if at all, in the graphical interface and anyone wishing to master the command line should start with the basics.
The 'cd' and 'ls' commands in tandem allow you to explore the full contents of your Linux system. In order to enter the root directory type 'cd /'. Enter 'ls' and you will be presented with a list of all the folders on your drive which will look something like this:-
bin dev initrd lib mnt root sys var
boot etc initrd.img lost+found opt sbin tmp vmlinuz
cdrom home initrd.img.old media proc srv usr vmlinuz.old
From here you can 'cd' into any one of these directories and their sub-directories listing the contents as you go with the 'ls' command. To return to your home folder just typ 'cd ~'. To return to the folder above the one that you are currently in, type 'cd ..'. There are resources in the links section below which will help you understand the Linux Directory Structure. Have fun exploring!
NOTE: In the above example we used a graphical editor - gedit, to create and edit our text files. Some of you may be aware that it is possible to do this in the terminal using the 'vi' editor. Whilst this is perfectly true it is not recommended for beginners. However, for the more adventurous souls amongst you, here is a quick guide to editing with vi:-
1. Open or create a text file - 'vi filename' ( e.g. 'vi proj1' )
2. Vi will open in 'command mode'. All the things you would normally do in a graphical editor by 'pointing and clicking' with a mouse are accomplished in 'command mode' . If you want to enter text or edit the existing contents of a file youmust enter 'insert' mode. To do this simply press the 'i' key on your keyboard.
3. Enter your text.
4. When you have finished editing press the 'Esc' key to re-enter 'command mode'. In order to quit and save, enter the following rather ungainly combination of characters:-
The colon indicates to 'vi' that this is a command. The 'w' and 'q' stand for write and quit. The exclamation mark tells vi to disregard errors.
If all of this appals or unnerves you stick to 'gedit' or 'Leafpad'.