欧卡2中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

需要三步,才能开始

只需两步,慢速开始

查看: 6032|回复: 0
收起左侧

[开源] Linux shell中的那些小把戏

[复制链接]
apking 发表于 2013-12-20 18:04 来自手机 | 显示全部楼层 |阅读模式

2013-12-20 Linux中国

我日常使用Linux shell(Bash),但是我经常忘记一些有用的命令或者shell技巧。是的,我能记住一些命令,但是肯定不会只在特定的任务上使用一次,所以我就开始在我的Dropbox账号里用文本文件写下这些Linux shell的小技巧,现在我决定共享它给你。这个表我以后还会更新。记住,这里的一些贴士需要在你的Linux发行版上安装额外的软件。

在bash中检查远程端口是否打开:

echo>/dev/tcp/8.8.8.8/53&&echo"open"
将进程挂起:

Ctrl+z
将进程移到前台:

fg
(译注,挂起的进程是不执行的,如果希望在后台执行,可以使用bg命令,并且指定通过jobs命令获得的任务号。)

生成随机16进制数字,n是字符的数量:

opensslrand-hexn
在当前shell中执行一个文件中的命令(译注:这个文件不是一个bash脚本,比如.bashrc、bash_profile等):

source/home/user/file.name
提取字符串的前5个字符:

variable:0:5SSHSSH:sshvvvuser@ipaddress使pemkeySSHsshuser@ipaddressikey.pemwget:wgetrnoparentreject"index.html"http://hostname/P/home/user/dirs:mkdirp/home/user/test,test1,test2:psaxwefwar:jarcvfname.warfile:ddif=/dev/zeroof=/tmp/output.imgbs=8kcount=256kconv=fdatasync;rmrf/tmp/output.img:hdparmTt/dev/sdamd5:echon"text"|md5sumxml:xmllintnooutfile.xmltar.gz:tarzxvfpackage.tar.gzCnewdircurlHTTP:curlIhttp://www.example.com(YYMMDDhhmm):toucht0712250000file使wgetftp:wgetmftp://username:password@hostname(16):LANG=c</dev/urandomtrdcAZaz09|headc{1:-16};echo;
快速创建一个文件的备份(扩展名是.bkp):

cpsome_file_name{,.bkp}
访问Windows共享:

smbclient-U"DOMAIN\user"//dc.domain.com/share/test/dir
运行history中的命令 (这里在history中的第100个):

!100
unzip到目录中:

unzippackage_name.zip-ddir_name
输入多行文字 (按 CTRL + d 退出):

cat>test.txt
创建空白的文件或者清空已存在的文件:

>test.txt
Ubuntu NTP服务器上更新日期:

ntpdatentp.ubuntu.com
netstat 显示所有IPv4的TCP监听的端口:

netstat-lnt4|awk'{print4}'|cut-f2-d:|grep-o'[0-9]*' 
将qcow2的镜像转化成raw格式: 
 
qemu-imgconvert-fqcow2-Orawprecise-server-cloudimg-amd64-disk1.img\ 
precise-server-cloudimg-amd64-disk1.raw 
重复运行命令并显示它的输出 (默认2秒重复一次): 
 
watchps-ef 
显示所有用户: 
 
getentpasswd 
以读写模式挂载根文件系统: 
 
mount-oremount,rw/ 
挂载目录 (适合于符号链接不能工作的情况下): 
 
mount--bind/source/destination 
发送DNS动态更新给DNS: 
 
nsupdate<<EOF 
updateadd
HOST86400AIPsendEOFgrepgrepr"sometext"/path/to/dir10:lsof/|awkif($7>1048576)print$7/1048576"MB"$9|sortnu|tailMB:freem|grepcache|awk/[09]/print$4"MB"vim:vim+somefilenamegitclonebranch(master):gitclonegit@github.com:name/app.gitbmastergitbranch(develop):gitcheckoutdevelopgitbranch(myfeature):gitbranchdmyfeatureGitbranch:gitpushorigin:branchNameGitpushbranch:gitpushuoriginmynewfeaturehistorycat!cat:phistorycat:!cat/home/user:find/home/usermaxdepth1typedemptytest.txt5060:<test.txtsedn50,60psudo(:mkdir/root/test,:sudomkdir/root/test)sudo:sudo!!RAMramdisk(/tmpram):mountttmpfstmpfs/tmpramosize=512mGrep:grepw"name"test.txt:echo"sometext"|sudoteea/path/filekill:killl(16):opensslrandbase6416bash:kill9:nmapp8081172.20.0.0/16gitemail:gitconfigglobaluser.email"me@example.com"commitmaster:gitpullrebaseoriginmastertxt/home/user:findiname"txt"execmvv/home/user:pastetest.txttest1.txtshell:pvdata.lognetcat:echo"hosts.sampleHost10date+tab:expandtest.txt>test1.txtbash:<<>>cmd:cdtar.gz(100MB):splitb100m/path/to/large/archive/path/to/output/filescatfiles>archivecurlHTTP:curlsLw"Ctrl+c:Ctrl+ :statc:lsblkf:find.typefexecegrepl"+""{}"\;
找出用tab缩进的文件:

find.-typef-execegrep-l$'\t'"{}"\;
用"="打印水平行

printf'%100s\n'|tr''=
更新: 2013年11月25日

via:http://www.techbar.me/linux-shell-tips/

译者:geekpi校对:wxy

本文由LCTT原创翻译,Linux中国荣誉推出



阅读原文举报
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

客服QQ936487697|联系我们|手机版|欧卡2中国 ( 湘ICP备11020288号-1 )

GMT+8, 2025-4-29 13:58 , Processed in 0.072965 second(s), 9 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表