LINUX Operating System: (Continued )
LINUX Operating System: (Continued )
In this example change file ownership to vivek user and list the permissions, run:
# chown vivek demo.txt
# ls -l demo.txt
III BCA LAMP TECHNOLOGY UNIT-1
Sample outputs:
chmod command:
chmod changes the permissions of each given file according to mode, where mode describes
the permissions to modify. Mode can be specified with octal numbers or with letters.
Syntax:
chmod [Options]... Mode [,Mode]... file...
chmod [Options]... Numeric_Mode file...
chmod [Options]... --reference=RFile file...
Numeric Mode:
Symbolic Mode:
III BCA LAMP TECHNOLOGY UNIT-1
Processes:
o An Process is an instance of a running program.
o The operating system tracks processes through a five-digit ID number known as the pid or the
process ID. Each process in the system has a unique pid.
o Useful commands associated with the processes.
ps(process status):
ps command used to list all currently running processes.
kill:
kill command is used to stop the currently running process.
If a process is running in the background, you should get its Job ID using the ps command.
After that, you can use the kill command to kill the process
top command:
The top command is a very useful tool for quickly showing processes sorted by various criteria.
It is an interactive diagnostic tool that updates frequently and shows information about physical
and virtual memory, CPU usage, load averages, and your busy processes.
Simple syntax to run top command and to see the statistics of CPU utilization by different
processes −
III BCA LAMP TECHNOLOGY UNIT-1
Commands:
man:
man is the interface used to view the system's reference manuals.
man is the system's manual viewer; it can be used to display manual pages, scroll up and down,
search for occurrences of specific text, and other useful functions.
Syntax: man [term]
III BCA LAMP TECHNOLOGY UNIT-1
mkdir:
The mkdir command in UNIX allows users to create directories or folders as they are referred
to in some operating systems.
The mkdir command can create multiple directories at once and also set permissions when
creating the directory.
The user running the command must have appropriate permissions on the parent directory to
create a directory or will receive a permission denied error.
To create a directory in UNIX or Linux using the mkdir command pass the name of directory to
the mkdir command.
mkdir mydirectory
ls
mydirectory
cd:
cd: change directory. It operates either on fixed path cd / usr/share/misc or on a relative path cd
bin.
The following are few shortcuts:
~ is the home directory. The command cd ~/bin means to go to /home/jrl/bin where jrl is the
name of user directory.
. is the current directory. The command cd. /bin means go to the directory bin below this one.
The command ./configure means execute the configure file in the current directory.
.. is the directory above this one. The command cd.. means go to the next directory up.
III BCA LAMP TECHNOLOGY UNIT-1
cd ../.. change working directory to parents parent directory or two levels up in the directory
structure.
cd ../../.. changing three levels up in the directory.
pwd:
‘pwd‘ stands for ‘Print Working Directory‘. As the name states, command ‘pwd‘ prints the
current working directory or simply the directory user is, at present.
It prints the current directory name with the complete path starting from root (/). This command
is built in shell command and is available on most of the shell – bash, Bourne shell, ksh,zsh,
etc.
Syntax: pwd [options]
ls:
The ls command is a command-line utility for listing the contents of a directory or directories
given to it via standard input.
III BCA LAMP TECHNOLOGY UNIT-1
It writes results to standard output. The ls command supports showing a variety of information
about files, sorting on a range of options and recursive listing.
To show the contents of a directory pass the directory name to the ls command. This will list
the contents of the directory in alphabetical order. If your terminal supports colours you may
see that file and directory listings are a different colours.
To show a long listing pass the -l option to the ls command. This will output detailed
information on the directory listing.
Syntax: ls –l
total 2
drwxr-xr-x 6 george users 4096 Oct 4 20:27 code
drwxr-xr-x 10 george users 4096 Oct 4 09:13 dotfiles
mv:
The mv command is a command line utility that moves files or directories from one place to
another . It supports moving single files, multiple files and directories. It can prompt before
overwriting and has an option to only move files that are new than the destination.
Move a file:To move a file using the mv command pass the name of the file and then the new
name for the file. In the following example the file foo.txt is renamed to bar.txt.
ls
foo.txt
mv foo.txt bar.txt
ls
bar.txt
Move files into directory: To move a file into a directory using the mv command pass the
name of the file and then the directory.
In the following example the file foo.txt is moved into the directory bar.
Output:
III BCA LAMP TECHNOLOGY UNIT-1
rm:
rm stands for remove here. rm command is used to remove objects such as files, directories,
symbolic links and so on from the file system like LINUX.
This command normally works silently and you should be very careful while running rm
command because once you delete the files then you are not able to recover the contents of files
and directories.
Syntax: rm [OPTION]... FILE...
Options used in rm command
o -i (Interactive Deletion): The -i option makes the command ask the user for confirmation
before removing each file, you have to press y for confirm deletion, any other key leaves the
file un-deleted.
o -f (Force Deletion): rm prompts for confirmation removal if a file is write protected. The -f
option overrides this minor protection and removes the file forcefully.
III BCA LAMP TECHNOLOGY UNIT-1
o -r (Recursive Deletion): With -r(or -R) option rm command performs a tree-walk and will
delete all the files and sub-directories recursively of the parent directory. At each stage it
deletes everything it finds. Normally, rm wouldn’t delete the directories but when used with
this option, it will delete.
cp:
The Linux cp command provides you the power to copy files and directories through the
command line.
Syntax: cp [OPTION]... SOURCE DEST
Basic copy operation:
Syntax: cp file1 ~/Desktop
The above command copies 'file1' present in the current working directory to the Desktop
directory.
Make cp prompt before overwriting:
If the destination where you are copying the file already contains a file of same name, then the
cp command silently overwrites the existing file. However, if you want, you can make the tool
prompt before overwriting is done. This can be done by running cp in interactive mode, which
is enabled using the -i option.
Force cp to not overwrite existing file
Syntax: cp file1 ~/Desktop
Sometimes, you might not want cp to overwrite an existing file, and you also don't want to
enable the interactive option then for these kind of situations, you can use the -n command line
option.
III BCA LAMP TECHNOLOGY UNIT-1
ln:
The ln command is used to create links between files.
ln creates a link to file TARGET with the name LINKNAME. If LINKNAME is omitted, a link
to TARGET is created in the current directory, using the name of TARGET as the
LINKNAME.
ln, by default, creates a hard link just like link does. So this ln command:
ln file1.txt file2.txt
We can also use ln to create symbolic links with the -s option. So the command:
ln -s file1.txt file2.txt
Will create a symbolic link to file1.txt named file2.txt. In contrast to our hard link example,
here's an illustration to help you visualize our symbolic link:
pushd:
The pushd adds a directory to the top of the stack.
The pushd command puts/adds directory paths onto a directory stack (history) and later
allowing you to navigate back to any directory in history. While you add directories to the
stack, it also echoes what’s existing in history (or “stack”).
$ pushd ~/dir1
III BCA LAMP TECHNOLOGY UNIT-1
~/dir1 ~
$ pushd ~/dir2
~/dir2 ~/dir1 ~
$ pushd ~/dir3
~/dir3 ~/dir2 ~/dir1 ~
popd:
It removes a directory from the top of the stack or history.
Once we start issuing popd (remove directory) commands, we see that the directory stack tosses
off the directory on the top of the stack as it retraces its steps back through the directories that
were previously visited.
$ pushd ~/dir3
~/dir3 ~/dir2 ~/dir1 ~
$ popd
~/dir2 ~/dir1 ~
$ popd
~/dir1 ~
$ popd
~
df:
The df command is a command line utility for reporting file system disk space usage. It can be
used to show the free space on a Unix or Linux computer and to understand the filesystems that
have been mounted. It supports showing usage in Bytes, Megabytes and Gigabytes.
To view disk space usage run the df command. This will print a table of information to standard
output. This can be useful to discover the amount of free space available on a system or
filesystems.
du:
du command (short for disk usage) is useful command which is used to find disk usage for files
& directories. du command when used with various options provides results in many formats.
Syntax: du [OPTION]... [FILE]..
grep:
The grep filter searches a file for a particular pattern of characters, and displays all lines that
contain that pattern. The pattern that is searched in the file is referred to as the regular
expression (grep stands for globally search for regular expression and print out).
III BCA LAMP TECHNOLOGY UNIT-1
III BCA LAMP TECHNOLOGY UNIT-1
locate:
The locate command is often the simplest and quickest way to find the locations of files and
directories on Linux.
Syntax : locate [options] name(s)
When used without any options, locate displays every absolute pathname for which the user has
access permission that contains any of the names of files and/or directories that are provided to
it as arguments (i.e., input data).
The absolute pathname, also referred to as the absolute path or the full path, is the hierarchy of
directories from the root directory to the designated file or directory.
find:
Find command used to search and locate list of files and directories based on conditions you
specify for files that match the arguments.
III BCA LAMP TECHNOLOGY UNIT-1
Find can be used in variety of conditions like you can find files by permissions, users, groups,
file type, date, size and other possible criteria.
Syntax :
more:
The more command is a command line utility for viewing the contents of a file or files once
screen at a time. It supports navigating forwards and backwards through a file and is primarily
used for viewing the contents of a file.
III BCA LAMP TECHNOLOGY UNIT-1
uname:
uname command is used to display the software and hardware information in current running
Linux system. uname command is default shell command in Linux.
Print certain system information. If no OPTION is specified, uname assumes the -s option.
Syntax: uname [OPTIONS]
ifconfig:
Ifconfig stands for "Interface Configuration" .It is a utility for Linux machines to configure,
assign, add, delete, control and query network interface in Linux machine.
III BCA LAMP TECHNOLOGY UNIT-1
Common Linux users uses IFCONFIG command to assign ip address and netmask to an
interface or to disable or enable a given interface.
netstat:
netstat (network statistics) is a command line tool for monitoring network connections both
incoming and outgoing as well as viewing routing tables, interface statistics etc.
It is very useful in terms of network troubleshooting and performance measurement.
netstat is one of the most basic network service debugging tools, telling you what ports are
open and whether any programs are listening on ports.
This tool is very important and much useful for Linux network administrators as well as system
administrators to monitor and troubleshoot their network related problems and determine
network traffic performance.
chkconfig:
chkconfig Command helps you to configure services for starting and stopping them
automatically when the Operating System boots. This is configured in the scripts,
/etc/rc.d/init.d. This tutorial covers the ground on installation of chkconfig command.
which:
Locate the executable file associated with a given command. which returns the pathnames of
the files (or links) which would be executed in the current environment, had the filename (or
filenames) been given as a command .
III BCA LAMP TECHNOLOGY UNIT-1
who:
Displays who is logged on to the system. The who command prints information about all users
who are currently logged in.
Options:
-a, --all : Same as using the options -b -d --login -p -r -t -T -u.
-H: Print a line of column headings.
-u: Print the idle time for each user, and the process ID
Command aliasing:
The alias command makes it possible to launch any command or group of commands (inclusive
of any options, arguments and redirection) by entering a pre-set string (i.e., sequence of
characters).
That is, it allows a user to create simple names or abbreviations (even consisting of just a single
character) for commands regardless of how complex the original commands are and then use
them in the same way that ordinary commands are used.
Listing and Creating Aliases:
The general syntax for the alias command:
III BCA LAMP TECHNOLOGY UNIT-1
/home:
The /home directory is a place where by default all user home directories are created.
III BCA LAMP TECHNOLOGY UNIT-1
These directories are a kind of personal place(Working space) for all the users other than root.
There will be a separate folder for each user in /home directory. For example if you have a user
called tom, then his default home directory is /home/tom. We can change this default folder
when creating user in Linux. Our tom user can do what ever he wants in /home/tom folder
where he have full rights on the files he created and owned in that folder.
The properties of /home folder:
A separate sub folder i.e., /home/<user-name> is present for each user.
Only user who owns this sub folder can access its content other than root user. So, tom user can
not access barbi user home directory content which is located at /home/barbi.
All his terminal properties, command history file, application setting files(~/.vimrc, ~/.ssh) etc..
everything is located in this folder.
/root:
Root is the default name for system administrator in a UNIX system – a super user who can do
anything and everything within the operating system. As a result, root login should be used
with special care. While working with a root login, we can end up doing a lot of harm to our
system as well as the data, accidentally.
Need for the root account:
Root login is required to perform actions which change the settings for all system-wide users or
to modify the users’ accounts.
We shall also have to use the root account for certain system operations.
To add new users to the system and administer the user data.
To install system-wide software.
To configure I/O devices like – a scanner or a TV tuner card, for example.
To configure system services like – a web or FTP server.
/usr/bin:
/usr/bin is a standard directory on Unix-like operating systems that contains most of the
executable files (i.e., ready-to-run programs) that are not needed for booting (i.e., starting) or
repairing the system.
III BCA LAMP TECHNOLOGY UNIT-1
/usr/bin is one of the major subdirectories of the /usr directory. /usr, in turn, is one of the largest
(in terms of disk space consumption) of the standard first tier directories in the root directory,
and it is the directory in which most standard programs are kept, along with on-line manuals
and most libraries (i.e., collections of code that are commonly used by programs).
The root directory, which is designated by a forward slash ( / ), is the directory that contains all
other directories on a system.
There are more than 1900 executable files in /usr/bin on a typical system.
A few of the most commonly used are awk, bc, bzip2, cal, clear, diff, du, env, file, find, finger,
free, gcc, gdm, gedit, gpg, gunzip, gzip, join, head, kate, last, less, locate, man, nl, pstree,
quota, spell, stat, strings, sudo, tail, telnet, time, top, uniq, vim, w, wc, which, who, whoami,
wvdial, xpdf and zip.
/bin:
bin Stands for binary. This folder contains base executables which are required for minimal
system working. These commands are available in runlevel 1 for basic administration.
Commands which are available in /bin folder is accessed by every one and can run by every
user. This folder contains basic commands such as cat, chmod, chgrp, chown, date, dir, dd, df,
ln, mv, echo and zipping tools such as bzip, gzip etc.
/sbin:
sbin stands for system binaries or super user binaries. This folder contains commands which
are required for changing system properties or system level settings such as disk management,
network management etc.
This folder is accessed some times by normal user but they can not execute any of the
commands located in this and what ever commands/scripts located in this folder are run only by
root user.
If you want to make normal user to run these commands you have to implement SUDO or
Powerbroker to give elevated access.
Some of the commands available in this folder are chkconfig, dhcpclient, fsck and it’s related
commands, ifconfig and it’s related commands, init and it’s related commands, lvm and it’s
related commands etc
III BCA LAMP TECHNOLOGY UNIT-1
/usr/sbin:
This folder is similar to /sbin. This folder contain system commands which are an extend set of
commands to /sbin folder for root user and not that much essential to run the machine. This
folder contain commands such as arp, adduser, cron, cups related commands, grub related
commands, kvm, ppp related commands, tcpdump etc.
/lib:
The lib folder is a library files directory which contains all helpful library files used by the
system. In simple terms, these are helpful files which are used by an application or a command
or a process for their proper execution. The commands in /bin or /sbin dynamic library files are
located just in this directory. The kernel modules are also located here.
usr/lib:
Contains dynamic libraries and support static files for the executables at /usr/bin and /usr/sbin.
You can create a subdirectory like /usr/lib/myproduct to contain your helper files, or dynamic
libraries that will be accessed only by your Software, without user intervention. A subdirectory
here can be used as a container for plugins and extensions.
usr/src:
The 'linux' sub-directory holds the Linux kernel sources, header-files and documentation.
usr/local:
The /usr/local hierarchy is for use by the system administrator when installing software locally.
It needs to be safe from being overwritten when the system software is updated. It may be used
for programs and data that are shareable among a group of hosts, but not found in /usr .
/usr/local is a place to install files built by the administrator usually by using the make
command (eg: ./configure; make; make install). The idea is to avoid clashes with files that are
part of the operating systems which would either be overwritten or overwrite the local ones
otherwise. eg. /usr/bin/foo is part of the OS while /usr/local/bin/foo is a local alternative.
/opt :[optional]
This is where optional (local) software is installed.
/opt is a directory where to install unbundled packages, each in its own subdirectory.
III BCA LAMP TECHNOLOGY UNIT-1
They are already built whole packages provided by an independent third party software
distributor. Unlike /usr/local stuff, these package follow the directory conventions.
For example someapp would be installed in /opt/someapp with one of its command being
/opt/someapp/bin/foo, its configuration file would be in /etc/opt/someapp/foo.conf, and its log
files in /var/opt/someapp/logs/foo.access.
usr/X11R6:
This directory containing libraries, executables, docs, fonts and much more concerning the X
Window System.
The programs that run on X only have their files in the '/usr/X11R6' hierarchy, while the others
use '/usr'.
This hierarchy is reserved for the X Window System, version 11 release 6, and related files.
XFree86 more compatible with the X Window System on other systems, the following
symbolic links must be present if /usr/X11R6 exists:
/usr/bin/X11 -> /usr/X11R6/bin
/usr/lib/X11 -> /usr/X11R6/lib/X11
/usr/include/X11 -> /usr/X11R6/include/X11
/tmp & /var/tmp:
the global temporary directories are /tmp and /var/tmp. Web browsers periodically write data
to the tmp directory during page views and downloads.
The /var/tmp directory is made available for programs that require temporary files or directories
that are preserved between system reboots. Therefore, data stored in /var/tmp is more persistent
than data in /tmp.
Files and directories located in /var/tmp must not be deleted when the system is booted.
Although data stored in /var/tmp is typically deleted in a site-specific manner, it is
recommended that deletions occur at a less frequent interval than /tmp.
/var:
Contains variable data like system logging files, mail and printer spool directories, and
transient and temporary files. Some portions of /var are not shareable between different
systems. For instance, /var/log, /var/lock, and /var/run. Other portions may be shared, notably
/var/mail, /var/cache/man, /var/cache/fonts, and /var/spool/news.
III BCA LAMP TECHNOLOGY UNIT-1
/var/spool:
Holds spool files, for instance for mail, news, and printing (lpd) and other queued work. Spool
files store data to be processed after the job currently occupying a device is finished or the
appropriate cron job is started. Each different spool has its own subdirectory below /var/spool,
e.g., the cron tables are stored in /var/spool/cron/crontabs.
/var/log:
Log files from the system and various programs/services, especially login (/var/log/wtmp,
which logs all logins and logouts into the system) and syslog (/var/log/messages, where all
kernel and system program message are usually stored). Files in /var/log can often grow
indefinitely, and may require cleaning at regular intervals.
/var/www:
Holds spool files, for instance for mail, news, and printing (lpd) and other queued work. Spool
files store data to be processed after the job currently occupying a device is finished or the
appropriate cron job is started. Each different spool has its own subdirectory below /var/spool,
e.g., the cron tables are stored in /var/spool/cron/crontabs.
/boot:
/boot is an important folder in Linux. /boot folder contains all the boot related info files and
folders such as grub.conf, vmlinuz image aka kernel etc.
/etc:
This is the nerve center of your system, it contains all system related configuration files in here
or in its sub-directories.
A "configuration file" is defined as a local file used to control the operation of a program; it
must be static and cannot be an executable binary.
For this reason, it's a good idea to backup this directory regularly. It will definitely save you a
lot of re-configuration later if you re-install or lose your current installation. Normally, no
binaries should be or are located here.
For example, Apache related configuration files under /etc/httpd/conf/ and send mail is under
/etc/mail/conf.
/etc/X11:
The X server and associated configuration files are stored in the /etc/X11/ directory.
III BCA LAMP TECHNOLOGY UNIT-1
The configuration file for the X server is /etc/X11/xorg.conf. When Red Hat Enterprise Linux is
installed, the configuration files for X are created using information gathered about the system
hardware during the installation process.
/etc/init.d:
init.d is the sub-directory of /etc directory in Linux file system.
init.d basically contains the bunch of start/stop scripts which are used to control
(start,stop,reload,restart) the daemon while the system is running or during boot.
If you look at /etc/init.d then you will notice all the scripts for different services of your system.
The scripts within the directory varies as per the applications installed in your system.
There are two types of scripts : K scripts & S scripts.
K scripts are known ‘kill’ scripts while S scripts are known as ‘startup’ scripts. Kill scripts
always run before startup scripts. The configuration file (.conf) of these scripts are located
under /etc/init & the scripts that are used as defaults are located under /etc/default.
Within /etc/init.d you’ll find the script rc.local ,this script is automatically executed after all
primarily priortized scripts are executed. Which means first K scripts are executed, following
by S scripts, then all other remaining init level scripts (if there’s any) & finally rc.local script.
Now to control any script, you’ll need the superuser (sudo/su) permission. You can manually
control the scripts using console/terminal. The syntax for controlling the script is like :
Syntax: # /etc/init.d/<script> <option>
Where <script> stands for the name of the script located under /etc/init.d which you wanna
control, and <option> can be replaced by the control commands like :
start,stop,reload,restart,force-reload.
Example: sudo /etc/init.d/pulseaudio reload
/mnt:
The /mnt directory and its subdirectories are intended for use as the temporary mount points for
mounting storage devices, such as CDROMs, floppy disks and USB (universal serial bus) key
drives. /mnt is a standard subdirectory of the root directory on Linux.
Mounting is the process of attaching an additional filesystem, which resides on a CDROM,
hard disk drive (HDD) or other storage device, to the currently accessible filesystem of a
computer.
III BCA LAMP TECHNOLOGY UNIT-1
The mount point is the directory in the currently accessible filesystem (typically an empty
directory) to which the additional filesystem is attached (i.e., mounted). It becomes the root
directory of the subtree from the newly added storage device, and that subtree becomes
accessible from that directory. Any original contents of the mount point become invisible and
inaccessible until the filesystem is unmounted (i.e., detached from the main filesystem).
/mnt can be empty, or it can contain subdirectories for mounting individual devices. Its
subdirectories on a typical system include /mnt/cdrom and /mnt/floppy; other subdirectories can
be created as desired.
/dev:
The /dev directory contains the special device files for all the devices. The device files are
created during installation, and later with the /dev/MAKEDEV script. The
/dev/MAKEDEV.local is a script written by the system administrator that creates local-only
device files or links
/proc:
Proc file system (procfs) is virtual file system created on fly when system boots and is
dissolved at time of system shut down.
It contains the useful information about the processes that are currently running, it is regarded
as control and information centre for kernel.
The proc file system also provides communication medium between kernel space and user
space.