mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
Merge pull request #525 from geekpi/master
[Translating] Linux shell tips and tricks
This commit is contained in:
commit
82ee876d03
@ -1,310 +1,310 @@
|
||||
Linux shell tips and tricks
|
||||
Linux sheel 贴士和技巧
|
||||
================================================================================
|
||||
I’m using Linux shell (Bash) on daily basis, but I often forgot some useful command or shell tip. Yes, I can remember commands, but I can’t say that if I used it just once for specific task. Then I started to write Linux shell tips in text file on my Dropbox account and now I decided to share that. This list will be updated over time. Also keep in mind that for some tips you will need to install additional software on your Linux distribution.
|
||||
我日常使用Linux shell(Bash),但是我经常忘记一些有用的命令或者shell技巧。是的,我能记住一些命令但是我不能说只在特定的任务上使用一次。那么我就开始在我的Dropbox账号里用文本文件写下这些Linux shell的贴士,现在我决定共享它。这个表我以后还会更新。记住,这里的一些贴士需要在你的Linux发行版上安装额外的软件。
|
||||
|
||||
Check if remote port is open with bash:
|
||||
在bash中检查远程端口是否打开:
|
||||
|
||||
echo >/dev/tcp/8.8.8.8/53 && echo "open"
|
||||
|
||||
Suspend process:
|
||||
终止进程:
|
||||
|
||||
Ctrl + z
|
||||
|
||||
Move process to foreground:
|
||||
将进程移到前台:
|
||||
|
||||
fg
|
||||
|
||||
Generate random hex number where n is number of characters:
|
||||
生成随机16进制数字,n是字符的数量:
|
||||
|
||||
openssl rand -hex n
|
||||
|
||||
Execute commands from a file in the current shell:
|
||||
在当前shell中从一个文件中执行命令:
|
||||
|
||||
source /home/user/file.name
|
||||
|
||||
Substring for first 5 characters:
|
||||
提取前5个字符的字串:
|
||||
|
||||
${variable:0:5}
|
||||
|
||||
SSH debug mode:
|
||||
SSH调试模式:
|
||||
|
||||
ssh -vvv user@ip_address
|
||||
|
||||
SSH with pem key:
|
||||
带pem key的SSH
|
||||
|
||||
ssh user@ip_address -i key.pem
|
||||
|
||||
Get complete directory listing to local directory with wget:
|
||||
用wget获取完整目录列表到本地目录:
|
||||
|
||||
wget -r --no-parent --reject "index.html*" http://hostname/ -P /home/user/dirs
|
||||
|
||||
Create multiple directories:
|
||||
创建多个目录:
|
||||
|
||||
mkdir -p /home/user/{test,test1,test2}
|
||||
|
||||
List processes tree with child processes:
|
||||
列出带子进程的进程树:
|
||||
|
||||
ps axwef
|
||||
|
||||
Create war file:
|
||||
创建war文件:
|
||||
|
||||
jar -cvf name.war file
|
||||
|
||||
Test disk write speed:
|
||||
测试磁盘写速度:
|
||||
|
||||
dd if=/dev/zero of=/tmp/output.img bs=8k count=256k conv=fdatasync; rm -rf /tmp/output.img
|
||||
|
||||
Test disk read speed:
|
||||
测试磁盘读速度:
|
||||
|
||||
hdparm -Tt /dev/sda
|
||||
|
||||
Get md5 hash from text:
|
||||
从文本中获取md5值:
|
||||
|
||||
echo -n "text" | md5sum
|
||||
|
||||
Check xml syntax:
|
||||
检测xml语法:
|
||||
|
||||
xmllint --noout file.xml
|
||||
|
||||
Extract tar.gz in new directory:
|
||||
在新的目录中提取tar.gz文件:
|
||||
|
||||
tar zxvf package.tar.gz -C new_dir
|
||||
|
||||
Get HTTP headers with curl:
|
||||
用curl获取HTTP头:
|
||||
|
||||
curl -I http://www.example.com
|
||||
|
||||
Modify timestamp of some file or directory (YYMMDDhhmm):
|
||||
修改一些文件或目录的时间戳 (YYMMDDhhmm):
|
||||
|
||||
touch -t 0712250000 file
|
||||
|
||||
Download from ftp using wget:
|
||||
使用wget从ftp下载:
|
||||
|
||||
wget -m ftp://username:password@hostname
|
||||
|
||||
Generate random password (16 char long in this case):
|
||||
生成随机密码 (本例中16位字符长):
|
||||
|
||||
LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;
|
||||
|
||||
Quickly create a backup of a file:
|
||||
快速创建一个文件的备份:
|
||||
|
||||
cp some_file_name{,.bkp}
|
||||
|
||||
Access Windows share:
|
||||
访问Windows共享:
|
||||
|
||||
smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir
|
||||
|
||||
Run command from history (here at line 100):
|
||||
在历史中运行命令 (这里在第100行):
|
||||
|
||||
!100
|
||||
|
||||
Unzip to directory:
|
||||
unzip到目录中:
|
||||
|
||||
unzip package_name.zip -d dir_name
|
||||
|
||||
Multiline text (CTRL + d to exit):
|
||||
多行文字 (按 CTRL + d 退出):
|
||||
|
||||
cat > test.txt
|
||||
|
||||
Create empty file or empty existing one:
|
||||
创建空白的文件或者已存在的文件:
|
||||
|
||||
> test.txt
|
||||
|
||||
Update date from Ubuntu NTP server:
|
||||
从Ubuntu NTP服务器上更新日期:
|
||||
|
||||
ntpdate ntp.ubuntu.com
|
||||
|
||||
netstat show all tcp4 listening ports:
|
||||
netstat 显示所有tcp4监听的端口:
|
||||
|
||||
netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'
|
||||
|
||||
Convert image from qcow2 to raw:
|
||||
将qcow2图像转化成raw:
|
||||
|
||||
qemu-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img \
|
||||
precise-server-cloudimg-amd64-disk1.raw
|
||||
|
||||
Run command repeatedly, displaying it's output (default every two seconds):
|
||||
重复运行命令,显示它的输出 (默认2s刷新):
|
||||
|
||||
watch ps -ef
|
||||
|
||||
List all users:
|
||||
显示所有用户:
|
||||
|
||||
getent passwd
|
||||
|
||||
Mount root in read/write mode:
|
||||
以读写模式挂载root:
|
||||
|
||||
mount -o remount,rw /
|
||||
|
||||
Mount a directory (for cases when symlinking will not work):
|
||||
挂在目录 (适合于符号链接不成功的情况下):
|
||||
|
||||
mount --bind /source /destination
|
||||
|
||||
Send dynamic update to DNS server:
|
||||
发送动态更新给DNS:
|
||||
|
||||
nsupdate < <EOF
|
||||
update add $HOST 86400 A $IP
|
||||
send
|
||||
EOF
|
||||
|
||||
Recursively grep all directories:
|
||||
递归grep所有目录
|
||||
|
||||
grep -r "some_text" /path/to/dir
|
||||
|
||||
List ten largest open files:
|
||||
列出10个最大的已打开的文件:
|
||||
|
||||
lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB "$9 }' | sort -n -u | tail
|
||||
|
||||
Show free RAM in MB:
|
||||
以MB显示空余内存:
|
||||
|
||||
free -m | grep cache | awk '/[0-9]/{ print $4" MB" }'
|
||||
|
||||
Open Vim and jump to end of file:
|
||||
打开vim并跳转到文件最后:
|
||||
|
||||
vim + some_file_name
|
||||
|
||||
Git clone specific branch (master):
|
||||
git clone特定branch (master):
|
||||
|
||||
git clone git@github.com:name/app.git -b master
|
||||
|
||||
Git switch to another branch (develop):
|
||||
git切换到另外一个branch (develop):
|
||||
|
||||
git checkout develop
|
||||
|
||||
Git delete branch (myfeature):
|
||||
git删除一个branch(myfeature):
|
||||
|
||||
git branch -d myfeature
|
||||
|
||||
Git delete remote branch:
|
||||
Git删除一个远程branch:
|
||||
|
||||
git push origin :branchName
|
||||
|
||||
Git push new branch to remote:
|
||||
Git push 新的branch到远程:
|
||||
|
||||
git push -u origin mynewfeature
|
||||
|
||||
Print out the last cat command from history:
|
||||
打印history中最后的cat命令
|
||||
|
||||
!cat:p
|
||||
|
||||
Run your last cat command from history:
|
||||
运行history中的最后的cat命令:
|
||||
|
||||
!cat
|
||||
|
||||
Find all empty subdirectories in /home/user:
|
||||
找出在/home/user中的所有空子目录:
|
||||
|
||||
find /home/user -maxdepth 1 -type d -empty
|
||||
|
||||
Get all from line 50 to 60 in test.txt:
|
||||
得到test.txt中50到60行的文本:
|
||||
|
||||
< test.txt sed -n '50,60p'
|
||||
|
||||
Run last command (if it was: mkdir /root/test, below will run: sudo mkdir /root/test):
|
||||
运行最后的命令 (如果是: mkdir /root/test, 下面会运行: sudo mkdir /root/test):
|
||||
|
||||
sudo !!
|
||||
|
||||
Create temporary RAM filesystem - ramdisk (first create /tmpram directory):
|
||||
创建临时RAM文件系统 - ramdisk (首先创建在 /tmpram 目录):
|
||||
|
||||
mount -t tmpfs tmpfs /tmpram -o size=512m
|
||||
|
||||
Grep whole words:
|
||||
Grep完整单词:
|
||||
|
||||
grep -w "name" test.txt
|
||||
|
||||
Append text to a file that requires raised privileges:
|
||||
需要特权模式在一个文件后追加文本:
|
||||
|
||||
echo "some text" | sudo tee -a /path/file
|
||||
|
||||
List all supported kill signals:
|
||||
列出所有的kill信号:
|
||||
|
||||
kill -l
|
||||
|
||||
Generate random password (16 characters long in this case):
|
||||
生成随机密码 (本例中16个字符长):
|
||||
|
||||
openssl rand -base64 16
|
||||
|
||||
Do not log last session in bash history:
|
||||
在bash历史中不记录最后的会话:
|
||||
|
||||
kill -9 $$
|
||||
|
||||
Scan network to find open port:
|
||||
扫描网络找出打开的端口:
|
||||
|
||||
nmap -p 8081 172.20.0.0/16
|
||||
|
||||
Set git email:
|
||||
设置git email:
|
||||
|
||||
git config --global user.email "me@example.com"
|
||||
|
||||
To sync with master if you have unpublished commits:
|
||||
如果你有未提交的commit,与master同步:
|
||||
|
||||
git pull --rebase origin master
|
||||
|
||||
Move all files with "txt" in name to /home/user:
|
||||
将文件中含有txt的所有文件移动到/home/user:
|
||||
|
||||
find -iname "*txt*" -exec mv -v {} /home/user \;
|
||||
|
||||
Put the file lines side by side:
|
||||
一行行合并文件:
|
||||
|
||||
paste test.txt test1.txt
|
||||
|
||||
Progress bar in shell:
|
||||
shell中的进度条:
|
||||
|
||||
pv data.log
|
||||
|
||||
Send the data to server with netcat:
|
||||
用netcat发送数据给服务器:
|
||||
|
||||
echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000
|
||||
|
||||
Convert tabs to spaces:
|
||||
转换tab到空格:
|
||||
|
||||
expand test.txt > test1.txt
|
||||
|
||||
Skip bash history:
|
||||
跳过bash历史:
|
||||
|
||||
< <space>>cmd
|
||||
|
||||
Go to the previous working directory:
|
||||
回到先前的工作目录:
|
||||
|
||||
cd -
|
||||
|
||||
Split large tar.gz archive (100MB each) and put it back:
|
||||
切割大的tar.gz文件 (每个 100MB) 并还原:
|
||||
|
||||
split –b 100m /path/to/large/archive /path/to/output/files
|
||||
cat files* > archive
|
||||
|
||||
Get HTTP status code with curl:
|
||||
用curl获取HTTP状态值:
|
||||
|
||||
curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null
|
||||
|
||||
When Ctrl + c not works:
|
||||
当 Ctrl + c 没用时:
|
||||
|
||||
Ctrl + \
|
||||
|
||||
Get file owner:
|
||||
获取文件所有者:
|
||||
|
||||
stat -c %U file.txt
|
||||
|
||||
List block devices:
|
||||
列出块设备:
|
||||
|
||||
lsblk -f
|
||||
|
||||
Find files with trailing spaces:
|
||||
找出末尾空格的文件:
|
||||
|
||||
find . -type f -exec egrep -l " +$" "{}" \;
|
||||
|
||||
Find files with tabs indentation:
|
||||
找出用tab缩进的文件:
|
||||
|
||||
find . -type f -exec egrep -l $'\t' "{}" \;
|
||||
|
||||
Print horizontal line with "=":
|
||||
用"="打印水平行
|
||||
|
||||
printf '%100s\n' | tr ' ' =
|
||||
|
||||
**UPDATE: November 25, 2013**
|
||||
**更新: 2013年11月25日**
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.techbar.me/linux-shell-tips/
|
||||
|
||||
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
|
||||
译者:[geekpi](https://github.com/geekpi) 校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
Loading…
Reference in New Issue
Block a user