1. Useful Links
Command line for beginners
The command line tutorial
Linux kernel web
2. Folder & File
cd /
cd /etc
cd ~
cd home
cd ..
cd ../.. # ../.. means go to parent of parent directory
cd ../../etc
cd - # change to last directory
pwd # show current directory
mkdir /temp/tutorial # create directory
mkdir dir1 dir2 dir3 # create multiple indipendent directories
mkdir -p dir1/dir2/dir3 # p means parent, create hierarchy directories
mkdir -p -v dir4/dir5 # v means verbose, create and print
mkdir -pv dir4/dir5 #
mkdir “another folder” # create folder named with spaced words
mkdir -p “folder 6”/”folder 7”
ls -l
ls > output.txt
ls -a # show all files including hidden files
cp file1 file2
cp dira ~/dirb
cp -r dira dirb # copy all contents in dira to dirb, r means recursion
cp -i file1 dira # if file1 in dira will be overwritten, ask user to confirm, i means interactive
cp dir2/combined.txt . # copy the txt file to current directory
cat output.txt # cat means cancatenate, show the content of file
cat test1.txt test2.txt test3.txt # show block of contents connecting multiple files
cat test?.txt # ? indicates single character. The representation is called wildcard
cat test* # * indicates zero or multiple characters
cat test* >> combined.txt # append combined files to the txt file
less combined.txt # view contents in pages
echo “this is a test” # show message in terminator
echo “this is a test” > out.txt # overwritten with new contents
echo “this is a test” >> out.txt # append new content
mv combined.txt dir1 # move files to the destination (the last term)
mv combined.txt ..
mv dir/* . # move all files in dir to current folder, one dot “.” means current directory
mv combined.txt test* dir3 dir2 # move files and folders to dir2
move combined.txt combined_backup.txt # rename the file
mv “dir 2” dir2 # rename the folder
rm combined.txt combined_backup.txt # remove files
rm folder_1 # remove one folder
rm -i t* # remove with interactive confirmation
rm -ir dira # remove directory
rmdir folder_* # remove only empty folders
rmdir -p dir1/dir2/dir3 # remove parent folders as well
rmdir -r folder_6 # remove folder and everything inside
wc -l file1.txt # count lines in file1.txt. l means line, wc means word count,
# complete procedure to count number of items inside a folder, but complicated
ls ~ > file_list.txt
wc -l file_list.txt
rm file_list.txt
# this is called piping, no intermediate file generated
ls ~ | wc -l
ls /etc | less
uniq # output unique lines of a file, only works on adjacent matching lines
cat file1 file2 # display file1 and file2 contents in serial
cat -n file1 # display file1 and its line number
cat combined.txt | unique | wc -l # count the unique lines of a text file
cat combined.txt | unique | less # inspect the unique lines of a text file
sort combined.txt | less # sort lines of text file and view
sort combined.txt | unique | wc -l # coutnt unique lines of a file
# 查看文件类型
file ~/.bashrc
file ~/.vimrc
file *
file /bin/pwd
3. System
su username # su means switch user
sudo # switch user and do this command, defaulting to 15mins
clear # clear screen, but still keep record
reset # clear the terminal
man man # 1~9 volume number meaning
man 1 ls # from volume 1 of man, shell command ls
man 2 open # from volume 2 of man, system call open command
info ls
ls --help
sudo apt-get
sudo apt-add-repository
sudo apt install tree # sudo means running it as superuser
# apt works with instruction to perform, rather than files
# install is the instruction and tree is its first parameter
sudo su # root shell, to run a series of commands as the superuser
.filename # hidden files with one dot prefixed
history -d 123
history -c
export HISTSIZE=0
find -name *Linux*.pdf
find /work/ -name "*.txt"
find /home -mtime -2 # 查找两天内改动的文件
grep -rn "abc" test1.txt # recursively find and display line number
# which, whereis 查找命令路径
which pwd
which gcc
whereis pwd 查找可执行、头文件和manual的位置
# remount file system to make it writable
sudo mount -o remount,rw /media/xxx/SSD2
4. Zip & Unzip
# gzip,单个文件压缩,不能压缩目录
# 相同的文件内容,如果文件名不同,压缩后大小不同
gzip -l file.gz # 显示压缩包内容
gzip -d file.gz # 解压,解压之后压缩包自动消失
gzip -kd file.gz # 解压,解压之后源文件保留
gzip file # 压缩文件,源文件消失
gzip -k file # 压缩文件,源文件保留
# bzip2,单个文件压缩,得到.bz2的文件。
# 语法与gzip相同,小文件使用gzip压缩,大文件使用bzip2压缩。
bzip2 -k folder
# tar,对多个目录和文件进行打包和压缩
# c生成文件包,z使用gzip压缩(命名后缀.tar.gz)
# v显示信息,f表示文件
tar czvf dira.tar.gz dira
# t查看内容
tar tvf dira.tar.gz
# x提取、解压,C解压到目录
tar xzvf dira.tar.gz -C /home/book
# j使用bzip2压缩(命名后缀.tar.bz2)
tar cjvf dira.tar.bz2
tar tvf dira.tar.bz2
tar xjvf dira.tar.bz2
5. Compilation
gcc -v # 查看gcc版本
gcc -o hello helloworld.c # 从源代码helloworld.c编译生成可执行程序hello