# 创建目录并进入
function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\"";
}
# 找到大于100M的文件
find / -type f -size +100M
# 找到文件名中含有mail的文件/文件夹
find /etc -name "*mail*"
# 找到修改时间在60天之前的文件
find . -mtime +60
# 找到修改时间在2天内的文件
find . -mtime -2
# 批量显示TS后缀且大于100M文件的详情
find . -type f -name '*.TS' -size +100M -exec ls -l {} \;
# 批量删除TS后缀且大于100M的文件
find . -type f -name '*.TS' -size +100M -exec rm -f {} \;
# 查找修改时间60天前的文件并打包
find /home/jsmith -type f -mtime +60 | xargs tar -cvf /tmp/`date '+%d%m%Y'_archive.tar`
# 标准输出重定向,只显示error信息
./shell-script.sh > /dev/null
# 标准错误信息重定向
./shell-script.sh 2> /dev/null
# 标准错误和输出都重定向
./shell-script.sh > /dev/null 2>&1
# 将所有大写转化为小写
tr A-Z a-z < department.txt
# 将所有小写转化为大写
tr a-z A-Z < employee.txt
# 删除log文件
find ~ -name '*.log' -print0 | xargs -0 rm -f
find /etc -name "*.conf" | xargs ls -l
cat url-list.txt | xargs wget -c
find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
ls *.jpg | xargs -n1 -i cp {} /external-harddrive/directory
# 以:分割,第一列
cut -d: -f 1 /etc/passwd
# 以:分割,第1和第3列
cut -d: -f 1,3 /etc/passwd
# 截取每行前边1-8个字符
cut -c 1-8 /etc/passwd
nohup ./backup.sh &
screen -S backup
# 以特定间隔时间执行命令
watch df -h
# thegeekstuff.txt
# Instruction Guides
1. Linux Sysadmin, Linux Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux
5. Productivity (Too many technologies to explore, not
much time available)
# Additional FAQS
6. Windows- Sysadmin, reboot etc.
# 将第一个Linux替换为Linux-Unix
sed 's/Linux/Linux-Unix/' thegeekstuff.txt
# 将所有Linux替换为Linux-Unix
sed 's/Linux/Linux-Unix/g' thegeekstuff.txt
# 将第2个出现的Linux替换为Linux-Unix
sed 's/Linux/Linux-Unix/2' thegeekstuff.txt
# 输出修改行并写入指定的output文件
sed -n 's/Linux/Linux-Unix/gpw output' thegeekstuff.txt
# 行正则匹配到-,则从-到行尾的字符被替换为空
sed '/\-/s/\-.*//g' thegeekstuff.txt
# 删除每行的后3个字符
sed 's/...$//' thegeekstuff.txt
# 直接修改源文件,去除#开头的注释
sed -e 's/#.*//' thegeekstuff.txt
# 直接修改源文件,去除#开头的注释并去除空行
sed -e 's/#.*//;/^$/d' thegeekstuff.txt
# 去除html的箭头标签
sed -e 's/<[^>]*>//g'
# 同时显示多个文件的日志
tail -f /var/log/syslog -f /var/log/auth.log
# 修改命令行提示符号
export PS1="\u@\h \w> "
# 修改系统时间
date {mmddhhmiyyyy.ss}
# Jan 31st 2009, 10:19 p.m, 53 seconds
date 013122192009.53
date +%Y%m%d -s "20090131"
date +%T -s "22:19:53"
date -s "01/31/2009 22:19:53"
# 显示时间
date +"%d-%m-%Y"
01-01-2009
date +"%d/%m/%Y"
# 01/01/2009
date +"%A,%B %d %Y"
# Thursday,January 01 2009
zip var-log-files.zip /var/log/*
zip -r var-log-dir.zip /var/log/
unzip var-log.zip
unzip -v var-log.zip
unzip -l var-log.zip
unzip -t var-log.zip
zip -P mysecurepwd var-log-protected.zip /var/log/*
unzip var-log-protected.zip
tar [options] [tar-archive-name] [other-file-names]
# 压缩文件
tar cvf /tmp/my_home_directory.tar /home/jsmith
# 显示压缩文件目录
tar tvf /tmp/my_home_directory.tar
# 提取压缩文件
tar xvf /tmp/my_home_directory.tar
# 指定提取目录
tar xvfz /tmp/my_home_directory.tar.gz -C /home/ramesh
# gzip压缩文件(*.tar.gz)
tar cvfz /tmp/my_home_directory.tar.gz /home/jsmith
tar xvfz /tmp/my_home_directory.tar.gz
tar tvfz /tmp/my_home_directory.tar.gz
# bzip压缩文件(*.tar.bz2)
tar cvfj /tmp/my_home_directory.tar.bz2 /home/jsmith
tar xvfj /tmp/my_home_directory.tar.bz2
tar tvfj /tmp/my_home_directory.tar.bz2
# CTRL+r 查找匹配历史
# CTRL+p 上一条命令
history -c # 清除历史
# 忽略重复命令
export HISTCONTROL=ignoredups
# 忽略以空格开头的命令
export HISTCONTROL=ignorespace
# 不记录历史
export HISTSIZE=0
# 创建swap分区
dd if=/dev/zero of=/home/swap-fs bs=1M count=512
ls -l /home/swap-fs
mkswap /home/swap-fs
swapon /home/swap-fs
# edit in /etc/fstab
/home/swap-fs swap swap defaults 0 0
# 生成ssh公钥
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
# 定时任务管理
crontab -e
{minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}
# run at 00:01am
1 0 * * * /root/bin/backup.sh
# run at weekday 11:59pm
59 11 * * 1,2,3,4,5 /root/bin/backup.sh
59 11 * * 1-5 /root/bin/backup.sh
# run every 5 minute
*/5 * * * * /root/bin/check-status.sh
# run at 13:10pm on lst of every month
10 13 1 * * /root/bin/full-backup.sh
# run at 11:00pm every weekday
0 23 * * 1-5 /root/bin/incremental-backup.sh
# 同步文件
rsync options source destination
# sync two directory in a local computer
# -z enable compression
# -v verbose
# -r recursive
# -a archive mode:will preserve symbolic link/permission/timestamp/owner/group
rsync -zvr /var/opt/installation/inventory/ /root/temp
# sync one file
rsync -v /var/lib/rpm/Pubkeys /root/temp/
# sync to remote machine
rsync -avz /root/temp/ thegeekstuff@192.168.200.10:/home/thegeekstuff/temp/
# sync from remote to local
rsync -avz thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
# netcat命令nc
# 从server1拷贝文件到server2
# 1. 在server2(102.168.200.27)上监听
nc -l 2222 > 1234.txt
# 2. 在server1上开启传输
nc -w 1 102.168.200.27 2222 < abc.txt
# 网络拷贝硬盘
# 1. server2(102.168.200.27)监听
nc -l -p 2222 | dd of=/dev/sda
# 2. server1执行传输
dd if=/dev/sda | nc 102.168.200.27 2222
# nc端口扫描
# 扫描20-30端口
nc -v -w 1 192.168.200.29 -z 20-30
ps axl
ps aux
ps axuf
ps U niuhe
netstat -tap
netstat --route # 路由表
sar
lsof