An Introduction to the Unix Command Line


Many people do not have much experience using linux or the command-line aspect of unix-based operating systems.

This is a very valuable skill, especially considering that it will allow you to remotely access CSIL computers via SSH. You can probably get by without learning much, but this guide will cover a bare minimum that anyone interacting with linux machines should know.

First open terminal. If you are in one the CSIL computer labs, log in and select Gnome as the desktop environment (it will be in a little dropdown box after you enter your password). After you have logged in, scroll over Activities in the top left corner of the screen. Then in the search box, type Terminal and double click the terminal icon. If you are connecting via SSH or PuTTY, then you will already be in terminal once you have successfully logged in.

Once you are in terminal, type in the following

pwd

and press enter. This means print working directory. Most people are familiar with going to "My Documents" in Windows and browsing through different folders. Similarly to folders in Windows, linux has directories. Directories and folders are the same thing with different names.

All linux machines have a directory known as the home directory, represented by a ~ symbol. This is where you will most likely be doing all your work.

In Windows and Mac, you navigate to different folders by double clicking on the one you want to open. For example, if I am in "My Documents" and I want to go into the folder "Word Documents", I will just click on the "Word Documents" icon. Terminal, however, does not support clicks and everything must be conveyed textually. In order to change directories, use the following command,

cd path/to/dir

For example, if you are in your home directory, within which there is another directory called "homework", you would type in either

cd homework

because homework is a directory within home, or

cd ~/homework

because that is the absolute path to the homework directory. The latter is more precise because it tells the computer exactly where to look. The former, on the other hand, implies that since you are currently within the home directory, a ~/ should be appended to the beginning of the relative path ("homework"). Keep in mind that the former will, therefore, only work when run in the home directory.

As of now, you probably don't have a homework directory so, in terminal, type

cd ~

and press enter, in order to make sure you are in your home directory. Then type

mkdir homework

and press enter. This will make a directory called homework. Once the directory is created, navigate to it using the cd command

cd homework

If you don't feel like typing "homework" all the way out, just type cd homew and press Tab. This should autocomplete it for you. Once you are in the homework directory, you can navigate back the home directory by entering either

cd ~

because that is the absolute path of the of the home directory, or

cd ..

which means change to the parent directory. In this scenario, "homework" is a directory within home, meaning that it is a "child" of home, thereby making home its "parent" directory.

Now we are going to make a file so you can learn how to remove, move, rename, and copy it. There are many ways to do this, but for this example we will create a file using a program called vim. Let's call the file myhw.txt:

First make sure you are in your home directory

cd ~

and then create the file,

vim myhw.txt

Vim is an example of an application that runs through the terminal. Although vim is not a standard bash command, it comes preinstalled on all of the CSIL machines.

Once you are in vim, press i, for insert, and type "Hello, World!". When you are done, press Esc and then :wq and then enter (this writes/saves the changes and then quits out of vim). Your new file myhw.txt has now been created.

In order to list the new file and every other file and directory within your current directory, type

ls

when you run this, you should see myhw.txt along with the homework directory and possibly some other files. In order to copy "myhw.txt" into the homework directory, type

cp myhw.txt homework

Meaning copy "myhw.txt" into "homework". The copy command can be generalized as follows,

cp what-you-want-to-copy where-you-want-to-copy-it

Now let's say that you want to rename it to "yourhw.txt"

mv myhw.txt yourhw.txt

This basically moves the contents of "myhw.txt" and put it inside "yourhw.txt". The mv and cp commands are very similar, but have one key difference: cp copies the file and leaves the original, whereas mv moves the file and deletes the original. Mv can also be used to move files to other directories. In order to move "yourhw.txt" into your home directory, enter either

mv yourhw.txt ~

to move it to the absoluate path of the home directory, or

mv yourhw.txt ..

in order to move it to the parent directory, which also happens to be "home".

Now that we have copied, renamed, and moved "myhw.txt", navigate to the home directory,

cd ~

and the enter

ls

in order to list your files. You should see myhw.txt, yourhw.txt, and the directory homework (among other directories). To prove that we really did copy and move myhw.txt, enter

more yourhw.txt

and it should display Hello, World! in the terminal. After you have done this, go ahead and remove myhw.txt and yourhw.txt by entering one of the following commands,

rm myhw.txt

and then

rm yourhw.txt

which removes each file individually, or

rm myhw.txt yourhw.txt

which removes the both at the same time, or

rm *hw.txt

which will remove all files ending in "hw.txt". Now in order to remove the homework directory, you have to include a flag, which is basically a parameter that specifies how the command should be run:

rm -r homework

the -r stands for recursive, which is the algorithm that rm must use in order to delete directories.

Now that you understand these basic commands, you are now ready to use the linux command line.

Last update:
2014-01-13 20:11
Author:
Liam Cardenas
Revision:
1.2
Average rating: 5 (2 Votes)

You cannot comment on this entry

Chuck Norris has counted to infinity. Twice.

Records in this category

Tags