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

linux常用命令

2016-11-04 13:26 134 查看
linux常用命令: 文件系统:
1.文件名严格区分大小写
2.文件可以使用除 / 以外的任意字符、
3.文件名长度不能超过255字符
4.以.点开头的文件为隐藏文件

常用命令第一组:
pwd:(printingworking directory)
显示工作目录
演示:
[root@localhost ~]# pwd
/root
[root@localhost ~]#

常用命令第二组:
cd:(changedirectory)
切换目录
cd [ /PATH/TO/SOMEDIR(路径) ]
cd后面的路径可有可无,如果没有,则表示切换到家目录
演示:
[root@localhost ~]# cd /etc/sysconfig
[root@localhost sysconfig]# pwd
/etc/sysconfig
[root@localhost sysconfig]# cd
[root@localhost ~]# pwd
/root
[root@localhost ~]#
注意在bash中,~表示家目录
cd ~:也表示切换到自己家目录
cd ~用户名(USERNAME):切换到指定用户的家目录
cd-:当前目录与上一级目录循环切换。
在bash解释器中,有两个环境变量,每切换一下目录,这两个变量的值就发生变化。
所以pwd其实就是引用PWD这个变量值来显示。
PWD:当前工作目录
OLDOWD:上一次的工作目录
我们可以用echo 用$来显示上面的两个变量的值。
~]#echo $PWD 或 ~]#echo $OLDPWD

演示:
[root@localhost log]# pwd
/var/log
[root@localhost log]# echo $PWD
/var/log
[root@localhost log]# cd /etc/sysconfig
[root@localhost sysconfig]# pwd
/etc/sysconfig
[root@localhost sysconfig]# echo $PWD
/etc/sysconfig
[root@localhost sysconfig]# echo $OLDPWD
/var/log
[root@localhost sysconfig]#
演示:
[root@localhost ~]# cd ~gentoo
[root@localhost gentoo]# pwd
/home/gentoo //普通用户的家目录统一在/home目录下,而管理员家目录在/root
[root@localhost gentoo]#
注意:只有管理员才能切换到普通用户的家目录

上面cd 用的是绝对路径,我们也可以用相对路径。
演示:
[root@localhost ~]# cd /etc/sysconfig
[root@localhost sysconfig]# pwd
/etc/sysconfig
[root@localhost sysconfig]# cd ..
[root@localhost etc]# pwd
/etc
[root@localhost etc]# cd ../var/log
[root@localhost log]# pwd
/var/log
[root@localhost log]#

在这里讲到了绝对路径和相对路径,在这里 . .. 使用很关键。

常用命令第三组:
ls: (list),列出指定目录下的内容
ls [OPTION]... [FILE]...
选项:
-a: 显示所有目录和文件,包括隐藏文件和目录
-A:显示隐藏文件和目录,但是除去 . 和 ..
-l: --long,长格式列表,即显示文件跟目录的详细属性信息
-S: 按文件大小排序(从大到小)
drwxr-xr-x. 2 root root 4096 Jul 24 22:16 Desktop
解释:
d:文件类型,种类有:-,d,b,c,l,s,p
rwxr-xr-x:
rwx:左三位,文件属主的权限;
r-x:中三位,文件数组的权限;
r-x:右三位,其他用户(非属主,属组)的权限。
. 表示有隐藏属性
2:数字表示文件被硬链接的次数
root: 文件的属主
root:文件的属组
4096:数字表示文件的大小,单位是字节。
Jul 24 22:16:文件最后一次被修改的时间。
Desktop:文件名

如上当用ls -l 命令时,显示如上信息,最后才是文件或者目录的名称。
演示:
[root@localhost ~]# ls -l
total 3888
-rw-------. 1 root root 1484 Jul 24 21:03 anaconda-ks.cfg
-rw-------. 1 root root 4952064 Jul 2423:04 core.10718
drwxr-xr-x.2 root root 4096 Jul 24 22:16 Desktop

-h:--human-readable:对文件大小单位换算,但是换算后的结果可能会非精确值。
演示:
[root@localhost~]# ls -lh
total 3.8M
-rw-------. 1 root root 1.5K Jul 24 21:03anaconda-ks.cfg
-rw-------. 1 root root 4.8M Jul 24 23:04core.10718
drwxr-xr-x. 2 root root 4.0K Jul 24 22:16Desktop

-d:查看目录自身属性,而非其内部的文件属性列表,并且与-l一起使用。
对比演示:

[root@localhost ~]# ls -l /var/log //查看目录下文件以及目录的属性
total 5592
-rw-------. 1 root root 2368 Jul 24 21:03 anaconda.ifcfg.log
-rw-------. 1 root root 23380 Jul 24 21:03 anaconda.log
-rw-------. 1 root root 43252 Jul 24 21:03 anaconda.program.log
-rw-------. 1 root root 272759 Jul 24 21:03 anaconda.storage.log

[root@localhost ~]# ls -ld /var/log //查看目录本身的属性,而非目录下文件或目录的属性
drwxr-xr-x. 14 root root 4096 Sep 18 21:12/var/log
[root@localhost ~]#

-r:--reverse 逆序显示
对比演示:
[root@localhost ~]# ls -l //默认显示是按照文件或目录名称的开头第一个字母a-z升序
total 3888
-rw-------. 1 root root 1484 Jul 24 21:03 anaconda-ks.cfg
-rw-------. 1 root root 4952064 Jul 2423:04 core.10718
drwxr-xr-x. 2 root root 4096 Jul 24 22:16 Desktop
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Documents
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Downloads
-rw-r--r--. 1 root root 55348 Jul 24 21:02 install.log
-rw-r--r--. 1 root root 10608 Jul 24 20:59 install.log.syslog
drwxr-xr-x. 2 root root 4096Jul 24 21:58 Music
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Pictures
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Public
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Templates
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Videos
[root@localhost ~]# ls -lr //逆序显示
total 3888
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Videos
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Templates
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Public
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Pictures
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Music
-rw-r--r--. 1 root root 10608 Jul 24 20:59 install.log.syslog
-rw-r--r--. 1 root root 55348 Jul 24 21:02 install.log
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Downloads
drwxr-xr-x. 2 root root 4096 Jul 24 21:58 Documents
drwxr-xr-x. 2 root root 4096 Jul 24 22:16 Desktop
-rw-------. 1 root root 4952064 Jul 2423:04 core.10718
-rw-------. 1 root root 1484 Jul 24 21:03 anaconda-ks.cfg
[root@localhost ~]#

-R:--recursive递归显示

常用命令第四组:
cat
cat - concatenate files and print on thestandard output(翻译:连接文件并且显示在标准输出上)
(concatenate ):文本文件内容查看工具,不能查看二进制文件。
怎样确定文件是文本文件还是二进制文件?
用命令:
file
~]#file /etc/fstab

演示:
[root@localhost ~]# file /etc/fstab
/etc/fstab:ASCII text
[root@localhost ~]#
演示操作:
[root@localhost ~]# cat /etc/passwd

[root@localhost~]# cat /etc/fstab

cat还有连接的功能,即将两个文件内容一并显示:
[root@localhost ~]# cat /etc/fstab

[root@localhost ~]# cat /etc/fstab /etc/issue

#
# /etc/fstab
# Created by anaconda on Sun Jul 2420:43:09 2016
#
# Accessible filesystems, by reference, aremaintained under '/dev/disk'
# See man pages fstab(5), findfs(8),mount(8) and/or blkid(8) for more info
#
UUID=9178142b-aba1-42e8-a984-521da92da283/ ext4 defaults 1 1
UUID=038bc1fd-9027-46c8-872d-bda4da62d67c/boot ext4 defaults 1 2
UUID=225e2fe0-5d7f-46b0-940a-c19a3d27f9a8/testdir ext4 defaults 1 2
UUID=9566dc6f-05de-43ec-be5f-59d430a47766swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
CentOSrelease 6.8 (Final)
Kernel\r on an \m

[root@localhost ~]#

-n: 给显示文件内容,统一加上行号源文件内容没有行号
[root@localhost ~]# cat -n /etc/fstab
-E:显示行结束符$
[root@localhost ~]# cat -E /etc/fstab

常用命令第五组:
tac
文件文本查看工具:
tac/etc/fstab
tac/etc/passwd
tac [OPTION]... [FILE]...
-n: 给显示的文本行编号
-E:显示行结束符$
对比演示:
[root@localhost ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Jul 2420:43:09 2016
#
# Accessible filesystems, by reference, aremaintained under '/dev/disk'
# See man pages fstab(5), findfs(8),mount(8) and/or blkid(8) for more info
#
UUID=9178142b-aba1-42e8-a984-521da92da283/ ext4 defaults 1 1
UUID=038bc1fd-9027-46c8-872d-bda4da62d67c/boot ext4 defaults 1 2
UUID=225e2fe0-5d7f-46b0-940a-c19a3d27f9a8/testdir ext4 defaults 1 2
UUID=9566dc6f-05de-43ec-be5f-59d430a47766swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@localhost ~]# tac /etc/fstab
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
UUID=9566dc6f-05de-43ec-be5f-59d430a47766swap swap defaults 0 0
UUID=225e2fe0-5d7f-46b0-940a-c19a3d27f9a8/testdir ext4 defaults 1 2
UUID=038bc1fd-9027-46c8-872d-bda4da62d67c/boot ext4 defaults 1 2
UUID=9178142b-aba1-42e8-a984-521da92da283/ ext4 defaults 1 1
#
# See man pages fstab(5), findfs(8),mount(8) and/or blkid(8) for more info
# Accessible filesystems, by reference, aremaintained under '/dev/disk'
#
# Created by anaconda on Sun Jul 2420:43:09 2016
# /etc/fstab
#

[root@localhost ~]#

常用命令第五组:
file
file - determine file type (翻译:确定文件 类型)这个是确定文件内容的类型。
演示:
[root@localhost ~]# file /etc/fstab
/etc/fstab: ASCII text
[root@localhost ~]# file /etc/issue //显示是文本文件
/etc/issue: ASCII text
[root@localhost ~]# file /bin/ls //显示是二进制文件
/bin/ls: ELF 64-bit LSB executable, x86-64,version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18,stripped
[root@localhost ~]#

常用命令第六组:
echo:回显
echo- display a line of text (翻译:显示一行文本)
语法:
echo [SHORT-OPTION]... [STRING]...
STRING可以使用引号,单引号双引号均可以使用。
单引号:强引用,变量引用不会执行替换;
双引号:弱引用,变量引用会被替换。
演示:
[root@localhost ~]# echo $SHELL
/bin/bash
[root@localhost ~]# echo '$SHELL'
$SHELL
[root@localhost ~]# echo "$SHELL"
/bin/bash
[root@localhost ~]#

语法:
echo LONG-OPTION
演示:
[root@localhost ~]# echo "hello everyone"
hello everyone
[root@localhost ~]#
注意:变量引用的正规符号 ${ 变量名 }只是,花括号可以省略。

-n:不进行换行
演示:
[root@localhost ~]# echo -n "helloeveryone"
hello everyone[root@localhost ~]#

-e:让转义符生效

\\ backslash

\a alert (BEL) 警报

\b backspace 退格
演示:
[root@localhost ~]# echo -e "hello\beveryone"
helleveryone
[root@localhost ~]#

\c produce no further output

\e escape

\f form feed

\n new line 换行
演示:
[root@localhost ~]# echo -e"hello \neveryone"
hello
everyone

\r carriage return回车符

\t horizontal tab 横向制表符
演示:
[root@localhost ~]# echo -e"hello \t everyone"
hello everyone

\v vertical tab 纵向制表符
演示:
[root@localhost ~]# echo -e"hello \v everyone"
hello
everyone
[root@localhost ~]#

拓展演示:
hello everyone[root@localhost ~]# echo "hello \neveryone"
hello \neveryone
[root@localhost~]# echo -e "hello \neveryone" //\n后留一个空格,显示的时候, everyone前面就会显示有个空格
hello
everyone
[root@localhost ~]#
另外echo还能实现颜色控制:




常用命令第七组:
关机或者重启命令:
shutdown //关机并切断电源
语法:
shutdown [OPTION]... TIME [MESSAGE]
OPTION:
-r:--reboot重启
-P:--poweroff关机
-h:--halt 关机
-c:--cancel取消上面的操作
TIME:
now:立刻关机
hh:mm:几点几分
+m:几分钟之后
+0就相当于now
MESSAGE:
其实这个message消息,相当于wall命令。
[root@localhost ~]# man wall
wall -- send a message to everybody’sterminal.(翻译:每个人的终端发送一条消息。)



另一台主机会收到如下信息:




在执行shutdown时,系统会自动向其他所有用户发送提示信息,如果要想取消shutdown命令,可以在本机上执行ctrl+c命令,也可以在其他管理员用户上,执行shutdown -c命令:

演示1:
[root@localhost ~]# shutdown -h now //关机

演示2:
[root@localhost ~]# shutdown -r +10

Broadcast message fromroot@localhost.localdomain
(/dev/pts/0)at 6:58 ...

The system is going down for reboot in 10minutes!
在另一台主机上会收到提示信息:
[root@localhost ~]#
Broadcast message fromroot@localhost.localdomain
(/dev/pts/0)at 6:58 ...

The system is going down for reboot in 10minutes!

Broadcast message fromroot@localhost.localdomain
(/dev/pts/0)at 6:59 ...

The system is going down for reboot in 9minutes!
如果在本地取消关机命令:直接按ctrl+c
^Cshutdown: Shutdown cancelled
[root@localhost ~]#
如果在其他主机上取消该命令,则执行#shutdown -c
[root@localhost ~]# shutdown -c
[root@localhost ~]#
在另一台主机上会显示:
shutdown: Shutdown cancelled
[root@localhost ~]#

所以shutdown命令,会自动向所有人发起wall信息
MESSAGE也可以指定为要发送的信息
[root@localhost ~]# shutdown -r +10"hello everyone"

Broadcast message fromroot@localhost.localdomain
(/dev/pts/0)at 7:10 ...

The system is going down for reboot in 10minutes!
helloeveryone

常用命令第八组:
日期相关的命令:
linux:系统启动时从硬件读取日期和时间信息,读取完成以后,就不在与硬件相关联,启动以后,就靠系统内核的时钟进行。
系统时钟
硬件时钟
date :查看系统时钟
date- print or set the system date and time(打印或设置系统日期和时间)
语法一:
date [OPTION]... [+FORMAT] //打印日期和时间
+FORMAT:
+是固定不变的
FORMAT:格式符,有很多种,但是都以%开头
%F:只显示年与日,例如:2016-08-10
演示:
[root@localhost ~]# date +%F
2016-09-19
[root@localhost ~]#
%T:只显示小时分钟秒,例如:02:23:45
演示:
[root@localhost ~]# date +%T
07:25:51
[root@localhost ~]#
在date命令中,+只能出现一次,如果要想将+%F与+%T一起显示,在显示时要用空格隔开,则要加上双引号引起来,以避免将空格解释成其他字符、
[root@localhost ~]# date +"%F %T"
2016-09-19 07:32:43
[root@localhost ~]#
[root@localhost ~]# date + "%F %T" //加号+与FORMAT格式符之间不能有空格。
date: extra operand `%F %T'
Try `date --help' for more information.
[root@localhost ~]#
%c: 显示哪年那月那日几点几分
[root@localhost ~]# date +%c
Mon 19 Sep 2016 07:36:44 AM EDT
[root@localhost ~]#
%Y:哪一年
[root@localhost ~]# date +%Y
2016
[root@localhost ~]#
%m:哪一月
[root@localhost ~]# date +%m
09
[root@localhost ~]#
%d:哪一日
[root@localhost ~]# date +%d
19
[root@localhost ~]#
%H:哪一小时
[root@localhost ~]# date +%H
07
[root@localhost ~]#
%M:哪一分钟
[root@localhost ~]# date +%M
44
[root@localhost ~]#
%S:哪一秒
[root@localhost ~]# date +%S
35
[root@localhost ~]#
%x:显示年/月/日
[root@localhost ~]# date +%x
09/19/2016
[root@localhost ~]#
%s:seconds since 1970-01-01 00:00:00 UTC(翻译:从1970年1月1日到现在的秒数)
[root@localhost ~]# date +%s //并且这个值时刻在累加
1474286264
[root@localhost ~]#
这样都拆开了,我们就可以任意组合各种显示:
演示1:用减号“-”来连接年月日
[root@localhost ~]# date +%Y-%m-%d //所以叫格式字符串
2016-09-19
[root@localhost ~]#
语法二:


date [-u|--utc|--universal][MMDDhhmm[[CC]YY][.ss]] //设置日期和时间



[-u|--utc|--universal]:不经常使用可以去掉


年可以设置4个数字,也可以设置2 个数字












date [MMDDhhmm[[CC]YY][.ss]]



关键字 月 日 时 分 年 秒
演示:
将时间设置为2013年10月10日8时8分8秒
[root@localhost ~]# date
Mon Sep 19 08:14:30 EDT 2016
[root@localhost ~]# date 101008082013.8
date: invalid date `101008082013.8'
[root@localhost ~]# date 101008082013.08
Thu Oct 10 08:08:08 EDT 2013
[root@localhost ~]# date 1010080813.08 //可以设置两位数的年份13年
Thu Oct 10 08:08:08 EDT 2013
[root@localhost ~]#

clock:硬件时钟
hwclock:硬件时钟
clock,hwclock:显示或设置硬件时间
[root@localhost ~]# hwclock //显示硬件时钟
Mon 19 Sep 2016 08:28:48 AM EDT -0.284459 seconds
[root@localhost ~]#
-s:--hctosys 以硬件时钟为准,设置系统时间
-w:--systohc:以系统时钟为准,设置硬件时钟
[root@localhost ~]# hwclock -w //设置硬件时钟
[root@localhost ~]# which clock //显示clock命令的所在位置
/sbin/clock
[root@localhost ~]# file /sbin/clock //显示文件内容的类型,是个软连接,说明clock与hwclock 是同一个命令
/sbin/clock: symbolic link to `hwclock'

cal :日历
语法:
cal [[ month] year]
[root@localhost ~]# cal //显示当月的日历
October 2013
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

[root@localhost ~]#
[root@localhost ~]# cal 2015 //显示2015年的全年日历

[root@localhost ~]# cal 4 2013 //查看指定月份日历
April 2013
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

[root@localhost ~]#

课外作业:
查man 手册。学习
which
which - shows the full path of (shell)commands.(翻译:显示(壳)命令的完整路径)
语法:
which[options] [--] programname [...]
演示:
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/bin/ls
[root@localhost ~]# which cd
/usr/bin/which: no cd in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost ~]# which cp
alias cp='cp -i'
/bin/cp
[root@localhost ~]#
whereis

who
who - show who is logged on(翻译:显示谁登录了系统)
演示:
[root@localhost ~]# who
root tty1 2016-09-18 20:40 (:0)
root pts/0 2016-09-19 03:15(10.1.250.23)
root pts/1 2016-09-19 04:08(10.1.250.23)
[root@localhost ~]#
w
w - Show who is logged on and what they aredoing.(翻译:显示登录是谁,在做什么)
语法:
w - [husfiV] [user]
演示:
[root@localhost ~]# w
08:36:19 up 12:07, 3 users, load average: 0.17, 0.17, 0.17
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 :0 20:40 ? 7.76s 7.76s /usr/bin/Xorg :0 -br-verbose -audit 4 -auth /va
root pts/0 10.1.250.23 03:15 ? 0.37s 0.00s man date
root pts/1 10.1.250.23 04:08 0.00s 0.28s 0.01s w
[root@localhost ~]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  命令 linux 常用