Linux shell tips and tricks

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

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
这个人很懒,什么都没有留下。

推荐标签 标签

  • API

    应用程序编程接口(Application Programming Interface)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。

    76 引用 • 429 回帖 • 5 关注
  • Netty

    Netty 是一个基于 NIO 的客户端-服务器编程框架,使用 Netty 可以让你快速、简单地开发出一个可维护、高性能的网络应用,例如实现了某种协议的客户、服务端应用。

    49 引用 • 33 回帖 • 21 关注
  • 思源笔记

    思源笔记是一款隐私优先的个人知识管理系统,支持完全离线使用,同时也支持端到端加密同步。

    融合块、大纲和双向链接,重构你的思维。

    20643 引用 • 80672 回帖 • 1 关注
  • MongoDB

    MongoDB(来自于英文单词“Humongous”,中文含义为“庞大”)是一个基于分布式文件存储的数据库,由 C++ 语言编写。旨在为应用提供可扩展的高性能数据存储解决方案。MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似 JSON 的 BSON 格式,因此可以存储比较复杂的数据类型。

    90 引用 • 59 回帖
  • V2EX

    V2EX 是创意工作者们的社区。这里目前汇聚了超过 400,000 名主要来自互联网行业、游戏行业和媒体行业的创意工作者。V2EX 希望能够成为创意工作者们的生活和事业的一部分。

    17 引用 • 236 回帖 • 371 关注
  • OpenResty

    OpenResty 是一个基于 NGINX 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

    17 引用 • 45 关注
  • 持续集成

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

    14 引用 • 7 回帖
  • Webswing

    Webswing 是一个能将任何 Swing 应用通过纯 HTML5 运行在浏览器中的 Web 服务器,详细介绍请看 将 Java Swing 应用变成 Web 应用

    1 引用 • 15 回帖 • 621 关注
  • PostgreSQL

    PostgreSQL 是一款功能强大的企业级数据库系统,在 BSD 开源许可证下发布。

    22 引用 • 22 回帖
  • TensorFlow

    TensorFlow 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数组,即张量(tensor)。

    20 引用 • 19 回帖 • 3 关注
  • 开源

    Open Source, Open Mind, Open Sight, Open Future!

    405 引用 • 3557 回帖
  • CAP

    CAP 指的是在一个分布式系统中, Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可兼得。

    11 引用 • 5 回帖 • 590 关注
  • 禅道

    禅道是一款国产的开源项目管理软件,她的核心管理思想基于敏捷方法 scrum,内置了产品管理和项目管理,同时又根据国内研发现状补充了测试管理、计划管理、发布管理、文档管理、事务管理等功能,在一个软件中就可以将软件研发中的需求、任务、bug、用例、计划、发布等要素有序的跟踪管理起来,完整地覆盖了项目管理的核心流程。

    6 引用 • 15 回帖 • 169 关注
  • Flutter

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

    39 引用 • 92 回帖 • 8 关注
  • Hprose

    Hprose 是一款先进的轻量级、跨语言、跨平台、无侵入式、高性能动态远程对象调用引擎库。它不仅简单易用,而且功能强大。你无需专门学习,只需看上几眼,就能用它轻松构建分布式应用系统。

    9 引用 • 17 回帖 • 606 关注
  • App

    App(应用程序,Application 的缩写)一般指手机软件。

    90 引用 • 383 回帖
  • 倾城之链
    23 引用 • 66 回帖 • 125 关注
  • CentOS

    CentOS(Community Enterprise Operating System)是 Linux 发行版之一,它是来自于 Red Hat Enterprise Linux 依照开放源代码规定释出的源代码所编译而成。由于出自同样的源代码,因此有些要求高度稳定的服务器以 CentOS 替代商业版的 Red Hat Enterprise Linux 使用。两者的不同在于 CentOS 并不包含封闭源代码软件。

    238 引用 • 224 回帖 • 2 关注
  • FreeMarker

    FreeMarker 是一款好用且功能强大的 Java 模版引擎。

    23 引用 • 20 回帖 • 444 关注
  • C

    C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

    83 引用 • 165 回帖 • 3 关注
  • danl
    98 关注
  • Flume

    Flume 是一套分布式的、可靠的,可用于有效地收集、聚合和搬运大量日志数据的服务架构。

    9 引用 • 6 回帖 • 616 关注
  • 分享

    有什么新发现就分享给大家吧!

    246 引用 • 1780 回帖
  • HTML

    HTML5 是 HTML 下一个的主要修订版本,现在仍处于发展阶段。广义论及 HTML5 时,实际指的是包括 HTML、CSS 和 JavaScript 在内的一套技术组合。

    103 引用 • 294 回帖
  • RYMCU

    RYMCU 致力于打造一个即严谨又活泼、专业又不失有趣,为数百万人服务的开源嵌入式知识学习交流平台。

    4 引用 • 6 回帖 • 42 关注
  • OpenShift

    红帽提供的 PaaS 云,支持多种编程语言,为开发人员提供了更为灵活的框架、存储选择。

    14 引用 • 20 回帖 • 617 关注
  • Logseq

    Logseq 是一个隐私优先、开源的知识库工具。

    Logseq is a joyful, open-source outliner that works on top of local plain-text Markdown and Org-mode files. Use it to write, organize and share your thoughts, keep your to-do list, and build your own digital garden.

    5 引用 • 62 回帖 • 10 关注