System Programming

" One vision, one purpose. "

Copyright © Tony's Studio 2020 - 2022


Chapter One - Linux Commands

1.1 System Status

1.1.1 echo

echo is not Echo :P. Often used with redirect.

1
echo [string | $var]

By default, echo will print a new line at the end, we can supress this by add -n parameter.

echo can also use escape character by add -e parameter, but only apply to parameters quoted with “ “ or ‘ ‘.

1.1.2 read

read can read user input. It will regard its parameter as variables and store the readin to them. -p can be used to add a promt info. Prompt it won’t add a new line, though.

1
2
3
$ read -p "Please enter your name: " name [Tony]
$ echo $name
$ Tony

1.1.3 date

System date. -s parameter will set system date.

1
2
3
4
5
6
7
date [-s string]
%t - Tab
%H - Hour(00~24)
%I - Hour(00~12)
%M - Minute
%S - Second
%j - Day in year

1.1.4 ps

Look for process state.

1
ps [aux | lax]

1.1.5 top

Similar to task manager in Windows.

1
top

1.1.6 pidof

Look up for pid of certain process.

1
pidof [params] [service_name]

1.1.7kill

Send signal to given process.

1
2
3
4
5
kill [-s signal] pid # Send signal(optional) to process.
kill -l # List all signals available.
# for example
kill -9 5023
kill -HUP 1034

Common signals:

1
2
3
1) HUP: Reload process.
9) SIGKILL: Kill process.
15) SIGTERM: Terminate process in normal way

However, kill -9 can only kill process in user state, it could not kill zombie process, or process in kernel state.

1.1.8 free

Show memory usage. -h for human readable format.

1
free [-h]

1.1.9 who

List user info, including username, terminal device and login time.

1
who

1.1.10 su/sudo

Switch user.

1
2
su username
sudo command

1.2.1 pwd

Current working directory.

1
pwd

1.2.2 cd

Change directory.

1
cd

1.2.3 ls

List directory.

1
2
3
4
5
6
7
8
ls [-l] [-a] [-F]
-a : show hidden items
-A : show hidden items without . and ..
-l : show detailed properties
-F : append symbol to file name
-R : list recursively
-h : usually -lh, human readable
-i : show inode

Output format.

1
2
3
4
5
6
7
8
9
drwxr-xr-x 2 tonix tonix 4096 Nov 15 18:32 flip
1.1) d/-/l/... : directory/file/link, etc.
1.2) rwx rwx rwx : Owner Group Others
2) 2 : How many links point to this file
3) tonix : Owner
4) tonix : Group
5) 4096 : size in Byte, directory is 4096
6) time : Last modified time
7) flip : file name

1.3.1 cat

Show small text file. -n for line number.

1
cat [-n] file

1.3.2 more

Show large text file.

1
more file

1.3.3 head/tail

Show first or last n lines of a text file. 10 lines by default. -n to set how many lines. For head, negative means all but last n lines.

1
2
head [-n number] file
tail [-n number] [-f] file

For tail, -f means flush file if the file changes during time, e.g. log file.

1.3.4 stat

List file status.

1
2
3
4
5
6
7
8
9
$ stat semaphore.h
File: semaphore.h
Size: 1306 Blocks: 8 IO Block: 4096 regular file
Device: 820h/2080d Inode: 7919 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ tonix) Gid: ( 1000/ tonix)
Access: 2022-12-24 14:39:40.595546548 +0800
Modify: 2022-11-15 21:10:10.903008259 +0800
Change: 2022-11-15 21:10:10.903008259 +0800
Birth: -

We can use -c parameter to indicate which property to show. e.g. -c %s show only size as a number.

1.4 File Management

1.4.1 touch

Create an empty file or set file time.

1
2
3
4
touch [-a] [-m] [-d] file
-a : atime - read time
-m : mtime - modified time
-d : both atime and mtime

1.4.2 mkdir

Make directory, huh.

1
2
mkdir [-p] directory
-p : create recursively

1.4.3 cp

Copy file or directory.

1
2
3
4
5
6
cp [-p] [-d] [-r] [-i] [-a] src dest
-p : Keep properties
-d : Keep link property
-r : Recursively
-i : Interactively
-a : Equal to -pdr

1.4.4 mv

Move file or directory. It is… dangerous, huh. :(

1
mv src dest

1.4.5 rm/rmdir

Remove file or directory. rmdir only delete empty directory.

1
2
3
4
rm [-r] [-f] file
-r : Recursively
rmdir [-p] empty_directory
$ rmdir -p a/b/c # c is delete is empty. If b, a is empty, they will be deleted, too.

1.4.6 grep

Text pattern match.

1
2
3
4
5
grep [-c] [-i] [-n] [-v] pattern file
-c : show only line number
-i : ignore case
-n : show line number
-v : inverse match

1.4.7 find

Find file or directory.

1
2
find [directory] condition operation
-name : file name

1.4.8 chmod

Change mode. Well, you know, the rwxrwxrwx stuff, each is represented with an octonary.

1
2
3
4
5
chmod [option] file
chmod u+x file # add execute permission for user
chmod g-r file # remove read permission for group
chmod o+w file # add write permission for others
chmod 777 file # enable all permission for all

Go back to [4.1.3.3 Execute Permission](#4.1.3.3 Execute Permission)


" Do or do not. There is no try. "

Copyright © Tony's Studio 2020 - 2022

- EOF -