您的位置:首页 > 运维架构 > Linux

50个最常用的UNIX/Linux命令

2015-05-14 15:47 274 查看

1.tar 命令示例

创建一个新的tar存档文件.

$ tar cvf archive_name[新存档文件名称].tar dirname[被存档文件名称]/


解压已存在的存档文件.

$ tar xvf archive_name.tar


查看已存在的存档文件中包含哪些文件

$ tar tvf archive_name.tar


2.grep 命令示例

在文件中搜索一个给定的字符串 .

$ grep -i "the" demo_file


显示匹配行和之后的3行数据.

$ grep -A 3 -i "example" demo_text


显示匹配行和之前的2行数据.

$ grep -B 2 "example" demo_text


显示之前的2行,之后的2行数据.

$ grep -C 2 "example" demo_text


在所有文件递归搜索对于一个给定的字符串

$ grep -r "ramesh" *


3.find 命令示例

使用文件名查找文件

$ find -iname "MyCProgram.c"


find命令找到的文件上执行命令

$ find -iname "MyCProgram.c" -exec md5sum {} \;


在主目录找到空的所有文件

$ find ~ -empty


4.ssh 命令示例

登录到远程主机

ssh -l jsmith remotehost.example.com


调试ssh客户机

ssh -v -l jsmith remotehost.example.com


显示ssh客户机版本

$ ssh -V


5.sed 命令示例

DOS文件复制到Unix时,您可能会在每一行的结束发现\ r \ n。这个例子将DOS文件格式转换为Unix文件格式使用sed命令.

$ sed 's/.$//' filename


以相反的顺序显示文件内容

$ sed -n '1!G;h;$p' thegeekstuff.txt


为文件中的所有non-empty-lines添加行号

$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'


6.awk 命令示例

使用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


7.vim 命令示例

去文件的第143行

$ vim +143 filename.txt


去文件中指定第一行

$ vim +/search-term filename.txt


以只读模式打开文件

$ vim -R /etc/passwd


8.diff 命令示例

比较时忽略空白

$ diff -w name_list.txt name_list_new.txt

2c2,3
< John Doe --- > John M Doe
> Jason Bourne


9.sort 命令示例

文件按升序排序

$ sort names.txt


以降序排序文件

$ sort -r names.txt


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"


配置oracle环境变量

$ 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等)

$ ls -lh
-rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz


基于最后修改时间(倒序)对文件排序

$ ls -ltr


形象的对特殊字符文件进行分类

$ ls -F


13. pwd 命令

pwd是显示当前的工作路经

14.cd 命令示例

使用“cd -”在两个目录之间进行切换

使用“shopt - s cdspell”自动更正cd目录名

15.gzip 命令

将已存在的文件压缩为*.gz格式的压缩文件

$ gzip test.txt


解压 *.gz文件

$ 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


17.unzip 命令示例

将已存在的文件压缩为*.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和安全性ftp(sftp)都是相同的命令。连接到远程服务器并下载多个文件,执行以下步骤:

$ ftp IP/hostname
ftp> mget *.html


查看文件的名称位于远程服务器下载之前,mls ftp命令如下所示

ftp> mls *.html -
/ftptest/features.html
/ftptest/index.html
/ftptest/othertools.html
/ftptest/samplereport.html
/ftptest/usage.html


20.crontab 命令

查看用户

$ crontab -u john -l


每10分钟执行工作

Schedule a cron job every 10 minutes.

*/10 * * * * /home/ramesh/check-disk-space


21.service 命令

服务命令是用于运行system V init脚本。i.e位于/etc/init.d/目录的完整路径,您可以使用服务命令。

检查服务的状态:

$ service ssh status


检查所有的服务的状态:

$ service --status-all


重启服务:

$ service ssh restart


22.ps 命令

ps命令用于显示信息系统中正在运行的进程.

虽然有很多的参数,可以使用ps命令,下面是一些常见的命令.

查看当前运行的进程:

$ ps -ef | more


查看当前运行的进程在哪个树结构.H选项代表层次结构

$ ps -efH | more


23.free 命令

这个命令用于显示免费使用交换系统中可用内存.

典型的输出命令.以字节输出显示

$ free
total       used       free     shared    buffers     cached
Mem:       3566408    1580220    1986188          0     203988     902960
-/+ buffers/cache:     473272    3093136
Swap:      4000176          0    4000176


如果你想快速检查你的系统有多少可用GB内存: 添加 -g选项。-k -b选项显示字节,千字节,兆字节使用 -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使用排序)。

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


显示属于oracle用户的进程

$ top -u oracle


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


以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杀死运行在Linux的进程,还可以使用killall、pkill xkill终止一个unix的进程。

$ ps -ef | grep vim
ramesh    7243  7222  9 22:43 pts/2    00:00:00 vim

$ kill -9 7243


27.rm 命令示例

删除文件之前,获取同意

$ rm -i filename.txt


删除匹配文件时,逐个获得同意

$ rm -i file*


递归删除示例目录下的所有文件和目录,同时删除示例目录本身

$ 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恰恰相反,覆盖file2没有提示.

mv - v将显示期间所发生的文件重命名

$ mv -v file1 file2


30.cat 命令示例

$ cat file1 file2


查看文件内容

$ 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


自动安装文件系统

/dev/sdb1 /u01 ext2 defaults 0 2


32.chmod 命令示例

使用chmod命令改变文件或目录的权限.

用户和组 (i.e read, write and execute ),给文件设置特定的权限.

$ chmod ug+rwx file.txt


组 (i.e read, write and execute ) 给文件设置特定的权限.

$ chmod g-rwx file.txt


递归地给所有子目录中的文件设置特定的权限.

$ chmod -R ug+rwx file.txt


33.chown 命令示例

chown 命令是用来改变文件的所有者和组.

$ chown oracle:dba dbora.sh


使用 -r选项,递归地更改所有权

$ chown -R oracle:dba /home/oracle


34.passwd 命令示例

修改密码

$ passwd


超级用户可以使用passwd命令,给他人重置密码。而不会提示输入当前用户的密码。

$ passwd USERNAME


为特定的用户删除密码。超级用户可以为特定的用户禁用密码。禁用密码后,用户可以登录无需输入密码.

$ passwd -d USERNAME


35.mkdir 命令示例

在主目录下创建一个temp目录

$ mkdir ~/temp


创建嵌套目录使用mkdir -p 命令。如果这些目录已经存在,它将不创建。如果任何这些目录不存在,将创建它们.

$ mkdir -p dir1/dir2/dir3/dir4/


36.ifconfig 命令示例

使用ifconfig命令来查看或配置Linux系统上的网络接口.

查看所有的网络接口以及网络状态

$ ifconfig -a


$ ifconfig eth0 up

$ ifconfig eth0 down


37.uname 命令示例

$ 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 命令示例

$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz


$ whereis -u -B /tmp -f lsmk
lsmk: /tmp/lsmk


39.whatis 命令示例

$ whatis ls
ls      (1)  - list directory contents

$ whatis ifconfig
ifconfig (8)         - configure a network interface


40.locate 命令示例

$ 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 crontab


$ man SECTION-NUMBER commandname


Following 8 sections are available in the man page.

General commands

System calls

C library functions

Special files (usually devices, those found in /dev) and drivers

File formats and conventions

Games and screensavers

Miscellaneous

System administration commands and daemons

For example, when you do whatis crontab, you’ll notice that crontab has two man pages (section 1 and section 5). To view section 5 of crontab man page, do the following.

$ whatis crontab
crontab (1)          - maintain crontab files for individual users (V3)
crontab (5)          - tables for driving cron

$ man 5 crontab


42.tail 命令示例

$ tail filename.txt


$ tail -n N filename.txt


$ tail -f log-file


43.less 命令示例

$ less huge-log-file.log


CTRL+F – forward one window
CTRL+B – backward one window


44.su 命令示例

$ su - USERNAME


[john@dev-server]$ su - raj -c 'ls'

[john@dev-server]$


$ su -s 'SHELLNAME' USERNAME


45.mysql 命令示例

$ mysql -u root -p -h 192.168.1.2


$ mysql -u root -p


46.yum 命令示例

$ yum install httpd


$ yum update httpd


$ yum remove httpd


47.rpm 命令示例

$ rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm


$ rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm


$ rpm -ev httpd


48.ping 命令示例

$ ping -c 5 gmail.com


49.date 命令示例

$ date -s "01/31/2010 23:59:53"


$ hwclock –systohc

$ hwclock --systohc –utc


50.wget 命令示例

$ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz[/code] 
$ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701[/code] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UNIX LINUX