Linux shell tips and tricks

本贴最后更新于 4348 天前,其中的信息可能已经时移世改

I’m using Linux shell 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: June 15, 2013

Send process to background:

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; 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 web headers with curl:

curl -v -D - http://hostname -o /dev/null

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

UPDATE: June 15, 2013

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

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
  • mainlove

    ! 确实很少人用

An
这个人很懒,什么都没有留下。

推荐标签 标签

  • 程序员

    程序员是从事程序开发、程序维护的专业人员。

    589 引用 • 3538 回帖
  • Gitea

    Gitea 是一个开源社区驱动的轻量级代码托管解决方案,后端采用 Go 编写,采用 MIT 许可证。

    5 引用 • 16 回帖 • 3 关注
  • VirtualBox

    VirtualBox 是一款开源虚拟机软件,最早由德国 Innotek 公司开发,由 Sun Microsystems 公司出品的软件,使用 Qt 编写,在 Sun 被 Oracle 收购后正式更名成 Oracle VM VirtualBox。

    10 引用 • 2 回帖 • 17 关注
  • ActiveMQ

    ActiveMQ 是 Apache 旗下的一款开源消息总线系统,它完整实现了 JMS 规范,是一个企业级的消息中间件。

    19 引用 • 13 回帖 • 679 关注
  • PWA

    PWA(Progressive Web App)是 Google 在 2015 年提出、2016 年 6 月开始推广的项目。它结合了一系列现代 Web 技术,在网页应用中实现和原生应用相近的用户体验。

    14 引用 • 69 回帖 • 175 关注
  • ZooKeeper

    ZooKeeper 是一个分布式的,开放源码的分布式应用程序协调服务,是 Google 的 Chubby 一个开源的实现,是 Hadoop 和 HBase 的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

    59 引用 • 29 回帖 • 1 关注
  • Office

    Office 现已更名为 Microsoft 365. Microsoft 365 将高级 Office 应用(如 Word、Excel 和 PowerPoint)与 1 TB 的 OneDrive 云存储空间、高级安全性等结合在一起,可帮助你在任何设备上完成操作。

    5 引用 • 34 回帖
  • Chrome

    Chrome 又称 Google 浏览器,是一个由谷歌公司开发的网页浏览器。该浏览器是基于其他开源软件所编写,包括 WebKit,目标是提升稳定性、速度和安全性,并创造出简单且有效率的使用者界面。

    63 引用 • 289 回帖 • 1 关注
  • 书籍

    宋真宗赵恒曾经说过:“书中自有黄金屋,书中自有颜如玉。”

    78 引用 • 396 回帖
  • frp

    frp 是一个可用于内网穿透的高性能的反向代理应用,支持 TCP、UDP、 HTTP 和 HTTPS 协议。

    20 引用 • 7 回帖
  • 招聘

    哪里都缺人,哪里都不缺人。

    188 引用 • 1057 回帖 • 2 关注
  • 又拍云

    又拍云是国内领先的 CDN 服务提供商,国家工信部认证通过的“可信云”,乌云众测平台认证的“安全云”,为移动时代的创业者提供新一代的 CDN 加速服务。

    20 引用 • 37 回帖 • 576 关注
  • 新人

    让我们欢迎这对新人。哦,不好意思说错了,让我们欢迎这位新人!
    新手上路,请谨慎驾驶!

    52 引用 • 228 回帖
  • 持续集成

    持续集成(Continuous Integration)是一种软件开发实践,即团队开发成员经常集成他们的工作,通过每个成员每天至少集成一次,也就意味着每天可能会发生多次集成。每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验证,从而尽早地发现集成错误。

    15 引用 • 7 回帖 • 1 关注
  • WebComponents

    Web Components 是 W3C 定义的标准,它给了前端开发者扩展浏览器标签的能力,可以方便地定制可复用组件,更好的进行模块化开发,解放了前端开发者的生产力。

    1 引用 • 8 关注
  • 正则表达式

    正则表达式(Regular Expression)使用单个字符串来描述、匹配一系列遵循某个句法规则的字符串。

    31 引用 • 94 回帖
  • GitBook

    GitBook 使您的团队可以轻松编写和维护高质量的文档。 分享知识,提高团队的工作效率,让用户满意。

    3 引用 • 8 回帖 • 2 关注
  • 人工智能

    人工智能(Artificial Intelligence)是研究、开发用于模拟、延伸和扩展人的智能的理论、方法、技术及应用系统的一门技术科学。

    167 引用 • 315 回帖
  • 学习

    “梦想从学习开始,事业从实践起步” —— 习近平

    174 引用 • 539 回帖
  • Google

    Google(Google Inc.,NASDAQ:GOOG)是一家美国上市公司(公有股份公司),于 1998 年 9 月 7 日以私有股份公司的形式创立,设计并管理一个互联网搜索引擎。Google 公司的总部称作“Googleplex”,它位于加利福尼亚山景城。Google 目前被公认为是全球规模最大的搜索引擎,它提供了简单易用的免费服务。不作恶(Don't be evil)是谷歌公司的一项非正式的公司口号。

    49 引用 • 192 回帖 • 2 关注
  • 快应用

    快应用 是基于手机硬件平台的新型应用形态;标准是由主流手机厂商组成的快应用联盟联合制定;快应用标准的诞生将在研发接口、能力接入、开发者服务等层面建设标准平台;以平台化的生态模式对个人开发者和企业开发者全品类开放。

    15 引用 • 127 回帖 • 3 关注
  • Flutter

    Flutter 是谷歌的移动 UI 框架,可以快速在 iOS 和 Android 上构建高质量的原生用户界面。 Flutter 可以与现有的代码一起工作,它正在被越来越多的开发者和组织使用,并且 Flutter 是完全免费、开源的。

    39 引用 • 92 回帖 • 4 关注
  • WiFiDog

    WiFiDog 是一套开源的无线热点认证管理工具,主要功能包括:位置相关的内容递送;用户认证和授权;集中式网络监控。

    1 引用 • 7 回帖 • 609 关注
  • danl
    165 关注
  • Mobi.css

    Mobi.css is a lightweight, flexible CSS framework that focus on mobile.

    1 引用 • 6 回帖 • 760 关注
  • Sandbox

    如果帖子标签含有 Sandbox ,则该帖子会被视为“测试帖”,主要用于测试社区功能,排查 bug 等,该标签下内容不定期进行清理。

    434 引用 • 1250 回帖 • 595 关注
  • 微信

    腾讯公司 2011 年 1 月 21 日推出的一款手机通讯软件。用户可以通过摇一摇、搜索号码、扫描二维码等添加好友和关注公众平台,同时可以将自己看到的精彩内容分享到微信朋友圈。

    133 引用 • 796 回帖 • 1 关注