Linux reference sheet
User info:
whoami
hostname
su username
sudo date
sudo su - switch to the superuser account
Command parameter format:
command -p 10
command --parameter=10
ls -a single character parameter name
ls --all full parameter name
Hotkey:
ctrl + A //光标跳到一行命令的开头。Home 键有相同的效果;
ctrl + E //光标跳到一行命令的结尾。End 键有相同的效果;
Command execution outside of $PATH:
/full/path/to/command
./command-in-this-dir
Chmod:
chmod ugoa+-=rwx file
vi:
x - delete a character
dd - delete a line
yy - copy a line
D - delete from current position till line end
p - paste
/ - search forward
? - search hehind
u - undo
:s/旧字符串/新字符串/g
find a file:
find /var/log -name "syslog"
sudo updatedb
locate filename | grep `pwd`
System monitoring:
du -h \\disk usage
Text manipulation:
grep -i #ignore case
grep -v #invert match
cut -d delimiter
cut -f N dispay the nth field
cut -d' ' -f2
grep bob /etc/passwd | cut -d: -f1,5 | sort | tr ":" " "
uniq test.txt - only remove consecutive lines
SCP, RSYNC and SFTP:
scp username@from_host:file.txt /local/directory/
scp file.txt username@to_host:/remote/directory/
sftp tecmint@27.48.137.6
sftp> put local.profile
Uploading local.profile to /tecmint/local.profile
sftp> get SettlementReport_1-10th.xls
Fetching /tecmint/SettlementReport_1-10th.xls to SettlementReport_1-10th.xls
rsync -arv --delete Images/ oscar@89.231.45.67:backups/
Tar:
tar -cvf sorting.tar sorting/ - create
tar -xvf sorting.tar -extract
tar -zcvf
tar -zxvf - add gunzip
zcat,zmore,zless, zgrep - add gunzip
Link:
ln
hard link to same inode
ln -s
soft link creates a new inode pointing to old filename
0 stdin, 1 stdout, 2 stderr:
cat not_exist_file.csv > results.txt 2> errors.log
cat not_exist_file.csv > results.txt 2>&1
cat not_exist_file.csv >> results.txt 2>&1 - append
nohup continues after user has logged out:
nohup cp node-v10.15.3.tar.gz node-v10.15.3-copy.tar.gz &
&&:&& 号前的命令执行成功,才会执行后面的命令。
||:|| 号前的命令执行失败,才会执行后面的命令。
分号:不论分号前的命令执行成功与否,都执行分号后的命令。前后命令之间没有相关性。
ifconfig
en: ethernet
lo: loopback
wlan: wireless network
' ' ignore special chars
" " takes $, `, and \
` ` execute
alias var="var1"
export var="var1"
#!/bin/bash
message='Hello World'
echo "The message is $message"
#!/bin/bash
message=`pwd`
echo "You are in the directory $message"
在 Bash 中,所有的变量都是字符串
env - show environment variable
./variable.sh 参数1 参数2 参数3 ...
$# :包含参数的数目。
$0 :包含被运行的脚本的名称 (我们的示例中就是 variable.sh )。
$1:包含第一个参数。
$2:包含第二个参数。
#!/bin/bash
array=('value0' 'value1' 'value2')
array[5]='value5'
echo ${array[1]}
if [ 条件测试 ]; then
做这个
fi
if [ 条件测试 1 ]
then
做事情 1
elif [ 条件测试 2 ]
then
做事情 2
elif [ 条件测试 3 ]
then
做事情 3
else
做其他事情
fi
#!/bin/bash
if [ $# -ge 1 ] && [ $1 = 'love' ]
then
echo "Great !"
echo "You know the password"
else
echo "You do not know the password"
fi
#!/bin/bash
read -p 'Enter a file : ' file
if [ ! -e $file ]
then
echo "$file does not exist"
else
echo "$file exists"
fi
#!/bin/bash
for animal in 'dog' 'cat' 'pig'
do
echo "Animal being analyzed : $animal"
done
#!/bin/bash
for file in `ls *.sh`
do
cp $file $file-copy
done
#!/bin/bash
while [ -z $response ] || [ $response != 'yes' ]
do
read -p 'Say yes : ' response
done
#!/bin/bash
# Verification of parameter(验证参数)
# If no parameter, use a default value(如果没有给出参数,那么用默认值 gallery.html )
if [ -z $1 ]
then
output='gallery.html'
else
output=$1
fi
# Preparation of files and folders(准备文件和目录)
echo '' > $output
if [ ! -e thumbnails ]
then
mkdir thumbnails
fi
# Beginning of HTML(HTML 文件的开头)
echo '<!DOCTYPE html>
<html>
<head>
<title>My Gallery</title>
</head>
<body>
<p>' >> $output
# Generation of thumbnails and the HTML web page(生成图片的缩略图和 HTML 的页面主体)
for image in `ls *.jpg *.png *.jpeg *.gif 2>/dev/null`
do
convert $image -thumbnail '200x200>' thumbnails/$image
echo ' <a href="'$image'"><img src="thumbnails/'$image'" alt=""/></a>' >> $output
done
# End of HTML(HTML 文件的结尾)
echo ' </p>
</body>
</html>' >> $output
#!/bin/bash
# Verification of parameter
# 确认参数
if [ -z $1 ]
then
echo "Please enter the file of dictionary !"
exit
fi
# Verification of file existence
# 确认文件存在
if [ ! -e $1 ]
then
echo "Please make sure that the file of dictionary exists !"
exit
fi
# Definition of function
# 函数定义
statistics () {
for char in {a..z}
do
echo "$char - `grep -io "$char" $1 | wc -l`" | tr /a-z/ /A-Z/ >> tmp.txt
done
sort -rn -k 2 -t - tmp.txt
rm tmp.txt
}
# Use of function
# 函数使用
statistics $1
其中 -r 和 -n 参数我们比较熟悉,-n 参数用于对数字排序,-r 参数用于倒序排列。
-k 参数用于指定根据哪几列进行排序,这里用 -k 2 表示根据第 2 列来排序。
-t 参数用于指定列和列之间用什么作为分隔符,这里用 -t - 表示分隔符是 - 。