You are on page 1of 3

Linux/Bash – notes date: November 17, 2016

1 Unix utilities and shell builtins mkdir makes directories (mkdir p: with parents as passwd changes user password:
needed, no error if existing).
The GNU Core Utilities are the basic file, shell and d deletes an account’s password (makes it empty),
text manipulation utilities (marked here with a mount mounts a filesystem. e expires an account’s password,
star*) of the GNU operating system. n sets the minimum number of days between
mv* moves (renames) files:
password changes,
1.1 File system f does not prompt before overwriting, w sets the number of days of warning before a
i prompts before overwriting. password change is required,
cat* concatenates and prints files (tac does the x sets the maximum number of days a password
same in reverse, rev reverses lines characterwise): pwd* prints name of current directory.
remains valid.
A shows nonprinting characters, rm* removes files or directories:
su changes user ID or becomes superuser.
b numbers nonempty output lines, f never prompts,
n numbers all output lines, i always prompts, sudo executes a command as another user.
s suppresses repeated empty output lines. r removes directories and their contents. tput initializes a terminal or queries terminfo
chmod changes permissions of a file. ugoa controls rmdir removes (empty) directories. database.
whose permissions will be changed: the owner (u), uname prints system information:
the group (g), other users (o) or all users (a). + adds split* splits a file into pieces:
selected file mode bits, - removes them, = adds b puts „size” bytes per output file, a all information, in the following order:
them and removes the unmentioned bits. rwx n generates „chunks” output files. s the kernel name,
selects file mode bits for the affected users: read (4), n the network node hostname,
write (2), execute or search for directories (1). tar stores and extracts files from a tape or disk r the kernel release,
archive. v the kernel version,
chown changes file owner and group, chgrp m the machine hardware name,
changes only group ownership. c creates a new archive,
x extracts files from an archive, p the processor type,
cksum prints CRC checksum with byte count and t lists the contents of an archive, i the hardware platform,
shasum computes or checks SHA message digests: f uses archive file or device o the operating system.
a algorithm: 1, 224, 256, 384, 512, 512224 or 512256, z uses zip/gzip uptime tells how long the system has been running.
b reads in binary mode, j bzip2 compression
k does not replace existing files when extracting wall writes a message to all users, write sends a
c reads SHA sums from the „files” and checks message to another user.
them. tee duplicates pipe content.
who shows who is logged on, w shows who is
cp* copies files and directories: touch changes file timestamps. logged on and what they are doing, whoami prints
a never follows symlinks, preserves all attributes, umask set file mode creation mask. effective userid.
b makes a backup of each existing destination file,
d nevers follows symlinks in „source”, 1.4 Text processing
f removes an existing destination file if needed,
1.2 Processes
i prompts before overwrite, chroot changes root directory. awk is a pattern scanning and processing language.
r copies directories recursively, basename strips directory and suffix from
at schedules commands to be executed once, at a
l hard links files instead, filenames, dirname strip last component from file
particular time in the future.
s makes symbolic links instead, name.
t copies all „source” arguments into „directory”, bg resumes suspended jobs in the background.
u copies only when the source file is newer than comm compares two sorted files line by line.
cron is a daemon to execute scheduled commands.
the destination file. csplit splits a file into sections determined by
fg resumes suspended jobs in the foreground. context lines.
dd if=file of=file bs=bytes count=n converts and
copies a file: kill sends a TERM signal to a process.
join joins lines of two files on a common field. paste
if reads from a file instead of stdin killall kills processes by name. merges lines of files. cut* removes sections from
of writes to a file instead of stdout each line of files:
nice changes process priority.
bs reads and writes up to „bytes” bytes at a time
d uses „delim” instead of Tab for field delimeter,
count copies only „n” input blocks pgrep, pkill looks up or signals processes based on
f selects only these fields.
name and other attributes.
df reports file system disk space usage.
diff compares files line by line.
ps reports a snapshot of the current processes.
du* estimates file space usage:
fmt is a simple optimal text formatter, fold wraps
pstree displays a tree of processes.
a writes counts for all files, not just directories, each input line to fit in specified width.
h prints sizes in human readable format, time runs programs and summarizes system
head* outputs the first part of files:
s diplays only a total. resource usage.
c the first „num” bytes,
file determines file type. top displays linux processes.
n the first „num” lines.
fsck checks and repairs a Linux filesystem.
1.3 User environment iconv converts text from one character encoding to
fuser identifies processes using files or sockets. another.
clear clears the terminal screen.
ln* makes hard links between files (only in the same less is opposite of more, a file perusal filter for crt
file system, does not work with directories): ln -s env runs a program in a modified environment. viewing.
makes symbolic links instead. exit terminates the calling process. nl* numbers lines of files:
ls lists directory contents: finger is a user information lookup program. s adds „string” after line number,
a does not ignore entries starting with dot history displays the history list with line numbers. w uses „number” columns for line numbers.
F appends indicator to entries
h prints human readable sizes logname prints user’s login name. printf formats and prints data.
i prints the index number of each file mesg displays (or does not display) messages from sed is a stream editor for filtering and transforming
l prints permissions, number of hard links, other users. text.
owner, group, size, last-modified date as well.
r reverses order while sorting shuf* generates random permutations:
R lists subdirectories recursively e treats each „arg” as an input line,
S sorts by file size (largest first) i treats each number .. through .. as an input line,
t sorts by modification time (newest first) n outputs at most „count” lines,
r output lines can be repeated (with -n).

page 1/3 author: Remigiusz Suwalski


Linux/Bash – notes date: November 17, 2016

sort* sorts lines of text files: ping tests the reachability of a host on an IP 1.10 Other commands
network by sending ICMP ECHO_REQUEST:
g compares general numerical values, base32 base64 chcon date dir expand factor groups
h compares human readable numbers, c stops after sending „count” packets, hostid hostname id install link md5sum mkfifo
n compares string numerical values, n numeric output only, avoids to lookup symbolic mknod mktemp nohup nproc numfmt pathchk pr
r reverses the results. names for host addresses. printenv readlink realpath runcon shred stat
stdbuf stty sum sync timeout touch truncate tsort
strings prints the strings of printable characters in rdate sets the system’s date from a remote host.
tty unexpand unlink users vdir
files.
rlogin is an OpenSSH SSH client (remote login
tail* outputs the last part of files: program) 2 Keyboard shortcuts
c the last „num” bytes, route shows and manipulates the IP routing table. 1. Ctrl+A cursor to start of line
f outputs appended data as the file grows, 2. Ctrl+C halts current command
ssh is an OpenSSH SSH client (remote login
n the last „num” lines. 3. Ctrl+D log out of current session current
program).
tr* translates or deletes characters: command
D (bind address) 4. Ctrl+E cursor to end of line
1. tr abc xyz changes a to x, . . ., p (port) 5. Ctrl+L clear the terminal
c uses the complement of „set1”, X (X11 forwarding) 6. Ctrl+U erases whole line
d deletes characters, does not translate, 7. Ctrl+W erase one word in current line
traceroute is a computer network diagnostic tool
s replaces each sequence of a repeated character 8. Ctrl+Z stops current command
for displaying the route (path) and measuring
that is listed in the last specified „set” with a
transit delays of packets across an Internet Protocol
single occurrence of that character.
(IP) network.
uniq* omits repeated lines:
wget is a non-interactive network downloader.
c prefixes lines by the number of occurences
A, R specifies lists of file suffixes or patterns (when
d only prints duplicate lines, one for each group
wildcard characters appear) to accept or reject,
f avoids comparing first fields
b goes to background immediately after startup,
i ignores differences in case
c continues getting a partially-downloaded file,
s avoids comparing first characters
m turns on options suitable for mirroring: infinite
w compares no more than n characters
recursion and time-stamping,
vim a programmers text editor. np does not ever ascend to the parent directory
when retrieving recursively,
wc* prints newline, word and byte counts:
U identifies as „agent-string” to the HTTP server.
c prints the byte counts, w waits the specified number of seconds between
l prints the newline counts, the retrievals (see also random-wait).
m prints the character counts,
w prints the word counts. 1.7 Searching
xargs builds and executes command lines from find searches for files in a directory hierarchy.
standard input.
grep prints lines matching a pattern.
yes outputs a string repeatedly until killed.
locate finds files by names.
1.5 Shell builtins whatis displays one-line manual page description.
alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . whereis locates the binary, source, and manual
page files for a command.
cd changes the shell working directory.
- to the previous directory. 1.8 Documentation
echo* displays a line of text: apropos searches the manual page names and
e enables interpretation of backslash escapes, descriptions.
n does not output the trailing newline. man is an interface to the online reference manuals.
test checks file types and compares values.
unset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.9 Miscellaneous
bc is an arbitrary precision calculator language.
wait waits for process to change state.
1. echo 'obase=16;255' | bc prints FF,
1.6 Networking 2. echo 'ibase=2;obase=A;10' | bc prints 2,
3. scale=10 (after bc -l) sets working precision.
curl transfers a URL.
dc is a reverse-polish desk calculator. One of the
dig is a DNS lookup utility (domain information oldest Unix utilities, predating even the invention
groper). of the C programming language.
x simplified reverse lookups. cal, ncal displays a calendar and the date of Easter.
host is a DNS lookup utility. e displays date of Easter,
ifconfig configures a network interface. j displays Julian days,
m displays the specified month,
inetd is a super-server daemon that provides w prints the numbers of the weeks,
Internet services. y displays a calendar for the specified year,
netcat: arbitrary TCP and UDP connections and 3 displays the previous, current and next month.
listens. date prints or set the system date and time.
netstat prints network connections, routing tables, lp prints files.
interface statistics, masquerade connections, and
multicast memberships. od dumps files in octal.

nslookup queries Internet name servers sleep delays for a specified amount of time.
interactively. true, false does nothing, (un)successfully.

page 2/3 author: Remigiusz Suwalski


Linux/Bash – notes date: November 17, 2016

3 Programming in Bash 1. $0, $1, . . . : name of the script itself, the first, second, etc. argument.
2. $* and $@ denote all the positional parameters.
The shebang (#!) at the head of a script indicates an interpreter for execution,
3. $#: the number of positional parameters
as in #!/bin/bash. Lines starting with a # (with the exception of shebang) are
4. $?: exit status of the most recently executed command.
comments and thus won’t be executed.
5. $$: the process ID of the shell.
There are always three default files open: stdin (the keyboard, file descriptor 6. $!: the process ID of the most recently executed command.
0), stdout (the screen, file descriptor 1) and stderr (error messages output to the
Popular classes:
screen, file descriptor 2).
These streams can be redirected: cmd > file redirects to a file (overwrites), 1. [:alnum:], letters and digits,
cmd >> file appends instead, m>n (or m>&n) redirects a file descriptor to a file 2. [:alpha:], alphabetic characters
(or another file descriptor), &>file redirects both stdout and stderr to a file; :> 3. [:digit:], digits,
file truncates file to zero length and | (pipe) serves as a command chaining 4. [:lower:],
tool. 5. [:punct:], punctuation characters,
6. [:upper:].
Variables are case sensitive and capitalized by default. Variables can also contain
digits and underscores, but a name starting with a digit is not allowed. Example:
var=value; echo $vars prints values. Special variables: 3.1 Regular expressions

page 3/3 author: Remigiusz Suwalski