Basic Commands and Wildcards

Working with time and date

[root@localhost root]#date

Tue Jun 7 13:44:10 BDT 2005

ð It shows the current date and time along with the Time Zone from BIOS.

[root@localhost root]# date –d “july 6,2004 11:48 AM”

Tue Jul 6 11:48:00 BDT 2004

ð To set a new time and date.

Working with Calendar

[root@localhost root]# cal

June 2005

Su Mo Tu We Th Fr Sa

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30

ð It shows the calendar of current month.

[root@localhost root]# cal 2004

ð It shows the calendar of the given year.

[root@localhost root]# cal 2004| less

ð It shows the calendar and you can use the “up” and “down” keys for verification. To exit this application you have to press ‘q’.

[root@localhost root]#cal 2004| more

ð It shows the calendar and you can use only “Enter” key for verification from up to down in screen. To exit this application you have to press ‘q’.

To clean the screen

[root@localhost root]# clear [ or Ctrl+l ]

ð It will clear the screen.

Another way to clean the screen is by pressing both keys “Ctrl” and ‘l’.

To verify the working users

[root@localhost root]# who

root tty1 Jun 7 13:35

sumon tty2 Jun 7 13:48

root pts/0 Jun 7 13:36

root pts/1 Jun 7 13:39

ð It displays the list of users who are logged in the system now.

1st column : showing the user name.

2nd column: showing the terminal number.

3rd column : showing the current month.

4th column : showing the date of current month.

5th column : showing the login time.

[root@localhost root]# who -u

root tty1 may 12 17:06 8956

sumon tty2 may 12 17:38 8960

ð In the last field it is showing the PID (Process Id) of user.

[root@localhost root]# who am I

root pts/1 Jun 7 13:39

ð It shows only that terminal used by me.

Help Commands

[root@localhost root]# man cal [Manual]

[root@localhost root]# info cal [Information]

[root@localhost root]# cal - -help [Help]

ð By using these above commands we can get helps about command and configuration file in Linux.

Binary Calculator

[root@localhost root]# bc

bc 1.06

Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

This is free software with ABSOLUTELY NO WARRANTY.

For details type `warranty'.

6+9

15

2*7

14

quit

ð It is a calculator in Linux system. We can add, multiply, divide, subtract etc by this application. To exit from the program we have to type “quit” in a new line and press “<Enter>”.

List directory contents - ls

Option

Performance

-a, --all

Do not hide entries starting with.

--author

print the author of each file.

-b, --escape

print octal escapes for non-graphic characters

--block-size=SIZE

use SIZE-byte blocks

-c

With -lt: sort by, and show, ctime (time of last modification of file status information) with -l: show ctime and sort by name otherwise: sort by ctime.

-d, --directory

Its directory entries instead of contents, and do not dereference symbolic links.

-G, --no-group

Inhibit display of group information.

-h

Print sizes in human readable format (e.g., 1K 234M 2G).

-i

Print index number of each file.

-l

Use a long listing format.

-r

Reverse order while sorting.

-R

List subdirectories recursively.

-s

Print size of each file, in blocks.

-t

Sort by modification time.

-Z, --context

Display security context so it fits on most displays. Displays only mode, user, group, and security context and file name.

[root@localhost root]# ls

anaconda-ks.cfg command.doc Desktop file: install.log install.log.syslog

ð The above command displays a list of directories and files from the current directory.

[root@localhost root]# ls –l

-rw-r--r-- 1 root root 1065 Apr 15 22:19 anaconda-ks.cfg

-rw-r--r-- 1 root root 10752 Jun 7 13:51 command.doc

drwxr-xr-x 3 root root 4096 May 10 17:25 Desktop

drwxr-xr-x 2 root root 4096 Jun 7 13:42 file:

-rw-r--r-- 1 root root 53254 Apr 15 22:19 install.log

-rw-r--r-- 1 root root 3005 Apr 15 22:18 I nstall.log.syslog

1st column : showing the security attribute.

2nd column: showing the linking path (as numeric).

3rd column : showing the owner of the file or directory.

4th column : showing group of the file or directory.

5th column : showing the size of file or directory.

6th column : showing the created date and time.

7th column : showing the file or directory name.

[root@localhost root]# ls –a

. .mozilla .openoffice-install-log

.. .gconfd anaconda-ks.cfg

.gnome .bash_history .openoffice-lock

.bash_logout .bash_profile .recently-used

.bashrc command.doc .config

.cshrc install.log install.log.syslog

.tcshrc .default_contexts Desktop

.mailcap file:

ð It shows all files and directories including hidden files and directories. In Linux system which files’ name started by dot (.) is hidden. If we remove this dot from the name then it becomes a normal file.

[root@localhost root]# ls –r

[root@localhost root]# ls –R

[root@localhost root]# ll [Long list]

ð This command is a short form or alias name of “ls –l” command.

To make new directory

[root@localhost root]# mkdir

ð To create new directory in current directory.

[root@localhost root]# mkdir -p

ð To cerate new directory with long path as /etc/sysconfig/newdirectory.

Present Working Directory

[root@localhost document]# pwd

/root/document

ð It shows whole path of the current working directory.

Change directory

[root@localhost root]# cd

ð To enter the given directory.

[root@localhost root]# cd ..

ð To go back to the parent directory from the current directory.

[root@localhost root]# cd

ð It always goes to in the user’s home directory.

[root@localhost root]# cd –

ð It goes to in last working directory.

[root@localhost root]# cd /etc/sysconfig

ð It goes to in “/etc/sysconfig” directory.

CAT Command

[root@localhost root]# cat > textfile

Welcome to the world of Matrix,

A new war of human against machine.

d^

ð Creating new file and storing data in the file we can use above command. To exit and save the file, press “Ctrl+d”.

[root@localhost root]# cat textfile

Welcome to the world of Matrix,

A new war of human against machine.

ð To display all contains of the file.

[root@localhost root]# cat >> textfile

But it will be finished by Nio.

d^

ð We can add other lines in the file from below of text which was written before.

[root@localhost root]# cat textfile

Welcome to the world of Matrix,

A new war of human against machine.

But it will be finished by Nio.

Spell checking with aspell

Interactive spell checker command is aspell. It is a easy way to check spelling in a file.

[root@localhost root]# aspell check letter.txt

We can create personal dictionaries through the following syntax.

look -quick spell check.

Example:

[root@localhost root]# aspell -l or l <>

[root@localhost root]# look exer

To Shutdown the Linux System

[root@localhost root]# shutdown –h now

[root@localhost root]# init 0

[root@localhost root]# power off

[root@localhost root]# halt

ð All commands are used to shutdown a Linux system.

To Restart the Linux System

[root@localhost root]# reboot

[root@localhost root]# init 6

[root@localhost root]# shutdown –r now

ð All commands are used to restart Linux system.

Copying Files and Directories

[root@localhost root]# cp

ð To copy one or more files from one directory to another’s.

[root@localhost root]# cp –r

ð To copy a directory with contained files and subdirectories to another place we can use the same command along with –r option. If we add another option –f with the command then the kernel start to perform his job without asking any question (mostly whether file will be replaced or not) to the user.

[root@localhost root]# cp –rf

Moving Files and Directories

[root@localhost root]# mv

ð To move one or more files from the directory to other’s.

[root@localhost root]# mv –r

ð To move a directory with contained files and subdirectories to another place we can use the same command attaching a –r option.

Removing Files and Directories

[root@localhost root]# rm

ð To remove one or more files.

[root@localhost root]# rm –r

ð To remove a directory with contained files and subdirectories.

[root@localhost root]# rm –rf

ð To remove the directory and all its contains as files and subdirectories we can use –rf option. The main different between –r and –rf is that when we utilize –r then the system will ask you that whether the directory will be deleted or not, but –rf will never ask to you any such question.

Creating Empty file

[root@localhost root]# touch <>

Example,

[root@localhost root]# touch f1 f2 f3 f4 f5

ð Creating five empty files we can use the above command.

[root@localhost root]# ll f1

MOUNT and UMOUNT Commands

All files accessible in a Linux system are arranged in one big tree, the file hierarchy, rooted at /. These files can be spread out over several devices. The mount command serves to attach the file system found on some device to the big file tree. Conversely, the umount command will detach it again.

The file /etc/fstab may contain lines describing what devices are usually mounted where, using which options.

Option

Description

-a

Mount all file systems (of the given types) mentioned in fstab.

-l

Add the ext2, ext3 and XFS labels in the mount output. Mount must have permission to read the disk device (e.g. be suid root) for this to work.

-n

Mount without writing in /etc/mtab. This is necessary for example when /etc is on a read-only file system.

-r

Mount the file system read-only. A synonym is -o ro.

-w

Mount the file system read/write. This is the default. A synonym is -o rw.

-L label

Mount the partition that has the specified label.

-U uuid

Mount the partition that has the specified uuid. These two options require the file /proc/partitions to exist.

-t vfstype

The argument following the -t is used to indicate the file system type. The file system types which are currently supported are: adfs, affs, autofs, coda, coherent, cramfs, devpts, efs, ext, ext2, ext3, hfs, hpfs, iso9660, jfs, minix, msdos, ncpfs, nfs, nfs4, ntfs, proc, qnx4, ramfs, reiserfs, romfs, smbfs, sysv, tmpfs, udf, ufs, umsdos, vfat, xenix, xfs, xiafs.

-o

Options are specified with a -o flag followed by a comma separated string of options. Some of these options are only useful when they appear in the /etc/fstab file. The following options apply to any file system that is being mounted (but not every file system actually honors them).

async

All I/O to the file system should be done asynchronously.

atime

Update inode access time for each access. This is the default.

auto

Can be mounted with the -a option.

defaults

Use default options: rw, suid, dev, exec, auto, nouser and async.

dev

Interpret character or block special devices on the file system.

_netdev

The filesystem resides on a device that requires network access (used to prevent the system from attempting to mount these filesystems until the network has been enabled on the system).

noatime

Do not update inode access times on this file system (e.g, for faster access on the news spool to speed up news servers).

noauto

Can only be mounted explicitly (i.e., the -a option will not cause the file system to be mounted).

nodev

Do not interpret character or block special devices on the file system.

exec

Permit execution of binaries.

noexec

Do not allow execution of any binaries on the mounted file system. This option might be useful for a server that has file systems containing binaries for architectures other than its own.

nosuid

Do not allow set-user-identifier or set-group-identifier bits to take effect. (This seems safe, but is in fact rather unsafe if you have suidperl installed).

nouser

Forbid an ordinary (i.e., non-root) user to mount the file system. This is the default.

remount

Attempt to remount an already-mounted file system. This is commonly used to change the mount flags for a file system, especially to make a readonly file system writeable. It does not change device or mount point.

ro

Mount the file system read-only.

rw

Mount the file system read-write.

suid

Allow set-user-identifier or set-group-identifier bits to take effect.

sync

All I/O to the file system should be done synchronously.

dirsync

All directory updates within the file system should be done synchronously. This affects the following system calls: creat, link, unlink, symlink, mkdir, rmdir, mknod and rename.

user

Allow an ordinary user to mount the file system. The name of the mounting user is written to mtab so that he can unmount the file system again. This option implies the options noexec, nosuid, and nodev (unless overridden by subsequent options, as in the option line

user,exec,dev,suid).

users

Allow every user to mount and unmount the file system. This option implies the options noexec, nosuid, and nodev (unless overridden by subsequent options, as in the option line users,exec,dev,suid).

In the Linux OS, we can't share any data directly from CD-Rom or Floppy. To utilize those devices we should mount first them to a mounting point. The syntax of mount command is given below,

mount <> <>

Cdrom:

[root@localhost root]# mount /dev/cdrom /mnt/cdrom

Or

[root@localhost root]# mount /mnt/cdrom

ð Now, CD-Rom drive is mounted to "/mnt/cdrom" point.

[root@localhost root]#cd /mnt/cdrom

[root@localhost cdrom]# ls

ð It displays all contains of CD.

Floppy:

[root@localhost root]# mount /dev/fd0 /mnt/floppy

Or

[root@localhost root]# mount /mnt/floppy

ð Now Floppy drive is mounted to "/mnt/floppy" point.

Note: Floppy drive in Linux OS is represented as fd0. If there are more floppy drives in a system then the name of those devices are fd1, fd2, fd3... ... and so on.

After mounting CD-Rom or Floppy, we can't or shouldn’t remove CD or Floppy from the drive without unmounting them. The syntax of umount command is given below,

umount <>

[root@localhost root]# umount /mnt/cdrom

ð CD-Rom is unmounted from "/mnt/cdrom".

[root@localhost root]# umount /mnt/floppy

ð Floppy drive is unmounted from "/mnt/floppy".

[root@localhost root]# eject

ð It automatically unmounts the CD-Rom from mounting point and ejects the CD drive. There are many options of eject command to use.

[root@localhost root]# eject -r

ð Performance of this command is as eject command.

[root@localhost root]# eject -t

ð It closes the tray of CD-Rom.

[root@localhost root]# mount

/dev/hda10 on / type ext3 (rw)

none on /proc type proc (rw)

none on /sys type sysfs (rw)

none on /dev/pts type devpts (rw,gid=5,mode=620)

/dev/hda8 on /boot type ext3 (rw)

none on /dev/shm type tmpfs (rw)

/dev/hdc on /mnt/cdrom type iso9660 (ro,nosuid,nodev)

ð It shows current status of mounting with you system.

Comparing Between Two Files

To compare between two files we use usually cmp, diff, comm commands. Syntax,

cmp <> <>

diff <> <>

comm <> <>

To illustrate, let first we create three files named file1, file2, file3 (file1 and file3 contains same data but file2 is different) and differ between each others.

[root@www root]# cat file1

The Stamford University

Dhanmondy, Dhaka,

Bangladesh

[root@www root]# cat file2

The Stamford University

Dhanmondy, Dhaka,

Student Name: Sumon Roy

Course: MBA

[root@www root]# cat file3

The Stamford University

Dhanmondy, Dhaka,

Bangladesh

[root@www root]# cmp file1 file2

file1 file2 differ: byte 43, line 3

ð cmp command finds differences between file1 and file2 and returns a output on screen.

[root@www root]# diff file1 file2

3c3,4

<>

---

> Student Name: Sumon Roy

> Course: MBA

[root@www root]# comm file1 file2

The Stamford University

Dhanmondy, Dhaka,

Bangladesh

Student Name: Sumon Roy

Course: MBA

ð comm command finds common and uncommon lines between two files and sends it on screen.

[root@www root]# comm file1 file3

The Stamford University

Dhanmondy, Dhaka,

Bangladesh

[root@www root]# diff file1 file3

[root@www root]# cmp file1 file3

ð diff and cmp commands don’t return any output while they don’t get any difference.

RPM – Red Hat Package Managers

To install, update, uninstall, we have to use rpm command. There are different options of rpm command. Such as,

General Options

Option

Performance

-v

Displays basic information about the RPM operation’s status.

-vv

Displays debugging information. Most useful to software packages and RPM developers.

--quiet

Displays only error information.

--help

Shows a usage summary.

--version

Shows the RPM version number.

--justdb

Updates only the database, not the file system.

Query Mode Options

-a

Query all install RPMs. Does not require a package specification.

--whatrequires

capability

Query all RPMs that need capability in order to function properly.

--whatprovides

capability

Query all RPMs that provide capability.

-f file

Query all RPM that owns file. Does not require a package specification.

-p group

List the package in the RPM group named group. Does not require a package specification.

-i

Display complete information about the queried RPM(s).

-R

List all RPMs on which the package depends.

--provides

List all of the capabilities the queried RPM(s) provides.

--changelog

Display change information about the queried RPM(s).

-l

List all of the files stored in the RPM.

-s

For each file in the original RPM, display its state, which is one of normal, not installed or replaced.

-d

List only the documentation files stored in the RPM.

-c

List only the configuration files stored in the queried RPM(s).

--dump

For each file stored in the queried RPMs, display its path, size, modification time, MD5 checksum, permissions, owner, group and whether it is a configuration file, documentation file, a device or a symlink (must be used with –l, -c or -d).

--last

Display the installation date and time of each RPM queried, starting with the most recently installed RPM.

--querytags

Print all known tags for use with the –qf option. Does not require a package specification.

--qf ‘formatstring’

Create a customized output format for displayed information, using formatstring as the model.

RPM Installation Options

--force

Install the package even it is already installed, install an older package version, or replace files already installed.

--h

Print up to 50 hash marks (#) to illustrate the progress of the installation.

--nodeps

Do not perform a dependency check before installing or upgrading a package.

--test

Do not install the package or update the database, just identify and display possible conflicts or dependency errors.

-v

Be slightly verbose and show some useful information during the installation.

RPM Upgrading Options

-U

For upgrade (upgrading a package, using –U, installs it even if an earlier version is not currently installed).

-F

For Freshen (freshening a package, using –F, installs it only if an earlier version is currently installed).

RPM Removing Option

-e

Removing or deleting RPMs.

Example,

[root@www RPMS]# rpm -ivh switchdesk-gui-4.0.3-1.noarch.rpm

warning: switchdesk-gui-4.0.3-1.noarch.rpm: V3 DSA signature: NOKEY, key ID 4f2a6fd2

Preparing... ########################################### [100%]

1:switchdesk-gui ########################################### [100%]

ð It displays the installation process of package.

[root@admin RPMS]# rpm -ivh --force --nodeps openoffice.org-libs-1.1.1-4.i386.rpm

[root@localhost root]# rpm -qa

ð It shows all packages name which are already installed in the system.

[root@localhost root]# rpm -q switchdesk

switchdesk-4.0.3-1

ð It shows the package name with version number because the package is installed. If the package is not installed, the command sends a message that ”switchdesk is not installed”.

[root@www root]# rpm -Uvh switchdesk-gui-4.0.3-1.noarch.rpm

[root@www root]# rpm -e switchdesk-gui

WC Command

To know how many lines, words and characters are stored in the file, we can use wc command to check it. Syntax,

wc

[root@www root]# wc file2

4 11 78 file2

1st column shows the number of lines.

2nd column shows the number of words.

3rd column shows the number of characters.

4th column shows the file name.

Some options of wc command are given below.

-l Display only number of lines.

-w Display only number of words.

-c Display only number of characters.

Example,

[root@localhost root]# wc -l file2

5


SORT Filter

Sort text to stdout keeping original file unchanged. The sort command helps us in customizing the output using various options. This filter arranges the input taken from the Standard Input in the alphabetical order. Common options are given below,

Option

Performance

-r

Reverses sort to sort descending.

-n

Numeric sort.

-f

Ignore (fold) case of characters in strings.

-u

Unique (remove duplicate lines in output).

-t ‘x’

Use x as field separator.

-k POS1

Sort from filed POS1.

-K POS1, POS2

Sort using field POS1 ending at POS2.

Example, lets we create a file named StudenName.

[root@www root]# cat StudenName

Sumon

Joy

Shovon

Sume

Azgar

Dhruba

Shifat

Parvez

[root@localhost root]# sort StudenName

Azgar

Dhruba

Joy

Parvez

Shifat

Shovon

Sume

Sumon

ð It shows all contains of the file and makes all data serially from a-z or 0-9.

[root@localhost root]# sort -r StudenName

Sumon

Sume

Shovon

Shifat

Parvez

Joy

Dhruba

Azgar

ð It shows all contains of the file and makes the data serially from z-a or 9-0.

[root@localhost root]# sort -r -o NewStudenName StudenName

ð When we give the sort command, it displays the data arranged differently in comparing the original file. But the file is never changed. If we want to save the output of sort command in a new file, then we can use the option -o. Here the output is saved in NewStudenName file which is showing below.

[root@www root]# cat NewStudenName

Sumon

Sume

Shovon

Shifat

Parvez

Joy

Dhruba

Azgar

GREP Filter

GREP - Globally Regular Search Print, by the command we can find a pattern from one or more files. Syntax,

grep

Option

Description

-v

Return lines that do not contain the pattern.

-n

Precede returned lines with numbers.

-c

Only returns a count of lines with the matching pattern.

-l

Only returns the name or names of files that have at least one line containing the pattern.

-r

Perform a recursive search of files, starting with the named directory.

Example,

[root@www root]# grep partition anaconda-ks.cfg

# The following is the partition information you requested

# Note that any partitions you deleted are not expressed

# here so unless you clear all partitions first, this is

ð It displays all that lines which contains the given pattern.

[root@localhost root]# grep -n partition anaconda-ks.cfg

15:# The following is the partition information you requested

16:# Note that any partitions you deleted are not expressed

17:# here so unless you clear all partitions first, this is

ð It displays all that lines along with the line number which contains the given pattern.

[root@www root]# grep -c partition anaconda-ks.cfg

3

ð It gives the number of pattern used in the file.

[root@localhost root]# grep -v <> <>

ð It shows other lines in which lines have no the pattern.

[root@www root]# grep -i PARTITION anaconda-ks.cfg

# The following is the partition information you requested

# Note that any partitions you deleted are not expressed

# here so unless you clear all partitions first, this is

ð It ignores the case sensitive of given pattern and shows all those lines which have the pattern.

[root@www root]# grep -l partition *

anaconda-ks.cfg

command.doc

redhat.doc

ð It shows all file names which contain the given pattern. The star ‘*’ is used as wildcard for files’ name.

HEAD and TAIL Command

[root@localhost root]# head

ð It shows first few lines of the file. If we add an option "-1" with head command then it will display first line only, if the option is "-2", then it will display first two lines, so on. By default this command will display only first ten lines from file.

[root@localhost root]# tail

ð It displays last ten lines from the file by default. We can use some option as "-1", "-2”and so on with tail command to get different lines number.

CUT and PAST Command

Consider we create a new file, called datafile. The text format of the file is given below.

[root@localhost root]# cat datafile

sumon math 68

azgar engl 50

druba phy 52

ð The spaces used in the file are created by "<Tab>" key.

[root@localhost root]# cut -f2 datafile

math

engl

phy

ð It shows only the second column of the file.

[root@localhost root]# cut -f2,3 datafile

math 68

engl 50

phy 52

ð It shows the second and third columns of datafile.

Let’s again create another file, "newfile", contains text formatted data given below,

[root@localhost root]# cat newfile

sumon:math:68

azgar:engl:50

druba:phy:52

There is no space in that file, so we can’t detach a separated column from the file. As a reason, we should add another option -d with cut command to separate different columns.

[root@localhost root]# cut -d":" -f1,3 newfile

sumon 68

azgar 50

druba 52

ð It shows the first and third column. The syntax of using option -d is given below:

-d"Which symbol is used inside the data field"

In the following table shows some examples of using –d option,

-d":"

-d" "

-d"@"

sumon:math:68

azgar:engl:50

druba:phy:52

sumon math 68

azgar engl 50

druba phy 52

sumon@math@68

azgar@engl@50

druba@phy@52

[root@localhost root]# past

ð To display two files at same time on the screen.

[root@localhost root]# past -d":"

ð To separate the output of both files by the symbol ":" we can use “–d” option.

PIPING

One of important concept in Linux is piping. We can use many command in one statement by utilizing piping. Some times you may find yourself in situation in which you need to send data from one command to another. Suppose you want to send a list of your file names to the printer to be printed. So you need two commands to do this: the “ls” command to generate a list of filenames and the “lpr” command to send the list to the printer. Such as,

[root@localhost root]# ls | lpr

ð It prints list of your file names. Piping is denoted by "|" symbol. In the below, there are more examples of piping:

[root@localhost root]# cat f1 | cut -f2 | head -2 | tail -1

ð In above statement we use four commands. Here first command cat sends its output to next command cut. Again cut command delivers the output to head command and so on. After finishing all four commands’ job, the output of tail command is displayed on screen.

[root@localhost root]# ls -l | wc -l

14

ð It shows the total number of how many files and directories are stored in /root directory.

[root@localhost root]# ls -l | grep ^d | wc -l

2

ð It shows the number of directories stored in /root directory. Here ‘^d’ means the first character of the line name is ‘d’. Again ‘$d’ means the last character of the line is ‘d’.

[root@localhost root]# ls -l | grep ^- | wc -l

11

ð It shows the number of files in /root.

[root@localhost root]# cat file1 | head -3 | tail -1

ð It shows only third number line from the file.

Using TEE Command in PIPING

Tee can provide two same outputs to send them to next two commands at the same time. It can be used in piping.

[root@localhost root]# ls -l | tee datafile | wc -l

14

ð Now, tee command makes double of ls command’s output and sends one’s to datafile to store and another’s to wc command. Now, we can verify the datafile file

[root@www root]# cat datafile

total 160

-rw-r--r-- 1 root root 1065 Apr 15 22:19 anaconda-ks.cfg

-rw-r--r-- 1 root root 13824 Jun 7 17:04 command.doc

drwxr-xr-x 3 root root 4096 May 10 17:25 Desktop

drwxr-xr-x 2 root root 4096 Jun 7 13:42 file:

-rw-r--r-- 1 root root 53 Jun 7 13:57 file1

-rw-r--r-- 1 root root 78 Jun 7 16:14 file2

-rw-r--r-- 1 root root 53 Jun 7 16:15 file3

-rw-r--r-- 1 root root 53254 Apr 15 22:19 install.log

-rw-r--r-- 1 root root 3005 Apr 15 22:18 install.log.syslog

-rw-r--r-- 1 root root 49 Jun 7 16:44 NewStudenName

-rw-r--r-- 1 root root 30720 Jun 7 16:47 redhat.doc

-rw-r--r-- 1 root root 16143 Jun 7 16:49 redhat.sxw

-rw-r--r-- 1 root root 49 Jun 7 16:41 StudenName.

[root@localhost root]# ls -l | tee | wc -l

ð The first output of tee goes to display on screen and another’s goes to wc command. Next, wc command’s output displays on screen.

Redirection

Programs can be instructed to take input a file other than the standard input i.e. the keyword and send the out put to a file instead of the standard output i.e. the Visual Display Unit. This is called Redirection. There are three types of redirection.

1 > - Input.

2 < - Output.

3 >> - Upending.

Standard input, output and error can be reconnected to alternate locations. Syntax:

command redirection operations

Redirection

Performance

Command > file

Command’s outputs are saved to the file.

Command >> file

Appends standard output of command to file.

Command < file

Command receives its input from file.

Command 2> file

Error messages from command are directed to file.

Command 2>> file

Error messages from command are appended to file.

Linux provides three I/O channels to processes for standard input and output.

1) Standard input – Keyboard is default.

2) Standard output – Terminal window is default.

3) Standard error – Terminal window is default.

Below, we give some examples.

[root@localhost root]# cat > datafile

Stamford University,

Dhaka, Bangladesh.

d^

ð The data of datafile are stored from Keyboard. Press Ctrl+d to save data in the file and to exit this application.

[root@localhost root]# sort -r filename > myfile

ð The output of sort command stores in myfile.

[root@localhost root]# cat >> datafile

Student Name: Susanta Kumar Roy

d^

ð The above line stores in the file, datafile to the end. Such as,

[root@localhost root]# cat datafile

Stamford University,

Dhaka, Bangladesh.

Student Name: Susanta Kumar Roy

[root@localhost root]# head -5 filename >> myfile

ð First five lines of “filename” file are stored in the myfile to the end.

[root@localhost root]# cat newfile <>

ð All contains of “datafile” file are stored in “newfile” file.

PROCESS Commands(PS,TOP,PSTREE)

A process has many components and properties,

1) exec thread.

2) PID.

3) Priority.

4) Memory context.

5) Environment.

6) File descriptions.

7) Security credentials.

Every process is given a PID (Process ID) by kernel and it must be difference each others. After booting the Linux, the processing of init starts first, so it is called parent process and it has many child processes. Again a child process can has one or many child processes. PID of init always is 0.

[root@localhost root]# ps

PID TTY TIME CMD

1862 pts/1 00:00:01 bash

2259 pts/1 00:00:00 ps

ð It shows all the background and foreground processes.

There are some options given below,

Option

Performance

-f

Does full listing.

-a

Select all with a tty except session leaders.

-l

Long format.

-u

Display user oriented format.

-t

Select by tty.

Syntax,

ps -t <>

Example,

[root@localhost root]# ps -t tty1

ð It shows all the processes which are running on given terminal name.

Some words are used in the output of pc or top command to show a process state (a process can be in one of many states). Such as,

process state

Description

R

Run able.

S

Sleeping.

T

Stopped.

D

Uninterruptible sleep.

Z

Defunct (zombie) process.

“top" utility which displays the top CPU processes. top is a real-time monitor, meaning that it displays an ongoing list of the most CPU intensive processes running. The display is updated every five seconds by default. Most of top‘s functionality can be controlled using either command line options or dynamically using top’s interactive interface.

Syntax: top [-cCiqsS] [-d delay] [-p pid] [-n num]

top command line options are given below,

Options

Description

-c

Display the command line arguments used to invoke the process.

-C

Display totals for CPU statistics on SMP machines.

-d delay

Pause delay seconds between updates.

-i

Ignore idle or zombie processes.

-n num

Refresh the display num times and then exit.

-p pid

Monitor processes with a process ID of pid (can be specified up to 20 times).

-q

Refresh constantly with no delay between updates.

-s

Run in secure mode, disabling potentially dangerous interactive commands.

-S

Show summary statistics for each process and its child processes (if any).

Example,

[root@localhost root]# top

top - 17:13:35 up 1:05, 3 users, load average: 0.71, 0.45, 0.41

Mem: 289556k total, 283940k used, 5616k free, 12960k buffers

Swap: 538136k total, 1548k used, 536588k free, 168500k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

2291 root 17 0 2992 912 1508 R 0.7 0.3 0:01.14 top

1 root 16 0 2688 516 1212 S 0.0 0.2 0:05.10 init

2 root 34 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0

3 root 5 -10 0 0 0 S 0.0 0.0 0:00.15 events/0

4 root 5 -10 0 0 0 S 0.0 0.0 0:00.00 kblockd/0

7 root 5 -10 0 0 0 S 0.0 0.0 0:00.00 khelper

5 root 25 0 0 0 0 S 0.0 0.0 0:00.00 khubd

6 root 15 0 0 0 0 S 0.0 0.0 0:00.00 kapmd

8 root 15 0 0 0 0 S 0.0 0.0 0:00.06 pdflush

9 root 15 0 0 0 0 S 0.0 0.0 0:00.12 pdflush

11 root 15 -10 0 0 0 S 0.0 0.0 0:00.00 aio/0

10 root 15 0 0 0 0 S 0.0 0.0 0:00.54 kswapd0

132 root 16 0 0 0 0 S 0.0 0.0 0:00.00 kseriod

1364 root 18 0 2996 828 1564 S 0.0 0.3 0:00.02 xinetd

ð It shows the task manager. We describe top’s display fields below.

Field

Description

PID

The process ID of each task.

USER

The user name of the user that started the process.

PRI

The process’s execution priority.

NI

The value used to modify the process’s execution priority, relative to other processes with the same priority (PRI).

SIZE

The amount of memory the process requires.

RSS

The amount of physical memory the process consumes.

SHARE

The amount of shared memory the process uses.

STAT

The process’s current CPU state.

%CPU

The percentage of total CPU time the process consumes.

%MEM

The percentage of total physical memory the process uses.

TIME

The total amount of CPU time that the process has been running.

COMMAND

The command used to invoke the process, show in parentheses if the task is currently swapped out.

Some top’s key are given below

Keys

Description

h

Displays all helped commands of top command.

r

To renice of PID value (The range of nice value is between +20 to -20).

k

To kill a process.

Note: After giving top command, it will show the information of zombie. It means that when we kill any process, at that time the PID of a process will be not removed. How many numbers of this type of PID are in the computer is showing as zombie number.

[root@localhost root]# pstree | less

ð It shows graphically of all the processes linking between parent and child.

Altering process scheduling priority – nice and renice

At process invocation time we can use nice command.

Syntax:

nice [-n adjustment] command

Processes are scheduled with a default priority of 0. Priority value can range from -20 (highest priority) to 19 (lowest).

Example: the nice command is used to modify this default niceness value,

[root@localhost root]# nice myprog

ð This runs the myprog program with a niceness value of 10. To set the niceness value to a different value, use the –n option.

[root@localhost root]# nice -n 15 myprog

renice changes the priority of a running process.

Syntax:

renice # [[-p 1 -g]PID] [[-u] user]

# is the priority value. Once a priority value is raised a non-privileged user can’t lower it.

Background and foreground process

Which process is running inside the computer but not showing on the monitor is called a background process. And which process is running inside the computer and also showing on the monitor, is called a foreground process. We can send a foreground process to background by adding & symbol after a command. Such examples are given below:

[root@localhost root]# cal | more &

[1] 2296

[root@localhost root]# date | less &

[2] 2298

ð Here we add "&" symbol after a command, so the command will be performed at background process.

If we want to see how many background processes are running in the system, we can use "jobs" command.

[root@localhost root]# jobs

[1] - Stopped cal | less

[2] + Stopped date | less

ð It shows also jobs Id and status of performance. "+" means which process is running in background now.

We can bring any background process to foreground by "fg" command. Syntax,

fg %[ Job's ID ]

Example,

[root@localhost root]# fg %1

ð Now, the process displays on monitor.

To run a background process in background we can use “bg” command. Syntax,

bg %[ Job's ID ]

Example,

[root@localhost root]# bg %1

[1]+ cal | less &

ð The background process runs in background now, which we can ensure by using “jobs” command.

[root@localhost root]# jobs

[1]+ Stopped cal | less

[2]- Stopped date | less

ð It actives the background jobs so "+" singe is replaced on %1.

Killing Process

This command is used to kill any running process. Syntax,

kill <> Or kill %[ Job's ID ]

Example,

[root@localhost root]# kill 8654

ð We can kill a process by using PID.

[root@localhost root]# kill %1

ð We can kill a background process by using job’s ID.

There are some signals using with “kill” command to kill a process in different way. Usually Linux has 63 types of signals. By default, two signals are used by “kill” command.

9 - SIGKILL

15 - SIGTERM

We can check all those Signals by”kill” command with adding “–l” option.

[root@localhost root]# kill –l

1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL

5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE

9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2

13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD

18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN

22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ

26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO

30) SIGPWR 31) SIGSYS 33) SIGRTMIN 34) SIGRTMIN+1

35) SIGRTMIN+2 36) SIGRTMIN+3 37) SIGRTMIN+4 38) SIGRTMIN+5

39) SIGRTMIN+6 40) SIGRTMIN+7 41) SIGRTMIN+8 42) SIGRTMIN+9

43) SIGRTMIN+10 44) SIGRTMIN+11 45) SIGRTMIN+12 46) SIGRTMIN+13

47) SIGRTMIN+14 48) SIGRTMIN+15 49) SIGRTMAX-15 50) SIGRTMAX-14

51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10

55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6

59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2

63) SIGRTMAX-1 64) SIGRTMAX

ð It shows all entire SIGNAL.

Example,

[root@localhost root]# kill -1 8034

[root@localhost root]# kill -9 %2

We can do logoff a user by “kill” command, i.e.

[root@localhost root]# who -u

root tty1 may 12 17:06 8956

sumon tty2 may 12 17:38 8960

joy tty5 may 12 17:55 9876

[root@localhost root]# kill 8956

ð The user who was using terminal-1 is logged off.

To find location of files or folder

Secure Locate provides a secure way to index and quickly search for files on your system. It uses incremental encoding just like GNU locate to compress its database to make searching faster, but it will also store file permissions and ownership so that users will not see files they do not have access to. Syntax,

locate

Example,

[root@localhost root]# locate *.c

[root@localhost root]# locate f1

ð It shows all possible paths where file could be stored.

This manual page documents the GNU version of find. find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name. Syntax,

find

find [Conditions] [Action]

Example,

[root@localhost root]# find /root anaconda-ks.cfg

ð Lists files and pathnames matching a specific the name or criteria.

Additional Options:

Options

Description

-atime +n,-n

List of files that were accessed more than or less than n days ago.

-mtime +n, -n

List of files that were modified more than or less than n days ago.

-user uname

List files owned by user uname.

-group gname

List files owned by group gname.

-perm mode

List files with the octal permission file.

-size n[c]

List files containing n blocks or if c is specified, n character long.

-type c

List files whose type is c. c can be b(block directory) or d (directory), p (FIFO or named pipe), l, s, f (plain file).

-iname pattern

A case insensitive version of –name pattern.

Finding and processing files through find command:

-exec action executes a command. Syntax:

find [conditions] -exec cmd {} \ ;

“Found” files are passed to cmd as arguments. {} is a place holder for the file.name. The escaped semicolon delimits the generated commands.

Checking Free Space – df and du

df command reports disk space usage with total kilobytes, kilobytes used, kilobytes free per file system. –h options with df command displays sizes in easier to read units.

[root@localhost root]# df /dev

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/hda10 5036284 3238808 1541644 68% /

[root@localhost root]# df -h /dev

Filesystem Size Used Avail Use% Mounted on

/dev/hda10 4.9G 3.1G 1.5G 68% /

du command reports disk space usage with kilobytes used per directory includes subtotals for each subdirectory. –s option only reports single directory summary and also takes –h option to displays sizes in easier to read units.

[root@localhost root]# $ du /etc

[root@localhost root]# $ du –s /etc

60616 /etc

Formatting A Floppy

Two types of format needed to prepare a floppy:

1) A low level format (rarely needed)

[root@localhost root]# fdformat /dev/fdoH1440

2) A filesystem, one of

[root@localhost root]# mkfs -t ext2 /dev/fd0

[root@localhost root]# mke2fs /dev/fd0

[root@localhost root]# mkfs -t vfat /dev/fd0

[root@localhost root]# mformat a: