0
Posted September 28, 2009 by Spyros in Linux Tips
 
 

An Introduction to Unix Commands

bash_shell
bash_shell

Hello friends, at this next tutorial i’m writing for codercaste.com i’ll be trying to explain some things about the unix commands. I happen to be a linux user mainly and i find that the command shell of the various linux distribution is definately the raw power of linux.

But, what is a shell?

Some years ago, Graphical Interfaces were not a choice. The only interface available to unix computers were the shell, an ugly looking black background where to navigate you needed to know how to use its commands, which meant that you needed to type. The shell would see what you typed and execute the commands you asked it to execute. Nowadays, GUIs (Graphical User Interfaces) are the standard for todays operating systems, which is kinda nice since most users don’t need to learn unix and computers are more appealing to them.

However, the power lies within the shell. Especially if you are a unix user, learning how to use the shell will make your life easier and enable to have raw power over your computer. For that reason, i decided to write a tutorial on how to use the shell, not just enumerating the various commands but really tell you what commands i tend to use and how they make my life easier. There are many shells, but i’ll focus on the most well known and used one, the bash shell.

The first shell commands, learning to navigate.

You may think that opening nautilus or your favorite file browser is faster, but i can assure you the opposite. If you learn to effectively use the shell, you’ll not only make your life easier but faster as well :) When you open the shell you see something like :

thought@laptop:~$ 

You can use the command ‘pwd’ to print the working directory, which is the current directory you’re in now.

thought@laptop:~$ pwd
/home/thought

So, you’re in your home. Let’s go to Desktop, which is under /home/thought/Desktop. Write:

‘cd De’ and press TAB

This will autocomplete your command and make it ‘cd Desktop’. Well, this is VERY IMPORTANT. The shell features an autocomplete command, which tries to guess what you wanted to write. Since the only directory at /home/thought that starts with ‘De’ is the Desktop directory, it managed to autocomplete. If there was also a directory named ‘Destroy’, it would not autocomplete, but if we pressed TAB twice, it would show the two options and if we wrote ‘cd Desk’, then it would auto complete.

Remember that, autocomplete is VERY VERY powerful.

Now, the command ‘cd’ we used means Change Directory and is used exactly for that. It changes to a directory. Doing a ‘cd ~’ would take us to our home directory. A ‘cd ..’ would takes us to the directory right below our current directory. So if we were at /etc/apt and typed ‘cd ..’ we would now be at /etc. Use pwd to see that it’s what happens.

The command ‘cd .’ means change to the current directory. While there is no need to change to the current directory, it’s important to know that and you’ll see that it’s used many times. Finally ‘cd -’ changes to the previous current directory.

Fine, but changing directory is not enough. Since we wanted to change to a new directory, there’s probably something that we need to do there. Suppose that we need to remove a file. Well, there was no real need to go there to remove it, but it’s an option.

Let’s say that we need to erase the file ‘new’ at /home/thought/Desktop. But let’s create it first. We can use the command ‘touch’ to quickly create a file.

thought@laptop:~/Desktop$ touch new
thought@laptop:~/Desktop$ ls -l new
-rw-r--r-- 1 thought thought 0 2009-05-06 20:10 new

Ok, we used ‘touch’ to create the file and then used ls -l to show that it’s there. ‘ls’ in fact means list and it lists the files we ask it to list, along with some attributes if we use the -l(long listing) switch. A kinda nice switch too is the -a switch, which lists the hidden files too(files that start with a dot, like .hidden).

Now, to remove it we can do something like ‘rm -f new’ now. ‘rm’ means remove and it’s also easy to remember. The -f switch means force, that is not ask us for any confirmation, but just erase. The rm -rf switches would remove a folder and the folders inside it(-r means recursive, a known programmers word).

Now if we were at current folder /home/thought, we could do this in two ways. Do a ‘cd Desktop’ and then ‘rm -f new’ or even better do a ‘rm -f ./Desktop/new’. Something that means ‘remove with force the file new which lies to /home/thought/(specified by the . meaning the current folder) and is under the Desktop folder’.

Ok, now we know how to remove a file, but how do we copy or cut a file ? To copy a file we use the command ‘cp’, easy to remember huh? :) The syntax is:

cp source target

How to remember that? I’ve found that the easiest way to remember is associate with something. I associate the cp command with the alphabet. Source starts with an S and target starts with a T. S is before T :D Sounds funny but believe it’s dead easy to remember. For instance doing a ‘cp ../new ~/Desktop’ under the current directory /home/thought/myPrograms would copy the file /home/thought/new to /home/thought/Desktop and keep a copy to /home/thought/new.

On the contrary, mv cuts the file and moves it to the folder we specify. For instance the command ‘mv ../new ~/Desktop’ would not only copy the file new but cut it and not keep a backup. Remember that cp is mainly used for files and in order to use it to copy files you need to specify the -r switch.

Another two commands that i often use are the ‘cat’ and ‘less’ commands. Specifying a command ‘cat new’ would just display(catalog) the file new under our current folder. The command less is almost the same, but ‘less new’ would display the contents of new, but if these were more than one page, it would display them in pages. Use space to see the next page and press q to quit.

Unix Pipes

Now that we learned some basic stuff, i want to say some basic things about unix pipes. Pipes are a way to pass data from a program’s output as another program’s input. To illustrate, suppose that we have a file named ‘stats’ with the contents:

1 2 3 4 5
11 22 33 44 55

Well, we could just use cat to see what it contains doing a :

thought@laptop:~$ pwd
/home/thought
thought@laptop:~$ cd Desktop/
thought@laptop:~/Desktop$ cat stats
1 2 3 4 5
11 22 33 44 55

However, it stats contained more data, we would be lost. Suppose that we needed to find in which line there was the number 33. If there were a hundred lines, we could be lost inside the file. However, the shell has the tools to help us. There’s a program know as ‘grep’ used to locate lines that match a pattern. So doing a :

thought@laptop:~/Desktop$ cat stats | grep 33
11 22 33 44 55

Returns the while where 33 was located. But what happened here? ‘cat’ was used to display the stats file, but the output, which is the content was not printed but REDIRECTED, PIPED to a new command, the grep command. Grep received that content and tried to find the line that matched the pattern ’33′ and returned it. There are some important switches to grep like -c which prints the number of the matching lines, -n which prints the line number where the line was found and some more.

Remember to use the command ‘man command’ to see the manual pages for a command, like ‘man grep’ to see grep’s switches.

So, you see what pipes are and see why they are important.

Hope that you earned some insight of the shell with this tutorial. These were some of the basic stuff in order to operate the shell and i tried to explain it as things came to mind, avoiding to be too technical. In the next shell tutorial i’ll be explaining about file redirections, permissions and some more advanced administrative commands, like scp and such.

Thanx for reading, hope you enjoyed and learned at the same time :D


Spyros