2019独角兽企业重金招聘Python工程师标准>>>

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.

UPDATE: November 25, 2013

Check if remote port is open with 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:

openssl rand -hex n

Execute commands from a file in the current shell:

source /home/user/file.name

Substring for first 5 characters:

${variable:0:5}

SSH debug mode:

ssh -vvv user@ip_address

SSH with pem key:

ssh user@ip_address -i key.pem

Get complete directory listing to local directory with 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:

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:

echo -n "text" | md5sum

Check xml syntax:

xmllint --noout file.xml

Extract tar.gz in new directory:

tar zxvf package.tar.gz -C new_dir

Get HTTP headers with curl:

curl -I http://www.example.com

Modify timestamp of some file or directory (YYMMDDhhmm):

touch -t 0712250000 file

Download from ftp using wget:

wget -m ftp://username:password@hostname

Generate random password (16 char long in this case):

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:

smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir

Run command from history (here at line 100):

!100

Unzip to directory:

unzip package_name.zip -d dir_name

Multiline text (CTRL + d to exit):

cat > test.txt

Create empty file or empty existing one:

> test.txt

Update date from Ubuntu NTP server:

ntpdate ntp.ubuntu.com

netstat show all tcp4 listening ports:

netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'

Convert image from qcow2 to 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):

watch ps -ef

List all users:

getent passwd

Mount root in read/write mode:

mount -o remount,rw /

Mount a directory (for cases when symlinking will not work):

mount --bind /source /destination

Send dynamic update to DNS server:

nsupdate < <EOF
update add $HOST 86400 A $IP
send
EOF

Recursively grep all directories:

grep -r "some_text" /path/to/dir

List ten largest open files:

lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB "$9 }' | sort -n -u | tail

Show free RAM in MB:

free -m | grep cache | awk '/[0-9]/{ print $4" MB" }'

Open Vim and jump to end of file:

vim + some_file_name

Git clone specific branch (master):

git clone git@github.com:name/app.git -b master

Git switch to another branch (develop):

git checkout develop

Git delete branch (myfeature):

git branch -d myfeature

Git delete remote branch:

git push origin :branchName

Git push new branch to remote:

git push -u origin mynewfeature

Print out the last cat command from history:

!cat:p

Run your last cat command from history:

!cat

Find all empty subdirectories in /home/user:

find /home/user -maxdepth 1 -type d -empty

Get all from line 50 to 60 in test.txt:

< test.txt sed -n '50,60p'

Run last command (if it was: mkdir /root/test, below will run: sudo mkdir /root/test):

sudo !!

Create temporary RAM filesystem - ramdisk (first create /tmpram directory):

mount -t tmpfs tmpfs /tmpram -o size=512m

Grep whole words:

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 -l

Generate random password (16 characters long in this case):

openssl rand -base64 16

Do not log last session in bash history:

kill -9 $$

Scan network to find open port:

nmap -p 8081 172.20.0.0/16

Set git email:

git config --global user.email "me@example.com"

To sync with master if you have unpublished commits:

git pull --rebase origin master

Move all files with "txt" in name to /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:

pv data.log

Send the data to server with netcat:

echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000

Convert tabs to spaces:

expand test.txt > test1.txt

Skip bash history:

< <space>>cmd

Go to the previous working directory:

cd -

Split large tar.gz archive (100MB each) and put it back:

split –b 100m /path/to/large/archive /path/to/output/files
cat files* > archive

Get HTTP status code with curl:

curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null

When Ctrl + c not works:

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:

find . -type f -exec egrep -l $'\t' "{}" \;

Print horizontal line with "=":

printf '%100s\n' | tr ' ' =

UPDATE: November 25, 2013

转载于:https://my.oschina.net/zungyiu/blog/180664

Linux shell tips and tricks相关推荐

  1. Linux Shell Tips小技巧

    文章目录 sed 指定行 删除文本 替换文本 小技巧 查找N天内修改文件 Shell写R语言 makefile写shell bad interpreter错误 替换换行符为空格 压缩并打包目录 重定向 ...

  2. Remote Development Tips and Tricks

    目录 SSH tips# Configuring key based authentication# Quick start: Using SSH keys# Improving your secur ...

  3. Linux—shell中$(( ))、$( )、``与${ }的区别

    命令替换 在bash中,$( )与` `(反引号)都是用来作命令替换的. 命令替换与变量替换差不多,都是用来重组命令行的,先完成引号里的命令行,然后将其结果替换出来,再重组成新的命令行. exp 1 ...

  4. linux shell find depth,搞定 Linux Shell 文本处理工具,看完这篇集锦就够了

    原标题:搞定 Linux Shell 文本处理工具,看完这篇集锦就够了 Linux Shell是一种基本功,由于怪异的语法加之较差的可读性,通常被Python等脚本代替.既然是基本功,那就需要掌握,毕 ...

  5. 第二十五期:搞定Linux Shell文本处理工具,看完这篇集锦就够了

    Linux Shell是一种基本功,由于怪异的语法加之较差的可读性,通常被Python等脚本代替.既然是基本功,那就需要掌握,毕竟学习Shell脚本的过程中,还是能了解到很多Linux系统的内容. L ...

  6. 搞定Linux Shell文本处理工具,看完这篇集锦就够了(转)

    Linux Shell是一种基本功,由于怪异的语法加之较差的可读性,通常被Python等脚本代替.既然是基本功,那就需要掌握,毕竟学习Shell脚本的过程中,还是能了解到很多Linux系统的内容. L ...

  7. Linux Shell 文本处理工具集锦 zz

    内容目录: find 文件查找 grep 文本搜索 xargs 命令行参数转换 sort 排序 uniq 消除重复行 用tr进行转换 cut 按列切分文本 paste 按列拼接文本 wc 统计行和字符 ...

  8. Juniper JUNOS Commands (Tips and Tricks)

    Juniper Networks has a Day one book for 'JunOS Tips, Techniques, and Templates 2011' in Junos Fundam ...

  9. Linux shell的简单学习

    Linux shell的简单学习 shell script 其实就是纯文本档,我们可以编辑这个档案,然后讥这个档案来帮我们一次执行多个指令, 戒者是刟用一些运算不逡辑刞断来帮我们达成某些功能. Lin ...

最新文章

  1. Go 编译的可执行文件是否有动态库链接?
  2. 前端img里面的src能是bmp么_实习|字节跳动前端实习生(非科班已定offer)三技术面+一HR面...
  3. 全网最经典26道Spring面试题总结,终获offer
  4. 安装 m2eclipse 插件
  5. Qt下使用OpenCV3打开摄像头并把图像显示到QLabel上
  6. wxWidgets:wxButton类用法
  7. ps、top 、free查看用户资源信息
  8. 计算机网络基础常考简答题,计算机网络基础知识简答题
  9. Sping : @InitBinder注解
  10. Docker学习总结(58)——Dockerfile中,ADD和COPY的区别?
  11. savefiledialog对话框的取消和确定按钮分别返回一个什么值?_确定按钮该放在左边还是右边?...
  12. tp3.2框架关闭日志记录
  13. BZOJ5475 WC2019数树(prufer+容斥原理+树形dp+多项式exp)
  14. 进阶14 File类、遍历、递归
  15. 操作系统——实验一(Linux基本操作)
  16. 2017ps计算机考证
  17. openLooKeng视图详解
  18. 周易六十四卦——水火既济卦
  19. 网易im即时通讯 移动端嵌入web
  20. 从loss的硬截断、软化到Focal Loss

热门文章

  1. 解决ubuntu系统root用户下Chrome无法启动问题
  2. php 微信分享链接怎么弄,PHP实现 微信--分享朋友链接
  3. linux火狐自动更新,CentOS 7手动更新firefox | Linux系统运维联盟
  4. 元宇宙综观—愿景、技术和应对
  5. 面试中,答不出产品方法论?4个方法教给你...
  6. 未来的语音世界——中国智能语音市场分析
  7. App 运营的指标具体都有哪些?
  8. 第四届大数据科学与工程国际会议(2019)
  9. 作者:黄媛洁(1992-),女,食品安全大数据技术北京市重点实验室、北京工商大学计算机与信息工程学院硕士生...
  10. 作者:沈志宏(1977-),男,博士,中国科学院计算机网络信息中心高级工程师...