-
Unix Programmer's Manual -- The book that is recommended to read along.
-
echo
prints what users type on the screen. When typing a password,echo
is turned off. -
CONTROL
==CTL
/CNTL
/CTRL
RETURN
==ctl-m
ctl-d
means no more inputctl-g
rings the bell on the terminalctl-h
is backspacectl-i
is tab
-
DELETE
==RUBOUT
/BREAK
/INTERRUPT
-
DELETE
stops a program immediately by pressingctl-c
-
Case distinctions matter, use lower case
-
The prompt in the shell uses a dollar sign or a percent sign.
$
means the user is non-root
-
If each letter is typed twice in the terminal, which is strange, you can read the commmand
stty
(set terminal options) in Section 1 of the Manual- Some UNIX commands:
who
-- list users that are using the machinewho am i
shows more details thanwhoami
stty -tabs
-- the system convert tabs into the right number of spaces
- Some UNIX commands:
-
For creating files: type
vi
/emacs
/ed
to use text editors -
ls
list names of all files in the current folderls -t
/ls -l
/ls -lt file-a
/ls -u
/ls -r
-
For printing files,
cat
/pr
-
For editing files,
mv
/cp
/rm
-
wc file-a
outputs:12 34 546 file-a
, 12 is line number, 34 is word number and 546 is character number. (line-word-chars) -
grep abc file-a
shows the line that matchesabc
in file-a, the search is line-by-line -
grep -v abc file-a
shows the lines that don't matchabc
in file-a (opposite to the above ) -
grep xxxx file-a file-b
can search several files at the same time -
sort file-a
, the sorting is line-by-line-
sort -r
reverse normal order -
sort -n
numeric order -
sort -nr
reverse numeric order -
sort -f
perform case-insensitive sorting -
sort -u
remove duplicate lines before sorting -
sort file-a -o file-b
write sorted output to file-b instead of dispaying it on the terminal
-
-
tail
-- last 10 lines of the file-
tail -n 2 file-a
/tail -2 file-a
print the last two lines of the file -
tail +2 file-a
prints the file starting from the second line -
tail -f
(follow) output appended data as file grows (used for monitoring logs or viewing real-time updates) -
tail -q file-a file-b
(quiet) suppress header info when viewing multi files -
tail -c 100 file-a
display last 100 bytes of the file
-
-
cmp
compare two files byte by byte, return the first byte position at which the files differ-
cmp -b a.txt b.txt
print differing bytes in octal -
cmp -i 100 a b
ignore the first 100 bytes of both files -
cmp -l
==cmp --verbose
-
cmp -s
==cmp --quiet
/cmp --silent
-
-
diff a.txt b.txt
compares two files line by line and display differences between two files, shows what actions(add/delete) should be taken to make file-a become file-b-
-u
/--unified
displays in a more user-friendly way -
-c
/--context
displays in a different way which provides additional context around the lines -
-r
/--recursive
shows difference of files and sub-folders(depth-1) in two folders, if there are same file names in two folders, then the content of files will be differed line by line. -
-q
/--brief
return xx and xx differ if two files differ, otherwise return None -
-i
/--ignore-case
perform a case-insensitive comparison -
-w
/--ignore-all-space
ignore all whitespace differences
-
-
sed
is stream editor, which changes the output of a file but does not change the file itself.-
sed 's/abc/new_abc/' file-a
replace the first abc with new_abc of each line in file a -
sed 's/abc/new_abc/2' file-a
replace the second abc with new_abc of each line in file a -
sed 's/abc/new_abc/g' file-a
replace all the abc with new_abc of each line in file a -
sed 's_abc_abcde_2g' file-a
replace all the abc with abcde since the second abc of each line in file a -
sed 's/abc/{&}/g' file-a
replace all abc with {abc} in file a -
sed 's/abc/{& &}/g' file-a
replace all abc with {abc abc} in file a -
sed '3 s/abc/new_abc/' file-a
replace the first abc with new_abc of the 3rd line in file a -
sed '2 d' file-a
delete the 2nd line of file a -
sed '5,$ d' file-a
delete the fifth line of file a to the end (only the first four lines of file a will be the output, file-a has no change) -
grep abc file-a
==sed -n '/abc/ p' file-a
-
grep -v abc file-a
==sed -n '/abc/ !p' file-a
-
sed '/unix/ i "Add a new line"' file-a
insert a line "Add a new line" before there is unix in each line of file-a -
sed '/unix/ c "Add a new line"' file-a
replace the line of file-a with "Add a new line" when there is unix in it -
sed 'y/ul/UL' file-a
replace all the lowercase of u and l with U and L in file-a
-
-
awk
can output a file using some special characters to make columns-
awk '{print $1}' file-a
output the first column of file-a (the default delimiter is space) -
awk -F"," '{print $1}' file-a
print the first col of file-a using "," as the delimiter -
awk -F"," '{print $1, $4, $7}' file-a
print the 1st, 4th, 7th column of file-a using "," as the delimiter -
awk -F"," 'BEGIN{sum=0}{sum=sum+$5;print sum}' file-a
add up the sum of the fifth col of file-a and prints the sum on every line (when the 5th col of the line is not a number or there is no 5th col in the line, then the value of the 5th col will be zero by default) -
awk -F"," 'BEGIN{sum=0;counter=0}{sum=sum+$5;counter=counter+1;print sum,counter}' file-a
prints sum and counter of each line in file-a -
awk -F"," 'BEGIN{sum=0}{sum=sum+$5}END{print sum}' file-a
print out the final sum of adding up all the value of the fifth col of the each line in file-a -
awk -F"," 'BEGIN{sum=0;counter=0}{sum=sum+$5;counter=counter+1;print sum,counter}' file-a
-
awk -F"," 'BEGIN{sum=0;counter=0}{sum=sum+$5;counter=counter+1}END{print sum/counter}' file-a
prints out the average value of 5th col of each line in file-a -
if the first line of a file is actually a table header, then you can set
counter=-1
in the above command
-
-
Filename shorthand is a service of the shell. If you want to turn off the special meaning of "*,?" etc, then enclose them in single quotes or precede the character with a backslash.
-
ls '?'
-
ls \*
-
-
Difference between
ls *
andecho *
ls *
list all the files and folders in the directory with a line break for each item, whileecho *
prints out all the names of files and folders continously without any space or line breaks -
Difference between
ls /
andecho /
ls /
list all folders in the directory whileecho /
only prints out "/" -
cp file-a file-b > file-c
copy all the contents of file a and b into file c by overriding file c -
cp file-a file-b >> file-c
copy all the contents of file a and b into file c by appending it to the end of file c -
sort < file-a
sort the contents in file-a -
who;date
execute commands in sequence -
who && date
execute commands in sequence after the first command runs successfully, if not, prints out error and then stop. -
top &
run the command in the background -
stty
list settings for the shell -
Some shell variables:
$PS1
represents the primary prompt string;$PATH
,$HOME
,$TERM
-
Use abbreviation for variables is ok, for example,
d=$HOME;echo $d
-
Everything in UNIX is a file.
-
File is a sequence of bytes.
-
A byte is 8 bits long.
-
-
$ ls -l junk
-rw-r--r-- 1 you 36 Sep 27 06:11 junk
$From above, junk is a file with 36 bytes created by you.
-
od file-b
orod -c file-a
prints out all the bytes in a file.(-c means interpret bytes as characters) (od is octal dump) -
od -x file-a
prints in hex,-d
in decimal. -
After each line, there is the ascii newline character, which is
012
in octal value.-
012
is\n
new line in octal value -
010
is\b
backspace -
011
is\t
tab -
015
is\r
carriage return
-
-
The kernel echoes RETURN as a carriage return and a newline, but stores only the newline in the input.
-
"CRLF" is carriage return and linefeed.
-
There is no end of file character in UNIX files. The kernel keeps track of file lengths.
-
When
read
is called, "zero byets being returned" means end-of-file. -
ctl-d
sends what you have typed to the terminal, if you typed nothing, then it looks like the end of file. -
The types of files does not be determined by names, but by the certain binary number written in the beginning of files.
-
The current directory is an attribute of a process.
-
du
disk usage -- how much disc space is consumed by files in a folder, including all its subdirectories. -
root
is the special user that carries super-user privileges. -
ls -l file-a
shows the permissions info of file a,ls -lg xxx
shows the group of the user instead of user name. -
File permissions:
rwx
, file type:-
/d
-
ls -l /bin/passwd
-rwsr-xr-x 1 root xxxxxxxxsr
means when the file is executed, the current user is given permissions to eit the file. In this case, any user can run thepasswd
command to change its own password.sr
is "set-uid" to root. It's a great idea to solve security problems. For example, updating a game program for players. But it needs the "set-uid" file to be correct. And the permissions of the file should be-
"only root user has 'write' access"
-
"everybody has 'execute' access"
-
-
For
chmod
, 4 for read, 2 for write and 1 for execute permission.-
chmod +x command-a
turn on a permission -
chmod -x command-a
turn off a permission
-
-
A file has several components:
-
name
-
contents
-
admin info, such permissions and modification times -- stored in "i-node"
-
In inode, there are three times:
-
last modified of the file
-
last used of the file
-
the time that the inode itself was last changed (eg. set permissions)
-
-
ls -i file-a
shows the i-number in decimal
-
-
ln
is to give more than one names to the same file. Their i-number is the same. -
df
disk free space -- shows available space on the mounted file subsystems. -
tee file-a
both prints output and writes output to file-a, eg.who | tee temp.txt
-
Commands
-
simplest command is a single word
-
a command ends with a newline or a semicolon ";" or ampersand "&"
-
date;who | wc
is different from(date;who) | wc
-
sleep 5 && date
is different fromsleep 5 & date
-
(sleep 5; echo tea is ready) &
-
cat file-a | grep abc &
==(cat file-a | grep abc) &
-
-
Command arguments and shell metacharacters
-
<, >, |, ; and & are not arguments
-
so are the characters that are used as regex, eg. *, ?
-
double quotes the special characters to prevent them being interpreted
-
so does a backslash \ do the job
-
echo "***"
==echo \*\*\*
-
p1 && p2
run p1; if successful, run p2 -
p1 || p2
run p1; if unsuccessful, run p2 -
echo $(p1)
execute command p1 -
echo */*
prints all files and folders in depth 1 and 2 -
grep "^-" file-a
find the line that begins with "-" in file a -
touch "\a"
create a file with name "\a"
-
-
create new commands
$ echo 'date' > a.txt; sh a.txt // single or double quotes will work
Thu Jun 1 01:12:14 EDT 2023
$ sh < a.txt
Thu Jun 1 01:12:14 EDT 2023
$ chmod 755 a.txt; ./a.txt
Thu Jun 1 01:12:14 EDT 2023
$ echo "chmod +x $1" > cx; sh cx cx // use single quotes here, double quotes won't work
$ ./cx a.txt; ./a.txt
Thu Jun 1 01:12:14 EDT 2023
-
The commands above can change to
$ echo 'chmod +x $*' > cx
so that it can take up different numbers of arguments. So does the commandecho 'wc -l $*' > lc
. -
$*
means you can take up flexible number of variables and args. -
echo grep "$*" file-a
here, because $* is double quoted