Linuxshell用法和技巧
服务器之间复制文件
scp-r文件名root@ip地址:所复制文件目录
服务器之间跳转
ssh-lrootIP地址
检查远程端口是否对bash开放:
echo>/dev/tcp/8.8.8.8/53&&echo"open"
让进程转入后台:
Ctrl+z
将进程转到前台:
fg
产生随机的十六进制数,其中n是字符数:
opensslrand-hexn
在当前shell里执行一个文件里的命令:
source/home/user/file.name
截取前5个字符:
${variable:0:5}
SSHdebug模式:
ssh-vvvuser@ip_address
SSHwithpemkey:
sshuser@ip_address-ikey.pem
用wget抓取完整的网站目录结构,存放到本地目录中:
wget-r--no-parent--reject"index.html*"http://hostname/-P/home/user/dirs
一次创建多个目录:
mkdir-p/home/user/{test,test1,test2}
列出包括子进程的进程树:
psaxwef
创建war文件:
jar-cvfname.warfile
测试硬盘写入速度:
ddif=/dev/zeroof=/tmp/output.imgbs=8kcount=256k;rm-rf/tmp/output.img
测试硬盘读取速度:
hdparm-Tt/dev/sda
获取文本的md5hash:
echo-n"text"|md5sum
检查xml格式:
xmllint--nooutfile.xml
将tar.gz提取到新目录里:
tarzxvfpackage.tar.gz-Cnew_dir
使用curl获取HTTP头信息:
curl-Ihttp://www.example.com
修改文件或目录的时间戳(YYMMDDhhmm):
touch-t0712250000file
用wget命令执行ftp下载:
wget-mftp://username:password@hostname
生成随机密码(例子里是16个字符长):
LANG=c</dev/urandomtr-dc_A-Z-a-z-0-9|head-c${1:-16};echo;
快速备份一个文件:
cpsome_file_name{,.bkp}
访问Windows共享目录:
smbclient-U"DOMAIN\user"//dc.domain.com/share/test/dir
执行历史记录里的命令(这里是第100行):
!100
解压:
unzippackage_name.zip-ddir_name
输入多行文字(CTRL+d退出):
cat>test.txt
创建空文件或清空一个现有文件:
>test.txt
与UbuntuNTPserver同步时间:
ntpdatentp.ubuntu.com
用netstat显示所有tcp4监听端口:
netstat-lnt4|awk'{print$4}'|cut-f2-d:|grep-o'[0-9]*'
qcow2镜像文件转换:
qemu-imgconvert-fqcow2-Orawprecise-server-cloudimg-amd64-disk1.img\
precise-server-cloudimg-amd64-disk1.raw
重复运行文件,显示其输出(缺省是2秒一次):
watchps-ef
所有用户列表:
getentpasswd
Mountrootinread/writemode:
mount-oremount,rw/
挂载一个目录(这是不能使用链接的情况):
mount--bind/source/destination
动态更新DNSserver:
nsupdate<<EOF
updateadd$HOST86400A$IP
send
EOF
递归grep所有目录:
grep-r"some_text"/path/to/dir
列出前10个最大的文件:
lsof/|awk'{if($7>1048576)print$7/1048576"MB"$9}'|sort-n-u|tail
显示剩余内存(MB):
free-m|grepcache|awk'/[0-9]/{print$4"MB"}'
打开Vim并跳到文件末:
vim+some_file_name
Git克隆指定分支(master):
[email protected]:name/app.git-bmaster
Git切换到其它分支(develop):
gitcheckoutdevelop
Git删除分支(myfeature):
gitbranch-dmyfeature
Git删除远程分支
gitpushorigin:branchName
Git将新分支推送到远程服务器:
gitpush-uoriginmynewfeature
打印历史记录中最后一次cat命令:
!cat:p
运行历史记录里最后一次cat命令:
!cat
找出/home/user下所有空子目录:
find/home/user-maxdepth1-typed-empty
获取test.txt文件中第50-60行内容:
<test.txtsed-n'50,60p'
运行最后一个命令(如果最后一个命令是mkdir/root/test,下面将会运行:sudomkdir/root/test):
sudo!!
创建临时RAM文件系统–ramdisk(先创建/tmpram目录):
mount-ttmpfstmpfs/tmpram-osize=512m
Grepwholewords:
grep-w"name"test.txt
在需要提升权限的情况下往一个文件里追加文本:
echo"sometext"|sudotee-a/path/file
列出所有killsignal参数:
kill-l
在bash历史记录里禁止记录最后一次会话:
kill-9$$
扫描网络寻找开放的端口:
nmap-p8081172.20.0.0/16
设置gitemail:
gitconfig--globaluser.email"[email protected]"
Tosyncwithmasterifyouhaveunpublishedcommits:
gitpull--rebaseoriginmaster
将所有文件名中含有”txt”的文件移入/home/user目录:
find-iname"*txt*"-execmv-v{}/home/user\;
将文件按行并列显示:
pastetest.txttest1.txt
shell里的进度条:
pvdata.log
使用netcat将数据发送到Graphiteserver:
echo"hosts.sampleHost10`date+%s`"|nc192.168.200.23000
将tabs转换成空格:
expandtest.txt>test1.txt
Skipbashhistory:
<space>cmd
去之前的工作目录:
cd-
拆分大体积的tar.gz文件(每个100MB),然后合并回去:
split–b100m/path/to/large/archive/path/to/output/files
catfiles*>archive
使用curl获取HTTPstatuscode:
curl-sL-w"%{http_code}\\n"www.example.com-o/dev/null
设置root密码,强化MySQL安全安装:
/usr/bin/mysql_secure_installation
当Ctrl+c不好使时:
Ctrl+\
获取文件owner:
stat-c%Ufile.txt
block设备列表:
lsblk-f
找出文件名结尾有空格的文件:
find.-typef-execegrep-l"+$"{}\;
找出文件名有tab缩进符的文件
find.-typef-execegrep-l$'\t'{}\;
用”=”打印出横线:
printf'%100s\n'|tr''=