小技巧: 实用的一行 Linux 命令

下面这些 Linux 命令都只有一行,虽然简短,但却非常实用。如若善于使用它们,定会给你的 Linux 使用过程带来便利。其中包括创建存档文件、递归查找文件内的字符串、搜索并替换文件里的内容、查看磁盘及目录占用情况等。

  1. 创建存档文件 tar -czpf folder_name.tar.gz folder_name 该命令将 folder_name 创建为 folder_name.tar.gz 存档文件。
  2. 递归查找文件内的字符串 find ./ -name ‘*.html’ -exec grep “breadcrumbs.inc.php” ‘{}’ \; -print 这条命令将查找所有包含 breadcrumbs.inc.php 的 HTML 文件。
  3. 搜索并替换文件里的内容 sed -i ’s/b/strong/g’ index.html 此命令搜索 index.html 文件中的 b 并将其替换为 strong。
  4. 查看目录的磁盘占用情况 du -h --max-depth=1 | sort -n -r

[via Micah Carrick]

Read More:

12 Comments

  1. 1 Gundamdriver Commented @ 2007-11-16 17:35Reply to this comment

    find ./ -name ‘*.html’

    原來是單引號啊~ 謝謝。

  2. 2 漂在北方的狼 Commented @ 2007-11-16 17:57Reply to this comment

    查看目录的磁盘占用情况

    du -h --max-depth=1 | sort -n -r

    我觉得,要达到这样的目的,还不如用 du -hs

  3. 3 AnThOnYho Commented @ 2007-11-16 19:13Reply to this comment

    http://linux.byexamples.com/ 这个网站有更多的例子和技巧。

  4. 4 lisir Commented @ 2007-11-16 23:49Reply to this comment

    2楼说的在理,看到man手册里说max-depth=1时与-s相同。

  5. 5 ahtya Commented @ 2007-11-17 0:30Reply to this comment

    强,收教了

  6. 6 qinyafei Commented @ 2007-11-17 1:13Reply to this comment

    那继续补充几个不太常用的吧…… =,= ls -ltr (ll -tr) 按时间排序 最下面是最新修改的文件 ls -Slhr (ll -Shr) 按文件大小排序 最下面是最大的

  7. 7 小狼诺夫 Commented @ 2007-11-17 11:14Reply to this comment

    alias ls='ls --color=auto' alias ll='ls -l' alias la='ll -a' alias man="TERMINFO=~/.terminfo/ LESS=C TERM=mostlike PAGER=less man" alias soft-install='sudo apt-get install' alias soft-remove='sudo apt-get autoremove' alias soft-update='sudo apt-get update;sudo apt-get upgrade;sudo apt-get dist-upgrade' alias soft-clear='sudo localepurge;sudo apt-get clean; sudo apt-get autoclean; sudo apt- get autoremove' alias soft-search='apt-cache search' alias df='df -h' alias grep='grep --color' alias psgrep='ps aux | grep' alias dirsize='SIZE=sudo du -sm |tr \. \B | tr [:blank:] \M;echo "size of current dir:$ SIZE"' alias unzip1='tar -xzvf' alias unzip2='tar -xjvf' alias rmd='rm -rf' alias checkupdate='apt-cache policy' alias showstat='apt-cache show' alias memsize="grep 'Memory' /var/log/dmesg" alias mypstree="pstree -Ghp $USER" alias useusb="sudo mount -t vfat /dev/sda1 /media/usb/ -o iocharset=utf8,rw,uid=1000,gid= 1000;rox /media/usb" alias closeusb="sudo umount /media/usb"

  8. 8 anonymous Commented @ 2007-11-17 13:10Reply to this comment

    递归查找文件内的字符 这样更简单:

    grep -ri "test string" *.html

  9. 9 xtra Commented @ 2007-11-17 19:23Reply to this comment

    sed -i ’s/b/strong/g’ index.html

    这个我试过似乎没作用???

  10. 10 wyg1258 Commented @ 2007-11-17 20:21Reply to this comment

    @小狼诺夫: 这个。。。干脆用语音识别算了

  11. 11 ericjonah Commented @ 2008-02-18 11:26Reply to this comment

    sed -e 's/b/strong/g' index.html

  12. 12 aiqin’s Ubuntu life » Blog Archive » 小技巧: 实用的一行 Linux 命令 Pingbacked @ 2008-04-12 9:13Reply to this comment

    [...] 小技巧: 实用的一行 Linux 命令 Posted in Ubuntu by admin on 11月 17th, 2007 摘自LinuxTOY: http://linuxtoy.org/archives/useful-linux-commands-2.html [...]