linux常用命令

linux常用命令

在linux中,绝大多数命令都参照 命令 选项 参数

选项:适用于调整命令的功能的

参数:指的是命令的操作对象,如果省略参数,是由于有默认参数的

目录操作命令

ls 显示目录下的内容

# ls 
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog

# ls --color=never  不显示颜色的显示目录下的文件名
[root@localhost ~]# ls --color=never
anaconda-ks.cfg  install.log  install.log.syslog

# ls -a 显示所有文件(包含隐藏文件)
[root@localhost ~]# ls -a
.  ..  anaconda-ks.cfg  .bash_logout  .bash_profile  .bashrc  .cshrc  install.log  install.log.syslog  .pki  .tcshrc

# ls -l 以长格式显示文件
[root@localhost ~]# ls -l
总用量 16
-rw-------. 1 root root 1098 6月   8 19:38 anaconda-ks.cfg
-rw-r--r--. 1 root root 8025 6月   8 19:38 install.log
-rw-r--r--. 1 root root 3384 6月   8 19:38 install.log.syslog

# ls -d 只显示目录
[root@localhost ~]# ls -l
总用量 16
-rw-------. 1 root root 1098 6月   8 19:38 anaconda-ks.cfg
-rw-r--r--. 1 root root 8025 6月   8 19:38 install.log
-rw-r--r--. 1 root root 3384 6月   8 19:38 install.log.syslog

# ls -i 查看inode节点号
[root@localhost ~]# ls -i
260965 anaconda-ks.cfg  260611 install.log  260612 install.log.syslog

cd 切换所在目录

# cd
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# 

# 进入相对路径
[root@localhost /]# cd /tmp/
[root@localhost tmp]# cd ../etc/yum.repos.d/

# 进入绝对路径
[root@localhost yum.repos.d]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ll
总用量 36
drwxr-xr-x. 2 root root 4096 6月   8 19:58 backup
-rw-r--r--. 1 root root 1991 3月  28 2017 CentOS-Base.repo
-rw-r--r--. 1 root root  647 3月  28 2017 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  289 3月  28 2017 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 3月  28 2017 CentOS-Media.repo
-rw-r--r--. 1 root root 7989 3月  28 2017 CentOS-Vault.repo
-rw-r--r--. 1 root root  957 11月  5 2012 epel.repo
-rw-r--r--. 1 root root 1056 11月  5 2012 epel-testing.repo

# cd -  返回上次所在目录
[root@localhost yum.repos.d]# pwd
/etc/yum.repos.d
[root@localhost yum.repos.d]# cd /
[root@localhost /]# cd -
/etc/yum.repos.d
[root@localhost yum.repos.d]# 

# cd ~	进入当前用户的家目录
[root@localhost yum.repos.d]# cd ~
[root@localhost ~]# 

# cd ..	进入上一级目录
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# cd ..
[root@localhost /]# pwd
/

pwd 显示当前所在目录

# pwd
[root@localhost yum.repos.d]# pwd
/etc/yum.repos.d

mkdir 创建目录

# mkdir
[root@localhost ~]# mkdir tmp
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  tmp

# mkdir -p 递归创建目录
[root@localhost ~]# mkdir -p a/b/c/d
[root@localhost ~]# tree a
a
└── b
    └── c
        └── d

3 directories, 0 files

rmdir 删除目录,只能删除空目录

# rmdir
[root@localhost ~]# rmdir d

rm -rf 删除文件和目录

# -r 递归,删除目录
# -f 强制
[root@localhost ~]# ls
a  anaconda-ks.cfg  install.log  install.log.syslog
[root@localhost ~]# rm -rf a
[root@localhost ~]# ll
总用量 16
-rw-------. 1 root root 1098 6月   8 19:38 anaconda-ks.cfg
-rw-r--r--. 1 root root 8025 6月   8 19:38 install.log
-rw-r--r--. 1 root root 3384 6月   8 19:38 install.log.syslog

tree 目录名显示指定目录下所有内容的目录树

# tree
[root@localhost ~]# tree /etc/yum.repos.d/
/etc/yum.repos.d/
├── backup
├── CentOS-Base.repo
├── CentOS-Debuginfo.repo
├── CentOS-fasttrack.repo
├── CentOS-Media.repo
├── CentOS-Vault.repo
├── epel.repo
└── epel-testing.repo

文件操作命令

touch 创建空文件或者修改文件最后一次访问时间

# touch
[root@localhost tmp]# touch replaceContent.py
[root@localhost tmp]# ls
replaceContent.py  yum.log

# 这里需要注意的是:任何操作系统都不允许创建同名的文件和目录

cat 查看文件

# cat
[root@localhost ~]# cat install.log
安装 libgcc-4.4.7-18.el6.x86_64
warning: libgcc-4.4.7-18.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
安装 setup-2.8.14-23.el6.noarch
安装 filesystem-2.4.30-3.el6.x86_64
......

# cat -n  显示行号
[root@localhost ~]# cat -n install.log
     1	安装 libgcc-4.4.7-18.el6.x86_64
     2	warning: libgcc-4.4.7-18.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
     3	安装 setup-2.8.14-23.el6.noarch
......

more 分屏显示文件内容

# more  b返回上一页,空格进入下一页
[root@localhost ~]# more install.log
安装 libgcc-4.4.7-18.el6.x86_64
warning: libgcc-4.4.7-18.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
安装 setup-2.8.14-23.el6.noarch
安装 filesystem-2.4.30-3.el6.x86_64

less 分行显示文件内容

# less  上下箭头分页
[root@localhost ~]# less install.log

安装 libgcc-4.4.7-18.el6.x86_64
warning: libgcc-4.4.7-18.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
安装 setup-2.8.14-23.el6.noarch

head 显示文件头,默认是10行

# head
[root@localhost ~]# head install.log
安装 libgcc-4.4.7-18.el6.x86_64
warning: libgcc-4.4.7-18.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
安装 setup-2.8.14-23.el6.noarch
安装 filesystem-2.4.30-3.el6.x86_64
安装 basesystem-10.0-4.el6.noarch
安装 ncurses-base-5.7-4.20090207.el6.x86_64
安装 kernel-firmware-2.6.32-696.el6.noarch
安装 tzdata-2016j-1.el6.noarch
安装 glibc-common-2.12-1.209.el6.x86_64
安装 nss-softokn-freebl-3.14.3-23.3.el6_8.x86_64

# head -n number  只显示Number行
[root@localhost ~]# head -n 5 install.log
安装 libgcc-4.4.7-18.el6.x86_64
warning: libgcc-4.4.7-18.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
安装 setup-2.8.14-23.el6.noarch
安装 filesystem-2.4.30-3.el6.x86_64
安装 basesystem-10.0-4.el6.noarch

tail 显示文件尾

# tail	默认是文件的最后10行
[root@localhost ~]# tail install.log
安装 efibootmgr-0.5.4-15.el6.x86_64
安装 sudo-1.8.6p3-27.el6.x86_64
安装 audit-2.4.5-6.el6.x86_64
安装 e2fsprogs-1.41.12-23.el6.x86_64
安装 xfsprogs-3.1.1-20.el6.x86_64
安装 acl-2.2.49-7.el6.x86_64
安装 attr-2.4.44-7.el6.x86_64
安装 bridge-utils-1.2-10.el6.x86_64
安装 rootfiles-8.1-6.1.el6.noarch
*** FINISHED INSTALLING PACKAGES ***[root@localhost ~]# 

# tail -n number  只显示文件尾部number行
[root@localhost ~]# tail -n 3 install.log
安装 bridge-utils-1.2-10.el6.x86_64
安装 rootfiles-8.1-6.1.el6.noarch
*** FINISHED INSTALLING PACKAGES ***[root@localhost ~]# 

# tailf	监听文件,用于监听实时日志
[root@localhost ~]# touch 1.txt
[root@localhost ~]# tailf 1.txt 
11111

cp 拷贝文件

cp -r	复制目录
cp -p	连带文件属性复制
cp -d	若源文件是链接文件,那么复制链接属性
cp -a 	相当于cp -rpd

mv 移动文件或者重命名文件

mv 源文件 目标地址
mv 源文件 目标文件

ln 链接文件

在linux文件中分为硬链接文件和软连接文件
硬链接文件:
	1.拥有相同的i节点和存储block块,可以看做是同一个文件。修改一个文件,另一个文件也会发生改变,删除其中一个,另一个文件还能打开
	2.可以通过i节点识别
    3.不能跨分区
    4.不能针对目录使用
    	a.硬链接不能连接目录
    	b.硬链接不能跨分区
软连接文件:
	1.类似Windows快捷方式
	2.软连接拥有自己的i节点和block块,但是数据中只保存原文件名和i节点号,并没有实际的文件数据,软连接目录大小不变
	3.lrwxrwxrwx是软连接
		a.软连接文件都是777
		b.软连接的最大权限是假的,不影响实际访问,源文件受限制权限
	4.删除任意文件,另一个都改变
	5.删除源文件,软连接不能使用会报错,红点闪烁
	
ln -s 源文件 目标文件	文件名必须要写绝对路径
	软连接需要绝对路径,不允许使用相对路径

创建软连接的原因,照顾管理员的使用习惯,照顾版本升级需要

权限位

[root@localhost ~]# ll /etc
总用量 1208
-rw-r--r--.  1 root root     16 6月   8 19:38 adjtime
-rw-r--r--.  1 root root   1512 1月  12 2010 aliases
-rw-r--r--.  1 root root  12288 6月   8 19:40 aliases.db
drwxr-xr-x.  2 root root   4096 6月   8 20:11 alternatives

从redhat6更新的,第11位代表这个文件被SELinux所保护,权限位是10位,第1位代表文件类型:
-	普通文件
d	目录文件
l	链接文件
b	块设备文件
c	字符设备文件
s	套接字文件
p	管道

chmod 修改权限

权限
	644	标准的读写权限
	755	标准的执行权限
	777	最大权限
	在服务器禁止出现777权限,除了系统自带的目录,不能手工更改apache权限,只需要把网页目录的所有者改成apache的管理员身份就可以了

权限的意义

1.权限对文件的意义
	r:读取文件的内容	more/cat/less/tail
	w:编辑、现在、修改文件内容,不包括删除文件	vi/echo
	r:可执行
	对于文件来说:执行权限是最高权限
2.权限对目录的意义
	r:可以查询目录下的文件名	ls
	w:具有修改目录结构的权限。如新增文件和目录,删除此目录下的文件,重命名等操作	touch rm mv cp
	x:可以进入目录
	对于目录来说:写权限才是最高权限,写权限要小心赋予

chown 改变所有者和所属组

# chown
[root@host-10-200-137-195 ~]# ll
total 8
-rw-r--r-- 1 root root  856 Jun  2 19:33 client.py
drwxr-xr-x 2 root root    6 Jun  6 11:16 root
-rw-r--r-- 1 root root 1024 Jun  2 19:26 server.py
[root@host-10-200-137-195 ~]# useradd xiao
useradd: user 'xiao' already exists
[root@host-10-200-137-195 ~]# chown xiao:xiao server.py 
[root@host-10-200-137-195 ~]# ll
total 8
-rw-r--r-- 1 root root  856 Jun  2 19:33 client.py
drwxr-xr-x 2 root root    6 Jun  6 11:16 root
-rw-r--r-- 1 xiao xiao 1024 Jun  2 19:26 server.py

**chrgp 所属组 文件名 **

查找命令

which 查找命令的命令,能看到相关别名

# which
[root@localhost ~]# which vi
alias vi='vim'
	/usr/bin/vim  # 实际上也相当于是查询别名的操作

whereis 查询命令的命令,以及帮助文档的路径

# whereis
[root@localhost ~]# whereis vi
vi: /bin/vi /usr/share/man/man1/vi.1.gz

和which相同点:
	可以查找出命令的绝对路径
和which不同点:
	whereis可以列出帮助文档的存放位置,而which可以查询命令的别名

**locate 按照文件名查找 按照数据库查找 **

# locate 
# 这个命令在centos6中有,在centos7中需要安装mlocate,然后初始化数据库updatedb
[root@localhost ~]# locate inittab
/etc/inittab
/usr/share/man/man5/inittab.5.gz
/usr/share/vim/vim74/syntax/inittab.vim

优点:
	在后台建立数据库/var/lib/mlocate,记录当前系统的文件名,按数据库查找
	耗费资源最小,速度快
	第一次更新数据量比较慢

更新数据库命令:updatedb
配置文件所在位置:/etc/updatedb.conf

# locate -i	不区分大小写查找
[root@localhost ~]# touch ABC.CONF
[root@localhost ~]# touch abc.conf
[root@localhost ~]# updatedb
[root@localhost ~]# locate abc.conf -i
/root/ABC.CONF
/root/abc.conf

# 配置文件内容
[root@localhost ~]# more /etc/updatedb.conf 
PRUNE_BIND_MOUNTS = "yes"
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fusectl gfs gfs2 gpf
s hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpf
s ubifs udf usbfs"
PRUNENAMES = ".git .hg .svn"
PRUNEPATHS = "/afs /media /net /sfs /tmp /udev /var/cache/ccache /var/spool/cups /var/spool/squid /var/tmp"

PRUNE_BIND_MOUNTS	配置文件生效

man 查看命令的帮助

find

# find	文件搜索命令(扫描整个根,文件越多,硬盘越大,速度越慢)

按照文件名去查找
[root@localhost ~]# find /etc -name init  # 在/etc目录下搜索init的文件(多一个字符都不行)
/etc/sysconfig/init
/etc/init
[root@localhost ~]# find /etc -name *init*	# 在/etc的目录下搜索包含init的文件,*是通配符
/etc/init.conf
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/sysconfig/init
/etc/inittab
/etc/rc.sysinit
/etc/rc.d/rc.sysinit
/etc/rc.d/init.d
/etc/pam.d/run_init
/etc/init.d
/etc/init
/etc/init/init-system-dbus.conf
/etc/iscsi/initiatorname.iscsi
/etc/selinux/targeted/contexts/initrc_context
/etc/security/namespace.init
[root@localhost ~]# find /etc -name init?  # ?代表单个字符 -iname是不去扥大小写的
[root@localhost ~]# find /etc -name init???
/etc/inittab

按照时间查找
[root@localhost ~]# find /etc -cmin -5  # 在/etc下查找5分钟被修改过属性的文件和目录 超过5分钟用+
/etc
/etc/cron.daily
/etc/cron.daily/makewhatis.cron
/etc/man.config


按照大小查找
find / -size +27k

按照所有者和所属组查找
[root@localhost ~]# find / -user xiao
[root@localhost ~]# find / -group xiao

根据i节点查找
[root@localhost ~]# ll -i
总用量 20
260617 -rw-r--r--. 1 root root    6 6月  10 23:05 1.txt
260971 -rw-r--r--. 1 root root    0 6月  11 01:53 abc.conf
260970 -rw-r--r--. 1 root root    0 6月  11 01:53 ABC.CONF
260965 -rw-------. 1 root root 1098 6月   8 19:38 anaconda-ks.cfg
260611 -rw-r--r--. 1 root root 8025 6月   8 19:38 install.log
260612 -rw-r--r--. 1 root root 3384 6月   8 19:38 install.log.syslog
[root@localhost ~]# find . -inum 260617
./1.txt


find /etc –size +80M –a –size -100M  # 在/etc的目录下查找大于80MB小于100MB的文件
	-a表示逻辑与and	两个条件都成立
	-o表示逻辑或or	两个条件成立一个都执行

grep 在文件内容中搜索字符串匹配的行并输出

# grep -i[指定字符串] 文件名		输出包含指定字符串的行
[root@localhost ~]# grep -i 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

# grep -v [指定字符串] 文件名	输出不包含指定字符串的行
[root@localhost ~]# cat 1.txt 
1
2
3
4
5
[root@localhost ~]# grep -v 2 1.txt 
1
3
4
5

find和grep的区别:
	find:在系统中搜索符合条件的文件名,如果需要匹配,使用通配符匹配,通配符是完全匹配的,find不支持文本刘操作,不支持管道符。
	grep:在文件内容中搜索负责条件的字符串的行并输出,如果需要匹配,使用正则表达式,正则表达式是包含匹配。管道符是文本流。

网络命令

netstat 查看系统网络状态

netstat [选项]
	-t	tcp端口
    -u	udp端口
	-l	监听
    -n	以IP和端口号显示,而不是主机名和协议名
# netstat -tln
[root@localhost ~]# netstat -tl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 *:ssh                       *:*                         LISTEN      
tcp        0      0 localhost:smtp              *:*                         LISTEN      
tcp        0      0 *:ssh                       *:*                         LISTEN      
tcp        0      0 localhost:smtp              *:*                         LISTEN      
[root@localhost ~]# netstat -tln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:25                      :::*                        LISTEN 

w 查询当前登录服务器的所有用户

# w
[root@localhost ~]# w
 02:16:55 up  3:47,  2 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                22:30    3:44m  0.09s  0.09s -bash
root     pts/0    192.168.190.1    22:32    0.00s  0.50s  0.04s w
其中第一行:
	02:16:55 up		代表开机时间
	3:47			代表登录时间
	2 users			代表当前有几个用户登录
	load average: 0.00, 0.00, 0.00	代表1/5/12分钟前的平均压力值
第二行:
	USER	代表用户名
	TTY		登录终端
	IP		登录的IP
	LOGIN@	登录的持续时间
	IDLE	idle用户的闲置时间
	JCPU	所有进程占用CPU时间
	PCPU	当前进程占用CPU时间
	WHAT	正在执行什么操作
登录终端:
	tty1-tty6	字符终端
	tty7	图形终端
	pts/0-255	远程终端

who 查询登录用户

# who
[root@localhost ~]# who
root     tty1         2018-06-10 22:30
root     pts/0        2018-06-10 22:32 (192.168.190.1)
登录用户名	登录终端	登录时间	登录的主机IP地址

last 列出登录系统的相关信息

# last
[root@localhost ~]# last
root     pts/1        192.168.190.1    Sun Jun 10 23:05 - 01:39  (02:33)    
root     pts/0        192.168.190.1    Sun Jun 10 22:32   still logged in   
root     tty1                          Sun Jun 10 22:30   still logged in   
reboot   system boot  2.6.32-696.el6.x Sun Jun 10 22:29 - 02:22  (03:52)    
root     pts/0        192.168.190.1    Fri Jun  8 20:01 - crash (2+02:28)   
root     tty1                          Fri Jun  8 19:41 - crash (2+02:48)   
reboot   system boot  2.6.32-696.el6.x Fri Jun  8 19:40 - 02:22 (2+06:42)   

wtmp begins Fri Jun  8 19:40:12 2018

# 所有登录信息都在/var/run/utmp下,但是是不能修改的

ifconfig 查看本机网络信息

所有系统常见服务都在/etc/services下

关机重启命令

# sync 数据同步,把内存中数据强制保存到硬盘里

# 关机命令
	1.shutdown -h now
	2.init 0 
	3.halt
	3.poweroff
# 重启命令
	1.shutdown -r now
	2.reboot
	3.init 6

linux中系统运行级别

0	关机
1	单用户
2	不完全多用户,不含NFS服务
3	完全多用户
4	未分配
5	图形界面
6	重启

挂载命令

# mount
linux中所有存储设备都必须挂载使用,包括硬盘

# mount -l	查询系统中已经挂载的设备,-l会显示卷标名称
[root@localhost ~]# mount -l
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

# mount -a 检测/etc/fstab的内容,自动挂载
	在这里,如果挂载的是U盘或者光盘,如果下次开机没有放入,那么系统就不会启动
	所以需要mount -a 检查下文件内容是否出错
# mount [-t 文件系统] [-l 卷标名] [-o 特殊选项] 设备文件名挂载
选项:
	-t	文件系统	ext3/ext4/iso9660
	-l	卷标名	挂载指定卷标的分区,而不是安装设备文件名挂载
	-o	特殊选项,可以指定挂载的额外选项,比如读写权限、同步异步等,如不制定默认值生效
	
# dumpe2fs	查看分区的详细信息
[root@localhost ~]# dumpe2fs -h /dev/sda1

原文地址:https://www.cnblogs.com/xiaoyafei/p/9163976.html