A UNIX/Linux in a nutshell

A UNIX/Linux in a nutshell
Introduction Linux/UNIX
Tommi Bergman
Computational Environment & Application
CSC – IT center for science Ltd.
Espoo, Finland
bergman – p.1/23
Shell Command line interface
Directories and files
Directory structure
Contents of a directory
Linux/UNIX Small utilities
Linux/UNIX redirecting
Linux/UNIX pipes
Reading text files
More on text files
Moving around in the directory tree
Environment variables
Creating directories
Adding and Removing
Moving and copying files
Shell Scripting
Directory and file permissions
Config files .emacsrc .bashrc etc.
Permissions: adding or removing
Command Reference
Online help on commands
bergman – p.2/23
Introduction to UNIX/Linux
UNIX/Linux are multitasking operating systems
So systems running multiple programs and hosting multiple users at
a time
usually textual command line shell for command input
you can also use GUI(graphical user interface)
Same basic system utilities are typically found on both systems
These system utilities include small programs for e.g. showing
contents for a directory: ls, for copying files: cp, finding text in files:
bergman – p.3/23
The Shell or command line interface
Program that enables the user to interact with the computer
Most common shells are called bash and tcsh, main differences
between shells being syntax and builtin commands
Usually commands follow a basic command structure
command -options targets
Linux/UNIX command prompt is case sensitive, so cp
Command is sort of a verb and handles the target according to
command names are usually acronyms of actual words as cp for
bergman – p.4/23
Directory structure
The filesystem is organized in a tree-like hierarchial directory
The uppermost directory in a filesystem is called the root directory
Files have a place in one of the directories (tree branches)
Command pwd will show you current working directory
[email protected] bergman> pwd
bergman – p.5/23
Contents of a directory
Command ls(list directory) will show the contents of current
directory with different switches you can change the output format
[email protected] linuxkurssi> ls -la
drwxr-xr-x 2 bergman csc 1024 Jan 30 15:47 .
drwxr-xr-x 4 bergman csc 512 Jan 28 15:02 ..
-rwx------ 1 bergman csc 41988 Feb 1 13:24 a.out
type, permissions,links, owner, group, size, date, name of the file
bergman – p.6/23
Moving in directory tree
Whereas in Graphical user interface you use mouse to navigate
through the directories, the cd command is used traverse the
directory tree in a command line interface
To enter subdirectory inside the current directory cd subdir , for
entering parent directory cd ..
also explicit path is possible e.g.
cd /wrk/userid/new model/
bergman – p.7/23
Directory and file handling commands
To add a directory mkdir and to remove directory rmdir
rmdir will only remove directories without content
To remove a file command rm is used
can also be used to remove directories with option -r which will
erase the directory and all its subrirectories
When creating files or directories the special characters are tricky,
best to avoid using them e.g. ’ ? ’, ’ - ’, ’ ’
one should especially avoid ,/,? and # since they have special
functions on most shells
bergman – p.8/23
Moving and copying files
For moving files around the filesystem there are few small
For copying cp
-options oldname new(name/dir)
to copy entire folder one must use -r option to recurse the folder
Also wildcards: all or part of the filename can be substituted with so
called wildcards ,?,[a-l]
For moving or renaming
mv -options oldname new(name/folder)
Command history
up/down arrows to flick through the previous commands
If you have started an unwanted program which takes long to
complete one can stop a program execution by pressing Ctrl-C
bergman – p.9/23
Directory and file permissions
All files and directories have a given set of permission attributes
The permissions are read, write and/or execute:
-rw-r--r-- 1 bergman csc 0 Jan 29 11:04 stuff.txt
There are three groups of users
The owner, user that has created the file
The group, user belongs to at least one group. At CSC, usually
group is named after the project leader
And the others, that is all other users
One can see the users own groups with command groups
bergman – p.10/23
Permission adding or removing
To change the permission there is command
chmod ugo(+/-)rwx
to add reading rights to user and group:
chmod ug+r
chmod +r
User can also give permissions to users of these groups to use the
files that he/she owns and to change the group of the file chgrp
Be careful what permissions you give to other users: If you give
write permissions to group others, anyone can destroy your
important data
Usually there is no need to give write permissions to the group
’other users’
bergman – p.11/23
Online help on commands
There are many small commands there is also help features in the
For shell basic utilities you can find help with info command
which will give menu of basic utilities which can be browsed with
cursor keys
info coreutils
The man command works for most programs
It prints out manual pages for command in question (e.g. man ls)
bergman – p.12/23
Linux/UNIX small utilities
There are a small utilities that make life a little easier
If you have misplaced important file for which you only know the
name or part of it. This will find you all files named “result”
[email protected]:/wrk/yourid> find -name "result" -print
For finding text in files
[email protected]:/wrk/yourid> grep pattern file
usually you don’t want to copy the same file to many different
directories. For this purpose it is possible to create symbolic links:
ln -s actualfile linkname
bergman – p.13/23
Linux/UNIX small utilities continued
for selecting columns from a file
[email protected]:/wrk/yourid> cut -f 2-3 file
You have a list of names you want sort in alphabethical order?
This will sort the lines in a file starting in column m and ending in
column n sort -k m-n file
For substituting parts of the text in (a) file(s) sed e.g. to change all
the words ’dry’ to word ’wet’
[email protected]:/wrk/yourid> sed ’s/dry/wet/g’ blah.txt
One can also create aliases for often used commands
alias lo logout
bergman – p.14/23
Linux/UNIX redirecting
Shells have a possibility to redirect files or output to commands
for giving a file instead of reading from keyboard
[email protected]:/wrk/yourid> cat < names.txt
for directing output to a file
To print all lines that have string ’House’ from file names.txt to file
[email protected]:/wrk/yourid> grep House names.txt > house.txt
to append output to end of a file
To add all lines that have string ’Road’ to file house.txt
[email protected]:/wrk/yourid> grep Road names.txt >> house.txt
bergman – p.15/23
Linux/UNIX pipes
It is also possible to redirect output of one program as input to
Almost all commands print result of their actions to “standard
output” which can be redirected to file or as input to other programs
using the pipe operator
To print out first 10 lines with a string ’black’ of the file output.txt:
[email protected]: /testdir> grep black output.txt | head [-10]
bergman – p.16/23
Reading text files
Several programs to printout textfiles: less (and more) printout
text a screenful at a time, cat will put all on the screen in one go
More and less quite similar programs, only More is a lot more
primitive and moving about the document is more complex
Less can be controlled with arrow and page up/down keys
There is also search option with / -key
To print a file to terminal screen use
cat [option] file
cat can also be used to join text files into one
cat file1 file2 file3 > bigfile
for splitting up a file there is
split -l 10 file.txt
bergman – p.17/23
More on textfiles
For printing only a part from the start or end of the file one can use
tail/head [option ] file
most common option is -n, where n is the number of lines to print
by combining these two one can print some intermidiate part of the
file head -100 output | tail -5
will print lines 95-100
To evaluate the size of textfiles we have
wc (wordcount)
it will tell you how the number of lines, words and bytes the file or
files have: 454 1937 15828 Nutshell.tex
For editing there is vi and emacs more on these later
bergman – p.18/23
Environment variables
The shell stores information about the current system in
environment variables
similar to variables in programming languages: environment
variable is some data that has a name assigned to it
They can be essential to the system opreation or for users own
needs purposes
$PATH command search path, $HOME home directory,
$LPDEST current printer or $IMPORTANT FILES
bergman – p.19/23
Environment variables continued
To see all current environment variables
They are userdefinable, but to add you must include the current
variable to the list of values
tcsh:setenv PATH ($PATH /some/new/directory/)
bash:export PATH=$PATH:/some/new/directory/
NB! environment variables are used with a $-sign, but are set
bergman – p.20/23
Shell scripting
Commands can be written to a text file to create scripts, so it is
possible to automate even complex tasks
They can be executed using shell-interpreter(sh scriptfile)
or by changing file permission to execute
Most of the normal programming language constructs are available
as e.g If-then-else for conditional branches and for-loops
for f in
echo sorting file $f
cat $f sort
Small example:
echo sorted file has been output to
bergman – p.21/23
Config files .emacsrc .bashrc etc.
Config files are often called the dot-files for their naming
conventions(.config) and stored at the users home directory
These can include almost any information from work directory to
redefining keyboard commands
When logging on the shell will process .login and .bashrc files
These are normal script files
They include usually environment variables and aliases, advanced
users may include many automated tasks to be run when logging on
bergman – p.22/23
Small command reference card
cd folder
move to folder
show contents of folder
remove file
for copying files
remove directory
make directory
less file
print lines with certain string
change permissions
show file screenful at a time
export PRINTER=spoolprint
set PRINTER spoolprint
find -name ’foot’ -print
ln -s file linkname
sort file
(bash) set environment
(tcsh) variable PRINTER
find file with ’foot’ in the name
show environment variables
create a link to file
sort lines in file
secure copy
head -n file
show n first lines of file
tail -n file
show n last lines of file
cut -f n-m file
cut columns n to m from file
bergman – p.23/23