常用的LINUX/UNIX命令

本文不是LINUX/UNIX命令手册, 而是关于部分命令的惯用方法.

cat

concatenate files and print on the standard output.

通常用于输出某文件的所有内容到标准输出: 'cat file'

e.g.

查看当前文件夹的最新文件的最后一行:cat `ls -1rt | tail -1` 或 cat $(ls -1rt | tail -1)

date

获取从1970-1-1 00:00:00到当前时间的秒数:

$ date +%s
1481443397

将指定的时间(格式的固定的,不能改变)转换为秒数:

$ date -d '2014-11-17 12:18:28' +%s
1416197908

将秒数转换为时间:

$ date -d@1481443397
2016年 12月 11日 星期日 16:03:17 BEIST

修改系统时间(root权限)

date -s "2017-01-05 16:04:00"

查看时区

date -R

Mon, 09 Jan 2017 13:28:15 +0800

上面的"+0800"即为时区

du

Estimate file space usage.

显示文件和目录所占用的磁盘空间。

-a 详细显示每个文件的大小

-g 以GB为单位计算

-k 以1024B为单位计算

-m 以MB为单位计算

-s 显示目录下的所有文件和子目录的总大小(不详细列出显示第一个文件,与-a相反)

e.g. 显示当前目录下的文件大小: du -ms *

find

search for files in a directory hierarchy.

查找文件.

e.g. 假设某人上传了一个中文名字的文件(有可能显示为乱码), 现在要删除这个文件, 但是unix主机没有中文输入法, 该如何删除呢?

ubuntu 10.4.1 linux $ ls -i
 150220 哈哈.c
ubuntu 10.4.1 linux $ find . -inum 150220 -print -exec rm {} \;
./哈哈.c
ubuntu 10.4.1 linux $ find . -inum 150220 -print
ubuntu 10.4.1 linux $

groupadd

增加名为test的用户组(root权限):

groupadd test

id

print real and effective user and group IDs.

显示用户的userid, groupid等, 默认显示当前用户.

以下是各平台的运行结果:

ubuntu 10.4.1 linux $ id
 uid=1000(mjn) gid=1000(mjn) groups=4(adm),20(dialout), 24(cdrom),46(plugdev),105(lpadmin),119(admin),122(sambashare),1000(mjn)
suse 11 linux $ id
 uid=1000(mjn) gid=1000(mjn) groups=4(adm)
redhat 6.2 linux $ id
 uid=1000(mjn) gid=1000(mjn) groups=4(adm)
aix 6.1 unix $ id
 uid=1000(mjn) gid=1000(mjn) groups=4(adm)
sunos 5.10 unix $ id
 uid=1000(mjn) gid=1000(mjn)

info

read Info documents.

相对于man, info的信息更详细. info显示的信息格式为Texinfo.

ln

建立链接

软链接(加参数-s来指定),类似windows下的快捷方式,删除软链接文件,不会影响原有文件,经常用于软件发布时,对外开放的动态库文件,指向某个版本的动态库,如以下mysql的动态库:

通过ls -l查看:

lrwxrwxrwx. 1 mysql mysql        20 May  6  2016 libmysqlclient.so -> libmysqlclient.so.18

其中,libmysqlclient.so指向libmysqlclient.so.18的软链接文件,建立上述软链接的方式:

ln -s libmysqlclient.so.18 libmysqlclient.so

ls

list directory contents.

'-i': 显示文件的索引编号(每个文件均唯一).

'-l': 显示的第一行将类似于'total 64', 64表示该目录下所有文件所占用的空间, 单位是KB.

'-h': 文件的大小显示地人性化, 如'total 64K', 小于1K的, 不显示单位.

e.g. 'ls -lrt', 按修改时间排序, 逆序显示详细信息. 此命令在文件很多, 且想看到最近被修改的文件时(例如日志目录下, 刚写入日志的文件), 很有用.

ubuntu 10.4.1 linux $ ls -lrth
 total 64K
 drwxr-xr-x 2 mjn mjn 4.0K 2013-03-27 20:11 make 
 -rw-r--r-- 1 mjn mjn 1.5K 2013-03-27 20:25 sockserver.c 
 -rw-r--r-- 1 mjn mjn  867 2013-05-23 22:26 main.c 
 -rw-r--r-- 1 mjn mjn   63 2013-05-23 22:28 msq.log 

'-1'(数字1): 每行显示一个文件名. 可以结果通过管道作为其他程序的输入, 进一步处理. 以下命令显示当前目录中文件的个数(命令'ls -1 | wc -l'中, 第一个'-1'是数字1, 第二个'-l'是字母l):

ubuntu 10.4.1 linux $ ls -1 | wc -l
 15

man

an interface to the on-line reference manuals.

查看函数或shell命令帮助.

nohup

run a command immune to hangups, with output to a non-tty.

将命令的标准输出附加(append)到文件'nohup.out'的末尾(文件在当前目录或$HOME目录), 或者通过'nohup COMMAND > file'输出到文件file中. 如果文件'nohup.out'不存在, 则自动创建.

ubuntu 10.4.1 linux $ nohup ls -m
 nohup: ignoring input and appending output to `nohup.out'
ubuntu 10.4.1 linux $ cat nohup.out 
 backup, bin, codes, Desktop

使用命令'nohup program &'可以使程序'program'安静地在后台运行.

netstat

Shows network status.

e.g.

1) 在AIX 6.1上查看8400端口是否被占用:

$ netstat -n | grep 8400
tcp4       0      0  *.8400                 *.*                    LISTEN

以上结果显示, 端口已经被占用

ps

report a snapshot of the current processes.

显示当前运行的进程.

'-u', 参数为用户名或者userid.

以下显示当前用户正在运行的进程:

ubuntu 10.4.1 linux $ ps -u `whoami` 
 2167 pts/1    00:00:00 bash 
 2283 pts/1    00:00:00 pager 
 2292 pts/0    00:00:00 ps 	

查看正在运行进程的线程:ps -T p {pid}

passwd

更改当前用户密码:passwd

root用户更改其他用户密码:passwd [user name]

pwd

查看当前路径

tail

output the last part of files.

'-f', 动态显示新增到文件末尾的内容. 可以用于查看正在写入的日志文件. 使用'Ctl+c'强制退出.

ubuntu 10.4.1 linux $ tail -f msq.log
 thread msqread start

ulimit

查看或设置系统参数的限制

显示中的unlimited表示无限制

-a 显示所有参数

-c 显示core文件的大小上限, 可以增加参数来指定core文件大小的上限, 如ulimit -c 1024. 表示上限为1024个block. 不过设置的值只在当前shell中有效.

MJN@MJN-THINK ~
$ ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 8
stack size              (kbytes, -s) 2025
cpu time               (seconds, -t) unlimited
max user processes              (-u) 256
virtual memory          (kbytes, -v) unlimited

参考: Securing and Optimizing Linux: RedHat Edition -A Hands on Guide -- Chapter 6. Linux General Optimization -- 6.10. The ulimit parameter

uname

print system information.

'-s', 默认参数,输出系统名称,通常用于shell脚本中判断系统的类型, 以决定用什么编译器.

ubuntu 10.4.1 linux $ uname
 Linux
suse 11 linux $ name
 Linux
redhat 6.2 linux $ name
 Linux
aix 6.1 unix $ uname
 AIX
sunos 5.10 unix $ uname
 SunOS

详细信息, 请参考另一篇文章: 查看CPU和系统内核位数.

useradd

新增用户

e.g.

AIX 7.1新增用户test, HOME目录为/home/test(如果不存在, 则创建, 因为指定了-m参数), 默认shell指定为/bin/sh, 用户组为staff:

useradd -m -d /home/test -s /bin/sh -g staff test

wc

print newline, word, and byte counts for each file.

显示文件中的行数,单词个数,字符个数.

参数如下:

-c, --bytes 字节计数

-m, --chars 字符计数

-l, --lines 换行计数

-w, --words 单词计数

e.g.

#显示文件中的行数, 单词数, 字符数
ubuntu 10.4.1 linux $ cat file.txt
A very intelligent turtle
Found programming UNIX a hurdle
    The system, you see,
    Run as slow as did he,
And that's not saying much for the turtle.
ubuntu 10.4.1 linux $ wc file.txt
  5  27 153 file.txt

#显示当前目录的文件数(另一种计算当前目录中文件的个数的方法, 请见本文中的'ls'命令)
MJN@MJN-THINK ~
$ ls
clib  src  std_test  test  thread
MJN@MJN-THINK ~
$ ls | wc -w
5

whatis

display manual page descriptions.

显示命令的简短描述.

ubuntu 10.4.1 linux $ whatis who
 who (1)              - show who is logged on

which

locate a command.

显示命令的简短描述.

ubuntu 10.4.1 linux $ which who
 /usr/bin/who

who

show who is logged on.

结合grep, 可以显示当前用户登录的ip(有可能多个人使用同一用户名登录).

ubuntu 10.4.1 linux $ who | grep `whoami`
 mjn      tty7         2013-05-23 22:22 (:0)
 mjn      pts/0        2013-05-23 22:22 (:0.0)

whoami

print effective userid.

显示当前'我是谁'

ubuntu 10.4.1 linux $ whoami
 mjn
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值