Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Linux Shell Commands

Linux

Uploaded by

kaysarahmed580
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Linux Shell Commands

Linux

Uploaded by

kaysarahmed580
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Cardiff School of Computer Science &

Informatics
(http://www.cardiff.ac.uk) (http://www.cardiff.ac.uk/computer-science)

Documentation & Facilities

Linux Shell Commands


The Shell is the command interpreter on Linux systems. This document
intoduces some of the basic features of the Shell and lists many of the
commands or programs available on the Linux computers in Cardiff School
of Computer Science & Informatics.

The Shell
The Linux command interpreter or shell is the program users interact with in a terminal emulation window. The
terminal emulation window can be one in the workstation's Graphical User Interface mate-terminal on Linux.
Alternatively, it can be an application such as SSH secure shell client or PuTTY on a Windows PC that's logged
into Linux over the network.

The shell used in the School of Computer Science & Informatics is bash Bourne Again Shell. There are other
shells available such as the Bourne Shell, the C-Shell and the TC-Shell, and you can choose to use a different
shell if you prefer. They all have similar characteristics but each has its own particular features. This document
assumes you are using bash .

Bash has the following features:

A command prompt which may be configured by the user. The default prompt is a dollar symbol preceded
by "bash" and the bash program's version number.

bash-2.05$

The shell, like other programs on Linux has an associated current directory. Programs running on Linux
use the current directory as the starting point when locating files. The shell command cd is used to
change the current directory to a different location in the Linux file system.
Commands are invoked by naming them. Most Linux commands are simply programs which are executed
by the shell. For example, to run the ls command which reads the the current directory and lists the
names of its files the following would be used.

bash-2.05$ ls

When you type a command name, the shell will check to see if the command is built-in and will otherwise
search a set of directories until it finds the program. This set is known as the search path. The search path
includes the current directory, your home directory and its subdirectory "bin". You can write your own
programs and invoke them simply by typing their names. If you store such a program in the directory ``bin''
it will be found and run no matter what your current directory is.
Commands often have argument strings which may, for instance, represent filenames. For example, the
below command changes the current directory to "bin" in your home directory. The tilde character means
your home directory to the shell.

bash-2.05$ cd ~/bin

Some commands need more than one argument. For example, the copy command takes two arguments
the file to copy and it's destination. This is shown below where fileA is copied to a new file, fileB.

bash-2.05$ cp fileA fileB

Some commands have flag or option argument strings usually beginning with ``-'' or ``-''. The flags modify
the behaviour of the program being invoked. The below command when invoked makes ls give a long
listing of files sorted by time of creation.

bash-2.05$ ls -lt

The shell will expand wildcards to match filenames in the current directory. For example, to give a directory
listing of the file with names "anything.c" use the following.

bash-2.05$ ls -l *.c

Most Linux commands and programs adhere to a concept of standard input and standard output. The
standard input is a stream of data which the program reads and the standard output is a stream of output
written by the program. Often these are both attached to the terminal so that input comes from your
keyboard and output goes to your screen. The shell lets you redirect the standard input and output.

bash-2.05$ cat < fileA

bash-2.05$ cat < fileA > fileB

The Shell has the facility to pipe the output of one program to the input of another. The pipe symbol is "|".
For example to count the number of words in fileA we can cat the file then pipe the output in the wc
program.

bash-2.05$ cat fileA | wc -w


405

You may assign aliases for commands or groups of commands that you may execute frequently or find
cumbersome to enter. For example we could assign an alias "countc" to count the number of C program
source files in the current directory using ls to list the files and wc to count the number of lines output.

alias countc="ls -l *.c | wc -l"

The shell has string and numeric valued variables.

bash-2.05$ x="Hello World!"


bash-2.05$ echo $x
Hello World!

Some variables are pre-set, e.g. $HOME is your home directory. Type set to see a list of assigned
variables.
Bash is an interpretive programming language with while loops, for loops, if-then-else statements and
many more. See the Linux on-line documentation for more details by typing the following command.
bash-2.05$ man bash

Scripts of shell commands can be written. These can be invoked in the same way as compiled programs
(i.e. just by naming them). For example, to create a script that counts the number of C program files in the
current directory we first create a file in ~/bin containing the following.

#! /bin/bash
ls -l *.c | wc -l

We must then make the file executable using the chmod command before we can run it like normal.

bash-2.05$ chmod +x ~/bin/countc


bash-2.05$ countc
45

The shell has ``job control''. Programs which don't require any terminal interaction can be run in the
background.

bash-2.05$ sort bigfile > sortedfile &


[1] 3470

The above puts the program sort in the background and the shell is available immediately for other
commands. The shell prints the job control number ("1" in this case) and the process identity number
("3470"). The special character Ctrl + z can be used to suspend a program which is running in the
foreground. Once stopped the bg command can be used to put the program in the background or fg
can be used to continue it in the foreground. If you have more than one job running in the background or
suspended, you can refer to them by their job number. To see your jobs and their job numbers use the
jobs command to list the status of all stopped or background jobs.
The shell has a history mechanism, it remembers the last few commands. The history command can
be used to list the last few commands executed along with a reference number.

bash-2.05$ history
515 cd ~
516 ls -lrt
517 ps -ef
518 pdflatex myfile.tex

In a workstation's terminal emulation windows, you can cut and paste from the history to rerun a
command. You can also use the symbol ``!'' to rerun any command from the history.

bash-2.05$ !518 # rerun command number 518 from the history


bash-2.05$ !ps # rerun the last command starting "ps"
bash-2.05$ !! # rerun the last command

See the manual page on bash for more details (type man bash ).

Bash has an additional mechanism which allows you to recall and edit previous commands using the keyboard
up-arrow key. If you press up-arrow, the last command re-appears on the terminal. Press up-arrow again to get
earlier commands. To rerun the command, press RETURN . To amend the command before rerunning it, use the
delete key to remove characters from the end or use the back-arrow key to reposition the cursor to delete or
insert characters within the command.

Shell Commands
Here is a summary of some of the commands available. For more details refer to the manual page of each
command. You can see these on-line by using the man command. Just type man followed by the name of the
command you want to see.

Logging out

Command Description

logout log out of a Linux terminal

Note, on a Linux workstation you will need to exit the Desktop Environment instead.

Files and Directories


These commands allow you to create directories and handle files.

Command Description Command Description

cat concatenate and print data file determine file type

lpr spool file for line printing mv move or rename files

cd change current directory find find files

lprm, cancel remove jobs from line printer pwd print working directory
queue
grep search file for regular
chgrp change file group expression

ls list and generate statistics rm, rmdir remove (unlink) files or


for files directories

chmod change file mode head give first few lines

mkdir make a new directory tail print last lines from file

cp copy file data just text justification program

more, page display file data at your touch update access and
terminal modification times of a file

lpq spool queue examination


program

File Editors
Editors are used to create and amend files.

Command Description Command Description

emacs GNU project Emacs pico easy text editor for vdus

xemacs emacs with mouse action pluma Mate GUI text editor

ex, edit line editor gedit GNOME text editor


Command Description

vi, vim standard text editor

Vi , pico and emacs are screen-based editors which run on a vdu or in a workstations terminal emulation
window; pluma , gedit and xemacs are graphical user interface (GUI) based editors with cut and paste and
mouse-controlled cursor positioning.

Manipulating data
The contents of files can be compared and altered with the following commands.

Command Description Command Description

awk pattern scanning and split split file into smaller files
processing language
expand, unexpand expand tabs to spaces, and
perl data manipulation language vice versa

cmp compare the contents of two tr translate characters


files
gawk pattern scanning and
paste merge file data processing language

comm compare sorted data uniq report repeated lines in a file

sed stream text editor join join files on some common


field
cut cut out selected fields of
each line of a file look find lines in sorted data

sort sort file data wc count words, lines, and


characters
diff differential file comparator

Compressed files
Files may be compressed to save space. Compressed files can be created and examined.

Command Description Command Description

gzip compress files zcat cat a compressed file

zmore file perusal filter for crt viewing of gunzip uncompress gzipped files
compressed text
zcmp, zdiff compare compressed files
uncompress uncompress files

Information
Manuals and documentation are available on-line. Go to our web site www.cs.cf.ac.uk/systems for web-based
documentation. The following Shell commands give information.

Command Description Command Description

apropos locate commands by keyword info displays command information


lookup pages online

man displays manual pages online yelp GNOME help viewer

Status
These commands list or alter information about the system.

Command Description Command Description

ps print process status statistics kill send a signal to a process

date print the date uptime display system status

quota -v display disk usage and limits last show last logins of users

reset reset terminal mode users print names of logged in users

du print amount of disk usage lun list user names or login ID

script keep script of terminal session vmstat report virtual memory statistics

stty set terminal options netstat show network status

groups show group memberships w show what logged in users are


doing
time time a command
who list logged in users
homequota show quota and file usage
printenv display value of a shell variable
iostat report I/O statistics

tty print current terminal name

Printing
Files can be printed using shell commands, using the GUI print manager, or direct from some applications.

You must specify a printer by name. Printers are called

Printer Printer
Name Location Name Location

tl1_lw Teaching Lab 1 (C/2.04) laser tl2_lw Teaching Lab 2 (C/2.05) laser
printer printer

tl3_lw Teaching Lab 3 (C/2.08) laser tl4_lw Teaching Lab 4 (C/2.10) laser
printer printer

Most commands which can be used to print files, expect the printer name to be given following a -P argument.

Files may be sent to the printers as simple text files or they may be processed in various ways for the laser
printers.

Command Description
Command Description

lpr -Pprinter send a file to a printer

dvips -Pprinter postprocess TeX file into Postscript and print on laser printer

a2ps -Pprinter format text file in PostScript and print on laser printer

Messages between Users


The Linux systems support on-screen messages to other users and world-wide electronic mail.

Command Description Command Description

write send a message to another local pine vdu-based mail utility


user
mail simple send or read mail program
wall send a message to all local users
thunderbird GUI mail handling tool on Linux

Networking
The School of Computer Science & Informatics is connected to the JANET Internet Protocol Service (JIPS), the
UK Universities' network.

These commands are used to send and receive files from Campus Linux hosts and from other hosts on JIPS
and the Internet, that permit such connections, around the world.

Command Description Command Description

ftp file transfer program telnet make terminal connection to


another host
tftp trivial file transfer program
ssh secure shell terminal or command
sftp secure shell file transfer program connection

rcp remote file copy rlogin remote login to a Linux host

scp secure shell remote file copy rsh remote shell

wget non-interactive network curl transfer data from a url


downloader
firefox web browser

google- web browser


chrome

These commands work only where the remote host permits such connections.

Programming
The following programming tools and languages are available.
General

Command Description Command Description

make maintain groups of programs nm print program's name list

size print program's sizes strip remove symbol table and


relocation bits

Command Description Command Description

cb C program beautifier ctrace C program debugger

gcc GNU ANSI C Compiler indent indent and format C program


source

cxref generate C program cross


reference

C++

Command Description

g++ GNU C++ Compiler

JAVA

Command Description

appletviewer JAVA applet viewer

javac JAVA compiler

eclipse Java integrated development environment on Linux

FORTRAN

Command Description

f95 GNU Fortran 95 compiler

Other Languages
(Not available on all systems).

Command Description Command Description

bc interactive arithmetic language python object-oriented programming


processor language

matlab maths package squeak smalltalk

gcl GNU Common Lisp php web page embedded language

perl general purpose language mathematica symbolic maths package

asp web page embedded language


Text Processing
TeX is a typesetting language used extensively in Linux and other operating systems for producing high-quality
printed documents. Another set of programs based on Troff is the standard Linux text formatting family used,
for example to format manual pages.
General Commands

Command Description Command Description

fmt simple text formatter acroread PDF viewer

evince GNOME PostScript previewer spell check text for spelling error

aspell interactive spelling checker

Troff

Command Description Command Description

eqn mathematical preprocessor for nroff text formatting language


troff
groff GNU troff interface for
tbl prepare tables for nroff or troff laserprinting

grap pic preprocessor for drawing pic troff preprocessor for drawing
graphs pictures

troff text formatting and typesetting


language

TeX

Command Description Command Description

tex text formatting and typesetting pdflatex latex formatter with PDF output

latex latex formatter xdvi dvi previewer

dvips convert a DVI file to POSTSCRIPT

Word Processing
LibreOffice is available on the School's Linux systems and attempts compatibilty with Microsoft Office.

Command Description

libreoffice start LibreOffice applications

Database Management
MySQL and Oracle are available.

Command Description

sqlplus run the Oracle SQL interpreter

mysql run the mysql SQL interpreter

sqldeveloper Oracle SQL Developer GUI interface

mysql-workbench GUI interface for MySQL

You might also like