Learning Linux - The basic

I've taken this post from my previous blog. I'm searching for various articles that I think will benefit lots of people. Oh, and I will put the comments also in the articles (if any).

Disclaimer : Warning! Long post.

1. Knowing basic directory in Linux

/ is the starting directory for Linux. It is known as root directory where it will store every directories and files available in the OS. Usually, common user like you and me, cannot write in the / directory. Only root or super-user can write into this directory. By controlling / directory, that person virtually can do any changes that he/she like into the system.

/boot
contains files that needed by boot loader such as LILO and GRUB to start the system. By deleting a certain file in this directory, may cause failure during booting process.

/etc contains most configuration files for the system. It range from configuration file for system look and feel, mount configuration to starting up the system daemons. The user's information is also stored in this directory.

/bin contains binary files that needed during bootup that might be used by normal users.

/sbin contains binary files that needed during bootup that might be used by super-user.

/usr/bin contains binary files that might be used by normal users.

/usr/sbin
contains binary files that might be used by super-user.

/tmp - all temporary files will be saved in here.

/var - this directory mostly contains files that need to be change by the system such as log files, spool directories, manual pages and etc.

/proc - this directory exist physically on the hard disk but the files are created during system uptime. I call the directory as virtual directory because all the files are stored in the RAM which mean after you shutdown the system, the files are gone from the directory.

/home - this directory purpose is to store user's home directory and user's files. Your home directory will be located in this directory. For example, user1 will have his/her home directory in /home/user1.

/root - this is a home directory for root.

2. Navigating Linux directories

cd is a Linux command that is use to navigate directories in Linux OS. The syntax for cd command is like this :

$ cd [/specify/your/directory/here]

For example, let's say that I want to go from my home directory to /usr/bin directory. I just use the cd command like this :

$ cd /usr/bin

After I execute the command, I'm now at /usr/bin directory. If I want to go back to my home directory, I can use either of these commands

$ cd

or ,

$ cd ~

Now, I'm back at my home directory.

3. Where am I?

Most newbie get lost when traversing Linux directories. To make sure you know where you are now you can use pwd command. pwd stands for 'print name of working directory'. For example, if I'm now at /var/www/htdocs, it will print /var/www/htdocs when you run pwd command.

$ pwd
/var/www/htdocs


The above is the example on how to use the command.

4. Show me the files

ls that stands for list is use to list files and directories available in a directory. Let say, you want to list files and directories in your home directory. You only have to type like this:

$ ls

If I run the command, I will get input like this (maybe you'll get a different result):

 Desktop/    examples/                lamtests-7.1.1.tar.gz
LINUX/ lam-mpi_examples.tar.gz pvm3/
OBJECT.tga lamtests-7.1.1/

If you see the files properly, you will notice that each of the files will have their own color. These coloring reflect the type of each file. For example, directory type will be represented by dark blue, maroon is for tar and compress files, green is for executable or binary files and etc. The coloring scheme can be changes based on the user needs but in the system we use the default coloring scheme.

Let's get back on how to use ls command. These are other ways of using ls command.

$ ls -a

The option will list all files in the directory including hidden files or also known as system files. Hidden files can be recognized easily because it's name start with .(dot) characters. For example, .mydir is a hidden file. Let's look what happen when we use ls -a option.

 ./             .bash_profile  .povrayrc       .ssh/         examples/           
../ .bashrc .qt/ .thumbnails/ lam-mpi_examples.tar.gz
.ICEauthority .gconf/ .recently-used Desktop/ lamtests-7.1.1/
.Xauthority .gconfd/ .rhosts LINUX/ lamtests-7.1.1.tar.gz
.bash_history .gnome/ .screenrc OBJECT.tga pvm3/

As you can see from the output, the hidden files and directories will be shown when you use ls -a command.

Some of us might one to view the details for each files and directories in a particular directory. It's not that hard to do it. Just type ls -l in the terminal and you can see the details for each files and directories. Let's look at the example.

 $ ls -l
total 452
drwx------ 3 ruzaimi users 136 2005-01-24 15:00 Desktop/
drwxr-xr-x 2 ruzaimi users 48 2005-01-28 12:35 LINUX/
-rw-r--r-- 1 ruzaimi users 18 2005-01-24 15:02 OBJECT.tga
drwxr-xr-x 13 ruzaimi users 432 2005-01-27 16:03 examples/
-rw-r--r-- 1 ruzaimi users 99074 2005-02-14 13:12 lam-mpi_examples.tar.gz
drwxr-xr-x 16 ruzaimi users 744 2005-01-28 11:32 lamtests-7.1.1/
-rw-r--r-- 1 ruzaimi users 342140 2005-01-28 11:24 lamtests-7.1.1.tar.gz
drwxr-xr-x 4 ruzaimi users 96 2005-02-13 14:10 pvm3/

That's the output that you get when you run ls -l command. The first column from the left is a file description, the second column is the number of object in the files or directories (for file the number is 1 but for directories the second column will reflect how much files or directories available in that particular directory), the third column is the files or directories owner, the fourth column is the name of group who own the files or directories, fifth column indicate the files or directories sizes, the sixth column is the date when the file is last modify, the next column is the time when the file is last modify and the last column indicate the files or directory names.

Beside using only one option at one time, you can also combine the options available for the command. For example, I can combine a and l option in ls command like this :

$ ls -al

To see other options for ls command, just type ls --help in the terminal. The command will list all options available for ls command.

5. I want to change my password

The first thing you need to do when login into your account is you need to change your password. To do this, use passwd command. You must supply the old password before entering a new password. Use a good password for your account like [*mYpaSswOrD8700*], that combine upper case and lower case letters, numbers and special characters.

 $ passwd
Changing password for testuser1
Old password:
Enter the new password (minimum of 5, maximum of 127 characters)
Please use a combination of upper and lower case letters and numbers.
New password:
Re-enter new password:
Password changed.

You've succesfully changed your password. It's a straight forward comamnd, isn't it?

6. Copying my files

To copy your file from one file to another, you need to use cp command. The command can be used like this :

$ cp file_A file_B


This example will copy file_A to file_B.

You can also copy multiple files into a directory.

$ cp file_A file_B file_C directory_Z

This command will copy file_A, file_B and file_C into directory_Z. If you run ls command in directory_Z, you will see the copy of these files in the directory.

7. Moving file(s)

We can move a file in Linux OS by using mv command. Like cp command, it will create another file at another place. The difference is only mv file will remove the original file. For example :

$ mv file_A file_B

This command will move file_A into file_B. file_A will be deleted afterwards. We can also move multiple files into a directory. Example :

$ mv file_A file_B file_C directory_Z

We can also move a directory to another place

$ mv directory_A directory_B

The above command will move all contents in directory_A into directory_B. If directory_B exist prior to the execution of the command, directory_A will be moved into directory_B.

8. How to make a new directory?

Directories can be used to store file(s) and other directories. In Linux OS every user has permission to create new directories in their respective home directory. We use mkdir command to create a new directory in the system. Example :

$ mkdir my_directory


The command will create a new directory name my_directory. How about if you want to create a new directory that reside on another new directory? We will use the options in mkdir.

$ mkdir -p directory_A/directory_B

The above command can be interpreted like this : "Create directory_B in directory_A. If directory_A is not exist, create directory_A before creating directory_B

9. I want to remove unuse file(s) and directory(ies)

For this purpose you need to use 2 command. rm command is use to remove file(s) but sometimes we also use to remove directory(ies). rmdir commmand is use to remove directory(ies). Look at the example below :

$ rm file_A

$ rm file_A file_b

$ rmdir directory_Z


The first command will remove file_A from the system, while the second command is a way to remove two or more files from the system. The third command is to remove an empty directory_Z. What if your directory is full of files and other directories? Let us try.

$ rmdir directory_Z
rmdir: `directory_Z/': Directory not empty


To remove un-empty directory, we need to use rm command with -r option. Sometimes, I also use -rf options to remove un-empty directory. Either two of these examples can remove un-empty directory :

$ rm -rf directory_Z

$ rm -r directory_Z


10. All process, show yourself!

We can view process using a few or several commands. Its all depends on the distro that you use. Basically, there are 3 command that can view process that running on your computer, ps, top and chkconfig. Depending on your distro, chkconfig may not work on some distro. So, what will this commands do?

ps is used just to print the snapshots of all the current processes run on your computer. There are several options that can be used with ps but I usually used it with -aux. -a is list all process, -u is list of process by users and -x is (err..I forgot..hehehe). If you want to use ps -aux command, I advised you to pipe the command with more. What is a pipe in Linux? I will explained it later if I have some free time. So, let's try.

$ ps -aux | more
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 628 52 ? S Sep10 0:04 init [4]
root 2 0.0 0.0 0 0 ? S Sep10 0:00 [keventd]
root 3 0.0 0.0 0 0 ? SN Sep10 0:00 [ksoftirqd_CPU0]
root 4 0.0 0.0 0 0 ? S Sep10 0:00 [kswapd]
root 5 0.0 0.0 0 0 ? S Sep10 0:00 [bdflush]
root 6 0.0 0.0 0 0 ? S Sep10 0:00 [kupdated]
root 11 0.0 0.0 0 0 ? S Sep10 0:00 [kreiserfsd]
root 73 0.0 0.2 1632 676 ? Ss Sep10 0:00 /usr/sbin/syslogd

That is among the process that will display itself if you use ps command. Sometimes the list is long (as in my case) but sometimes the list is short. It is depends on what are the process run on your computer.

top is used to display real time information about all process running on your computer. What I mean by real time is that it will display all the necessary information such as number of tasks currently running on your computer, CPU, memory and swap usage and all related information regarding the process. Try it yourself.

$ top
top - 07:48:33 up 10 days, 19:45, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 66 total, 2 running, 56 sleeping, 8 stopped, 0 zombie
Cpu(s): 0.7% user, 0.3% system, 0.0% nice, 99.0% idle
Mem: 239628k total, 232684k used, 6944k free, 38408k buffers
Swap: 996020k total, 8176k used, 987844k free, 59860k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
27941 root 16 0 1084 1084 844 R 0.3 0.5 0:00.34 top
1 root 8 0 60 52 32 S 0.0 0.0 0:04.57 init
2 root 9 0 0 0 0 S 0.0 0.0 0:00.02 keventd
3 root 19 19 0 0 0 S 0.0 0.0 0:00.02 ksoftirqd_CPU0


So, as you can see, my computer is currently running 66 taks but only 2 are running and the others are sleeping (maybe because of the 'cold' weather in the laboratory). Anyway, as you can see both list PID for the process. PID is process id and it is very very important when you want to stop the process (if the process is hanging or to your horror you discover that your program is running on infinite loop and can't be stopped using Ctrl + C or Ctrl + Z key functions).

I will not touch chkconfig in here. If you used Fedora Core, CentOS or any other Linux distro that is derived from Red Hat Linux, just type chkconfig --list or you can read the manual of chkconfig using man chkconfig.

11. You, nasty process! Begone!
Beside knowing on how run applications and daemons in Linux environment, you also need to know how to stop nasty apps from terrorizing your computer. If your process is hanging in command environment or terminal, it can be easily stop using Ctrl + C and Ctrl + Z. But sometimes you also want to stop daemons and apps that currently running at the background or if your Firefox freeze. What can you do?

First, you need to find the ID for the process. Type ps command with pipe option like this :

$ ps -aux | grep type_your_process_name_in_here

If you expected that the list will be long, add other pipe options :

$ ps -aux | grep type_your_process_name_in_here | more

For example, just imagine my Firefox is currenly hang right now. So, what can I do. I just type this command :

ps -aux | grep firefox
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
root 3961 0.0 0.5 2472 1308 ? S Sep10 0:00 /bin/sh /usr/bin/firefox
root 3972 0.0 0.5 2512 1340 ? S Sep10 0:00 /bin/sh /usr/lib/firefox-1.5.0.7/run-mozilla.sh /usr/lib/firefox-1.5.0.7/firefox-bin
root 3977 0.1 15.2 53812 36536 ? S Sep10 20:23 /usr/lib/firefox-1.5.0.7/firefox-bin
root 3979 0.0 15.2 53812 36536 ? S Sep10 0:00 /usr/lib/firefox-1.5.0.7/firefox-bin
root 3980 0.0 15.2 53812 36536 ? S Sep10 0:00 /usr/lib/firefox-1.5.0.7/firefox-bin
root 3981 0.0 15.2 53812 36536 ? S Sep10 0:00 /usr/lib/firefox-1.5.0.7/firefox-bin


See that the firefox has several PID. So, what can you do? Try to get the earliest process ID (that is true for process or apps that fork itself). In this example, the earliest PID for firefox is 3961. Kill the process using kill PID command.

$ kill 3961

Try to use ps command once again after you finish killing the process. See, the process is now gone.

Too tired to add more. I will add later.

No comments: