Beruflich Dokumente
Kultur Dokumente
version 1.0
Editor: Ahmed Baraka
Document Purpose
This document is edited to be a quick reference of Linux essential commands. It can be used
by Linux beginners as a reminder of basic Linux commands usage. It cannot be used to learn
Linux from scratch.
The document is oriented based on the required task, the command(s) to do the task, basic
syntax of the command, and examples. No explanation will be presented.
Usage Terms
Anyone is authorized to copy this document to any means of storage and present it in any format to any
individual or organization for non-commercial purpose free.
No individual or organization may use this document for commercial purpose without a written
permission from the editor.
There is no warranty of any type for the code or information presented in this document. The editor is
not responsible for any loses or damage resulted from using the information or executing the code in
this document.
If any one wishes to correct a statement or a typing error or add a new piece of information, please
send the request to info@ahmedbaraka.com . If the modification is acceptable, it will be added to
the document, the version of the document will be incremented and the modifier name will be listed in
the version history list.
Page 1
Version History
Version
1.0
Date
Updates
25-June-2009
Initial document.
Resources
Resource Name
Page 2
Contents
Introduction ___________________________________________ 7
Unix variants _______________________________________________7
GNU/Linux distributions ______________________________________7
Page 3
Page 5
Page 6
Introduction
Unix variants
Unix, GNU/Linux, Solaris, IRIX, HP-UX, FreeBSD, OpenBSD, NetBSD, Darwin (Mac),
and more...
GNU/Linux distributions
Page 7
Ubuntu, Edubuntu, Debian, RedHat, Fedora, Slackware, SuSE, Darwin, and more...
Family tree of the GNU/Linux distributions
Getting Started
Virtual Consoles
Changing password
passwd
Logging-In
date
u
cal
y
cal 2010
bc
seq
w
s 'b'
seq 7
seq -5 5
Page 8
seq 1 2 10
# from 1 to 10 increment by 2
# separated by spaces
Getting Help
man
info
apropos
whatis
LinuxQuestions.org
exact matches
man ls
man k copy
apropos copy
man f copy
ls -- help
info cp
whatis who
Handy shortcuts
# up(down)_key
# <something-incomplete> TAB
completes path/file_name
# Ctrl+a
# Ctrl+e
# Ctrl+d
# Ctrl+k
# Ctrl+y
Page 9
pwd
cd
ls
regular file
directory
symbolic link
named pipe
socket
cp
rm
mv
mkdir
rmdir
cd ..
# one level up
cd
# home directory
cd -
# previous directory
ls a
ls l
# long listing
ls R
ls d
ls lh
ls ld
ls i
ls S
ls t
ls r
ls l --time-style=STYLE
cp file1 file2
Page 10
cp p file1 file2
cp file1 dir1
cp file1 file2 dir1
cp ./dir1/* dir2
cp r dir1 dir2
cp a dir1 dir2
mv file1 file2
mv file1 ./dir1
rm file1
rm -r dir1
rm -rf dir
rm -- -myfile
touch {doc,sheet}_{jan,feb,mar}
touch file{0..6}.txt
file
file myfile
Viewing Files
cat
less
ENTER
/text
quit
Page 11
ln
ls l in case of soft link, it displays the link name and the referenced file
ln s filename
du h s ~
updatedb or locate -u
locate "*.png"
locate "*.png" -q
# supress errors
# case-insensitive search
-ctime n
-user uname
-group gname
-o
-not
-perm mode
-perm -mode
ALL of the permission bits mode are set for the file.
Page 12
Any of the permission bits mode are set for the file.
-perm +mode
-regex pattern Full path filename (not only filename) matches regular expression
pattern.
-mtime n
-mtime +n
-mtime -n
-mmin n
daystart
measure time in the options above from the beginning of the current
day instead of 24 hours ago.
-newer <file>
# excatly 1 MB
Page 13
Archiving Files
tar tf archive_name
Compression Utilities
gzip v file(s)
bzip2 v file
better compression
gunzip filename.gz
gzip d filename.gz
gunzip c filename.gz
bunzip2 v file
Page 14
passwd
su username
su - username
whoami
groups, id
users, who, w
# who is logged on
last
# login/reboot history
Changing Permissions
umask
Page 15
Special Permission
chmod Xnnn
suid and sgid are effective on executable files: the program runs with permissions of the
owner, not the executor.
sticky bit: files in the directory can be deleted by the owner or the root, regardless of
the directory write permission.
Sgid: files created in the directory will inherit its group affiliation from the directory,
rather than the user.
ls -l /usr/bin/passwd
-r-s--x--x
1 root root
ls -ld /tmp/
drwxrwxrwt
10 root root
Page 16
[a-z]
[^a-z]
ls file*
ls ??file
ls file[1-9]
ls file[^6-0]
History Tricks
history
Brace Expansion: { }
Arithmetic: $[]
\ backslash makes the next character literal and can be used as last character on line to
continue command on next line.
\b
Backspace.
\e
Escape.
\f
Form feed.
\n
Newline.
\r
Carriage return.
\t
Horizontal tab.
Page 17
\v
Vertical tab.
\\
Backslash.
\NNN
Filename Expansion Characters: used with commands handling files and directories:
*
[list]
[!list] except the characters in the list. For example: a[!b]c matches aac a-c adc, but
not abc.
echo "This system's name is $(hostname)"
echo $'Note the space below\n'
# equivalent to $[$X*$Y]
VARIABLE=value
echo $VARIABLE
To see list of the local variables that configure the shell, see the Shell Variables section
of the bash man page.
PS1
the prompt
\d
date
\h
short hostname
\t
time
\u
user name
\w
\!
\$
HISTFILESIZE
COLUMNS
LINES
Page 18
HOME
EUID
PS1="\u\w\$"
Aliases
alias
alias lf="ls Fca"
alias rm="rm i"
\rm r myfile
Type
type
type rm
tyupe cate
Environment Variables
export VARIABLE=value
HOME
LANG
PWD
LESS
export EDITOR
export EDITOR=/usr/bin/pico
EDITOR=
which
which xboard
Page 19
su -
su
graphical terminals
executed scripts
Login Sells
/etc/profile
/etc/profile.d
~/.bash_profile
~/.bashrc
/etc/bashrc
Non-Login Shells
~/.bashrc
/etc/bashrc
/etc/profile.d
Logout Script
~/.bash_logout
script <filename>
exit
Page 20
>
tee - read from standard input and write to standard output and files
Page 21
ENTER
/text
quit
head
tail
head -n 5 .bash_profile
tail -n 5 .bash_profile
tail -n 5 -f mylogfile
cut
-d
-f
-c
wc
-l for only line count
-w for only word count
Page 22
Sorting Text
sort
-r
-n
-f
-u
-t c
-k X
# sort by uid
Eliminating Duplicates
Comparing Files
diff
diff file1 file2
aspell
aspell check letter.txt
aspell list < letter.txt
aspell list < letter.txt | wc -l
Page 23
Converting Characters
Combining Files
paste combines files horizontally and separate the pasted lines by TAB by default.
paste d: ids.txt names.txt > merged.txt
expand
Regular Expressions
Wildcard Characters
another single character
.
[abc]
[a-c]
Modifiers
number of the previous character
*
\+
\?
\{i\}
\{i,\}
Anchors
match the beginning or end of a line or word
^
\<
\>
Other expressions
[:alnum:]
Alpha-numeric characters 0 to 9 OR A to Z or a to z
Page 24
[:alpha:]
[:cntrl:]
Control characters
[:digit:]
Digits 0 to 9
Linux Fundamentals by Commands and Examples
[:graph:]
Graphics characters
[:print:]
Printable characters
[:punct:]
[:space:]
[:blank:]
^S[^ ]* R
# the last name begins with S and first name begins with R.
^[M-Z].*[12]
# the last name begins with a letter from M to Z and where the
#
phone number ends with a 1 or 2.
'^..$'
'^.\{17\}$'
\(.*l
Except word anchors, basic regular expressions requiring a preceding backslash do not
require backslash
Used by:
o
egrep
grep E
awk
to
to
to
to
to
search case-insensitively
print line numbers of matches
print lines not containing pattern
include the X lines after each match
include the X lines before each match
Page 25
sed
(stream editor) uses regular expressions in search string (but not in replace)
sed 's/\<[Cc]at\>/& and dog/g' petsfile # whatever found (Cat or cat), it will
# be replaced with cat and dog
sed '10,40s/cat/dog/g' petsfile
containing "end"
awk
All extended regular expressions work except curly brace counters. To use them, use -posix or --re-interval options.
awk ' { print } ' myfile
Page 26
Move by word: w, b
Move by sentence: ), (
Move by paragraph: }, {
Jump to line x: xG
Jump to end: G
Insert mode
o
Ex Mode
o
/, n, N Search
:%s/cat/dog/gi
change (c)
cut (d)
yank (y)
Page 27
Possible target:
Line
as in action
Letter
Word
Sentence ahead
Sentence behind
Paragraph above
Paragraph below
U undo all changes to the current line since the cursor landed on the line.
Visual Mode
:q
:set showmode
:set ic
:set nu
:set nonu
Page 28
Managing Processes
Listing Processes
-a
all processes except session leaders and processes not associated with a
terminal.
-A
-e
-H
-- sort some
c
C
r
R
s
S
T
U
u
v
-o CODE prints custom information where CODE taken from the following list:
Code
%cpu
%mem
Bsdstart
bsdtime
comm.
Egid
egroup
etime
euid
euser
fgid
fname
fuid
fuser
label
lstart
lwp
Page 29
Header
%CPU
%MEM
START
TIME
COMMAND
EGID
EGROUP
ELAPSED
EUID
EUSER
FGID
COMMAND
FUID
FUSER
LABEL
STARTED
LWP
Description
cpu utilization of the process in "##.#" format. (alias pcpu)
physical memory in percentage. (alias pmem)
time the command started.
accumulated cpu time, user + system. "MMM:SS"
command name (only the executable name)
effective group ID number of the process (alias gid)
effective group ID of the process. (alias group)
elapsed time since the process was started, [[dd-]hh:]mm:ss.
effective user ID. (alias uid)
effective user name.
filesystem access group ID. (alias fsgid)
first 8 bytes of the base name of the proces executable file.
filesystem access user ID. (alias fsuid)
filesystem access user ID.
security label (used for SE Linux context data).
time the command started.
lwp (light weight process, or thread)
Linux Fundamentals by Commands and Examples
ni
NI
nlwp
pgid PGID
NLWP
process
pid
ppid
psr
rgid
rss
PID process
PPID
PSR
RGID
RSS
ruid
ruser
s
RUID
RUSER
S
sched
sess
sig
sigcatch
sigignore
sigmask
start_time
stat
suid
suser
time
tname
tt
tty
vsize
vsz
SCH
SESS
PENDING
CAUGHT
IGNORED
BLOCKED
START
STAT
SUID
SUSER
TIME
TTY
TT
TT
VSZ
VSZ
Process statuses:
R
Runnable: executing
Uninterruptible sleep
ps e
ps ef
ps eH
Page 30
ps eLf
# to show threads
ps el
kill l
nice [-n adj ] command where adj between -20 (highest) and 19 (lowest)
renice 15 u john
jobs
You can use the following code example to test the commands:
(while true; do echo n B >> file.log; sleep 1; done) &
Page 31
Compound Commands
Put the list between () to run them all in a subshell (treat it all as a one command)
date; who | wc l >> mylogfile.txt
Scheduling a Process
at time <commands> commands entered one per line, terminate with Ctl+D
atrm [user|atJobID]
removes at jobs
at 8:00pm December 7
at 7 am Thursday
at now + 5 minutes
at midnight + 23 minutes
-l
-r
-e
edit. After exit from the editor, the modified crontab will be installed
Cronttab file:
o
Space delimited
Fields: minute, hour (0-23), day of month (0-31), month (1-12), and day of
week (0=Sun to 6).
Min
Hour
DoM
Month
DoW
Page 32
Commands
Handling Input
read
words are separated by default with space. IFS variable controls the separator.
#!/bin/bash
read p "Enter the words:" word1 word2 word3
echo "Word1 : $word1"
echo "Word2 : $word2"
echo "Word3 : $word3"
Parameter :%s\n" $1
Page 33
Using Functions
Arguments passed to a function are accessed by its positional parameters $1, $2 etc.
Exit Status
exit
Conditional Execution
<cmd1> || <cmd2>
else
fi
if [ $retval != 0 ]; then
echo "There was an error running the application"
exit $retval
fi
Page 34
Page 35
#!/bin/sh
# Guess the number game.
ANSWER=5
CORRECT=false
Disrupting Loops
continue
break
File Tests
file exists
-f
-d
-x
-h
-r
-s
-w
-O
-G
Page 36
String Tests
String operators:
-z STRING
-n STRING
STRING1 = STRING2
STRING1 != STRING2
-o OPTION
Logical Tests
Logical Operators
! EXPR
EXPR1 -a EXPR2
EXPR1 -o EXPR2
Comparison
Comparison Operators
arg1 OP arg2
Page 37