One: Special symbol usage collation
Arithmetic comparison
-eq equals
-ne Not equal to
-GT Greater than
-lt less than
-ge greater than or equal to
-le less than or equal to
-a logic with and &&
-o logic or or | |
[$var-eq 0] #当 $var equals 0 o'clock, returns true
[$var-ne 0] #当 $var is not 0 o'clock, returns true
[$var 1-ne 0-a $var 2-gt 2]
[$var 1-ne 0-o $var 2-gt 2]
string comparison
[[$str 1 = $str 2]] #当str1等于str2时, returns True. Note: There is a space before and after the = number, and if you forget to add a space, it is treated as an assignment
[[$str 1 = = $str 2]]
[[$str 1! = $STR 2]]
[[$str 1 > $str 2]]
[[$str 1 < $STR 2]]
[[-Z $str 1]] #如果str1包含的是空字符串 returns TRUE.
[[-N $str 1]] #如果str1包含的是非空字符串 returns TRUE.
file system related conditions flags
[-F $var] # Returns true if the given variable name contains normal file path strength or filename.
[-X $var] #如果给定的变量包含的文件可执行, returns True.
[-D $var] #如果给定的变量包含的是目录, returns True.
[-e $var] #如果给定的变量包含的文件存在, returns True.
[-c $var] #如果给定的变量包含的是一个字符设备文件的路劲, returns True.
[-B $var] #如果给定的变量包含的是一个块设备文件的路劲, returns True.
[-W $var] #如果给定的变量包含的文件可写 returns TRUE.
[-R $var] #如果给定的变量包含的文件可读, returns True.
[-l $var] #如果给定的变量包含的是一个符号链接 returns TRUE.
----------------------------------
fpath= "/etc/passwd"
If [-e $fpath]; then
Echo File exists;
Else
Echo dones not exist;
Fi
----------------------------------
REDIRECT Symbols ">" and ">>"
echo "This is a sample text 1" > Temp.txt #输出重定向到文件并清空文件原本的内容
echo "This is a sample text 2" >> temp.txt #输出重定向到文件不清空文件原本的内容
Pwe >> Temp.txt
field separators and iterators
-----------------------------------
EG1:
Data= "Name,sex,rollno,location"
oldifs= $IFS #IFS是存储定界符的环境变量
Ifs=,now,
for item in $data;
Do
echo Item: $item
Done
ifs= $oldIFS
EG2:
#!/bin/bash
line= user information for "Root:x:0:0:root:/root:/bin/bash" #/etc/passwd
Oldifs= $IFS;
Ifs= ":"
Count=0
For item in $line
Do
[$count-eq 0] && user= $item;
[$count-eq 6] && shell= $item;
Let Count + +
Done
ifs= $oldIFSecho $user \ ' s shell is $shell
-----------------------------------
Two: mathematical operations, images and arrays
Let's perform basic arithmetic operations directly
Let Result=nol+nol2
Let no1++
Let no1--
Similar
result=$[No1 + NO2]
result=$[$no 1 + 5]
result=$ ((no1 + 50))
Expr is used for basic arithmetic operations
result= ' Expr 3 + 4 '
result=$ (expr $no 1 +5)
BC Advanced mathematical arithmetic tools for floating point numbers
echo "4 * 0.56" | Bc
------------------------------
no=54;
Result= ' echo ' $no * 1.5 | BC '
Echo $result
------------------------------
array
array_var= (1 2 3 4 5 6) #定义数组 br>array_var[6]=7;
Echo ${array_var[6]} #7
-----------------------------
index=5
Echo ${ array_var[$index]} #6
-----------------------------
Echo ${array_var[*]} #1234567 Print all Values
Echo ${array_var[@]} #1234567
Echo ${#array_var [*]} #7 Print the number of array elements
--------------------------------------------------
declare-a ass_array #单独将变量名声明为关联数组
ass_array= ([index1]=var1 [index2]=var2)
Ass_array[index3]=var3;
ASS_ARRAY[INDEX4]=VAR4;
--------------------------------------------------
Echo ${!ass_array[*]} # Gets the index list of the array
Echo ${!ass_array[@]} #同上
image-related
you need Iamgemagick support to use convert. sudo apt-get install ImageMagick
Convert image.png image.jpg #将一种图像格式转换为另一种图像格式
Convert Image.png-resize 1024x768 image.png #改变WIDTHxHEIGHT调整图像大小, width and height must provide at least one
convert Image.png- Resize 1024x image.png #提供了WIDTH, automatically scale by image scale
Convert image.png-resize "50%" Image.png ; #使用百分比缩放图像
For i in *; Do MENCODER-OAC mp3lame-lameopts vbr=3-ovcxvid-xvidencopts fixed_quant=4-of avi $i-o ' echo $i | Sed-e ' s/rmvb$/avi/'; Done #批量将rmvb转为avi
For i in *; Do convert-resize 30%x30% $ sm-$1; Done #批量缩小图片到30%
For i in *; Do convert $i ' echo $i | Sed-e ' s/jpg$/png/'; Done #批量转换jpg到png
For i in *; Do inkscape $i –export-png= ' echo $i | Sed-e ' s/svg$/png/'; Done #批量将svg转为png
Find. -name *.jpg-exec wc-c {} \; | awk ' {print '} ' |awk ' {a+=$1}end{print A} ' #统计当前目录下所有jpg文件的尺寸
Identify-verbose xxx.jpg #去掉文件中的 ^m
Cat FileName | tr-d "^m" > NewFile #获取jpg的扩展信息 (Exif)
sudo apt-get install abcde;abcde-o mp3-b #CD catch rail for MP3 (lossy)
sudo apt-get install abcde;abcde-o flac-b #CD capture for FLAC (lossless)
Three: Partial terminal command usage collation
Terminal information
Tput cols #获取终端列数
Tput Lines #获取终端行数
Tput longname #获取终端名
Tput Bold #设置文本样式为粗体
Tput SETB No #设置终端背景色 no 0 to 7
Tput SETF No #设置终端前景色
Tput Cup #定位光标
Date Time
Date #打印纪元时
Date +%s #打印此时的时间戳
Cal #显示日历
Date-s Mm/dd/yy #设置日期
Date-s hh:mm #设置时间
HWCLOCK–SYSTOHC #将时间写入CMOS
Hwclock–show #查看CMOS时间
Hwclock–hctosys #读取CMOS时间
sudo ntpdate ntp.ubuntu.com #从服务器上同步时间
sudo cp/usr/share/zoneinfo/asia/shanghai/etc/localtime #设置电脑的时区为上海
sudo sed-ie ' s/utc=yes/utc=no/g '/etc/default/rcs;sudo HWCLOCK–SYSTOHC #关闭UTC, writes the current time to CMOS
Switch machine
sudo halt #命令关机
sudo shutdown-h now #现在关机
sudo shutdown-h 23:00 #晚上11点自动关机
sudo shutdown-h +60 #60分钟后关机
sudo reboot #命令重启电脑
sudo shutdown-r now #现在重启电脑
Synclient touchpadoff=1 #关闭笔记本的触摸板
Synclient touchpadoff=0 #开启笔记本的触摸板
User login Information
Who #获取当前登陆的用户信息
W #同上
Users #列出当前登录主机的用户列表
Uptime #查看系统已经通电运行了多长时间
Last #获取前一次的启动及用户登录会话的信息
Last User #获取单个用户的会话信息
Inotifywait-m-r-e create,move,delete,open,close,access,modify,attrib/data/test-q >> test.log #监视/data/test Mesh Record creation, move, delete, open, close, access, modify, file metadata are modified and other events. -m continuous monitoring,-r recursive monitoring directory,-e time list to monitor,-Q simplifies output
System Information
which LS #输出作为参数的命令的所在位置
Whereis ls #输出作为参数的命令所在的位置和该命令手册的位置及源码的路劲
Whatis ls #输出作为参数的命令的简短的描述信息, this information comes from the Manual of the command
Hostname #输出系统的主机名
Uname-n #同上
Uname-a #打印Linux的内核版本, hardware architecture, and more
Uname-r #打印内核发行版本
Uname-m #打印主机类型
Cat/proc/cpuinfo #打印cpu相关信息
Cat/proc/meminfo #打印内存的详细信息
Cat/proc/meminfo | Head-l #打印系统可用内存总量
Cat/proc/partitions #列出系统的分区信息
Cat/proc/5346/status #查看PID为5346的进程的信息
Cat/proc/cpuinfo | Head-n 5 | Tail-l #打印处理器名称
Fdisk-l #同上
LSHW #获取系统的详细信息
Lsmod #查看内核加载的模块
Lspci #查看PCI设备
Lsusb-v #查看USB设备
sudo lshw #显示当前硬件信息
sudo fdisk-l #查看硬盘的分区
sudo fdisk/dev/sda #硬盘分区
sudo iostat-x 2 #查看硬盘当前读写情况
sudo mkfs.ext3/dev/sda1 #硬盘格式化
Ulimit-n 4096 or echo 4096 >/proc/sys/fs/file-max #增加系统最大打开文件个数
Sleep 3 #延迟3秒再往下执行
More file #分页查看文件内容
less file #可控分页查 See file contents
sudo vim/etc/motd #修改系统登录信息
Logger log_message #向syslo Log information in G
alias install= ' sudo apt-get install ' #别名: Use install instead of sudo apt-get install command
Export http_proxy=http:// Xx.xx.xx.xx:xxx #终端设置代理
zenity--info--text "This is a message" #用zenity创建一个信息框, which shows the text "This is a message"
Con Vmv-r-F cp936-t utf8–notest–nosmart * #转换文件名由GBK为UTF8
iconv-f gbk-t UTF8 $i > NewFile #转换文件内容由GBK到UTF8 Br>sudo apt-get Install Python-mutagen;find. -iname ' *.mp3′-execdir mid3iconv-e GBK {} \; #转换 MP3 Tag encoding
recording and playback of terminal session
------------------------------------------ ------------------
script-t 2> timing.log-a output.session #开始录制终端会话
type commands; #要录制的会话命令
...
..
Exit #结束录制
Scriptreplay timing.log output.session #播放命令序列输出
-------------------------------------------- ----------------
Open Two terminals: T1 and T2
Enter the command in T1: Mkfifo scriptfifo
Enter command in T2: Cat Scriptfifo
Return T1, input: script-f Scriptfifo
to accept the session, enter exit
------------------------------------------------------------
Read Terminal input: Read
Read-n 5 var #从输入中读取5个字符并存入变量var
read-s var #用不回显的方式读取输入到变量var
Read-p "Enter input:" var #显示提示信息
Read-t 5 var #读取5秒内的输入到变量var
Read-d ":" var #用界定符 ":" End input line
Echo $var
--------------------------------------
Do not display when entering a password:
#!/bin/sh
Echo-e "Enter Password:"
Stty-echo
Read password
Stty Echo
Echo
Echo Password read.
--------------------------------------
Symbolic Links
Ln-s/var/www ~/web #在当前用户的home目录下创建名为web的符号链接. Link point to/var/www
Ls-l Web | awk ' {print $ #/var/www} '
Readlink Web #/var/www
Ls-l | grep "^l" | awk ' {print $8} ' #grep选出以l开头的行之后用awk打印出第8列.
Loopback Files and Mounts
DD If=dev/zero of=loopbackfile.img bs=1g count=1 #创建一个环回文件, size 1GB
MKFS.EXT4 loopbackfile.img #将文件格式化为ext4文件系统
sudo file loopbackfile.img #检查文件系统
Mount-o Loop loopback.img/mnt/loopback #将环回文件挂载到/mnt/loopback directory (-O loop to mount Loopback file system)
LOSETUP/DEV/LOOP1 loopback.img # (1)
Mount/dev/loop1/mnt/loopback # (2) Mount the loopback.img to the directory/mnt/loopback, this loopback file is connected to a device called/DEV/LOOP1
Losetup-o 32256/dev/loop2 loopback.img #在lookback. IMG To create a partition and mount the first partition,-O represents the offset. The first partition starts after the No. 32256 byte of the hard disk.
sudo umount/mnt/sda1 #卸载
Quick positioning
Pushd/var/www #将该目录压进栈, and switch to the directory
PUSHD/USR/SRC #同上
Dirs #查看栈中的元素
pushd +2 #切换到栈中第3个元素存放的目录
POPD +2 #移除栈中第3个元素存放的目录
Package related
Aptitude Search Pakege #查找软件库pakege中的软件
dpkg-s filename #查找文件属于哪个包
dpkg-l XXX #查看软件xxx安装内容
Apt-cache Search Partitions #使用正则表达式partitions查找软件库中的软件
Apt-file Search filename #查找文件属于哪个包
Apt-cache depends xxx #查询软件xxx依赖哪些包
Apt-cache rdepends xxx #查询软件xxx被哪些包依赖
Apt-cache Show K3b #显示包的信息
Apt-cache Pkgnames #显示系统全部可用包的名称
Apt-cache Stats #显示系统安装包的统计信息
sudo apt-cdrom add #增加一个光盘源
sudo apt-get AutoClean #清理旧版本的软件缓存
sudo apt-get update
sudo apt-get dist-upgrade #系统升级
sudo apt-get autoremove #删除系统不再使用的孤立软件
Lsb_release-a or Cat/etc/issue #查看Ubuntu版本
Dpkg-l |grep ^rc|awk ' {print $} ' |sudo Xargs dpkg-p #清除已删除包的残馀配置文件
Export Git_ssl_no_verify=1 #系统不信任github网站时执行
Linux Learning: Special symbols, mathematical operations, images and arrays, and partial terminal command usage collation