显示标签为“unix/linux”的博文。显示所有博文
显示标签为“unix/linux”的博文。显示所有博文

(转载)50个最常用的unix/linux命令



本文以例子的方式列出了在linux/unix中最常用的50个命令,同时,本文可能不会非常详细地介绍每个命令,只是给出一些基本介绍。

1. tar

创建一个tar包 $ tar cvf archive_name.tar dirname/



解压tar包$ tar xvf archive_name.tar



查看tar包中有哪些文件$ tar tvf archive_name.tar



更多的例子 The Ultimate Tar Command Tutorial with 10 Practical Examples

2. grep

在文件中查找特定字符串(大小写敏感)$ grep -i "the" demo_file



打印匹配的行,同时输出其后的3行 $ grep -A 3 -i "example" demo_text



递归的查找$ grep -r "ramesh" * 更多的例子: <a href="http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/">Get a Grip on the Grep! – 15 Practical Grep Command Examples </a> 3. find 用名字来查找文件(大小写敏感) [code language=bash]# find -iname "MyCProgram.c"



对查找出来的文件执行命令 $ find -iname "MyCProgram.c" -exec md5sum {} \;



查找$HOME文件下的的空文件 # find ~ -empty



更多的例子: Mommy, I found it! — 15 Practical Linux Find Command Examples

4. ssh

登录到远程主机ssh -l jsmith remotehost.example.com



调试ssh客户端(显示更多的信息)ssh -v -l jsmith remotehost.example.com



显示ssh版本

$ ssh -VOpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003



更多的例子: 5 Basic Linux SSH Client Commands

5. sed

当你从嗯windows拷贝文件到unix时,你会发现在每行末尾有”\r\n”,本例子将dos的文件格式转换为unix文件格式$sed 's/.$//' filename



反向输出文件内容 $ sed -n '1!G;h;$p' thegeekstuff.txt



为文件中的所有非空行添加行号$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'



更多的例子: Advanced Sed Substitution Examples

6. awk

删除重复行$ awk '!($0 in array) { array[$0]; print }' temp



输出/etc/passwd中所有uid gid相同的行$awk -F ':' '$3==$4' passwd.txt



输出文件的某些域$ awk '{print $2,$5;}' employee.txt



更多的例子 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

7. vim

编辑文件的143行$ vim +143 filename.txt



进入匹配到的第一行 $ vim +/search-term filename.txt



以只读方式打开文件$ vim -R /etc/passwd



更多的例子: How To Record and Play in Vim Editor

8. diff

比较的时候忽略空白行# diff -w name_list.txt name_list_new.txt 2c2,3 < John Doe --- > John M Doe > Jason Bourne



更多的例子 Top 4 File Difference Tools on UNIX / Linux – Diff, Colordiff, Wdiff, Vimdiff

9. sort command examples

以升序排列文件$ sort names.txt



以降序排列文件$ sort -r names.txt



以第三列排序文件 $ sort -t: -k 3n /etc/passwd | more



10. export

查看oracle的环境变量 $ export | grep ORACLE declare -x ORACLE_BASE="/u01/app/oracle" declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0" declare -x ORACLE_SID="med" declare -x ORACLE_TERM="xterm"



export一个变量$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0



11. xargs

将所有的图片拷贝的外置硬盘# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory



找到系统中所有的jpg图片,并且打包# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz



下载url-list.txt提到的所有URLS# cat url-list.txt | xargs wget –c



12. ls

以易读的方式显示文件大小(例子 KB, MB etc.,)$ ls -lh -rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz



以最后修改时间给文件反向排序 $ ls -ltr



显示文件的分类$ ls -F



更多的例子: Unix LS Command: 15 Practical Examples

13. pwd

pwd用来显示工作目录

14. cd Cd - 可以在两个最近使用的目录之间切换 shopt -s cdspell 可以自动更改cd到某个目录时的拼写错误



更多的例子: 6 Awesome Linux cd command Hacks

15. gzip

创建一个.gz文档$ gzip test.txt



解压缩 $ gzip -d test.txt.gz



显示压缩文件的压缩比率$ gzip -l *.gz compressed uncompressed ratio uncompressed_name 23709 97975 75.8% asp-patch-rpms.txt



16. bzip2

创建*.bz2 文档 $ bzip2 test.txt



解压缩*.bz2 文档bzip2 -d test.txt.bz2



更多的例子: BZ is Eazy! bzip2, bzgrep, bzcmp, bzdiff, bzcat, bzless, bzmore examples

17. unzip

解压缩a *.zip$ unzip test.zip



查看*.zip文件,不解压 $ unzip -l jasper.zip Archive: jasper.zip Length Date Time Name -------- ---- ---- ---- 40995 11-30-98 23:50 META-INF/MANIFEST.MF 32169 08-25-98 21:07 classes_ 15964 08-25-98 21:07 classes_names 10542 08-25-98 21:07 classes_ncomp



18. shutdown

立刻关机 # shutdown -h now



10分钟后关机# shutdown -h +10



重启# shutdown -r now



在重启期间强制检查文件系统 # shutdown -Fr now



19. ftp

ftp和sftp都有类似的命令。链接远程主机,下载文件$ ftp IP/hostname ftp> mget *.html



在下载之前查看远程主机的文件的名ftp> mls *.html - /ftptest/features.html /ftptest/index.html /ftptest/othertools.html /ftptest/samplereport.html /ftptest/usage.html



更多的例子: FTP and SFTP Beginners Guide with 10 Examples

20. crontab

查看特定用户的crontab列表# crontab -u john -l



设置cron任务每10分钟执行*/10 * * * * /home/ramesh/check-disk-space



更多的例子: Linux Crontab: 15 Awesome Cron Job Examples

21. service

Service用来允许 system V 格式的初始化脚本 ,比如说,与其带着全路径运行 /etc/init.d/ 下的命令, 你可以使用service命令

显示某个服务的状态# service ssh status



显示所有服务的状态service --status-all



重启某个服务 # service ssh restart



22. ps

ps 显示系统中进程的信息。ps有很多的参数,但主要的有下面的

查看当前所有进程 $ ps -ef | more



以树形结构查看$ ps -efH | more



23. free

显示系统中空闲,已用,交换内存的信息

常规的free命令输出。通常输出单位是字节$ free total used free shared buffers cached Mem: 3566408 1580220 1986188 0 203988 902960 -/+ buffers/cache: 473272 3093136 Swap: 4000176 0 4000176



如果加参数-g,以GB单位显示,而-b以字节,-k 以千字节,-m以m单位显示 $ free -g total used free shared buffers cached Mem: 3 1 1 0 0 0 -/+ buffers/cache: 0 2 Swap: 3 0 3



如果你想查看所有的内存(包括swap)加-t选项 ramesh@ramesh-laptop:~$ free -t total used free shared buffers cached Mem: 3566408 1592148 1974260 0 204260 912556 -/+ buffers/cache: 475332 3091076 Swap: 4000176 0 4000176 Total: 7566584 1592148 5974436



24. top

top 显示系统中的cpu消耗量最大的进程 。要对输出以列排序的话,按“O”,得到下面的项目,你可以以下面的选项进行排序。 Current Sort Field: P for window 1:Def Select sort field via field letter, type any other key to return a: PID = Process Id v: nDRT = Dirty Pages count d: UID = User Id y: WCHAN = Sleeping in Function e: USER = User Name z: Flags = Task Flags ........



-u显示属于某个用户的进程$ top -u oracle



更多的例子 Can You Top This? 15 Practical Linux Top Command Examples

25. df

显示文件系统的磁盘使用情况。默认输出以字节为单位$ df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda1 29530400 3233104 24797232 12% / /dev/sda2 120367992 50171596 64082060 44% /home



df -h 以GB位单位输出 ramesh@ramesh-laptop:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 29G 3.1G 24G 12% / /dev/sda2 115G 48G 62G 44% /home



-T 选项显示文件系统类型 ramesh@ramesh-laptop:~$ df -T Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/sda1 ext4 29530400 3233120 24797216 12% / /dev/sda2 ext4 120367992 50171596 64082060 44% /home


26. kill kill 用来终止一个进程。首先,用ps -ef查找进程ID,然后用kill -9来杀掉这个进程。你还可以运用killall,pkill,xkill来杀掉一个unix进程 $ ps -ef | grep vim ramesh 7243 7222 9 22:43 pts/2 00:00:00 vim
$ kill -9 7243



更多的例子: 4 Ways to Kill a Process – kill, killall, pkill, xkill 27. rm 在删除文件前提示 $ rm -i filename.txt



可以在文件名参数中使用通配符 交互删除所有的file开头的文件 $ rm -i file*



删除example文件夹本身以及其下文件夹 $ rm -r example



28. cp 将file1复制至file2,同时保存文件的修改时间和权限 $ cp -p file1 file2



将file1复制至file2,如果file2存在提示是否覆盖 $ cp -i file1 file2



29. mv 将 file1 重命名为 file2. 如果file2存在,则提示是否覆盖 $ mv -i file1 file2



mv -f 和mv -i相反,强制覆盖 mv -v 则显示详细信息。通常在使用shell的通配符时非常有用 $ mv -v file1 file2



30. cat 可以同时显示许多文件。file1之后输出file2的内容 $ cat file1 file2



cat -n 选项会在每个输出行上加上行号 $ cat -n /etc/logrotate.conf 1 /var/log/btmp { 2 missingok 3 monthly 4 create 0660 root utmp 5 rotate 1 6 }



31. mount 在挂载之前,先创建一个目录 # mkdir /u01 # mount /dev/sdb1 /u01



写到fstab中以实现自动挂载 /dev/sdb1 /u01 ext2 defaults 0 2



32. chmod chmod 用来改变文件和文件夹的权限 给于文件所属主和属组所有权限 $ chmod ug+rwx file.txt



删除文件所属组的一切权限 $ chmod g-rwx file.txt



将文件权限递归的赋予子目录下的所有文件 $ chmod -R ug+rwx file.txt



更多的例子: 7 Chmod Command Examples for Beginners 33. chown chown 用来改变文件的属主和属组 将文件的属主变为oracle,属组变为db $ chown oracle:dba dbora.sh



-R用来递归 $ chown -R oracle:dba /home/oracle



34. passwd 用来修改配置用户的密码 $ passwd



root用户可以用passwd来修改其他用户的密码 # passwd USERNAME



移除某个用户的密码,一旦密码被移除,用户可以无密码登录 # passwd -d USERNAME



35. mkdir 在主目录下创建一个temp目录 $ mkdir ~/temp



创建一个多层的目录.如果相应的目录不存在,则创建. $ mkdir -p dir1/dir2/dir3/dir4/



36. ifconfig 查看和编辑网络链接. 查看所有网络链接情况 $ ifconfig -a



激活和停止某个网卡 $ ifconfig eth0 up



$ ifconfig eth0 down 更多的例子: Ifconfig: 7 Examples To Configure Network Interface 37. uname Uname 列出系统的重要信息,,比如:— 内核名,主机名,内核版本 ,cpu类型等等 以ubuntu系统为例 $ uname -a Linux john-laptop 2.6.32-24-generic #41-Ubuntu SMP Thu Aug 19 01:12:52 UTC 2010 i686 GNU/Linux



38. whereis 查找某个unix所在的地方(例如ls命令在哪里) $ whereis ls ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz



你可以使用-B参数来替代whereis默认寻找的路径.在/tmp下寻找可执行文件lsmk并显示出来 $ whereis -u -B /tmp -f lsmk lsmk: /tmp/lsmk



39. whatis Whatis 显示一行有关某个命令的描述 $ whatis ls ls (1) - list directory contents
$ whatis ifconfig ifconfig (8) - configure a network interface



40. locate c 用locate命令快速的查找某个文件,或者许多文件.用updatedb命令创建locate寻找时使用的数据库 下列例子显示系统中包含单词crontab的文件 $ locate crontab /etc/anacrontab /etc/crontab /usr/bin/crontab /usr/share/doc/cron/examples/crontab2english.pl.gz /usr/share/man/man1/crontab.1.gz /usr/share/man/man5/anacrontab.5.gz /usr/share/man/man5/crontab.5.gz /usr/share/vim/vim72/syntax/crontab.vim



41. man 显示特定命令的man手册 $ man crontab



当一个命令的man手册有许多部分时,你可以指定读取特定部分] $ man SECTION-NUMBER commandname



man手册的8个部分 1、Standard commands (标准命令) 2、System calls (系统调用) 3、Library functions (库函数) 4、Special devices (设备说明) 5、File formats (文件格式) 6、Games and toys (游戏和娱乐) 7、Miscellaneous (杂项) 8、Administrative Commands (管理员命令) 比如说, 输入whatis crontab,会发现有2部分,1和5,查看5部分的man手册 $ whatis crontab crontab (1) - maintain crontab files for individual users (V3) crontab (5) - tables for driving cron $ man 5 crontab



42. tail 输出一个文件的最后10行 $ tail filename.txt



显示filename.txt的最后N行 $ tail -n N filename.txt



查看文件的实时状态tail -f. 此命令通常用来查看持续增长的log文件, 可用Ctrl-C中断 $ tail -f log-file



更多的例子: 3 Methods To View tail -f output of Multiple Log Files in One Terminal 43. less less 在查看大型日志文件时非常有效, 他不需要在完全打开文件的情况下查看文件内容 $ less huge-log-file.log



当使用less命令打开文件时,下边这两个快捷键非常有用 CTRL+F – forward one window



CTRL+B – backward one window 更多的例子 Unix Less Command: 10 Tips for Effective Navigation 44. su 切换到另外一个用户,超级用户可以切换到任意用户而不输入密码 $ su - USERNAME



仅仅以另外一个用户执行一次特定命令,以下例子中,john以raj的身份执行ls命令.执行之后马上回复john用户身份 [john@dev-server]$ su - raj -c 'ls'



[john@dev-server]$ 登录某个特定用户,同时指定使用某个shell $ su -s 'SHELLNAME' USERNAME



45. mysql mysql 可能是linux系统上最广泛使用的开源数据库了,尽管你在自己的服务器上没有使用mysql数据库,但是你实际上可能是一个mysql的终端用户,使用mysql命令链接远程数据库 链接远程数据库,通常会提示输入密码 $ mysql -u root -p -h 192.168.1.2



链接本地mysql数据库 $ mysql -u root -p



如果你想指定mysql的root密码,在-p之后输入,记住!不要留任何空白,紧跟着输入 46. yum 安装apache服务器 $ yum install httpd



升级apache服务器 $ yum update httpd



写在apache服务器. $ yum remove httpd



47. rpm 安装apache # rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm



升级apache # rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm



移除apache # rpm -ev httpd



更多的例子: RPM Command: 15 Examples to Install, Uninstall, Upgrade, Query RPM Packages 48. ping Ping远程主机,ping5次 $ ping -c 5 gmail.com



更多的例子: Ping Tutorial: 15 Effective Ping Command Examples 49. date 设置系统时间 # date -s "01/31/2010 23:59:53"



一旦你改变了系统时间,你需要将系统时间和硬件时间同步更新 # hwclock –systohc # hwclock --systohc –utc



50. wget 快捷有效地下载软件,音乐,视频的wget命令 $ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz



下载,但是起另外一个名字 $ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701



更多额例子 The Ultimate Wget Download Guide With 15 Awesome Examples
原文地址:http://www.freetstar.com/50-top-frequently-used-unixlinux-commands-1