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

Red Hat linux 6.5 学习使用笔记《一》linux系统常用命令

2016-12-04 19:59 871 查看
《一》linux系统常用命令

1. 命令行简介

命令提示符

· #: 标识root

[root@localhost ~]# pwd
/root
[root@localhost ~]#


· $: 标识普通用户

[kevin@localhost root]$ pwd
/root
[kevin@localhost root]$


命令格式:命令 选项 参数

选项:

短选项: -
多个选项可以组合:-a -b = -ab
长选项: --


参数:命令的作用对象

2.查看命令帮助

命令 –help

在命令是内置命令时可以使用长选项–help

help 命令

在命令是外部命令是可以使用help

man 命令

无论长选项还是短选项都可以使用man命令

info 命令

查看在线帮助信息

3.查看命令是内置命令还是外部命令,用type命令查看

内置命令:linux系统内置

外部命令:存在于配置文件中的命令

[root@localhost ~]# type --help
-bash: type: --: invalid option
type: usage: type [-afptP] name [name ...]
[root@localhost ~]#


查看得知cd是内置命令:

[root@localhost ~]# type cd
cd is a shell builtin
[root@localhost ~]#


查看得知clear都是外部命令(存在于配置文件中):

[root@localhost ~]# type clear
clear is /usr/bin/clear
[root@localhost ~]#


用help查看内置命令

[kevin@localhost /]$ help cd
cd: cd [-L|-P] [dir]
Change the shell working directory.

Change the current directory to DIR.  The default DIR is the value of the
HOME shell variable.

The variable CDPATH defines the search path for the directory containing
DIR.  Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory.  If DIR begins
with a slash (/), then CDPATH is not used.

If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be  a variable name.  If that variable has a value,
its value is used for DIR.

Options:
-L  force symbolic links to be followed
-P  use the physical directory structure without following symbolic
links

The default is to follow symbolic links, as if `-L' were specified.

Exit Status:
Returns 0 if the directory is changed; non-zero otherwise.
[kevin@localhost /]$


4.用man命令查看ls命令的帮助

man命令:
分章节:
1:用户命令(/bin, /usr/bin, /usr/local/bin)
2:系统调用
3:库用户
4:特殊文件(设备文件)
5:文件格式(配置文件的语法)
6:游戏
7:杂项(Miscellaneous)
8: 管理命令(/sbin, /usr/sbin, /usr/local/sbin)
显示格式:
NAME:命令名称及功能简要说明
SYNOPSIS:用法说明,包括可用的选项
DESCRIPTION:命令功能的详尽说明,可能包括每一个选项的意义
OPTIONS:说明每一个选项的意义
FILES:此命令相关的配置文件
BUGS:
EXAMPLES:使用示例
SEE ALSO:另外参照
翻屏:
向后翻一屏:SPACE
向前翻一屏:b
向后翻一行:ENTER
向前翻一行:k
查找:
/KEYWORD: (向后查找)
n: 下一个
N:前一个

?KEYWORD:(向前查找)
n: 下一个
N:前一个

q: 退出


[root@localhost ~]#man ls


LS(1)                      User Commands                 LS(1)

NAME(全名)
ls - list directory contents

SYNOPSIS(概要)
ls [OPTION]... [FILE]...
ls 可选参数 可重复使用 可选文件 可重复使用

DESCRIPTION(详细)
List information about the FILEs (the current directory by default).  Sort
entries alphabetically if none of -cftuvSUX nor --sort.

Mandatory arguments to long options are mandatory for short options too.

-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
......................
......................
......................


5. 使用–help显示ls命令的帮助信息

[root@localhost /]# ls --help
用法:ls [选项]... [文件]...
列出 FILE 的信息(默认为当前目录)。
如果不指定-cftuvSUX 或--sort 选项,则根据字母大小排序。

长选项必须使用的参数对于短选项时也是必需使用的。

-a, --all             不隐藏任何以. 开始的项目
-A, --almost-all      列出除. 及.. 以外的任何项目
--author          与-l 同时使用时列出每个文件的作者
-b, --escape          以八进制溢出序列表示不可打印的字符
......................
......................
......................


-l:长格式显示
-h:和ls合并做单位转换
-a: 显示以.开头的隐藏文件
. 表示当前目录
.. 表示父目录
-A:除了隐藏文件全显示
-d: 显示目录自身属性
-i: index node, inode
-r: 逆序显示
-R: 递归(recursive)显示


显示隐藏文件( 不隐藏任何以. 开始的项目)

[root@localhost /]# ls
bin   dev  home  lib64       media  mnt  opt   root  selinux  sys  usr
boot  etc  lib   lost+found  misc   net  proc  sbin  srv      tmp  var
[root@localhost /]# ls -a
.          bin    dev   lib         media  net   .pulse         sbin     sys  var
..         boot   etc   lib64       misc   opt   .pulse-cookie  selinux  tmp
.autofsck  .dbus  home  lost+found  mnt    proc  root           srv      usr
[root@localhost /]#


列出除. 及.. 以外的任何项目

[kevin@localhost /]$ ls -A
.autofsck  boot   dev  home  lib64       media  mnt  opt   root  selinux  sys  usr
bin        .dbus  etc  lib   lost+found  misc   net  proc  sbin  srv      tmp  var
[kevin@localhost /]$


使用较长格式列出信息

[kevin@localhost /]$ ls -l
total 98
dr-xr-xr-x.   2 root root  4096 Dec  4 16:29 bin
dr-xr-xr-x.   5 root root  1024 Dec  4 02:45 boot
drwxr-xr-x.  18 root root  3820 Dec  4 15:43 dev
drwxr-xr-x. 121 root root 12288 Dec  4 16:29 etc
drwxr-xr-x.   4 root root  4096 Dec  4 02:51 home
dr-xr-xr-x.  11 root root  4096 Dec  4 02:40 lib
dr-xr-xr-x.   9 root root 12288 Dec  4 16:29 lib64
drwx------.   2 root root 16384 Dec  4 02:33 lost+found
drwxr-xr-x.   2 root root  4096 Dec  4 03:00 media
drwxr-xr-x.   2 root root     0 Dec  4 15:42 misc
drwxr-xr-x.   3 root root  4096 Dec  4 03:02 mnt
drwxr-xr-x.   2 root root     0 Dec  4 15:42 net
drwxr-xr-x.   3 root root  4096 Dec  4 02:43 opt
dr-xr-xr-x. 207 root root     0 Dec  4 15:42 proc
dr-xr-x---.   7 root root  4096 Dec  4 18:53 root
dr-xr-xr-x.   2 root root 12288 Dec  4 16:29 sbin
drwxr-xr-x.   7 root root     0 Dec  4 15:42 selinux
drwxr-xr-x.   2 root root  4096 Jun 28  2011 srv
drwxr-xr-x.  13 root root     0 Dec  4 15:42 sys
drwxrwxrwt.  19 root root  4096 Dec  4 17:19 tmp
drwxr-xr-x.  13 root root  4096 Dec  4 02:34 usr
drwxr-xr-x.  22 root root  4096 Dec  4 02:43 var
[kevin@localhost /]$


ls -l 显示文件类型格式简介

比如:
drwxr-xr-x.  22 root root  4096 Dec  4 02:43 var
-:普通文件 (f)
d: 目录文件
b: 块设备文件 (block)
c: 字符设备文件 (character)
l: 符号链接文件(symbolic link file)
p: 命令管道文件(pipe)
s: 套接字文件(socket)

文件权限:9位,每3位一组,每一组:rwx(读,写,执行), r--
文件硬链接的次数
文件的属主(owner)
文件的属组(group)
文件大小(size),单位是字节
时间戳(timestamp):最近一次被修改的时间


与-l 一起,以易于阅读的格式输出文件大小(例如 1K 234M 2G)

[kevin@localhost /]$ls -l -h 可以两个选项合在一起写,
比如:
[kevin@localhost /]$ls -lh
或者
[kevin@localhost /]$ls -hl


[kevin@localhost /]$ ls -lh
total 98K
dr-xr-xr-x.   2 root root 4.0K Dec  4 16:29 bin
dr-xr-xr-x.   5 root root 1.0K Dec  4 02:45 boot
drwxr-xr-x.  18 root root 3.8K Dec  4 15:43 dev
drwxr-xr-x. 121 root root  12K Dec  4 16:29 etc
drwxr-xr-x.   4 root root 4.0K Dec  4 02:51 home
dr-xr-xr-x.  11 root root 4.0K Dec  4 02:40 lib
dr-xr-xr-x.   9 root root  12K Dec  4 16:29 lib64
drwx------.   2 root root  16K Dec  4 02:33 lost+found
drwxr-xr-x.   2 root root 4.0K Dec  4 03:00 media
drwxr-xr-x.   2 root root    0 Dec  4 15:42 misc
drwxr-xr-x.   3 root root 4.0K Dec  4 03:02 mnt
drwxr-xr-x.   2 root root    0 Dec  4 15:42 net
drwxr-xr-x.   3 root root 4.0K Dec  4 02:43 opt
dr-xr-xr-x. 207 root root    0 Dec  4 15:42 proc
dr-xr-x---.   7 root root 4.0K Dec  4 18:53 root
dr-xr-xr-x.   2 root root  12K Dec  4 16:29 sbin
drwxr-xr-x.   7 root root    0 Dec  4 15:42 selinux
drwxr-xr-x.   2 root root 4.0K Jun 28  2011 srv
drwxr-xr-x.  13 root root    0 Dec  4 15:42 sys
drwxrwxrwt.  19 root root 4.0K Dec  4 17:19 tmp
drwxr-xr-x.  13 root root 4.0K Dec  4 02:34 usr
drwxr-xr-x.  22 root root 4.0K Dec  4 02:43 var
[kevin@localhost /]$


6.cd命令:change directory

cd ~USERNAME: 进入指定用户的家目录
cd -:在当前目录和前一次所在的目录之间来回切换


[root@localhost kevin]# cd ~kevin
[root@localhost kevin]#


[root@localhost ~]# cd -
/home/kevin
[root@localhost kevin]# cd -
/root
[root@localhost ~]#


7.时间管理

linux粉两种时间:系统时间和硬件时间

[root@localhost ~]# date
2016年 12月 04日 星期日 20:04:46 CST


date命令:

用法:date [选项]… [+格式]

 或:date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]

以给定的格式显示当前时间,或是设置系统日期。

-d,–date=字符串 显示指定字符串所描述的时间,而非当前时间

[root@localhost ~]# date -d "2016-11-11 12:00:01"
2016年 11月 11日 星期五 12:00:01 CST
[root@localhost ~]# date -d "2016/11/11 12:00:01"
2016年 11月 11日 星期五 12:00:01 CST


-f,–file=日期文件 类似–date,从日期文件中按行读入时间描述

[root@localhost ~]# echo "2016/11/11 12:00:01" > date.txt
[root@localhost ~]# date -f date.txt
2016年 11月 11日 星期五 12:00:01 CST


-r, –reference=文件 显示文件指定文件的最后修改时间

[root@localhost /]# date -r bin
2016年 12月 04日 星期日 16:52:56 CST


-R, –rfc-2822 以RFC 2822格式输出日期和时间

例如:2006年8月7日,星期一 12:34:56 -0600

–rfc-3339=TIMESPEC 以RFC 3339 格式输出日期和时间。

TIMESPEC=
date',
seconds’,或 `ns’

表示日期和时间的显示精度。

日期和时间单元由单个的空格分开:

2006-08-07 12:34:56-06:00

[root@localhost /]# date -R
Sun, 04 Dec 2016 20:17:42 +0800


-s, –set=字符串 设置指定字符串来分开时间

[root@localhost /]# date -s "2016/11/11 12:00:01"
2016年 11月 11日 星期五 12:00:01 CST
[root@localhost /]# date
2016年 11月 11日 星期五 12:00:06 CST


-u, –utc, –universal 输出或者设置协调的通用时间

–help 显示此帮助信息并退出

–version 显示版本信息并退出

[root@localhost /]# date -u
2016年 11月 11日 星期五 04:01:03 UTC


给定的格式FORMAT 控制着输出,解释序列如下:

%% 一个文字的 %

%a 当前locale 的星期名缩写(例如: 日,代表星期日)

%A 当前locale 的星期名全称 (如:星期日)

%b 当前locale 的月名缩写 (如:一,代表一月)

%B 当前locale 的月名全称 (如:一月)

%c 当前locale 的日期和时间 (如:2005年3月3日 星期四 23:05:25)

%C 世纪;比如 %Y,通常为省略当前年份的后两位数字(例如:20)

%d 按月计的日期(例如:01)

%D 按月计的日期;等于%m/%d/%y

%e 按月计的日期,添加空格,等于%_d

%F 完整日期格式,等价于 %Y-%m-%d

%g ISO-8601 格式年份的最后两位 (参见%G)

%G ISO-8601 格式年份 (参见%V),一般只和 %V 结合使用

%h 等于%b

%H 小时(00-23)

%I 小时(00-12)

%j 按年计的日期(001-366)

%k 时(0-23)

%l 时(1-12)

%m 月份(01-12)

%M 分(00-59)

%n 换行

%N 纳秒(000000000-999999999)

%p 当前locale 下的”上午”或者”下午”,未知时输出为空

%P 与%p 类似,但是输出小写字母

%r 当前locale 下的 12 小时时钟时间 (如:11:11:04 下午)

%R 24 小时时间的时和分,等价于 %H:%M

%s 自UTC 时间 1970-01-01 00:00:00 以来所经过的秒数

%S 秒(00-60)

%t 输出制表符 Tab

%T 时间,等于%H:%M:%S

%u 星期,1 代表星期一

%U 一年中的第几周,以周日为每星期第一天(00-53)

%V ISO-8601 格式规范下的一年中第几周,以周一为每星期第一天(01-53)

%w 一星期中的第几日(0-6),0 代表周一

%W 一年中的第几周,以周一为每星期第一天(00-53)

%x 当前locale 下的日期描述 (如:12/31/99)

%X 当前locale 下的时间描述 (如:23:13:48)

%y 年份最后两位数位 (00-99)

%Y 年份

%z +hhmm 数字时区(例如,-0400)

%:z +hh:mm 数字时区(例如,-04:00)

%::z +hh:mm:ss 数字时区(例如,-04:00:00)

%:::z 数字时区带有必要的精度 (例如,-04,+05:30)

%Z 按字母表排序的时区缩写 (例如,EDT)

默认情况下,日期的数字区域以0 填充。

以下可选标记可以跟在”%”后:

(连字符)不填充该域

_ (下划线)以空格填充

0 (数字0)以0 填充

^ 如果可能,使用大写字母

# 如果可能,使用相反的大小写

在任何标记之后还允许一个可选的域宽度指定,它是一个十进制数字。

作为一个可选的修饰声明,它可以是E,在可能的情况下使用本地环境关联的

表示方式;或者是O,在可能的情况下使用本地环境关联的数字符号。

请向bug-coreutils@gnu.org 报告date 的错误

GNU coreutils 项目主页:http://www.gnu.org/software/coreutils/

GNU 软件一般性帮助:http://www.gnu.org/gethelp/

请向http://translationproject.org/team/zh_CN.html 报告date 的翻译错误

要获取完整文档,请运行:info coreutils ‘date invocation’

[root@localhost /]# date "+Today is %y年%m月%d日 %H:%M:%S \nToday is sunday"
Today is 16年12月04日 20:28:51 \nToday is sunday
[root@localhost /]# date +%s
1480854578
[root@localhost /]# date +%d
04
[root@localhost /]# date +%D
12/04/16
[root@localhost /]# date +%T
20:29:48
[root@localhost /]#


8.显示硬件时间hwclock命令

hwclock - query and set the hardware clock (RTC)

Usage: hwclock [function] [options…]

Functions:

-h | –help show this help

-r | –show read hardware clock and print result

–set set the rtc to the time given with –date

[root@localhost /]# hwclock -r
2016年12月04日 星期日 20时34分00秒  -0.177733 seconds


-s | –hctosys set the system time from the hardware clock

[root@localhost /]# hwclock
2016年12月04日 星期日 20时37分13秒  -0.813986 seconds
[root@localhost /]# date
2016年 12月 04日 星期日 20:37:14 CST
[root@localhost /]# date -s "2014/11/11 10:10:10"
2014年 11月 11日 星期二 10:10:10 CST
[root@localhost /]# date
2014年 11月 11日 星期二 10:10:14 CST
[root@localhost /]# hwclock -s
[root@localhost /]# date
2016年 12月 04日 星期日 20:37:32 CST


-w | –systohc set the hardware clock to the current system time

–systz set the system time based on the current timezone

–adjust adjust the rtc to account for systematic drift since

the clock was last set or adjusted

[root@localhost /]# date -s "2014/11/11 10:10:10"
2014年 11月 11日 星期二 10:10:10 CST
[root@localhost /]# hwclock
2016年12月04日 星期日 20时38分50秒  -0.704484 seconds
[root@localhost /]# hwclock -w
[root@localhost /]# hwclock
2014年11月11日 星期二 10时10分24秒  -0.813863 seconds


-c | –compare periodically compare the system clock with the CMOS clock

–getepoch print out the kernel’s hardware clock epoch value

–setepoch set the kernel’s hardware clock epoch value to the

value given with –epoch

-v | –version print out the version of hwclock to stdout

[root@localhost /]# hwclock -c
hw-time      system-time         freq-offset-ppm   tick
1480855369   1480855368.864707
1480855379   1480855378.889313              2461     25
1480855389   1480855388.893876              1458     15
1480855399   1480855398.915054              1678     17
1480855409   1480855408.917412              1318     13
1480855419   1480855418.939550              1497     15
1480855429   1480855428.940992              1271     13
1480855439   1480855438.963341              1409     14
1480855449   1480855448.967438              1284     13
1480855459   1480855458.990187              1394     14
1480855469   1480855468.995829              1311     13
1480855479   1480855479.018921              1402     14
1480855489   1480855489.020411              1298     13
1480855499   1480855499.043206              1373     14


Options:

-u | –utc the hardware clock is kept in UTC

[root@localhost /]# hwclock -u
Mon 05 Dec 2016 08:45:21 AM CST  -1.036827 seconds


-f | –rtc=path special /dev/… file to use instead of default

–directisa access the ISA bus directly instead of /dev/rtc

–badyear ignore rtc’s year because the bios is broken

–date specifies the time to which to set the hardware clock

–epoch=year specifies the year which is the beginning of the

hardware clock’s epoch value

–noadjfile do not access /etc/adjtime. Requires the use of

either –utc or –localtime

–adjfile=path specifies the path to the adjust file (default is

/etc/adjtime)

–test do everything except actually updating the hardware

clock or anything else

[root@localhost /]# hwclock --test
2016年12月04日 星期日 20时47分44秒  -0.407453 seconds


-D | –debug debug mode

[root@localhost /]# hwclock -D
hwclock from util-linux-ng 2.17.2
Using /dev interface to clock.
Last drift adjustment done at 1480881424 seconds after 1969
Last calibration done at 1480881424 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2016/12/05 00:46:14
Hw clock time : 2016/12/05 00:46:14 = 1480898774 seconds since 1969
Mon 05 Dec 2016 08:46:14 AM CST  -0.892241 seconds


9.查看日历cal命令(calendar)

[root@localhost /]# cal --help
cal:无效选项 -- -
usage: cal [-13smjyV] [[[day] month] year]


[root@localhost /]# cal 12 12 2016
十二月 2016
日 一 二 三 四 五 六
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 /]# cal 12 2016
十二月 2016
日 一 二 三 四 五 六
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


10.echo显示命令

[root@localhost /]# type echo
echo is a shell builtin
[root@localhost /]# help echo


echo: echo [-neE] [arg …]

Write arguments to the standard output.

Display the ARGs on the standard output followed by a newline.

Options:

-n do not append a newline

-e enable interpretation of the following backslash escapes【使反斜杠转义后的解释生效】

-E explicitly suppress interpretation of backslash escapes

【显式地抑制反斜杠转义的解释】

`echo’ interprets the following backslash-escaped characters:

\a alert (bell)【警告】

\b backspace【退格(删除)】

\c suppress further output【进一步抑制输出】

\e escape character【转义字符】

\f form feed【格式符】

[root@localhost /]# echo -e "Today is \f sunday"
Today is
sunday
[root@localhost /]#


\n new line

\r carriage return【回车】

\t horizontal tab【横向制表符】

\v vertical tab【纵向制表符】

\ backslash

[root@localhost /]# echo -e "Today is \\ sunday"
Today is \ sunday


\0 nnn the character whose ASCII code is NNN (octal). NNN can be

0 to 3 octal digits

\x HH the eight-bit character whose value is HH (hexadecimal). HH

can be one or two hex digits

Exit Status:

Returns success unless a write error occurs.

11.printf命令打印字符

NAME

printf - format and print data

SYNOPSIS

printf FORMAT [ARGUMENT]…

printf OPTION

DESCRIPTION

Print ARGUMENT(s) according to FORMAT, or execute according to OPTION:

--help display this help and exit

--version
output version information and exit


FORMAT controls the output as in C printf. Interpreted sequences are:

\"     double quote

\\     backslash

\a     alert (BEL)

\b     backspace

\c     produce no further output

\e     escape

\f     form feed

\n     new line

\r     carriage return

\t     horizontal tab
\t     horizontal tab

\v     vertical tab

\NNN   byte with octal value NNN (1 to 3 digits)

\xHH   byte with hexadecimal value HH (1 to 2 digits)

\uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits)

\UHHHHHHHH
Unicode character with hex value HHHHHHHH (8 digits)

%%     a single %

%b     ARGUMENT  as  a  string  with  ‘\’ escapes interpreted, except that
octal escapes are of the form \0 or \0NNN

and all C format specifications ending with  one  of  diouxXfeEgGcs,  with
ARGUMENTs converted to proper type first.  Variable widths are handled.

NOTE:  your shell may have its own version of printf, which usually super-
sedes the version described here.  Please refer to your shell’s documenta-
tion for details about the options it supports.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: