Saturday, January 03, 2004

Linux fundamentals

the source for this and future continuing blogs is the lpi certification tutorial series by daniel robbins on ibm developerworks. daniel robbins happens to be the creator of gentoo linux, a distro i have mentioned earlier.

we'll start of with some basic bash commands.
bash is a shell, an acronym for "Bourne-again shell". there are many shells like csh, tsh .. to know which shell yu're on type
$ echo $SHELL
output will be like /bin/bash
in the above eg., $ is the prompt and $SHELL is an environment variable. for the root user the # is the prompt.
you can $ exit to logout.


cd
this like in win means change directory. the path can be relative (/usr/bin) or absolute (../java/bin).
the tilde ~ character signifies the current users home directory. so if i say
$ cd ~
i go to my home directory. this can be displayed be an env variable $HOME.


pwd
this command displays the present working directory.
$ pwd
/home/rahul


ls
this command is equivalent to dir. it has many imp options.

files in the specified directory will be displayed.
$ ls /usr
X11R6 doc lib man sbin
bin include portage share tmp

By specifying the -a option, you can see all of the files in a directory, including hidden files those that begin with . As you can see in the following example, ls -a reveals the . and .. special directory links:
$ ls -a
. bin include share tmp
.. distfiles local src X11R6
.hide.txt

here .hide.txt was a hidden file. any file which starts with a . is a hidden file. similarly a file name ending with a ~ is a backup file.

for a more verbose listing use the -l option
$ ls -l /usr
drwxr-xr-x 7 root root 168 Nov 24 14:02 X11R6
drwxr-xr-x 2 root root 14576 Dec 27 08:56 bin
drwxr-xr-x 2 root root 8856 Dec 26 12:47 distfiles
lrwxrwxrwx 1 root root 9 Dec 22 20:57 doc -> share/doc

the first column displays permissions information for each item in the listing. that i'll explain later. the next column lists the number of links to each filesystem object, which i'll explain with inodes and/or linking. the third and fourth columns list the owner and group, respectively. the fifth column lists the object size. the sixth column is the "last modified" time or "mtime" of the object. the last column is the object's name. If the file is a symbolic link, you'll see a trailing -> and the path to which the symbolic link points.

and you can always combine options like
$ ls -la /

Finally, the -i ls option can be used to display the inode numbers of the filesystem objects in the listing:
$ ls -i /usr
1409 X11R6 314258 i686-linux
43090 libexec 13394 sbin
inodes i'll have to cover later


mkdir
similar to dir, for making directories
$mkdir newdir


touch
this command is used to update the modifies time of a file. if the file does not exist then a blank file is created
$ touch britney
this will create a file called britney if it does not exist. this command can be used in a case where i want to fool the compiler to compile a source file evennthough it has not been changed.


echo
the echo command, which takes its arguments and prints them to standard output.
$ echo "firstfile"
firstfile


echo and redirection
Now, the same echo command with output redirection:
$ echo "firstfile" > copyme
the > command redirects the output from the std output to a file, copyme in this case. there are other redirections also like < for acting as a source rather than the std input.
$ cat < src > dest
the above command will copy the contents of a file src to dest.


cat and cp
to display the contents of the file on the terminal, use the cat command:
$ cat copyme
firstfile
now, we can use a basic invocation of the cp command to create a copiedme file from the original copyme file:
$ cp copyme copiedme
on investigation, we find that they are truly separate files; their inode numbers are
different:
$ ls -i copyme copiedme
648284 copiedme 650704 copyme


mv
use the mv command to rename "copiedme" to "movedme". the inode number will remain the same; however, the filename that points to the inode will change.
$ mv copiedme movedme
$ ls -i movedme
648284 movedme
a moved file's inode number will remain the same as long as the destination file resides on the same filesystem as the source file. the same filesystem as the source file.

No comments: