Lecture 3-Linux Commands
Lecture 3-Linux Commands
UNIX Commands
• A command is a program which interacts with the kernel to provide
the environment and perform the functions called for by the user.
• A command can be: a built-in shell command; an executable shell file,
known as a shell script; or a source compiled, object code file.
• The shell is a command line interpreter. The user interacts with the
kernel through the shell. You can write ASCII (text) scripts to be acted
upon by a shell.
UNIX Shell
• The shell sits between you and the operating system, acting as a
command interpreter.
• It reads your terminal input and translates the commands into actions
taken by the system. The shell is analogous to command.com in DOS.
• When you log into the system you are given a default shell.
• When the shell starts up it reads its startup files and may set
environment variables, command search paths, and command
aliases, and executes any commands specified in these files.
UNIX Shell
• The original shell was the Bourne shell, sh.
• Every Unix platform will either have the Bourne shell,
or a Bourne compatible shell available.
• The default prompt for the Bourne shell is $ (or #, for
the root user).
• Another popular shell is C Shell. The default prompt
for the C shell is %.
UNIX Shell
• Numerous other shells are available from the network. Almost all of
them are based on either sh or csh with extensions to provide job
control to sh, allow in-line editing of commands, page through
previously executed commands, provide command name completion
and custom prompt, etc.
• Some of the more well known of these may be on your favorite Unix
system: the Korn shell, ksh, by David Korn and the Bourne Again
SHell, bash, from the Free Software Foundations GNU project, both
based on sh, the T-C shell, tcsh, and the extended C shell, cshe, both
based on csh.
Shell Programming
• You can write shell programs by creating scripts containing a series of
shell commands.
• The first line of the script should start with #! which indicates to the
kernel that the script is directly executable.
• You immediately follow this with the name of the shell, or program
(spaces are allowed), to execute, using the full path name. So to set
up a Bourne shell script the first line would be: #! /bin/sh
Shell Programming
• The first line is followed by commands
• Within the scripts # indicates a comment from that point until the
end of the line, with #! being a special case if found as the first
characters of the file.
• #!/bin/bash
• cd /tmp
• mkdir t
• You also need to specify that the script is executable by setting the
proper bits on the file with chmod, e.g.:
• $ chmod +x shell_script
LINUX COMMANDS
• File Management and Viewing
• Filesystem Mangement
• Help, Job and Process Management
• Network Management
• System Management
• User Management
• Printing and Programming
• Document Preparation
• Miscellaneous
Command Structure
• Command <Options> <Arguments>
• Multiple commands separated by ; can be executed one after the
other
Help Facilities for Commands
• To understand the working of the command and possible options use (man command)
• Using the GNU Info System (info, info command)
• Listing a Description of a Program (whatis command)
• Many tools have a long−style option, `−−help', that outputs usage information about the tool,
including the options and arguments the tool takes. Ex: whoami –help
• Type command Indicate how a command name is interpreted
• which command Display which executable program will be executed
• README and Other Program Documentation Files
• Many software packages installed on your system have documentation files residing in the /usr/share/doc directory
Pipelines
• Pipelines - the capability of commands to read data from standard
input and send to standard output is utilized by a shell feature.
• Using the pipe operator “|”, the standard output of one command
can be piped into the standard input of another.
eg. $ who | wc −l
ls -l /usr/bin | less
•cd Change the current directory. With no arguments "cd" changes to the users
home directory. (cd <directory path>)
•chmod Change the file permissions.
Ex: chmod 751 myfile : change the file permissions to rwx for owner, rx for group
and x for others
Ex: chmod go=+r myfile : Add read permission for the group and others (character
meanings u-user, g-group, o-other, + add permission,-remove,r-read,w-write,x-exe)
Ex: chmod +s myfile - Setuid bit on the file which allows the program to run with user
or group privileges of the file.
Linux File Management and Viewing
There are three such special permissions within Linux. They are:
•setuid — used only for applications, this permission indicates that the application is to run as the
owner of the file and not as the user executing the application. It is indicated by the character s in
place of the x in the owner category. If the owner of the file does not have execute permissions, the
S is capitalized to reflect this fact.
•setgid — used primarily for applications, this permission indicates that the application is to run as
the group owning the file and not as the group of the user executing the application. The setgid
permission is indicated by the character s in place of the x in the group category. If the group owner
of the file or directory does not have execute permissions, the S is capitalized to reflect this fact.
•sticky bit — used primarily on directories, this bit dictates that a file created in the directory can
be removed only by the user that created the file. It is indicated by the character t in place of the x
in the everyone category. If the everyone category does not have execute permissions, the T is
capitalized to reflect this fact.
Linux File Management and Viewing
Manipulating files and directories
•ls List contents of a directory.
Ex: ls, ls –l , ls –al, ls –ld, ls –R
(-rwxrwxr-x 1 juan juan 0 Sep 26 12:25 foo )
|more will list page wise
•cp Copy files and directories
cp item1 item2
•mkdir Make a directory.
Ex: mkdir <directory name> : Makes a directory
Ex mkdir –p /www/chache/var/log will create all the directories starting from www.
•mv Move or rename a file or directory.
Ex: mv <source> <destination>
•rm Delete files (Remove files). (rm –rf <directory/file>)
•rmdir Remove a directory. The directory must be empty. (rmdir <directory>)
•ln Create hard and symbolic links ()
ln file link - Create hard link
ln -s item link -create symbolic link , item is either a file or directory
Linux File Management and Viewing
Searching for files
•find command
• Search for files in a directory hierarchy
• Find files (find <start directory> -name <file name> -print)
• Ex: find /home –name readme -print
(Search for readme starting at home and output full path.)
“/home" = Search starting at the home directory and proceed through all its subdirectories
"-name readme" = Search for a file named readme
"-print" = Output the full path to that file
•locate command
• Find files by name
• File locating program that uses the slocate database.
locate bin/zip
locate zip | grep bin
Ex: locate –u to create the database,
locate <file/directory> to find file/directory
•touch Change file timestamps to the current time. Make the file if it doesn't exist. (touch <filename>)
•stat Display file or file system status
Linux File Management and Viewing
• pwd Print or list the present working directory with full path.
• whereis Locate the binary and man page files for a command.
(whereis <program/command>)
• which Show full path of commands where given commands reside.
(which <command>)
Linux File Management and Viewing
File viewing and editing
•emacs Full screen editor.
•pico Simple text editor.
•vi Editor with a command mode and text mode. Starts in command mode.
•gedit GUI Text Editor
Linux File Management and Viewing
File compression, backing up and restoring
• compress Compress data.
• uncompress Expand data.
• cpio Can store files on tapes. to/from archives.
• gzip - zip a file to a gz file.
• gunzip - unzip a gz file.
• tar Archives files and directories. Can store files and directories on tapes.
Ex: tar -zcvf <destination> <files/directories> - Archive copy groups of files. tar –zxvf
<compressed file> to uncompress
• zip – Compresses a file to a .zip file.
• unzip – Uncompresses a file with .zip extension.
Linux File Management and Viewing
Text processing
•cat View a file and Concatenate files and print on the standard output
Ex: cat filename or
•cmp Compare two files
•sort Sort lines of text files
•uniq Report or omit repeated lines
•cut Remove sections from each line of files.
•diff Show the differences between files or Compare files line by line
Ex: diff file1 file2 : Find differences between file1 & file2.
•echo Display a line of text.
Linux Filesystem Management
• badblocks Used to search a disk or partition for badblocks. (badblocks device)
(badblocks hda)
• df Shows the disk free space on one or more filesystems. (df –k, df -h)
• du Shows how much disk space a directory and all its files contain. (du <directory>,
du –sk <directory>, du –sh <directory>)
• fsck Filesystem check. Must not be run on a mounted file system. (fsck <filesystem>)
• sync Synchronize data on disk with memory. `sync' writes any data buffered in
memory out to disk.
• mount Used to mount a filesystem. Complement is umount. (mount <filesystem>,
mount –a)
• umount Unmounts a filesystem. Complement is mount. (umount <filesystem>)
Network Management Commands
• Some of the commands that we will cover may (depending on your distribution)
require the installation of additional packages from your distribution’s
repositories, and some may require superuser privileges to execute.
• dnsdomainname Show the systems DNS domain name
• hostname Used to show or set the name of your machine for networking
• nisdomainname Show or set systems NIS/YP domain name
• ypdomainname Show or set the system's NIS/YP domain name
Network Management Commands
• arp This program lets the user read or modify their arp cache.
• If you want to lookup or resolve a DNS name or an IP address, you can use host or dig tools
• dig Send domain name query packets to name servers for debugging or testing.
• host Lookup DNS name
• finger Display information about the system users. Ex: finger Deepak
• ftp File transfer program. (ftp <remote system name/ip)
• ip To show your current IP address, or to get a list of all the IP addresses in use on your system. (#ip address )
• hostname command or uname -n. Display the current hostname
• ifconfig Configure a network interface. Note: get a list of network devices on your system, run ifconfig -a or ip
link.
ifconfig eth0 address 172.31.2.2 netmask 255.255.0.0
• ifdown Shutdown a network interface. Ex: ifdown eth0
• ifup Brings a network interface up. Ex: ifup eth0
Network Management Commands
• netconf A GUI interactive program to let you configure a network on
Redhat systems.
• netconfig Another GUI step by step network configuration program.
• netstat Displays information about the systems network connections,
including port connections, routing tables, and more. The command
"netstar -r" will display the routing table. (netsat –rn) Network
Troubleshooting
• nslookup Used to query DNS servers for information about hosts.
Network Management Commands
• ping Send ICMP ECHO_REQUEST packets to network hosts. (ping
<remote-host-name/ip) Network Troubleshooting
• route Show or manipulate the IP routing table. (route, route add,
route del)
• showmount Show mount information for an NFS server.
• wget Non-interactive network downloader
• ssh Secure Login (ssh <remote system name/ip>)
Network Management Commands
• tcpdump Dump traffic on a network. Prints out headers of packets that
match the boolean expression. (tcpdump eth0) packet sniffing and
Network Troubleshooting
• telnet User interface to the TELNET protocol, setting up a remote console
session. (telnet <remote system name/ip>) Network Troubleshooting
• traceroute Print the route that packets take to the specified network host.
(traceroute <remote system name/ip>) Network Troubleshooting
• Network files
• /etc/hosts – the file contains a list of IP addresses and hostnames.
• /etc/nsswitch.conf - NSS stands for Name Service Switch and it controls the order in which lookups are
performed.
• The /etc/services - the file translates human-readable names into port numbers.
Network Management Commands
Communications commands (includes mail)
• elm Electronic mail.
• pine Program for internet news and e-mail, Can send documents,
graphics, local & remote messages.
• talk Lets two parties talk simultaneously.
• mail To send a mail
System Management Commands
Runtime level management
• exit Terminates the shell.
• halt Stop the system.
• init Process control initialization. (init 3)
• logout Log the user off the system.
• poweroff Brings the system down.
• reboot Reboot the system.
• runlevel List the current and previous runlevel.
• shutdown If your system has many users, use the command shutdown -h +time ‘<message>`, where
time is the time in minutes until the system is halted, and message is a short explanation of why the
system is shutting down.
Ex: # shutdown -h +10 'We will install a new disk. System should be back on-line in three hours.‘
System Management Commands
• passwd Set a user's pass word. (passwd, passwd <username>)
• quota Display users' limits and current disk usage. (quota, quota
<username>)
• quotaoff Turns filesystem quotas off.
• quotaon Turns filesystem quotas on.
• quotacheck Used to check a filesystem for usage, and update the
quota.user file.
• edquota Used to edit user or group quotas. (edquota <username>)
System Management Commands
• su Single user login. (su -)
• useradd Create a new user or update default new user information. (useradd –g
<group> -s <shell> -c <comment> –d <home directory> <username>
• userdel Delete a user account and related files. (userdel <user name>)
• usermod Modify a user account.
• users Print the user names of users currently logged in.
• wall Send a message to everybody's terminal. (wall “text message”)
• who Display the users logged in.
• whoami Print effective user id.
System Management Commands
System Time
• cal Calendar. (cal, cal 2005)
• date Print or set the system date and time. (date, date
MMDDhhmm[[CC]YY][.ss])
• hwclock Set or read the hardware CMOS clock.
• uptime Reports how long the system has been running.
Linux Job & Process Management
Listing process and displaying information
• ps Get the status of one or more processes.
• PPID-parent process ID ; PID-process ID
Eg: ps ax |more to see all processes including daemons
Eg : ps –ef | grep <process>
• ps command with options
• ps -e - Display all processes.
• ps -ef - Display all processes with a full format listing.
• ps -eH - Display a process tree.
• ps -e —forest - Display a process tree.
• ps -u username - Display processes running as username
• Instead of running the ps command and piping its output to the grep command, you can use the pgrep command, which is built for just
this purpose. It prints the PIDs of commands that match the search pattern you supply to it.
• Other commands to view running process
• pstree - Display running processes in a tree format.
• top - Interactive process viewer.
• htop - Interactive process viewer. This command is less common than top and may not be
Linux Job & Process Management
Running Processes in the Foreground and Background
• Ctrl-c - Kill the foreground process.
• Ctrl-z - Suspend the foreground process.
• bg [%num] - Background a suspended process.
• fg [%num] - Foreground a background process.
• kill [%num] - Kill a process by job number or PID.
• jobs [%num] - List jobs.
Killing Processes
• Ctrl-c - Kills the foreground process.
• kill [signal] pid - Send a signal to a process.
• kill -l - Display a list of signals.
Linux Job & Process Management
Memory Usage
• The top command also displays memory utilization for a system.
• You can also use the free command to display memory usage. To display in units of megabytes, use the -m option. To display in
gigabytes, use the -g option. Use the –t option to display the totals.