Monday, November 7, 2011

Linux Commands for Beginners

We had organized a talk for the Infotech Student Organization to introduce the students to Linux command line. It was often the case that a student was put in front of a Linux box and was asked to do 'X' research while he had no idea why there was no 'Start' button in the bottom left corner.

A few weeks ago, in a similar situation at UBC, I have seen a student open nautilus as root, navigate to /usr/bin and drag/drop files there. It was an aweful sight.

Back to the talk for Infotech, I was asked to give the actual talk with some demonstrations and examples to the people. I created a tutorial which contains what I believe is the fastest way to get a person started on a bash terminal. It might not be the most conceptually accurate, but it's guaranteed to get your started, _fast_. It is necessary that you read the tutorial in sequence, because I have organized it so that there is not a thing that I will use before explaining first. It will save you a lot of headache.

So let's start:

Command line arguments
when you want to execute a program on a linux terminal, you type the name of the program and press “Enter” or “Return”. Sometimes the programs needs input, for example, a program that prints the date could need an input specifying how would you like the date to be formatted. We will see an example on that later. To give a program input on the terminal, you type the name of the program, then space then the first input then space then the second and so on. it would look like this “ls -l -a -h”, we will see what this does later on.

ls
ls stand for “list” it is use to list all the files and directories in the current directory. ls can have my options. check “man” for details (I will show you how to do that in a few minutes).

Repeating a previous command
on a linux terminal, there is something called “history”, this basically means that the terminal saves the history of previously executed commands. You can access this history in the simplest way by hitting the “up arrow” button, each time you hit it it will go one command further in the past.

ctrl+c
ctrl+c means pressing the control (ctrl) key first, then while holding it down, pressing the “c” key. It is using to interrupt the previously entered command or delete the command currently being typed entierly.

man
man is an interface that is use to read the manuals of programs. Most programs have those manuals correctly setup on your system just type “man command” to see the manual. The manual will open in a “less” window. Check below for further details. If the man of a program doesn’t exist, it’s a convention to try “command --help”, most of the programs will have this print a short description of the program as well as all the options that can be used with it. Try “man ls” and “ls --help”. You can scroll inside the manpage with “arrow up” and “arrow down” or “page up” and “page down” keys. More options are on the way when we discuss “less”.

cd
cd stands for “change directory” it is used to change to a directory. it takes one argument which is either a relative path or an absolute path to a directory. Like this “cd /home/linux/linuxtut” or if you are in /home/linux then “cd linuxtut”. If you call “cd” only without any arguments, it takes you to your home directory. The home directory is specified in the environment varialble “$HOME”. More about that later.

pwd
pwd prints out on the command line the current working directory, if you get lost and don’t know where you are, just type “pwd” and it will tell you the full/absolute path to the directory you are currently in.
Remember, you can always type “man command” to know how the command should be used.

mkdir and mkdirhier (mkdir -p)
mkdir is used to create a new directory. it takes one argument, either the relative of absolute path of the directory you want to create. if you want to create dir1 inside dir2 and dir2 doesn’t exist, mkdir will fail. you can use “mkdir -p” or “mkdirhier”(not available on all systems) to create both dir2 and dir1. It will create even the whole hierarchy of directories if more than 2 exist.

cp and cp -r
cp is used to copy a file, enter “cp original copy” both original and copy are the relative or absolute paths to the files. You can also enter “cp original dir” and the file original will be copied with the same filename into “dir”.
cp -r is used to copy a complete directory. r stands for “recursive”. type “cp -r originaldir copydir” also using absolute or relative paths.

rm and rm -r
rm is used to delete a file, it stands for “remove”. “rm file” will remove “file”. to remove a directory and all the files inside it, you can use “rm -r dir”. “rm -r file” will also work. r also stands for recursive. Use rm -r only with caution as it can delete a huge amount of files/directories in a single command.

rmdir
if you want to remove a directory, rmdir can be used, but rmdir will work only if you have removed all files and directories from inside the directory you want to delete.
you can make rmdir behave like rm -r if you say “rmdir --ignore-fail-on-non-empty dirname”.

mv (rename)
in linux, we don’t have a “rename” option since renaming something is like “moving” it. mv stands for “move” and it takes two arguments “mv original target”, original and target can be absolute or relative paths to a file or a directory.

cat
now we start with file viewing and manipulation commands. cat stands for “concatenate” . It takes one argument “cat file” and it will print that file to the output of the command line allowing you to see the contents of the file. We use cat to view the contents of small files.

more
more is a program that allows you to view a large file and scroll through it. Otherwise it would be very large to “cat” on the terminal. With more, you get one page of the file, and when you press “space” the next page is displayed. You cannot go back in more.

less
less is a name given to the program that is opposite to “more” because it does the opposite to what more does. You can remember which to use by this anecdote “less is more than more”. Because less allows you to scroll both up and down as well as search the file. It’s used like this “less file”.

head
head is used like cat but it only concatenates the start of the file. It will print the first 10 lines of a file when used like this “head file”. you can change this by using “head -n x file” it will print x lines from the start of the file.

tail
tail will do the same as head, but to the end of the file. you can use the same -n option.

grep
grep is a very powerful command that allows you to search a file for some pattern and it would print out the lines containing that pattern. For example, you had a .c file and wanted to see if a specific variable is used in it you can use “grep pattern file” and grep will print all the lines containing your pattern. the -i option tells grep to “ignore case”. the -r tells grep that you will specify a directory instead of a file and that grep should search this directory recursively. the -n option tells grep to output the line number for the output lines.

echo
echo is used as follows “echo string” it will simply repeat that string for you. so the output of “echo Hello” is “Hello”. But echo is particularly helpful when you want to see the values of environment variables, you can use it as follows “echo $VARIABLE” and it would output the value of that variable.

chmod
When you "ls -l" you get some crazy symbols on the left similar to "drwxr-xr-x". These represent the file permissions. Here is how they work.
You have 10 letters: "0123456789"
0 will be 'd' if it's a directory and '-' otherwise
123, 456, 789 correspond to the rights for "owner", "group" (owner's group), "every body else"
so, 1,4,7 are for the "read" right
2,5,8 are for the "write" right
3,6,9 are for the "execute" right
If you have the right, there will be an 'r', 'w', or 'x' respectively, otherwise a '-'
The command to change those is chmod. To change something, you must have a "write" right, or be a root.
chmod works in 2 ways:
Either write "chmod xxx file" where x is a single octadecimal digit corresponding to the rights you give to each group, where the 1st x is for the 1st group and so on.
As a side note, here is a table for octadecimal transformation into binary:

  • 0 -> 000
  • 1 -> 001
  • 2 -> 010
  • 3 -> 011
  • 4 -> 100
  • 5 -> 101
  • 6 -> 110
  • 7 -> 111
so, now map the 3 binary digits to "rwx" and you get your rights for one group. For example, 5, which is 101, represents r-x rights.
the other way to use chmod is to do use some letters
  • 'u' = user (owner)
  • 'g' = group
  • 'o' = others
  • 'a' = all (the 3 permissions)

  • 'r' = read
  • 'w' = write
  • 'x' = execute

  • '-' = remove
  • '+' = add
and you write it such as "chmod [a|u|g|o] [+|-] [r|w|x]". What this means in english is, write chmod, then write any combination of "augo" then write '+' or '-' then write any combination of "rwx". So u+x will give the owner execute right and og-rw will remove the read and write from others and group.
Take care because sometimes a-r or a-w or u-r or u-w can be dangerous if you don't have root access. There are a lot of other options, consult the chmod manual (man chmod) for the full details.

chown (user:group)
chown is use to change the owner of a file. "chown new_owner:new_group file". Don't forget to check "man chown".

pidof
pidof stands for “process id of”. It searches in all the running processes and returns the id of the matching ones. it takes one argument “pidof process-name”

kill
kill is used to kill a process. I takes 1 argument, the pid of the processor. it is often used in combination with pidof in this way “kill `pidof process-name`” those are not ‘ single quotes. see below for more about that.

``
those characters are called “back tick” they are enters by hitting the key right below ESC on a US english keyboard. They are used to tell bash to execute the command between them first then put its output in this place then execute the other command. they can be used like this “kill `pidof process-name`” and this would be exactly equal to “kill process-id” if you know the process id.

|
we call this the “pipe” character. it’s on your keyboard usually at Shift-\. The pipe does exactly what it sounds like, it pipes the standard output from the command before it to the standard input of the command after it. We usually use | to search in the output of a command for example like this “lspci | grep -i “audio”. lspci lists all the pci devices connected to your computer and its output goes to the input of grep. grep searches the input for the word “audio” and the -i tells grep to do a case insensitive search.

>, >> and <
both of those characters are called redirection characters. We will not go into much details about them. In their simplest use, we use them to direction an output of a command to a file. this way “pwd > file” for example. > will create the file if it doesn’t exist or overwrite it if it exists. >> is used in exactly the same way except if the file exists it will append to it instead of overwrite it.
< is used to direct a file to the standard input of a program. It is not very often used because most software that needs a file as input will have the option of taking the filename and reading the file itself. but you can try it with grep “grep -i pattern < file”

date
date is used to output the current date. it can also be used to format a date in the format you would like. read the man page

cal
cal will ouput the calendar.

uptime
uptime outputs mainly how long has it been since the system was started. check the man page to know the meaning of the other things.

vim and nano
vim and nano are terminal based text editors. If you need to edit a file, try "nano file" this will open that file in a terminal based text editor called nano (notice that file is a command line argument to nano). You can navigate nano using the arrow keys. Some help is displayed in the bar at the bottom. For example, ctrl+x will exit nano.

wget
wget is a command that fetches/downloads a file from the internet using different protocols.