Linux基础继续

Posted by

  hi

ls 选项 文件或目录
选项:
-a 显示所有文件,包括隐藏文件
-l 显示详细信息
-d 显示目录属性
-h 人性化显示文件大小
-i 显示inode

初识50个Linux命令,初识50Linux命令

虽然今天是最最美好的周六(前不着工作日后不着工作日),但老子还要来改报告,但额就是不想改,你拿我有啥办法啊。。。

pwd 当前路径

1. 【命令】:cat

【功能说明】:

concatenate files and print on the standard output
#连接文件并打印到标准输出,有标准输出的都可以用重定向定向导入到文件里面

【语法格式】:

cat [OPTION]…[FILE]…

【选项参数】:

参数

说明

简解

-b,–number-nonblank

number nonempty output lines

非空输出行编号

-n,–number

number all outputnlines

所有行输出编号

【示例】:

1、基本用法:查看文件内容

[[email protected]
~]# cat /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

 

2、把log1.txt的文件内容加上行号输入log2.txt这个文件里

[[email protected]
~]# cat -n log1.txt

     1  2012-1

     2  2013-1

     3  2014-1

     4

     5

[[email protected]
~]# cat -n log1.txt>log2.txt

[[email protected]
~]# cat log2.txt

     1  2012-1

     2  2013-1

     3  2014-1

     4

     5

 

3、把文件log1.txt里面的文件空格不显示行号加入log2.txt中

[[email protected]
~]# cat -n log1.txt

     1  2012-1

     2  2013-1

     3  2014-1

     4

     5

[[email protected]
~]# cat -b log1.txt

     1  2012-1

     2  2013-1

     3  2014-1

 

 

[[email protected]
~]# cat -b log1.txt>log2.txt

[[email protected]
~]# cat log2.txt

     1  2012-1

     2  2013-1

     3  2014-1

 

4、同时显示log1.txt和log2.txt

[[email protected]
~]# cat log1.txt log2.txt

2012-1

2013-1

2014-1

 

 

     1  2012-1

     2  2013-1

     3  2014-1

 

5、使用cat编辑文档内容

[[email protected]
~]# cat >log1.txt<<EOF

> 2012-3

> 2013-3

> 2014-3

> EOF

[[email protected]
~]# cat log1.txt

2012-3

2013-3

2014-3

 

6、使用cat追加编辑文档内容

[[email protected]
data]# cat >>oldboy.txt<<EOF

> ni hao

> jin tian tian qi zhen hao

> EOF

[[email protected]
data]# cat oldboy.txt

I am oldboy

I am study linux

ni hao

jin tian tian qi zhen hao

 

 

争取完结Linux基础

cd 切换目录

2. 【命令】:cd

【功能说明】:

Change the current directory to DIR #更改当前路径为DIR

【语法格式】:

cd [-L|-P] [dir]

【选项参数】:

参数

说明

简解

-L

force symbolic links to be followed

 

-P

use the physical directory structure without following symbolic

 

 

 

【示例】:

1、基本用法:更改目录路径到根目录

[[email protected]
~]# cd /

[[email protected]
/]#

 

2、基本用法:更改目录路径到上级目录

[[email protected]
~]# cd ..

[[email protected]
/]#

 

3、更改目录路径到上一次使用的目录

[[email protected]
/]# cd –

/root

[[email protected]
~]#

 

 

一、Linux常用命令(三)

mkdir -p [目录名]
-p 递归创建

3. 【命令】:mkdir

【功能说明】:

make directories   #创建目录

【语法格式】:

mkdir [OPTION]… DIRECTORY…

【选项参数】:

参数

说明

简解

-m, –mode

set file mode (as in chmod), not a=rwx – umask

设定权限<模式>(类似chmod),而不是rwxrwxrwx减umask

-p, –parents

no error if existing, make parent directories as needed

递归创建目录

-v, –verbose

print a message for each created directory

每次创建新目录都显示信息

-Z

set the SELinux security context of each created directory to CTX

 

【示例】:

1、基本用法:创建目录

[[email protected]
~]# mkdir /data

[[email protected]
~]# cd /data

[[email protected]
data]# pwd

/data

 

2、递归创建多个目录

[[email protected]
data]# mkdir -p /oldboy/oldboy

[[email protected]
data]# cd /oldboy/oldboy

[[email protected]
oldboy]# pwd

/oldboy/oldboy

 

 

 

4、帮助命令

rmdir 删除目录
删除的是空目录

4. 【命令】:ls

【功能说明】:

list directory contents   #列出目标目录中所有的子目录和文件

【语法格式】:

ls [OPTION]… [FILE]…

【选项参数】:

参数

说明

简解

-a, –all

do not ignore entries starting with.

列出目录下的所有文件

-A, –almost-all

do not list implied.and ..

同-a,但不列出“.”(表示当前目录)和“..”(表示当前目录的父目录)

-b, –escape

print octal escapes for nongraphic characters

把文件名中不可输出的字符用反斜杠加字符编号的形式列出

-B, –ignore-backups

do not list implied entries ending with ~

不输出以“~”结尾的备份文件

-c

sort by ctime

配合-lt;根据ctime排序及显示ctime(文件状态最后更改的时间)配合-l:显示ctime但根据名称排序否则:根据ctime排序

-C

list entries by clumns

每栏由上至下列出项目

-d, –directory

list directory entries instead of contentsm,and do not dereference symbolic links

将目录象文件一样显示,而不是显示其下的文件

-D, –dired

generate output designed for Emacs’ dired mode

产生适合Emacs的dired模式使用的结果

-f

do not sort,enable -aU,disable -ls –color

对输出的文件不进行排序,-aU选项生效,-1st选项失效

-F –classify

append indicator(one of */=>@|)to entries

在每个文件名后附上一个字符以说明该文件的类型,“*”表示可执行的普通
文件;“/”表示目录;“@”表示符号链接;“|”表示FIFOs;“=”表示套
接字(sockets)

-g

like -l,but do not list owner

类似-l,但不列出所有者

-h, –human-readable

with -l,print sizes in human readable format

以容易理解的格式列出文件大小

-H, –dereference-command-line

follow symbolic links listed on the command line

使用命令列中的符号链接指示的真正目的地

-i, –inode

print the index number of each file

打印出每个文件的inode号

-I,–ignore=PATTERN

do not list implied entries matching shell PATTERN

不打印出任何符合shell万用字符<样式>的项目

-k

like –block-size=1K

以k字节的形式表示文件的大小

-l

use a long listing format

除了文件名之外,还将文件的权限、所有者、文件大小等信息详细列出来

-L,–dereference

when showing file information for a symbolic link,show information for the file the link references rather than for the link itself

当显示符号链接的文件信息时,显示符号链接所指示的对象而并非符号链接本身的信息。

-m

fill width with a comma separated list of entries

所有项目以逗号分隔,并填满整行行宽

-n, –numeric-uid-gid

like -l,but list numeric user and group IDs

用数字的UID,GID代替名称

-N, –literal

print raw entry names

不限制文件长度

-o

like -l,but do not list group information

类似-l,显示文件的除组信息外的详细信息

-p, –indicator-style=slash

append / indicator to directories

对目录添加“/”符号

-q, –hide-control-chars

print ? instead of non graphic characters

用?号代替不可输出的字符

-Q, –quote-name

enclose entry names in double quotes

把输出的文件名用双引号括起来

-r, –reverse

reverse order while sorting

依相反次序排列

-R, –recursive

list subdirectories recursively

同时列出所有子目录层

-s, –size

print the allocated size of each file,in blocks

以块大小为单位列出所有文件的大小

-S

sort by file size

根据文件大小排序

-t

sort by modification time

以文件修改时间排序

-T, –tabsize=COLS

assume tab stops at each COLS instead of 8

 

-u

with -lt: sort by, and show, access time with

-l:show access time and sort by name

otherwise: sort by access time

配合-lt:显示访问时间而且依访问时间排序

配合-l:显示访问时间但根据名称排序

其他:根据访问时间排序

-U

do not sort; list entries in directory order

不进行排序;依文件系统原有的次序列出项目

-v

natural sort of (version) numbers within text

根据版本进行排序

-w, –width=COLS

assume screen width instead of current value

自行指定屏幕宽度而不使用目前的数值

-x

list entries by lines instead of by columns

逐行列出项目而不是逐栏列出

-X

sort alphabetically by entry extension

根据扩展名排序

-1

list one file per line

每行只列出一个文件

 

 

【示例】:

1、基本用法:查看目录内容

[[email protected]
~]# ls

anaconda-ks.cfg  data  install.log  install.log.syslog  log1.txt
 log2.txt  test.txt

 

2、基本用法:查看目录全部内容(包含隐藏文件)

[[email protected]
~]# ls -la

total 88

dr-xr-x—.  3 root root  4096 Mar  7 18:16 .

dr-xr-xr-x. 24 root root  4096 Mar  9 17:00 ..

-rw——-.  1 root root  1079 Mar  3 23:14 anaconda-ks.cfg

-rw——-.  1 root root  5003 Mar  9 04:13 .bash_history

-rw-r–r–.  1 root root    18 May 20  2009 .bash_logout

-rw-r–r–.  1 root root   176 May 20  2009 .bash_profile

-rw-r–r–.  1 root root   176 Sep 23  2004 .bashrc

-rw-r–r–.  1 root root   100 Sep 23  2004 .cshrc

drwxr-xr-x.  3 root root  4096 Mar  9 01:47 data

-rw-r–r–.  1 root root 22179 Mar  3 23:14 install.log

-rw-r–r–.  1 root root  5890 Mar  3 23:13 install.log.syslog

-rw-r–r–.  1 root root    18 Mar  7 18:13 log1.txt

-rw-r–r–.  1 root root    44 Mar  7 17:53 log2.txt

-rw-r–r–.  1 root root   129 Dec  4  2004 .tcshrc

-rw-r–r–.  1 root root     0 Mar  7 18:27 test.txt

 

3、基本用法:查看目录(使用长模式)

[[email protected]
~]# ls -l

total 52

-rw——-. 1 root root  1079 Mar  3 23:14 anaconda-ks.cfg

drwxr-xr-x. 3 root root  4096 Mar  9 01:47 data

-rw-r–r–. 1 root root 22179 Mar  3 23:14 install.log

-rw-r–r–. 1 root root  5890 Mar  3 23:13 install.log.syslog

-rw-r–r–. 1 root root    18 Mar  7 18:13 log1.txt

-rw-r–r–. 1 root root    44 Mar  7 17:53 log2.txt

-rw-r–r–. 1 root root     0 Mar  7 18:27 test.txt

 

4.1 帮助命令man

删除命令
rm -rf 要删除的目录或文件名

5. 【命令】:pwd

【功能说明】:

print name of current/working directory   #打印当前目录路径

【语法格式】:

pwd [OPTION]

【选项参数】:

参数

说明

简解

-L, –logical

use PWD from environment,even if it contains symlinks

当目录为链接路径时,显示链接路径

-P, –physical

avoid all symlinks

显示实际物理路径,而非使用链接

【示例】:

1、基本用法:显示当前目录所在路径

[[email protected]
~]# pwd

/root

[[email protected]
~]#

 

2、显示当前目录的物理路径

[[email protected]
~]# cd /etc/init.d

[[email protected]
init.d]# pwd -P

/etc/rc.d/init.d

 

3、显示当前目录的链接路径

[[email protected]
~]# cd /etc/init.d

[[email protected]
init.d]# pwd -L

/etc/init.d

 

 

man 命令

拷贝命令
cp 选项 源文件/目录 目标目录

6. 【命令】:touch

【功能说明】:

change file timestamps   #创建文件或改变文件的时间戳

【语法格式】:

pwd [OPTION]… FILE…

【选项参数】:

参数

说明

简解

-a

change only the access time

只更改存取时间

-c, –no-create

do not create any files

不新建任何文档

-d, –date=STRING

parse STRING and use it instead of current time

使用指定的日期时间,而非现在的时间

-f

(ignored)

忽略

-h, –no-dereference

affect each symbolic link instead of any referenced file (useful only on systems

that can change the timestamps of a symlink)

会影响符号链接本身,而非符号链接所指示的目的地

-m

change only the modification time

只更改变动时间

-r, –reference=FILE

use this file’s times instead of current time

把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同

-t STAMP

use [[CC]YY]MMDDhhmm[.ss] instead of current time

使用指定格式的日期时间,而非当前时间

–time=WORD

change  the  specified time: WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m

使用WORD指定的时间:access、atime、use都等于-a选项的效果,而modify、mtime等于-m选项的效果

【示例】:

1、基本用法:创建新文件

[[email protected]
data]# touch log2012.log log2013.log

[[email protected]
data]# ll

total 0

-rw-r–r–. 1 root root 0 Mar  9 17:48 log2012.log

-rw-r–r–. 1 root root 0 Mar  9 17:48 log2013.log

 

2、不创建不存在的文件

[[email protected]
data]# touch -c log2014.log

[[email protected]
data]# ll

total 0

-rw-r–r–. 1 root root 0 Mar  9 17:48 log2012.log

-rw-r–r–. 1 root root 0 Mar  9 17:48 log2013.log

 

3、更新log2012.log的时间和log.log时间戳相同

[[email protected]
data]# touch log.log

[[email protected]
data]# ll

total 0

-rw-r–r–. 1 root root 0 Mar  9  2016 log2012.log

-rw-r–r–. 1 root root 0 Mar  9  2016 log2013.log

-rw-r–r–. 1 root root 0 May 23 00:00 log.log

[[email protected]
data]# touch -r log.log log2012.log

[[email protected]
data]# ll

total 0

-rw-r–r–. 1 root root 0 May 23 00:00 log2012.log

-rw-r–r–. 1 root root 0 Mar  9  2016 log2013.log

-rw-r–r–. 1 root root 0 May 23 00:00 log.log

 

4、设定文件时间戳

[[email protected]
data]# touch -t 201211142234.50 log2016.log

[[email protected]
data]# ll

total 0

-rw-r–r–. 1 root root 0 Nov 14  2012 log2016.log

 

【说明】:

     -t  time 使用指定的时间值 time 作为指定文件相应时间戳记的新值.此处的 time规定为如下形式的十进制数:      

      [[CC]YY]MMDDhhmm[.SS]     

  
   这里,CC为年数中的前两位,即”世纪数”;YY为年数的后两位,即某世纪中的年数.如果不给出CC的值,则touch   将把年数CCYY限定在1969–2068之内.MM为月数,DD为天将把年数CCYY限定在1969–2068之内.MM为月数,DD为天数,hh 为小时数(几点),mm为分钟数,SS为秒数.此处秒的设定范围是0–61,这样可以处理闰秒.这些数字组成的时间是环境变量TZ指定的时区中的一个时 间.由于系统的限制,早于1970年1月1日的时间是错误的。

 

–例子

剪切改名命令
mv 源文件/目录 目标目录

7. 【命令】:echo

【功能说明】:

display a line of text   #打印输出

【语法格式】:

echo [SHORT-OPTION]… [STRING]…

echo LONG-OPTION

【选项参数】:

参数

说明

简解

-n

do not output the trailing newline

不要在最后自动换行

-e

enable interpretation of backslash escapes

若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出

  \a 发出警告声;
   \b 删除前一个字符;
   \c 最后不加上换行符号;
   \f 换行但光标仍旧停留在原来的位置;
   \n 换行且光标移至行首;
   \r 光标移至行首,但不换行;
   \t 插入tab;
   \v 与\f相同;
   \\ 插入\字符;
   \nnn 插入nnn(八进制)所代表的ASCII字符;

-E

disable interpretation of backslash escapes (default)

不解释转意字符

【示例】:

1、基本用法:打印内容

[[email protected]
data]# echo “I am studying linux.”

I am studying linux.

 

2、打印变量内容

[[email protected]
data]# echo $PATH

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

 

 

man ls

创建软连接/硬链接
硬链接
ln 路径文件名 文件目录文件名
软连接
ln -s 绝对路径文件名 文件目录文件名

8. 【命令】:xargs

【功能说明】:

build and execute command lines from standard input
 #从标准输入获取内容创建和执行命令

【语法格式】:

xargs  [-0prtx]  [-E  eof-str]  [-e[eof-str]]
 [–eof[=eof-str]] [–null] [-d delimiter]

       [–delimiter delimiter]  [-I  replace-str]
 [-i[replace-str]]  [–replace[=replace-str]]

       [-l[max-lines]]  [-L max-lines]
[–max-lines[=max-lines]] [-n max-args] [–max-args=max-

       args] [-s  max-chars]  [–max-chars=max-chars]  [-P
 max-procs]  [–max-procs=max-procs]

       [–interactive]     [–verbose]     [–exit]
   [–no-run-if-empty]    [–arg-file=file]

       [–show-limits] [–version] [–help] [command
[initial-arguments]]

【选项参数】:

参数

说明

简解

 

 

 

【示例】:

1、基本用法:与find配合使用,删除/data目录下所有.txt文件

[[email protected]
~]# find /data -type f -name “*.txt”|xargs rm -f

[[email protected]
~]# cd /data

[[email protected]
data]# ll

total 0

 

 

 

LS(1) User Commands LS(1)

文件搜索

9. 【命令】:mv

【功能说明】:

 move (rename) files #移动或重命名文件

【语法格式】:

mv [OPTION]… [-T] SOURCE DEST

mv [OPTION]… SOURCE… DIRECTORY

mv [OPTION]… -t DIRECTORY SOURCE…

【选项参数】:

参数

说明

简解

–backup[=CONTROL]

make a backup of each existing destination file

备份模式:若需覆盖文件,则覆盖前先行备份

-b

like –backup but does not accept an argument

若需覆盖文件,则覆盖前先行备份。

-f, –force

do not prompt before overwriting

force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖

-i, –interactive

prompt before overwrite

若目标文件 (destination) 已经存在时,就会询问是否覆盖

-n, –no-clobber

do not overwrite an existing file

 

-S, –suffix=SUFFIX

override the usual backup suffix

为备份文件指定后缀,而不使用默认的后缀;

-t, –target-directory=DIRECTORY

move all SOURCE arguments into DIRECTORY

指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后

-T, –no-target-directory

treat DEST as a normal file

 

-u, –update

move  only  when  the  SOURCE file is newer than the destination file or when the destination file is missing

若目标文件已经存在,且 source 比较新,才会更新(update)

-v, –verbose

explain what is being done

显示程序执行过程

【示例】:

1、基本用法:移动文件

[[email protected]
~]# mv ett.txt /root/data

[[email protected]
~]# cd data

[[email protected]
data]# ll

total 4

-rw-r–r–. 1 root root 292 Mar  9 13:28 ett.txt

 

2、基本用法:移动目录

[[email protected]
~]# mv /data /root

[[email protected]
~]# ll

total 56

-rw——-. 1 root root  1079 Mar  3 23:14 anaconda-ks.cfg

drwxr-xr-x. 2 root root  4096 Mar  9 13:56 data

-rw-r–r–. 1 root root 22179 Mar  3 23:14 install.log

 

3、更改文件名

[[email protected]
data]# ll

total 4

-rw-r–r–. 1 root root 292 Mar  9 13:28 ett.txt

[[email protected]
data]# mv ett.txt oldboy.txt

[[email protected]
data]# ll

total 4

-rw-r–r–. 1 root root 292 Mar  9 13:28 oldboy.txt

 

4、多个文件移动到同一个目录

[[email protected] test3]# mv -t /opt/soft/test/test4/ log1.txt log2.txt 
log3.txt 

[[email protected] test3]# cd ..

[[email protected] test]# cd test4/

[[email protected] test4]# ll

总计 12

-rw-r–r– 1 root root  8 10-28 06:15 log1.txt

-rw-r–r– 1 root root 12 10-28 06:15 log2.txt

-rw-r–r– 1 root root 13 10-28 06:16 log3.txt

 

 

NAME
ls – list directory contents

locate 文件名 搜索速度快
在后台数据库中按文件名搜索
updatedb 更新数据库

10. 【命令】:rm

【功能说明】:

 remove files or directories #移除文件或目录

【语法格式】:

rm [OPTION]… FILE…

【选项参数】:

参数

说明

简解

-f, –force

ignore nonexistent files,never prompt

忽略不存在的文件,从不给出提示

-i

prompt before every removal

进行交互式删除

-I

prompt once before removing more than three files, or when removing  recursively. Less intrusive than -i, while still giving protection against most mistakes

 

-r, -R, –recursive

remove directories and their contents recursively

指示rm将参数中列出的全部目录和子目录均递归地删除

-v, –verbose

explain what is being done

显示命令执行过程

【示例】:

1、基本用法:删除文件

[[email protected]
~]# rm log1.txt

rm: remove regular file `log1.txt’? y

[[email protected]
~]# ll

total 52

-rw——-. 1 root root  1079 Mar  3 23:14 anaconda-ks.cfg

drwxr-xr-x. 2 root root  4096 Mar  9 13:56 data

-rw-r–r–. 1 root root 22179 Mar  3 23:14 install.log

-rw-r–r–. 1 root root  5890 Mar  3 23:13 install.log.syslog

-rw-r–r–. 1 root root    44 Mar  7 17:53 log2.txt

-rw-r–r–. 1 root root    18 Mar  9 13:19 oldboy.txt

 

2、基本用法:删除目录

[[email protected]
data]# rm -r a

rm: descend into directory `a’? y

rm: descend into directory `a/b’? y

rm: remove regular empty file `a/b/oldboy.txt’? y

rm: remove directory `a/b/c’? y

rm: remove directory `a/b’? y

rm: remove regular empty file `a/oldboy.txt’? y

rm: remove directory `a’? y

 

SYNOPSIS
ls [OPTION]… [FILE]…

whereis 命令名
-b 只查找可执行文件
-m 只查找帮助文件

11. 【命令】:find

【功能说明】:

 search for files in a directory hierarchy #查找文档

【语法格式】:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path…]
[expression]

【选项参数】:

参数或选项

说明

简解

-atime n

File was last accessed n*24 hours ago.  When find figures out  how  many  24-hour periods  ago  the  file  was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.

根据最后一次的访问时间查找文件

-ctime n

File’s status was last changed n*24 hours ago.  See the comments  for  -atime  to understand how rounding affects the interpretation of file status change times.

根据最后一次的改变时间查找文件

-mtime n

File’s data was last modified n*24 hours ago.  See the  comments  for  -atime  to understand how rounding affects the interpretation of file modification times.

根据最后一次的修改时间查找文件

-type c

File is of type c:

b      block (buffered) special

c      character (unbuffered) special

d      directory

p      named pipe (FIFO)

f      regular file

l      symbolic  link;  this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken.  If you want  to  search for symbolic links when -L is in effect, use -xtype.

s      socket

D      door (Solaris)

查找某一类型的文件

b   块设备文件

d   目录

c   字符设备文件

p   管道文件

l    符号链接文件

f   普通文件

s   socket文件

-name pattern

Base  of  file name (the path with the leading directories removed) matches shell pattern pattern.  The metacharacters (‘*’, ‘?’, and ‘[]’)  match  a  ‘.’  at  the start  of  the  base name (this is a change in findutils-4.2.2; see section STAN-DARDS CONFORMANCE below).  To ignore a directory and  the  files  under  it,  use -prune; see an example in the description of -path.  Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns.  The filename matching is performed with the use of the fnmatch(3) library function.   Don’t forget to enclose the pattern  in quotes in order to protect it from expansion by the shell.

按文件名查找文件

-perm mode

File’s  permission  bits  are  exactly  mode (octal or symbolic).  Since an exact match is required, if you want to use this form for symbolic modes, you may  have  to specify a rather complex mode string.  For example ‘-perm g=w’ will only match files which have mode 0020 (that is, ones for which group write permission is the only permission set).  It is more likely that you will want to use the ‘/’ or ‘-’ forms, for example ‘-perm -g=w’, which matches any file with group write  permis- sion.  See the EXAMPLES section for some illustrative examples.

按照文件权限来查找文件

-size n[cwbkMG]

File  uses  n  units  of space.  The following suffixes can be used:

‘b’    for 512-byte blocks (this is the default if  no  suffix is used)

‘c’    for bytes

‘w’    for two-byte words

‘k’    for Kilobytes (units of 1024 bytes)

‘M’    for Megabytes (units of 1048576 bytes)

‘G’    for Gigabytes (units of 1073741824 bytes)

 

按大小查找

【示例】:

1、基本用法:按文件名查找文件

[[email protected]
~]# find /root/data -name “*.txt”

/root/data/oldboy.txt

 

2、基本用法:按文件类型查找文件

[[email protected]
~]# find /root/data -type f

/root/data/oldboy.txt

 

3、查找文件名称不为oldboy.txt的所有文件

[[email protected]
~]# ll

total 52

-rw——-. 1 root root  1079 Mar  3 23:14 anaconda-ks.cfg

drwxr-xr-x. 2 root root  4096 Mar  9 15:09 data

-rw-r–r–. 1 root root 22179 Mar  3 23:14 install.log

-rw-r–r–. 1 root root  5890 Mar  3 23:13 install.log.syslog

-rw-r–r–. 1 root root    44 Mar  7 17:53 log2.txt

-rw-r–r–. 1 root root    18 Mar  9 13:19 oldboy.txt

[[email protected]
~]# find /root ! -name “oldboy.txt”

/root

/root/.bash_history

/root/.bashrc

/root/.bash_logout

/root/.bash_profile

/root/data

/root/install.log

/root/log2.txt

/root/.lesshst

/root/.cshrc

/root/anaconda-ks.cfg

/root/install.log.syslog

/root/.tcshrc

 

4、利用find查找删除扩展名为txt的所有文件

[[email protected]
data]# ll

total 4

-rw-r–r–. 1 root root   0 Mar  9 16:13 1.txt

-rw-r–r–. 1 root root   0 Mar  9 16:13 2.txt

-rw-r–r–. 1 root root   0 Mar  9 16:13 3.txt

-rw-r–r–. 1 root root 292 Mar  9 13:28 oldboy.txt

[[email protected]
data]# find /root/data -type f -name “*.txt”|xargs rm -f

[[email protected]
data]# ll

total 0

 

【说明】:

1、按修改时间查找文件,+7表示距今天7天以前,7表示距今天整7天,-7表示最近7天

2、-o表示或者的意思,-a表示并且的意思,!表示取反

 

 

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

which 文件名
搜索命令所在路径及别名

12. 【命令】:grep

【功能说明】:

 print lines matching a pattern #用于过滤、搜索特定字符

【语法格式】:

grep [OPTIONS] PATTERN [FILE…]

grep [OPTIONS] [-e PATTERN | -f FILE] [FILE…]

【选项参数】:

参数

说明

简解

-a, –text

Process   a  binary  file  as  if  it  were  text;  this  is  equivalent  to  the –binary-files=text option.

不要忽略二进制的数据

-A NUM , –after-context=NUM

Print  NUM  lines  of  trailing  context  after  matching  lines.   Places a line containing  a  group  separator  (described  under   –group-separator)   between contiguous groups of matches.  With the -o or –only-matching option, this has no effect and a warning is given.

除了显示符合范本样式的那一列之外,并显示该行之后的内容

-b, –byte-offset

Print  the  0-based byte offset within the input file before each line of output. If -o (–only-matching) is specified, print  the  offset  of  the  matching  part  itself.

在显示符合样式的那一行之前,标示出该行第一个字符的编号

-B NUM, –before-context=NUM

Print NUM lines  of  leading  context  before  matching  lines.   Places  a  line containing   a   group  separator  (described  under  –group-separator)  between contiguous groups of matches.  With the -o or –only-matching option, this has no effect and a warning is given.

除了显示符合样式的那一行之外,并显示该行之前的内容

-c, –count

Suppress  normal  output;  instead print a count of matching lines for each input  file. With the -v, –invert-match option (see below), count non-matching  lines. (-c is specified by POSIX.)

计算符合样式的列数

-C NUM, -NUM, –context=NUM

Print  NUM  lines  of output context.  Places a line containing a group separator (described under –group-separator) between contiguous groups of  matches.   With the -o or –only-matching option, this has no effect and a warning is given.

除了显示符合样式的那一行之外,并显示该行之前后各n行内容

-d ACTION, –directories=ACTION

If an input file is a directory, use ACTION to process it.  By default, ACTION is read,  i.e.,  read directories just as if they were ordinary files.  If ACTION is skip, silently skip directories.  If ACTION is recurse, read all files under each directory,  recursively, following symbolic links only if they are on the command line.  This is equivalent to the -r option.

当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。

-e PATTERN, –regexp=PATTERN

Use  PATTERN  as  the  pattern.   This  can  be  used  to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-).  (-e is  specified by POSIX.)

使用正则表达式

-E, –extended-regexp

Interpret PATTERN as an extended regular expression (ERE,  see  below).   (-E is specified by POSIX.)

使用拓展的正则表达式

-f FILE, –file=FILE

Obtain  patterns from FILE, one per line.  The empty file contains zero patterns, and therefore matches nothing.  (-f is specified by POSIX.)

指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式。

-h, –no-filename

Suppress the prefixing of file names on output.  This is the default  when  there is only one file (or only standard input) to search.

在显示符合样式的那一行之前,不标示该行所属的文件名称。

-H, –with-filename

Print  the file name for each match.  This is the default when there is more than one file to search.

在显示符合样式的那一行之前,表示该行所属的文件名称。

-i, –ignore-case

Ignore case distinctions in both  the  PATTERN  and  the  input  files.   (-i is specified by POSIX.)

忽略字符大小写的差别。

-l, –files-with-matches

Suppress normal output; instead print the name of  each  input  file  from  which output  would  normally  have  been printed.  The scanning will stop on the first match.  (-l is specified by POSIX.)

列出文件内容符合指定的样式的文件名称。

-L, –files-without-match

Suppress normal output; instead print the name of each input file from  which  no output  would  normally  have  been printed.  The scanning will stop on the first match.

列出文件内容不符合指定的样式的文件名称。

-n, –line-number

Prefix  each  line  of output with the 1-based line number within its input file. (-n is specified by POSIX.)

在显示符合样式的那一行之前,标示出该行的列数编号。

-q, –quiet, –silent

Quiet; do not write anything to standard  output.   Exit  immediately  with  zero status  if any match is found, even if an error was detected.  Also see the -s or –no-messages option.  (-q is specified by POSIX.)

不显示任何信息。

-r, –recursive

Read  all  files under each directory, recursively, following symbolic links only if they are on the command line.  This is equivalent to the -d recurse option.

如果文件参数是目录,该选项将递归搜索该目录下的所有子目录和文件。同-R

-s, –no-messages

Suppress error messages about nonexistent or unreadable files.  Portability note: unlike  GNU  grep,  7th  Edition  Unix  grep did not conform to POSIX, because it lacked -q and its -s option behaved like GNU grep’s -q  option.   USG-style  grep also  lacked  -q but its -s option behaved like GNU grep.  Portable shell scripts should avoid both -q and -s and should redirect  standard  and  error  output  to /dev/null instead.  (-s is specified by POSIX.)

不显示错误信息。

-v, –invert-match

Invert  the sense of matching, to select non-matching lines.  (-v is specified by POSIX.)

显示不包含匹配文本的所有行。

-V, –version

Print  the  version  number  of grep to the standard output stream.  This version number should be included in all bug reports (see below).

显示版本信息。

-w, –word-regexp

Select only those lines containing matches that form whole words.   The  test  is that  the  matching  substring  must  either  be at the beginning of the line, or preceded by a non-word constituent character.  Similarly, it must  be  either  at  the  end  of  the  line  or  followed by a non-word constituent character.  Word- constituent characters are letters, digits, and the underscore.

只显示完整单词的匹配

-x, –line-regexp

Select only those matches that exactly match the whole line.  (-x is specified by POSIX.)

只显示完整行的匹配

【示例】:

1、基本用法:显示文件中包含字符串的行

[[email protected]
~]# cat oldboy.txt

test

liyao

oldboy

[[email protected]
~]# grep “oldboy” oldboy.txt

oldboy

[[email protected]
~]#

 

2、基本用法:显示文件中不包含字符串的行

[[email protected]
~]# grep -v “oldboy” oldboy.txt

test

liyao

 

3、除了显示匹配的一行外,并显示该行之后的n行

[[email protected]
~]# grep 20 -A 10 test.txt

20

21

22

23

24

25

26

27

28

29

30

 

 

【说明】:

grep的正则表达式:

^  #锚定行的开始 如:’^grep’匹配所有以grep开头的行。    

$  #锚定行的结束 如:’grep$’匹配所有以grep结尾的行。    

.  #匹配一个非换行符的字符 如:’gr.p’匹配gr后接一个任意字符,然后是p。    

*  #匹配零个或多个先前字符 如:’*grep’匹配所有一个或多个空格后紧跟grep的行。    

.*   #一起用代表任意字符。   

[]   #匹配一个指定范围内的字符,如'[Gg]rep’匹配Grep和grep。    

[^]  #匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep’匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。    

\(..\)  #标记匹配字符,如’\(love\)’,love被标记为1。    

\<      #锚定单词的开始,如:’\<grep’匹配包含以grep开头的单词的行。    

\>      #锚定单词的结束,如’grep\>’匹配包含以grep结尾的单词的行。    

x\{m\}  #重复字符x,m次,如:’0\{5\}’匹配包含5个o的行。    

x\{m,\}  #重复字符x,至少m次,如:’o\{5,\}’匹配至少有5个o的行。    

x\{m,n\}  #重复字符x,至少m次,不多于n次,如:’o\{5,10\}’匹配5–10个o的行。   

\w    #匹配文字和数字字符,也就是[A-Za-z0-9],如:’G\w*p’匹配以G后跟零个或多个文字或数字字符,然后是p。   

\W    #\w的反置形式,匹配一个或多个非单词字符,如点号句号等。   

\b    #单词锁定符,如: ‘\bgrep\b’只匹配grep。  

 

POSIX字符:

    为了在不同国家的字符编码中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字符类,如[:alnum:]是[A-Za-z0-9]的另一个写法。要把它们放到[]号内才能成为正则表达式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字符类。

[:alnum:]    #文字数字字符   

[:alpha:]    #文字字符   

[:digit:]    #数字字符   

[:graph:]    #非空字符(非空格、控制字符)   

[:lower:]    #小写字符   

[:cntrl:]    #控制字符   

[:print:]    #非空字符(包括空格)   

[:punct:]    #标点符号   

[:space:]    #所有空白字符(新行,空格,制表符)   

[:upper:]    #大写字符   

[:xdigit:]   #十六进制数字(0-9,a-f,A-F)

 

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

find 搜索范围 搜索条件 find 是完全匹配

13. 【命令】:head

【功能说明】:

output the first part of files  #打印文件的前几行

【语法格式】:

head [OPTION]… [FILE]…

【选项参数】:

参数

说明

简解

-c, –bytes=[-]K

print the first K bytes of each file; with  the  leading  ‘-’,print all but the last K bytes of each file

按前K字节打印文件

-n, –lines=[-]K

print  the  first  K  lines  instead of the first 10; with the leading ‘-’, print all but the last K lines of each file

按前K行打印文件

-q, –quiet, –silent

never print headers giving file names

隐藏文件名

-v, –verbose

always print headers giving file names

显示文件名

 

 

 

【示例】:

1、基本用法:打印文件前几行

[[email protected]
~]# head -n 13 test.txt

1

2

3

4

5

6

7

8

9

10

11

12

13

 

2、打印文件前K个字节

[[email protected]
~]# head -c 20 test.txt

1

2

3

4

5

6

7

8

9

10[[email protected]
~]#

 

3、显示文件名

[[email protected]
~]# head -v test.txt

==> test.txt <==

1

2

3

4

5

6

7

8

9

10

 

-a, –all
do not ignore entries starting with .

  • 匹配任意内容 ?匹配任意一个字符 [ ] 匹配任意一个中括号内的字符
    find / -name install.log
    find / -name “install*”
    find / -name “install[ad]”
    find /root -iname install.log -iname不区分大小写
    find /root -user root 按照所有者搜索
    find /root -nouser 查找没有所有者的文件

14. 【命令】:cp

【功能说明】:

copy files and directories  #复制文件或目录

【语法格式】:

cp [OPTION]… [-T] SOURCE DEST

cp [OPTION]… SOURCE… DIRECTORY

cp [OPTION]… -t DIRECTORY SOURCE…

【选项参数】:

参数

说明

简解

-a, –archive

same as -dR –preserve=all

保持源文件的原有结构和属性,与选项“-dpR”相同

–backup[=CONTROL]

make a backup of each existing destination file

做一个已存在文件的备份

-b

like –backup but does not accept an argument

覆盖已存在的目标文件前将目标文件备份

–copy-contents

copy contents of special files when recursive

 

-d

same as –no-dereference –preserve=links

如果复制的源文件是符号链接,仅复制符号链接本身,而且保留符号链接所指向的目标文件或目录

-f, –force

if an existing destination file cannot be  opened,  remove  it and try again (redundant if the -n option is used)

强制覆盖已经存在的目标文件,不提示用户确认

-i, –interactive

prompt before overwrite (overrides a previous -n option)

在覆盖已存在的目标文件前提示用户进行确认

-H

follow command-line symbolic links in SOURCE

 

-l, –link

link files instead of copying

为源文件创建硬链接

-L, –dereference

always follow symbolic links in SOURCE

 

-n, –no-clobber

do  not  overwrite  an  existing file (overrides a previous -i option)

 

-P, –no-dereference

never follow symbolic links in SOURCE

 

-p

same as –preserve=mode,ownership,timestamps

复制文件时保持源文件的所有者、权限信息以及时间属性

–preserve[=ATTR_LIST]

preserve  the  specified  attributes   (default:   mode,owner-ship,timestamps),  if possible additional attributes: context, links, xattr, all

 

-c

same as –preserve=context

 

–no-preserve=ATTR_LIST

don’t preserve the specified attributes

 

–parents

use full source file name under DIRECTORY

 

-R, -r, –recursive

copy directories recursively

对目录进行复制操作,采用递归的操作方式

–reflink[=WHEN]

control clone/CoW copies. See below.

 

–remove-destination

remove each existing destination  file  before  attempting  to  open it (contrast with –force)

 

–sparse=WHEN

control creation of sparse files. See below.

 

–strip-trailing-slashes

remove any trailing slashes from each SOURCE argument

 

-s, –symbolic-link

make symbolic links instead of copying

为源文件创建符号链接

-S, –suffix=SUFFIX

override the usual backup suffix

在备份文件时,用指定的后缀“SUFFIX”代替文件名的默认后缀

-t, –target-directory=DIRECTORY

copy all SOURCE arguments into DIRECTORY

 

-T, –no-target-directory

treat DEST as a normal file

 

-u, –update

copy  only  when the SOURCE file is newer than the destination file or when the destination file is missing

当目标文件不存在或者源文件比目标文件新时才进行复制操作

-v, –verbose

explain what is being done

详细显示指令执行的操作

-x, –one-file-system

stay on this file system

复制的文件或目录存放的文件系统,必须与cp指令执行时所处的文件系统相同,否则不复制,亦不处理位于其他分区的文件

-Z, –context=CONTEXT

set security context of copy to CONTEXT

 

【示例】:

1、基本用法:复制文件到指定位置

[[email protected]
~]# cp test.txt data

[[email protected]
~]# cat data/test.txt

1

2

3

4

5

6

7

 

2、基本用法:复制目录到指定位置

[[email protected]
~]# cp -r /root/data /tmp

[[email protected]
~]# cd /tmp/data

[[email protected]
data]#

 

3、复制时保持文件属性

[[email protected]
~]# ll

total 56

-rw——-. 1 root root  1079 Mar  3 23:14 anaconda-ks.cfg

drwxr-xr-x. 7 root root  4096 Mar 13 16:25 data

-rw-r–r–. 1 root root 22179 Mar  3 23:14 install.log

-rw-r–r–. 1 root root  5890 Mar  3 23:13 install.log.syslog

-rw-r–r–. 1 root root    44 Mar  7 17:53 log2.txt

-rw-r–r–. 1 root root    18 Mar  9 13:19 oldboy.txt

-rw-r–r–. 1 root root   292 Mar 13 15:13 test.txt

[[email protected]
~]# cp -p test.txt /tmp

[[email protected]
~]# ll /tmp

total 108

drwxr-xr-x  7 root root   4096 Mar 13 16:28 data

-rw-r–r–  1 root root    292 Mar 13 15:13 test.txt

-rw——-. 1 root root      0 Mar  3 23:09 yum.log

-rw——-. 1 root root 100203 Mar  4 00:39
yum_save_tx-2016-03-04-00-39i_5ZVm.yumtx

 

 

-A, –almost-all
do not list implied . and ..

find /var/log/ -mtime +10 查找10天前修改的文件
-10 10天内修改文件
10 第10天当天修改的文件
+10 十天前修改的文件
atime 文件访问时间
ctime 改变文件属性
mtime 修改文件内容

15. 【命令】:tail

【功能说明】:

output the last part of files  #打印文件末尾几行

【语法格式】:

tail [OPTION]… [FILE]…

【选项参数】:

参数

说明

简解

-c, –bytes=K

output the last K bytes; alternatively, use -c  +K  to  output bytes starting with the Kth of each file

输出文件尾部的N个字节内容

-f, –follow[={name|descriptor}]

output  appended  data  as  the  file grows; -f, –follow, and –follow=descriptor are equivalent

显示文件最新追加的内容

-F

same as –follow=name –retry

与选项“–follow=name”和“–retry”连用时的功能相同

-n, –lines=K

output the last K lines, instead of the last 10; or use -n  +K  to output lines starting with the Kth

输出文件尾部K行内容

–max-unchanged-stats=N

with  –follow=name,  reopen a FILE which has not changed size after N (default 5) iterations to see if it has been  unlinked or  renamed  (this  is  the  usual case of rotated log files). With inotify, this option is rarely useful.

参看texinfo文档(默认为5)

–pid=PID

with -f, terminate after process ID, PID dies

与-f合用,表示在进程ID,PID死掉之后结束

-q, –quiet, –silent

never output headers giving file names

不显示文件名

–retry

keep trying to open a file even when it is or becomes inacces-sible; useful when following by name, i.e., with –follow=name

即使文件不可访问或者文件稍后变得不可访问,都始终尝试打开文件

-s, –sleep-interval=N

with -f, sleep  for  approximately  N  seconds  (default  1.0) between iterations.

 

With  inotify and –pid=P, check process P at least once every N seconds.

与“-f”连用,指定监视文件变化时间所间隔的秒数

-v, –verbose

always output headers giving file names

总在开头显示文件名

【示例】:

1、基本用法:显示文件末尾的n行

[[email protected]
~]# tail -8 test.txt

93

94

95

96

97

98

99

100

 

2、在开头显示文件名

[[email protected]
~]# tail -v -n 8 test.txt

==> test.txt <==

93

94

95

96

97

98

99

100

 

–author
with -l, print the author of each file

find . -size 25k 查找文件大小是25KB的文件
-25k 小于25kb的文件
25k 等于25kb的文件
+25k 大于25kb的文件

16. 【命令】:alias

【功能说明】:

Define or display aliases.  #显示或设置别名

【语法格式】:

alias [-p] [name[=value] … ]

【选项参数】:

参数

说明

简解

-p

Print all defined aliases in a reusable format

打印已经设置的命令别名

【示例】:

1、基本用法:打印已经设置的别名(-p可以省略)

[[email protected]
~]# alias -p

alias cp=’cp -i’

alias l.=’ls -d .* –color=auto’

alias ll=’ls -l –color=auto’

alias ls=’ls –color=auto’

alias mv=’mv -i’

alias rm=’rm -i’

alias which=’alias | /usr/bin/which –tty-only –read-alias –show-dot
–show-tilde’

 

2、基本用法:设置新别名

[[email protected]
~]# alias rm=’echo “Do not use rm!”‘

[[email protected]
~]# rm -f test.txt

Do not use rm! -f test.txt

[[email protected]
~]# alias

alias cp=’cp -i’

alias l.=’ls -d .* –color=auto’

alias ll=’ls -l –color=auto’

alias ls=’ls –color=auto’

alias mv=’mv -i’

alias rm=’echo “Do not use rm!”‘

alias which=’alias | /usr/bin/which –tty-only –read-alias –show-dot
–show-tilde’

 

 

-b, –escape
print octal escapes for nongraphic characters

find . -inum 262422 查找i节点是262422的文件

17. 【命令】:unalias

【功能说明】:

Remove each NAME from the list of defined aliases.  #取消别名

【语法格式】:

unalias [-a] name [name …]

【选项参数】:

参数

说明

简解

-a

remove all alias definitions.

取消所有别名

【示例】:

1、基本用法:取消别名

[[email protected]
~]# alias

alias cp=’cp -i’

alias l.=’ls -d .* –color=auto’

alias ll=’ls -l –color=auto’

alias ls=’ls –color=auto’

alias mv=’mv -i’

alias rm=’echo “Do not use rm!”‘

alias which=’alias | /usr/bin/which –tty-only –read-alias –show-dot
–show-tilde’

[[email protected]
~]# unalias rm

[[email protected]
~]# alias

alias cp=’cp -i’

alias l.=’ls -d .* –color=auto’

alias ll=’ls -l –color=auto’

alias ls=’ls –color=auto’

alias mv=’mv -i’

alias which=’alias | /usr/bin/which –tty-only –read-alias –show-dot
–show-tilde’

 

2、取消所有别名

[[email protected]
~]# alias

alias cp=’cp -i’

alias l.=’ls -d .* –color=auto’

alias ll=’ls -l –color=auto’

alias ls=’ls –color=auto’

alias mv=’mv -i’

alias which=’alias | /usr/bin/which –tty-only –read-alias –show-dot
–show-tilde’

[[email protected]
~]# unalias -a

[[email protected]
~]# alias

[[email protected]
~]#

 

–block-size=SIZE
use SIZE-byte blocks. See SIZE format below

find /etc -size +20k -a -size -50k
查找/etc/目录下,大于20kb并且小于50kb的文件
-a and 逻辑与,两个条件都满足
-o or 逻辑或,两个条件满足一个即可

18. 【命令】:seq

【功能说明】:

print a sequence of numbers  #生成数字序列

【语法格式】:

seq [OPTION]… LAST

seq [OPTION]… FIRST LAST

seq [OPTION]… FIRST INCREMENT LAST

【选项参数】:

参数

说明

简解

-f, –format=FORMAT

use printf style floating-point FORMAT

格式化输出

-s, –separator=STRING

use STRING to separate numbers (default: \n)

指定分隔符,默认是换行符

-w, –equal-width

equalize width by padding with leading zeroes

输出同宽数列,不足的位数用0补齐

【示例】:

1、基本用法:生成数字序列

[[email protected]
~]# seq 10

1

2

3

4

5

6

7

8

9

10

 

2、格式化输出

[[email protected]
~]# seq -f 100%g 10

1001

1002

1003

1004

1005

1006

1007

1008

1009

10010

3、指定分隔符

[[email protected]
~]# seq -s “…” 10

1…2…3…4…5…6…7…8…9…10

[[email protected]
~]# 

 

4、输出同宽数列

[[email protected]
~]# seq -w 10

01

02

03

04

05

06

07

08

09

10

 

-B, –ignore-backups
:

find /etc -size +20k -a -size -50k -exec ls -lh {} ;
查找/etc/目录下,大于20kb并且小于50kb的文件,并显示详细信息
-exec/-ok 命令 {} \;对搜索结果执行操作

19. 【命令】:sed

【功能说明】:

stream editor for filtering and transforming text
 #用于过滤和转化文本的流编辑器

【语法格式】:

sed [OPTION]… {script-only-if-no-other-script} [input-file]…

【选项参数】:

参数

说明

简解

-n, –quiet, –silent

suppress automatic printing of pattern space

取消默认输出,只显示操作过的那一行

-e script, –expression=script

add the script to the commands to be executed

直接在命令列模式上进行sed的动作编辑

-f script-file, –file=script-file

add the contents of script-file to the commands to be executed

运行脚本内的动作

–follow-symlinks

follow symlinks when processing  in  place;  hard  links  will still be broken

 

-i[SUFFIX], –in-place[=SUFFIX]

edit files in place (makes backup if extension supplied).  The default operation mode is to break symbolic  and  hard  links.This can be changed with –follow-symlinks and –copy

直接修改读取的文件内容

-c, –copy

use  copy  instead  of rename when shuffling files in -i mode. While this will avoid breaking links (symbolic or  hard),  the resulting editing operation is not atomic.  This is rarely the desired mode; –follow-symlinks is usually enough, and  it  is  both faster and more secure.

 

-l N, –line-length=N

specify the desired line-wrap length for the ‘l’ command

 

–posix

disable all GNU extensions.

 

-r, –regexp-extended

use extended regular expressions in the script

在脚本中应用拓展的正则表达式

-s, –separate

consider  files as separate rather than as a single continuous long stream

 

-u, –unbuffered

load minimal amounts of data from the input  files  and  flush  the output buffers more often

 

 

【示例】:

1、基本用法:仅列出 /etc/passwd 文件内的第 5-7 行

[[email protected]
~]# nl /etc/passwd | sed -n ‘5,7p’

     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

     6  sync:x:5:0:sync:/sbin:/bin/sync

     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

 

2、利用sed替换取IP地址

[[email protected]
~]# ifconfig eth0      

eth0      Link encap:Ethernet  HWaddr 00:0C:29:9C:9A:32  

          inet addr:10.0.0.8  Bcast:10.0.0.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe9c:9a32/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:641 errors:0 dropped:0 overruns:0 frame:0

          TX packets:321 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:56546 (55.2 KiB)  TX bytes:40576 (39.6 KiB)

 

[[email protected]
~]# /sbin/ifconfig eth0 | grep ‘inet addr’ | sed ‘s#^.*addr:##g’ |
sed ‘s#Bcast.*$##g’

10.0.0.8

 

3、直接修改文件内容

[[email protected]
~]# cat test.txt

1

2

3

4

5

6

7

8

9

10

[[email protected]
~]# sed -i ‘s#5#500#g’ test.txt

[[email protected]
~]# cat test.txt

1

2

3

4

500

6

7

8

9

10

 

【特别说明】:

动作说明: [n1[,n2]]function

n1, n2
:不见得会存在,一般代表『选择进行动作的行数』,举例来说,如果我的动作是需要在
10 到 20 行之间进行的,则『 10,20[动作行为] 』

function:

a :新增, a
的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~

c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!

d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;

i :插入, i
的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);

p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~

s :取代,可以直接进行取代的工作哩!通常这个 s
的动作可以搭配正则表达式!例如 1,20s#old#new#g 就是啦!

 

学点英文,这些东西看起来都是没问题的。

grep 选项 字符串 文件名 在文件当中匹配符合条件的字符串
选项:
-i 忽略大小写
-v 排除指定字符串
grep “size” anaconda-ks.cfg 查找在anaconda-ks.cfg文件中包含“size”的行

20. 【命令】:man

【功能说明】:

format and display the on-line manual pages  #查看在线帮助手册

【语法格式】:

man [-acdDfFhkKtwW] [–path] [-m system] [-p string] [-C
config_file]

       [-M pathlist]  [-P  pager]  [-B  browser]  [-H
 htmlpager]  [-S  sec-

       tion_list] [section] name …

【选项参数】:

参数

说明

简解

 

 

 

 

 

 

 

 

 

【示例】:

1、基本用法:查看在线帮助

[[email protected]
~]# man ls

LS(1)                            User Commands
                          LS(1)

 

NAME

       ls – list directory contents

 

SYNOPSIS

       ls [OPTION]… [FILE]…

以下省略

 

 

在命令行输入/+想看的,比如/-g就可以跳转,同时n键是下一个符合的查找。

man 命令 获取指定命令的帮助
man ls 查看ls的帮助

21. 【命令】:useradd

【功能说明】:

create a new user or update default new user information
  #创建新用户或更新默认新用户的信息

【语法格式】:

useradd [options] LOGIN

useradd -D

useradd -D [options]

【选项参数】:

参数

说明

简解

-c, –comment COMMENT

Any text string. It is generally a short description of the login, and is currently used as the field for the user’s full name.

加上备注文字

-d, –home HOME_DIR

The new user will be created using HOME_DIR as the value for the user’s login directory. The default is to append the LOGIN name to BASE_DIR and use that as the login directory name. The parent directory of HOME_DIR must exist otherwise the home directory cannot be created.

指定用户登入时的起始目录

-D,  defaults

See below, the subsection "Changing the default values".

变更预设值

-e, –expiredate EXPIRE_DATE

The date on which the user account will be disabled.

The date is specified in the format YYYY-MM-DD.

 

If not specified, useradd will use the default  expiry date specified by the EXPIRE variable in /etc/default/useradd, or an empty string (no expiry) by default.

指定账号的有效期限

-f, –inactive INACTIVE

The number of days after a password expires until the account is permanently disabled. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature.

 

If not specified, useradd will use the default  inactivity period specified by the INACTIVE variable  in /etc/default/useradd, or -1 by default.

指定在密码过期后多少天即关闭该账号

-g, –gid GROUP

The group name or number of the user’s initial login group. The group name must exist. A group number must refer to an already existing group.

指定用户所属的群组

-G, –groups GROUP1[,GROUP2,…[,GROUPN]]]

A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. The default is for  the user to belong only to the initial group.

指定用户所属的附加群组

-m, –create-home

Create the user’s home directory if it does not exist. The files and directories contained in the skeleton directory (which can be defined with the -k option) will be copied to the home directory.

 

自动建立用户的登入目录

-M

Do not create the user’s home directory, even if the system wide setting from /etc/login.defs (CREATE_HOME) is set to yes.

不要自动建立用户的登入目录

-N, –no-user-group

Do not create a group with the same name as the user, but add the user to the group specified by the -g option or by the GROUP variable in /etc/default/useradd.

取消建立以用户名称为名的群组

-r, –system

Create a system account.

建立系统账号

-s, –shell SHELL

The name of the user’s login shell. The default is to leave this field blank, which causes the system to select the default login shell specified by the SHELL variable in /etc/default/useradd, or an empty string by default.

指定用户登入后使用的shell

-u, –uid UID

The numerical value of the user’s ID. This value must be unique, unless the -o option is used. The value must be non-negative. The default is to use the smallest ID value greater than or equal to UID_MIN and greater than every other user.

指定用户ID

【示例】:

1、基本用法:创建新用户

[[email protected]
~]# useradd test

[[email protected]
~]# cat /etc/passwd|tail -1

test:x:501:501::/home/test:/bin/bash

 

q退出。

.zip压缩文件/压缩目录
zip 压缩文件名 源文件
zip -r 压缩文件名 源目录
解压缩zip文件
unzip 压缩文件

22. 【命令】:passwd

【功能说明】:

update user’s authentication tokens  #为用户设置或修改密码

【语法格式】:

passwd  [-k]  [-l]  [-u [-f]] [-d] [-e] [-n mindays] [-x

       maxdays] [-w warndays] [-i inactivedays] [-S]  [–stdin]

       [username]

【选项参数】:

参数

说明

简解

-d

This  is  a quick way to delete a password for an account. It will set the named account  password-less. Available to root only.

删除密码,仅有系统管理者才能使用

-k

The option -k,  is  used  to  indicate  that  the update  should only be for expired authentication tokens (passwords); the user wishes to keep their non-expired tokens as before.

设置只有在密码过期失效后,方能更新

-l

This option is used to lock the specified account and it is available to root only. The locking  is performed  by  rendering  the  encrypted password into  an  invalid  string   (by   prefixing   the  encrypted string with an !).

锁住密码

-S

This will output a short  information  about  the  status  of  the  password  for  a  given account.Available to root user only.

列出密码的相关信息,仅有系统管理者才能使用

-u

This  is  the  reverse of the -l option – it will unlock the account password  by  removing  the  !prefix. This option is available to root only. By default passwd will refuse to create a  password-less  account (it will not unlock an account that has only "!" as a password). The force option  -f  will override this protection.

解开已上锁的账号

【示例】:

1、基本用法:设置用户密码

[[email protected]
~]# passwd oldboy

Changing password for user oldboy.

New password:

BAD PASSWORD: it is too simplistic/systematic

BAD PASSWORD: is too simple

Retype new password:

passwd: all authentication tokens updated successfully.

 

 

gzip 源文件
压缩为.gz格式的压缩文件,源文件会消失

23. 【命令】:uname

【功能说明】:

print system information  #打印系统信息

【语法格式】:

uname [OPTION]…

【选项参数】:

参数

说明

简解

-a, –all

print all information, in  the  following  order, except omit -p and -i if unknown:

显示全部的信息

-s, –kernel-name

print the kernel name

显示操作系统类型

-n, –nodename

print the network node hostname

显示主机名

-r, –kernel-release

print the kernel release

显示核心版本号

-v, –kernel-version

print the kernel version

显示操作系统的版本

-m, –machine

print the machine hardware name

显示机器架构类型

-p, –processor

print the processor type or "unknown"

 

-i, –hardware-platform

print the hardware platform or "unknown"

显示硬件平台

-o, –operating-system

print the operating system

显示操作系统

【示例】:

1、基本用法:显示全部信息

[[email protected]
~]# uname -a

Linux oldboy 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015
x86_64 x86_64 x86_64 GNU/Linux

 

2、基本用法:显示主机名

[[email protected]
~]# uname -n

oldboy

 

3、显示核心版本号

[[email protected]
~]# uname -r

2.6.32-573.el6.x86_64

 

4、显示机器架构类型

[[email protected]
~]# uname -m

x86_64

 

 

man是官方的帮助手段。

gzip -c 源文件 > 压缩文件
压缩为.gz格式,源文件保留 例如:gzip -c cangls > cangls.gz

24. 【命令】:hostname

【功能说明】:

show or set the system’s host name  #显示和设置主机名

【语法格式】:

hostname   [-v]  [-a]  [–alias]  [-d]  [–domain]  [-f]

       [–fqdn] [-A]  [–all-fqdns]  [-i]  [–ip-address]
 [-I]

       [–all-ip-addresses] [–long] [-s] [–short] [-y]
[–yp]

       [–nis]

hostname [-v] [-F filename] [–file filename] [hostname]

【选项参数】:

参数

说明

简解

-v, –verbose

Be verbose and tell what’s going on.

详细信息模式

-a, –alias

Display the alias name of the host (if used)

显示主机别名

-d, –domain

Display the name of the DNS domain. Don’t use the  command  domainname  to  get  the DNS domain name because it will show the NIS domain name and  not  the DNS domain name. Use dnsdomainname instead.

显示DNS域名

-f, –fqdn, –long

Display the FQDN (Fully Qualified Domain Name). A FQDN  consists  of  a short host name and the DNS domain name. Unless you are using bind or NIS for host  lookups you can change the FQDN and the DNS domain name (which is part of the  FQDN)  in  the /etc/hosts  file. See the warnings in section THE FQDN above, and  avoid  using  this  option;  use hostname –all-fqdns instead

显示FQDN名称

-i, –ip-address

Display the IP address(es) of the host. Note that this works only if the host name can be resolved.

Avoid using this option; use  hostname  –all-ip- addresses instead

显示主机的ip地址

-s, –short

Display  the  short  host  name. This is the host name cut at the first dot.

显示短主机名称,在第一个点处截断

-y, –yp, –nis

Display  the  NIS  domain name. If a parameter is given (or –file name ) then root can also set  a new NIS domain

显示NIS域名

【示例】:

1、基本用法:显示主机名

[[email protected]
~]# hostname

oldboy

 

2、基本用法:修改主机名

[[email protected]
~]# hostname oldgirl

[[email protected]
~]# hostname

oldgirl

 

 

man man可以查看man的用法

gzip -r 目录
压缩目录下的所有的子文件,但是不能压缩目录

25. 【命令】:runlevel

【功能说明】:

output previous and current runlevel  #查看系统运行状态

【语法格式】:

runlevel [OPTION]…  [UTMP]

【选项参数】:

参数

说明

简解

–quiet

Does  not  output  the  current and previous run-level, nor does it output unknown in the case  of error (but it will exit with an error code).

 

This  may  be  used to test for the presence of a runlevel entry, or to check  for  errors  reading from the file.

不显示当前运行状态

【示例】:

1、基本用法:查看系统运行状态

[[email protected]
~]# runlevel

N 3

 

 

MANUAL SECTIONS
The standard sections of the manual include:

解压缩gz文件
gzip -d 压缩文件
gunzip 压缩文件

26. 【命令】:init

【功能说明】:

Upstart process management daemon  #设置系统运行状态

【语法格式】:

init [OPTION]…

【选项参数】:

参数

说明

简解

-v, –verbose

Outputs  verbose messages about job state changes and event emissions to the system console or log,useful for debugging boot.

显示详细信息

-q, –quiet

reduce output to errors only

只显示错误

【示例】:

1、基本用法:关机

[[email protected]
~]# init 0

 

2、基本用法:重启

[[email protected]
~]# init 6

 

 

 

1 User Commands

bzip2 源文件
压缩为.bz2格式,不保留源文件
bzip2 -k 源文件
压缩之后保留源文件
注意:bzip2命令不能压缩目录

27. 【命令】:shutdown

【功能说明】:

bring the system down  #关机

【语法格式】:

shutdown [OPTION]…  TIME [MESSAGE]

【选项参数】:

参数

说明

简解

-r

Requests that the system be rebooted after it has been brought down

重启系统

-h

Requests that the system be either halted or pow-ered off after it has been brought down, with the choice as to which left up to the system.

关机

-H

Requests that the system be halted after  it  has  been brought down

 

-P

Requests  that the system be powered off after it has been brought down.

 

-c

Cancels a running shutdown.  TIME is  not  specified with this option, the first argument is MES-SAGE.

当执行“shutdown -h 11:50”指令时,只要按+键就可以中断关机的指令

-k

Only send out the warning  messages  and  disable  logins, do not actually bring the system down

只发送警告信息,不真关机

【示例】:

1、基本用法:立即关机

[[email protected]
~]# shutdown -h now

 

2、指定12点关机

[[email protected]
~]# shutdown -h 12:00

 

Broadcast message from
[email protected]

        (/dev/pts/0) at 11:54 …

 

The system is going down for halt in 6 minutes!

 

 

 

2 System Calls

解压缩bzip文件,-k保留解压缩文件
bzip -d 压缩文件
bunzip2 压缩文件

28. 【命令】:reboot

【功能说明】:

reboot the system  #重启系统

【语法格式】:

reboot [OPTION]…

【选项参数】:

参数

说明

简解

-f, –force

Does  not invoke shutdown(8) and instead performs the actual action you would expect from the name.

强制重新开机

-p, –poweroff

Instructs  the  halt command to instead behave as poweroff

 

-w, –wtmp-only

Does not call shutdown(8) or the reboot(2) system call  and instead only writes the shutdown record to /var/log/wtmp

仅做测试,并不真正将系统重新开机,只会把重开机的数据写入/var/log目录下的wtmp记录文件。

–verbose

Outputs  slightly  more  verbose  messages   when rebooting,  useful  for  debugging  problems with shutdown.

 

-n, –no-sync

don’t sync before reboot or halt

 

【示例】:

1、基本用法:重启系统

[[email protected]
~]# reboot

 

 

3 C Library Functions

打包命令 tar
tar -cvf 打包文件名 源文件
选项:
-c 打包
-v 显示过程
-f 指定打包后的文件名
例如: tar -cvf longzls.tar longzls

29. 【命令】:history

【功能说明】:

Display or manipulate the history list.  #查看命令历史

【语法格式】:

history [-c] [-d offset] [n] or history -anrw [filename] or
history -ps arg [arg…]

【选项参数】:

参数

说明

简解

-c

clear the history list by deleting all of the entries

清空当前历史命令

-d

offset delete the history entry at offset OFFSET

 

-a

append history lines from this session to the history file

将历史命令缓冲区中命令写入历史命令文件中

-n

read all history lines not already read from the history file

 

-r

read the history file and append the contents to the history list

将历史命令文件中的命令读入当前历史命令缓冲区

-w

write the current history to the history file and append them to the history list

 

-p

perform history expansion on each ARG and display the result without storing it in the history list

 

-s

append the ARGs to the history list as a single entry

 

【示例】:

1、基本用法:查看命令历史

[[email protected]
~]# history

    1  cd /usr/local/

    2  cd ..

    3  pwd

    4  cd ../..

    5  cd –

    6  cd ~

    7  cd /

    8  ll

9  ls -la

……

 

 

4 Devices and Special Files

解打包命令
tar -xvf 打包文件名
选项:
-x 解打包
例如: tar -xvf longzls.tar

30. 【命令】:dmesg

【功能说明】:

print or control the kernel ring buffer  #查看系统故障信息

【语法格式】:

dmesg [-c] [-r] [-n level] [-s bufsize]

【选项参数】:

参数

说明

简解

-c

Clear the ring buffer contents after printing.

显示信息后,清除ring buffer中的内容

-r

Print  the  raw message buffer, i.e., don’t strip  the log level prefixes.

 

-s bufsize

Use a buffer of size bufsize to query the  kernel ring  buffer.   This  is  16392 by default.  (The default kernel syslog buffer  size  was  4096  at  first,  8192  since 1.3.54, 16384 since 2.1.113.) If you have set the kernel buffer  to  be  larger than  the default then this option can be used to view the entire buffer

设置ring buffer大小,预设置为8196,刚好等于ring buffer的大小

-n level

Set the level at which  logging  of  messages  is done  to the console.  For example, -n 1 prevents all messages, except panic messages, from appear-ing  on  the console.  All levels of messages are still written to /proc/kmsg,  so  syslogd(8)  can

 still  be  used  to  control exactly where kernel messages appear.  When the  -n  option  is  used,dmesg  will  not  print  or clear the kernel ring buffer

设置记录信息的层级

 

 

 

【示例】:

1、基本用法:查看信息

[[email protected]
~]# dmesg

Initializing cgroup subsys cpuset

Initializing cgroup subsys cpu

Linux version 2.6.32-573.el6.x86_64
([email protected])
(gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) ) #1 SMP Thu Jul
23 15:44:03 UTC 2015

Command line: ro root=UUID=8fc55236-ce36-4cc4-9924-202ed5142065
rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD
SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us
rd_NO_DM rhgb quiet

 

5 File Formats and Conventions

其实.tar.gz格式是先打包为.tar格式,再压缩为.gz格式

31. 【命令】:ifup、ifdown

【功能说明】:

ifup – bring a network interface up  #开启网卡

ifdown – take a network interface down  #关闭网卡

【语法格式】:

ifup IFACE [boot]

ifdown IFACE

【选项参数】:

参数

说明

简解

 

 

 

【示例】:

1、基本用法:关闭、开启网卡

[[email protected]
~]# ifdown eth0

[[email protected]
~]# ifup eth0

Determining if ip address 10.0.0.8 is already in use for device eth0…

 

6 Games et. Al.

tar -zcvf 压缩报名.tar.gz 源文件
选项:
-z 压缩为.tar.gz格式
tar -zxvf 压缩包名.tar.gz
选项:
-x 解压缩.tar.gz格式

32. 【命令】:nl

【功能说明】:

number lines of files  #显示文件行号

【语法格式】:

nl [OPTION]… [FILE]…

【选项参数】:

参数

说明

简解

-b, –body-numbering=STYLE

use STYLE for numbering body lines

指定行号指定的方式,主要有两种:

-b a :表示不论是否为空行,也同样列出行号(类似 cat -n);

-b t :如果有空行,空的那一行不要列出行号(默认值);

-d, –section-delimiter=CC

use CC for separating logical pages

 

-f, –footer-numbering=STYLE

use STYLE for numbering footer lines

 

-h, –header-numbering=STYLE

use STYLE for numbering header lines

 

-i, –line-increment=NUMBER

line number increment at each line

 

-l, –join-blank-lines=NUMBER

group of NUMBER empty lines counted as one

 

-n, –number-format=FORMAT

insert line numbers according to FORMAT

列出行号表示的方法,主要有三种:

-n ln :行号在萤幕的最左方显示;

-n rn :行号在自己栏位的最右方显示,且不加 0 ;

-n rz :行号在自己栏位的最右方显示,且加 0 ;

-p, –no-renumber

do not reset line numbers at logical pages

在逻辑定界符处不重新开始计算

-s, –number-separator=STRING

add STRING after (possible) line number

 

-v, –starting-line-number=NUMBER

first line number on each logical page

 

-w, –number-width=NUMBER

use NUMBER columns for line numbers

行号栏位的占用的位数

 

【示例】:

1、基本用法:显示行号(默认空行不显示)

[[email protected]
~]# nl test.txt

     1  10

     2  14

     3  99

       

       

       

     4  8a1

     5  69

 

2、显示行号(空行也显示)

[[email protected]
~]# nl -b a test.txt

     1  10

     2  14

     3  99

     4

     5

     6

     7  8a1

     8  69

 

3、让行号前自动补上0,统一输出格式

[[email protected]
~]# nl -b a -n rz test.txt

000001  10

000002  14

000003  99

000004

000005

000006

000007  8a1

000008  69

 

4、调整统一输出格式为三位数字

[[email protected]
~]# nl -b a -n rz -w 3 test.txt

001     10

002     14

003     99

004

005

006

007     8a1

008     69

 

 

7 Miscellanea

tar -jcvf 压缩包名.tar.bz2 源文件
选项:
-z 压缩为.tar.bz2格式
tar -jxvf 压缩包名.tar.bz2
选项:
-x 解压缩.tar.bz2格式

33. 【命令】:less

【功能说明】:

opposite of more  #分屏查看文本(可回退)

【语法格式】:

       less -?

       less –help

       less -V

       less –version

       less [-[+]aBcCdeEfFgGiIJKLmMnNqQrRsSuUVwWX~]

            [-b space] [-h lines] [-j line] [-k keyfile]

            [-{oO} logfile] [-p pattern] [-P prompt] [-t tag]

            [-T tagsfile] [-x tab,…] [-y lines] [-[z] lines]

            [-# shift] [+[+]cmd] [–] [filename]…

【选项参数】:

参数

说明

简解

-bn

Specifies the amount of buffer  space  less  will use  for  each  file, in units of kilobytes (1024 bytes).  By default 64K of buffer space  is  used for each file (unless the file is a pipe; see the -B option).  The -b option specifies instead that n  kilobytes  of  buffer space should be used for each file.  If n is -1, buffer  space  is  unlimited;  that  is, the entire file can be read into memory.

设置缓冲区的大小

-e

Causes less to automatically exit the second time it reaches end-of-file.  By default, the only way to exit less is via the "q" command.

当文件显示结束后,自动离开

-f

Forces non-regular files to be opened.   (A  non-regular  file  is a directory or a device special file.)  Also suppresses the warning message  when a  binary  file is opened.  By default, less will refuse to open non-regular files.  Note that some operating  systems  will not allow directories to be read, even if -f is set

强迫打开特殊文件,例如外围设备代号、目录和二进制文件

-g

Normally,  less  will highlight ALL strings which match the last search  command.   The  -g  option changes  this behavior to highlight only the particular string which was found by the last search command.   This  can  cause  less to run somewhat  faster than the default

只标志最后搜索的关键词

-i

Causes searches to ignore case; that  is,  uppercase  and  lowercase  are  considered  identical. This option is ignored if any  uppercase  letters appear  in the search pattern; in other words, if a pattern contains uppercase letters,  then  that  search does not ignore case

忽略搜索时的大小写

-m

Causes less to prompt verbosely (like more), with the  percent  into  the  file.   By default, less prompts with a colon

显示类似more命令的百分比

-N

Causes a line  number  to  be  displayed  at  the beginning of each line in the display.

显示每行的行号

-ofilename

Causes  less  to copy its input to the named file as it is being viewed.  This  applies  only  when the  input  file is a pipe, not an ordinary file. If the file already exists,  less  will  ask  for confirmation before overwriting it

将less 输出的内容在指定文件中保存起来

-Q

Causes  totally  "quiet"  operation: the terminal bell is never rung.

不使用警告音

-s

Causes  consecutive  blank  lines  to be squeezed into a single blank line.  This  is  useful  when viewing nroff output

显示连续空行为一行

-S

Causes  lines  longer than the screen width to be chopped rather than folded.  That is, the portion of  a  long  line that does not fit in the screen width is not shown.  The default is to fold  long lines; that is, display the remainder on the next line

行过长时间将超出部分舍弃

-xn

Sets  tab stops.  If only one n is specified, tab stops are set at multiples  of  n.   If  multiple  values  separated  by  commas  are specified, tab stops are set at those positions, and  then  continue with the same spacing as the last two.  For example, -x9,17 will set tabs at positions 9, 17,25, 33, etc. The default for n is 8

将“tab”键显示为规定的数字空格

【示例】:

1、基本用法:查看文件

[[email protected]
~]# less test.txt

10

14

99

 

 

 

8a1

69

38

 

【特别说明】

1.全屏导航

ctrl + F – 向前移动一屏

ctrl + B – 向后移动一屏

ctrl + D – 向前移动半屏

ctrl + U – 向后移动半屏

 

2.单行导航

j – 向前移动一行

k – 向后移动一行

 

3.其它导航

G – 移动到最后一行

g – 移动到第一行

q / ZZ – 退出 less 命令

 

4.其它有用的命令

v – 使用配置的编辑器编辑当前文件

h – 显示 less 的帮助文档

&pattern – 仅显示匹配模式的行,而不是整个文件

 

5.标记导航

当使用 less 查看大文件时,可以在任何一个位置作标记,可以通过命令导航到标有特定标记的文本位置:

ma – 使用 a 标记文本的当前位置

‘a – 导航到标记 a 处

 

8 System Administration tools and Deamons

tar -jxvf 压缩包名.tar.bz2 -C 目录名
解压到指定目录中
tar -zcvf /tmp/test.tar.gz jp abc.cfg
压缩多个文件到同一压缩包,并保存到指定文件目录
tar -ztvf test.tar.gz
只查看压缩包中的内容,不接呀

34. 【命令】:more

【功能说明】:

file perusal filter for crt viewing  #分屏查看文件(不可回退)

【语法格式】:

more [-dlfpcsu] [-num] [+/pattern] [+linenum] [file …]

【选项参数】:

参数

说明

简解

+num

Start at line number num

从笫n行开始显示

-num

This option specifies an integer which is the screen size (in lines)

定义屏幕大小为n行

+/pattern

The +/ option specifies a string that will be searched for before each file is displayed

在每个档案显示前搜寻该字串(pattern),然后从该字串前两行之后开始显示

-c

Do not scroll.  Instead, paint each screen from the top, clearing the remainder of each line as it is displayed.

从顶部清屏,然后显示

-d

more will prompt the user with the message "[Press space to continue, ’q’ to quit.]" and will display"[Press ’h’ for instructions.]" instead of ringing the bell when an illegal key is pressed

提示“Press space to continue,’q’ to quit(按空格键继续,按q键退出)”,禁用响铃功能

-l

more usually treats ^L (form feed) as a special character, and will pause after any line that contains a form feed.  The -l option will prevent this behavior.

忽略Ctrl+l(换页)字符

-p

Do not scroll.  Instead, clear the whole screen and then display the text

通过清除窗口而不是滚屏来对文件进行换页,与-c选项相似

-s

Squeeze multiple blank lines into one.

把连续的多个空行显示为一行

-u

Suppress underlining

把文件内容中的下画线去掉

【示例】:

1、基本用法:查看文件

[[email protected]
~]# more test.txt

10

14

99

 

 

 

8a1

69

38

【特别说明】

Enter    向下n行,需要定义。默认为1行

Ctrl+F   向下滚动一屏

空格键  向下滚动一屏

Ctrl+B  返回上一屏

=         输出当前行的行号

:f      输出文件名和当前行的行号

V        调用vi编辑器

!命令   调用Shell,并执行命令 

q         退出more

 

这里有man的级别,比如刚才的ls,就是ls(1).

shutdown命令
shutdown [选项] 时间
选项:
-c 取消前一个关机命令
-h 关机
-r 重启

35. 【命令】:wc

【功能说明】:

print newline, word, and byte counts for each file  #打印行数、字数

【语法格式】:

wc [OPTION]… [FILE]…

wc [OPTION]… –files0-from=F

【选项参数】:

参数

说明

简解

-c, –bytes

print the byte counts

打印字节数

-m, –chars

print the character counts

打印字符数

-l, –lines

print the newline counts

打印行数

–files0-from=F

read input from the files specified by NUL-terminated names in file F; If F is – then read  names from standard input

 

-L, –max-line-length

print the length of the longest line

打印最长行的长度

-w, –words

print the word counts

打印单词数

【示例】:

1、基本用法:打印行数、单词数、字节数及文件名

[[email protected]
~]# wc test.txt

 9  6 22 test.txt

 

2、只打印统计数字不打印文件名

[[email protected]
~]# cat test.txt|wc -l

9

 

 

查看命令的man等级,man -f 命令

重启命令
reboot
退出登陆命令
logout

36. 【命令】:chkconfig

【功能说明】:

updates and queries runlevel information for system services
 #管理开机自启动程序

【语法格式】:

chkconfig [–list] [–type type][name]

chkconfig –add name

chkconfig –del name

chkconfig –override name

chkconfig   [–level   levels]   [–type   type]
   name<on|off|reset|resetpriorities>

       chkconfig [–level levels] [–type type] name

【选项参数】:

参数

说明

简解

–level levels

Specifies the run levels an operation should pertain  to. It is given as a string of numbers from 0 to 6. For example, –level  35  specifies  run- levels 3 and 5

指定系统服务要在哪一个执行等级中开启或关毕

–add name

This  option adds a new service for management by chkconfig.  When a new service is added,  chkconfig  ensures  that the service has either a start or a kill entry in every runlevel.  If  any  run-level is missing such an entry, chkconfig creates the appropriate entry as specified by the default values  in  the  init  script.  Note that default entries in  LSB-delimited  ’INIT  INFO’  sections take precedence over the default runlevels in the initscript; if any  Required-Start  or  Required-Stop entries are present, the start and stop priorities of the script will be adjusted to account for these dependencies

增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据

–del name

The service is removed from chkconfig management,and any symbolic links  in  /etc/rc[0-6].d  which pertain to it are removed.Note  that  future package installs for this service may run chkconfig –add, which  will  re-add such  links.  To disable a service, run chkconfig name off.

删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的叙述文件内删除相关数据

–list name

This option lists all of the services which  chkconfig  knows about, and whether they are stopped or started in each runlevel. If  name  is  specified,  information  in only display about service name.

罗列所有服务在各执行等级上的开启、关闭情况

【示例】:

1、基本用法:查看所有服务在各执行等级上的开启、关闭情况

[[email protected]
~]# chkconfig –list

abrt-ccpp       0:off   1:off   2:off   3:off   4:off   5:off   6:off

abrtd           0:off   1:off   2:off   3:off   4:off   5:off   6:off

acpid           0:off   1:off   2:off   3:off   4:off   5:off   6:off

atd             0:off   1:off   2:off   3:off   4:off   5:off   6:off

auditd          0:off   1:off   2:off   3:off   4:off   5:off   6:off

blk-availability        0:off   1:on    2:off   3:off   4:off
  5:off6:off

cpuspeed        0:off   1:on    2:off   3:off   4:off   5:off   6:off

crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off

haldaemon       0:off   1:off   2:off   3:off   4:off   5:off   6:off

……

 

2、基本用法:在指定执行等级上关闭(开启)一个服务

[[email protected]
~]# chkconfig –level 2345 atd off

[[email protected]
~]# chkconfig –list|grep “atd”

atd             0:off   1:off   2:off   3:off   4:off   5:off   6:off

 

 

man 数字(级别) 命令

mount
查询系统中已经挂在的设备
mount -a
依据配置文件/etc/fstab的内容,自动挂载

37. 【命令】:tar

【功能说明】:

manual page for tar 1.23  #打包命令

【语法格式】:

tar [OPTION…] [FILE]…

【选项参数】:

参数

说明

简解

-A

append tar files to an archive

新增压缩文件到已存在的压缩

-c

create a new archive

建立新的压缩文件

-d

find differences between archive and file system

记录文件的差别

-r

append files to the end of an archive

添加文件到已经压缩的文件结尾

-u

only append files newer than copy in archive

添加改变了和现有的文件到已经存在的压缩文件

-x

extract files from an archive

从压缩的文件中提取文件

-t

list the contents of an archive

显示压缩文件的内容

-z

filter the archive through gzip

支持gzip解压文件

-j

filter the archive through bzip2

支持bzip2解压文件

-v

verbosely list files processed

显示操作过程

-W

attempt to verify the archive after writing it

确认压缩文件的正确性

-C

change to directory DIR

切换到指定目录

-f

use archive file or device ARCHIVE

指定压缩文件

【示例】:

1、基本用法:打包文件zcvf

[[email protected]
~]# tar zcvf data.tar.gz ./data

./data/

./data/d.txt

./data/oldboy.txt

./data/dir5/

./data/dir1/

./data/test.txt

./data/c.txt

./data/dir2/

./data/dir3/

./data/dir4/

./data/e.txt

[[email protected]
~]# ls

anaconda-ks.cfg  install.log         oldboy         test.txt

data             install.log.syslog  oldboy.tar.gz

data.tar.gz      log2.txt            oldboy.txt

 

2、基本用法:解包zxvf

[[email protected]
tmp]# tar zxvf data.tar.gz

./data/

./data/d.txt

./data/oldboy.txt

./data/dir5/

./data/dir1/

./data/test.txt

./data/c.txt

./data/dir2/

./data/dir3/

./data/dir4/

./data/e.txt

[[email protected]
tmp]# ls

data  data.tar.gz  file1.txt  file2.txt  oldboy  test.txt

 

3、批量排除打包

[[email protected] opt]# cat paichu.log
stu05  
stu06  
stu07
[[email protected] opt]# tar zcvfX paichuX.tar.gz paichu.log ./test 
./test/
./test/stu03
./test/stu01
./test/stu02
./test/stu09
./test/stu08
./test/stu10
./test/stu04
[[email protected] opt]# tar tf paichuX.tar.gz 
./test/
./test/stu03
./test/stu01
./test/stu02
./test/stu09
./test/stu08
./test/stu10
./test/stu04

 

[root@localhost ~]# man -f ls
ls (1) – list directory contents
ls (1p) – list directory contents

挂载命令格式
mount [-t文件系统] [-o特殊选项] 设备文件名 挂载点
选项:
-t
文件系统:加入文件系统类型来指定挂载的类型,可以ext3、ext4、iso9660等文件系统
-o 特殊选项:可以指定挂载的额外选项
mkdir /mnt/cdrom/
建立挂载点

38. 【命令】:cut

【功能说明】:

remove sections from each line of files  #

【语法格式】:

cut OPTION… [FILE]…

【选项参数】:

参数

说明

简解

-b, –bytes=LIST

select only these bytes

以字节为单位进行分割

-c, –characters=LIST

select only these characters

以字符为单位进行分割

-d, –delimiter=DELIM

use DELIM instead of TAB for field delimiter

自定义分隔符,默认为制表符

-f, –fields=LIST

select  only these fields;  also print any line that contains  no  delimiter  character,  unless  the  -soption is specified

与-d一起使用,指定显示哪个区域

-n

with -b: don’t split multibyte characters

取消分割多字节字符仅和 -b 标志一起使用。如果字符的最后一个字节落在由 -b 标志的 List 参数指示的范围之内,该字符将被写出;否则,该字符将被排除。

–complement

complement  the set of selected bytes, characters or fields

补足被选择的字节、字符或字段

-s, –only-delimited

do not print lines not containing delimiters

不打印不含分隔符的行

–output-delimiter=STRING

use STRING as the output delimiter the default is to use the input delimiter

指定分隔符打印样式

【示例】:

1、基本用法:以字节为单位取列

[[email protected]
~]# cat oldboy.txt

inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0

[[email protected]
~]# cut -b6-18 oldboy.txt

addr:10.0.0.8

 

2、基本用法:以字符为单位取列

[[email protected]
~]# cat oldboy.txt

inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0

[[email protected]
~]# cut -c6-18 oldboy.txt

addr:10.0.0.8

 

3、基本用法:以分隔符为单位取列

[[email protected]
~]# cat /etc/passwd|head -5

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[[email protected]
~]# cat /etc/passwd|head -5|cut -d’:’ -f5

root

bin

daemon

adm

lp

 

 

[root@localhost ~]# man -f passwd
passwd (1) – update user’s authentication tokens
passwd (5) – password file
passwd [sslpasswd] (1ssl) – compute password hashes
[root@localhost ~]# man 5 passwd

mount -t iso9660 /dev/cdrom /mnt/cdrom/
挂载光盘
mount /dev/sr0 /mnt/cdrom/
挂载光盘(同上)
umount 设备文件名/挂载点

39. 【命令】:tr

【功能说明】:

translate or delete characters  #转换或删除字符

【语法格式】:

tr [OPTION]… SET1 [SET2]

【选项参数】:

参数

说明

简解

-c, -C, –complement

use the complement of SET1

取代所有不属于第一字符集的字符

-d, –delete

delete characters in SET1, do not translate

删除所有属于第一字符集的字符

-s, –squeeze-repeats

replace each input sequence of a repeated  character that  is  listed in SET1 with a single occurrence of that character

把连续重复的字符以单独一个字符表示

-t, –truncate-set1

first truncate SET1 to length of SET2

先删除第一字符集较第二字符集多出的字符

【示例】:

1、基本用法:替换字符

[[email protected]
~]# echo abcdef|tr ‘a-f’ ‘x-za-c’

xyzabc

 

2、删除字符

[[email protected]
~]# echo abcdef|tr -d ‘a-c’

def

 

3、连续重复的字符以单独一个字符表示

[[email protected]
~]# echo aaabbbccc|tr -s ‘abc’

abc

[[email protected]
~]# echo aaabbbccc|tr -s ‘a’

abbbccc

 

 

用whereis再来一次

fdisk -l 查看U盘设备文件名
mount -t vfat /dev/sdb1 /mnt/usb/ 挂载U盘
注意:Linux默认不支持NTFS文件系统

40. 【命令】:stat

【功能说明】:

display file or file system status
 #显示文件和文件系统状态(查看文件属性)

【语法格式】:

stat [OPTION]… FILE…

【选项参数】:

参数

说明

简解

-L, –dereference

follow links

如果是链接文件,则获取原文件信息

-Z, –context

print the SELinux security context

打印SELinux安全上下文信息

-f, –file-system

display file system status instead of file status

显示文件系统状态而非文件状态

-c  –format=FORMAT

use  the  specified  FORMAT  instead of the default; output a newline after each use of FORMAT

自定义输出格式,结尾有换行

–printf=FORMAT

like –format, but interpret backslash escapes,  and do  not output a mandatory trailing newline.  If you want a newline, include \n in FORMAT.

自定义输出格式,结尾无换行符,需手动添加

-t, –terse

print the information in terse form

以简洁方式输出信息

【示例】:

1、基本用法:查看文件信息

[[email protected]
~]# stat test.txt

  File: `test.txt’

  Size: 22              Blocks: 8          IO Block: 4096   regular file

Device: 803h/2051d      Inode: 11628       Links: 1

Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2016-03-19 16:08:55.616298620 +0800

Modify: 2016-03-19 16:08:44.504300222 +0800

Change: 2016-03-19 16:08:44.505300149 +0800

 

2、利用stat取文件权限属性

[[email protected]
~]# stat test.txt

  File: `test.txt’

  Size: 22              Blocks: 8          IO Block: 4096   regular file

Device: 803h/2051d      Inode: 11628       Links: 1

Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)

……

[[email protected]
~]# stat -c %a test.txt

644

 

[root@localhost ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man5/passwd.5.gz
/usr/share/man/man1/passwd.1.gz

查看登录用户信息
w
命令输出:
USER:登录的用户名
TTY:登录的终端
FROM:从哪个IP地址登录
LOGIN@:登录时间
IDLE:用户闲置时间
JCPU:指的是和该终端连接的所有进程占用的时间。这个时间里并不包括过去的后台作业时间,但却包括当前运行的后台作业所占用的时间
PCPU:是指当前进程所占用的时间
WHAT:当前正在运行的命令

41. 【命令】:file

【功能说明】:

determine file type  #显示文件类型

【语法格式】:

file [-bchikLNnprsvz0] [–apple] [–mime-encoding]

          [–mime-type] [-e testname] [-F separator] [-f
namefile]

          [-m magicfiles] file …

file -C [-m magicfiles]

file [–help]

【选项参数】:

参数

说明

简解

-b

Do not prepend filenames to output lines (brief mode)

列出文件辨识结果时,不显示文件名称。

-c

Cause a checking printout of the parsed form of the magic file.  This is usually used in conjunction with the -m flag to debug a new magic file before installing it

详细显示指令执行过程,便于排错或分析程序执行的情形

-f

Read the names of the files to be examined from namefile (one per line) before the argument list.Either namefile or at least one filename argument must be present; to test the standard input, use ‘-’ as a filename argument.

列出文件中文件名的文件类型

-F

Use the specified string as the separator between the filename and the file result returned. Defaults to‘:’.

使用指定分隔符号替换输出文件名后的默认的“:”分隔符

-i

Causes the file command to output mime type strings rather than the more traditional human readable ones.Thus it may say ‘text/plain; charset=us-ascii’ rather than ‘ASCII text’.  In order for this option to work,file changes the way it handles files recognized by the command itself (such as many of the text file types, directories etc), and makes use of an alternative ‘magic’ file.  (See the FILES section, below)

输出mime类型的字符串

-L

option causes symlinks to be followed, as the like-named option in ls(1) (on systems that support symbolic links).  This is the default if the environment variable POSIXLY_CORRECT is defined

查看对应软链接对应文件的文件类型

-z

Try to look inside compressed files.

尝试去解读压缩文件的内容

【示例】:

1、基本用法:显示文件类型

[[email protected]
~]# file test.txt

test.txt: ASCII text

[[email protected]
~]# file /data

/data: directory

 

 

也可以看到。

who

42. 【命令】:last

【功能说明】:

show listing of last logged in users  #查看用户登录信息

【语法格式】:

last  [-R]  [-num]  [  -n num ] [-adFiowx] [ -f file ] [ -t

       YYYYMMDDHHMMSS ] [name…]  [tty…]

       lastb [-R] [-num] [  -n  num  ]  [  -f  file  ]
 [-adFiowx]

       [name…]  [tty…]

【选项参数】:

参数

说明

简解

-a

Display  the  hostname in the last column. Useful in combination with the next flag

将登录系统的的主机名称或IP地址,显示在最后一行

-d

For non-local logins, Linux stores not only the host name  of  the remote host but its IP number as well.This option translates the IP  number  back  into  a hostname

将IP地址转换成主机名称

-f

Specifies a file to search other than /var/log/wtmp

指定记录文件,默认是显示/var/log目录下的wtmp文件的记录,但/var/log目录下得btmp能显示的内容更丰富,可以显示远程登录,例如ssh登录 ,包括失败的登录请求

-i

This  option  is  like -d in that it displays the IP number of the remote host, but it  displays  the  IP  number in numbers-and-dots notation.

-i显示特定ip登录的情况。跟踪用 -i显示特定ip登录的情况。跟踪用

-o

Read  an  old-type wtmp file (written by linux-libc5 applications).

 

-n

This is a count telling last how many lines to show.

-n <显示列数>或-<显示列数>  设置列出名单的显示列数

-w

Display full user and domain names in the output

 

-R

Suppresses the display of the hostname field

不显示登入系统的主机名称或IP(省略 hostname 的栏位)

-t

Display  the  state  of  logins  as of the specified time.  This is useful, e.g., to determine easily who was  logged  in at a particular time — specify that time with -t and look for "still logged in"

显示YYYYMMDDHHMMSS之前的信息

-x

Display the system shutdown entries  and  run  level  changes.

显示系统关闭、用户登录和退出的历史

【示例】:

1、基本用法:查看用户登录信息

[[email protected]
~]# last

oldboy   pts/0        10.0.0.1         Sun Mar 20 12:15   still logged
in   

reboot   system boot  2.6.32-573.el6.x Sun Mar 20 12:15 – 15:08  (02:53)
   

oldboy   pts/1        10.0.0.1         Sat Mar 19 15:36 – down   (06:49)
   

root     tty1                          Sat Mar 19 15:35 – down   (06:50)

 

2、显示最后登录系统的N条记录

[[email protected]
~]# last -n 10

oldboy   pts/0        10.0.0.1         Sun Mar 20 12:15   still logged
in   

reboot   system boot  2.6.32-573.el6.x Sun Mar 20 12:15 – 15:25  (03:10)
   

oldboy   pts/1        10.0.0.1         Sat Mar 19 15:36 – down   (06:49)
   

root     tty1                          Sat Mar 19 15:35 – down   (06:50)
   

oldboy   pts/0        10.0.0.1         Sat Mar 19 10:28 – 15:52  (05:24)
   

reboot   system boot  2.6.32-573.el6.x Sat Mar 19 10:28 – 22:25  (11:57)
   

root     tty1                          Sat Mar 19 10:27 – down   (00:00)
   

oldboy   pts/0        10.0.0.1         Sat Mar 19 10:20 – down   (00:07)
   

oldboy   pts/0        10.0.0.1         Sat Mar 19 10:19 – 10:20  (00:00)
   

reboot   system boot  2.6.32-573.el6.x Sat Mar 19 10:19 – 10:27  (00:08)
   

 

wtmp begins Thu Mar  3 23:14:45 2016

 

3、将IP地址转换成主机名称

[[email protected]
~]# last -n 5 -d

oldboy   pts/0        localhost        Sun Mar 20 12:15   still logged
in   

reboot   system boot  0.0.0.0          Sun Mar 20 12:15 – 15:27  (03:11)
   

oldboy   pts/1        localhost        Sat Mar 19 15:36 – down   (06:49)
   

root     tty1         0.0.0.0          Sat Mar 19 15:35 – down   (06:50)
   

oldboy   pts/0        localhost        Sat Mar 19 10:28 – 15:52  (05:24)

 

4、指定/var/log/btmp文件,查看登录系统的用户相关信息

[[email protected]
~]# last -n 5 -f /var/log/btmp

 

btmp begins Thu Mar  3 23:14:22 2016

 

 

再用whatis试一下

last 查询当前登录和过去登录的用户信息
last命令默认是读取/var/log/wtmp文件数据

43. 【命令】:lastlog

【功能说明】:

reports the most recent login of all users or of a given user
 #查看最近登录的用户信息

【语法格式】:

lastlog [options]

【选项参数】:

参数

说明

简解

-b, –before DAYS

Print only lastlog records older than DAYS

显示指定天数前的登录信息

-h, –help

Display help message and exit.

显示帮助

-t, –time DAYS

Print the lastlog records more recent than DAYS

显示指定天数以来的登录信息

-u, –user LOGIN|RANGE

Print the lastlog record of the specified user(s)

显示指定用户的最近登录信息

【示例】:

1、基本用法:显示最近登录的用户信息

[[email protected]
~]# lastlog

Username         Port     From             Latest

root             tty1                      Sat Mar 19 15:35:02 +0800
2016

bin                                        **Never logged in**

daemon                                     **Never logged in**

adm                                        **Never logged in**

lp                                         **Never logged in**

sync                                       **Never logged in**

shutdown                                   **Never logged in**

halt                                       **Never logged in**

mail                                       **Never logged in**

……

oldboy           pts/0    10.0.0.1         Sun Mar 20 12:15:35 +0800
2016

test                                       **Never logged in**

 

2、显示某一个用户最近登录信息

[[email protected]
~]# lastlog -u oldboy

Username         Port     From             Latest

oldboy           pts/0    10.0.0.1         Sun Mar 20 12:15:35 +0800
2016

 

 

[root@localhost ~]# whatis ls
ls (1) – list directory contents
ls (1p) – list directory contents
[root@localhost ~]# whatis passwd
passwd (1) – update user’s authentication tokens
passwd (5) – password file
passwd [sslpasswd] (1ssl) – compute password hashes

lastlog 查看所有用户的最后一次登录时间

44. 【命令】:df

【功能说明】:

report file system disk space usage  #查看系统磁盘空间的使用情况

【语法格式】:

df [OPTION]… [FILE]…

【选项参数】:

参数

说明

简解

-a

include dummy file systems

包含全部的文件系统

-B

use SIZE-byte blocks

以指定的区块大小来显示区块数目

-h

print sizes in human readable format (e.g., 1K 234M 2G)

以可读性较高的方式来显示信息

-H

likewise, but use powers of 1000 not 1024

与-h相同,但在计算时是以1000Bytes为换算单位而非1024 Bytes

-i

list inode information instead of block usage

显示inode的信息

-k

like –block-size=1K

指定区块大小为1024字节

-l

limit listing to local file systems

仅显示本地的文件系统

–no-sync

do not invoke sync before getting usage info (default)

在取得磁盘使用信息前,不要执行sync指令,此为预设值

-P

use the POSIX output format

使用POSIX的输出格式

–sync

invoke sync before getting usage info

在取得磁盘使用信息前,先执行sync指令

-t

limit listing to file systems of type TYPE

仅显示指定文件系统类型的磁盘信息

-T

print file system type

显示文件系统的类型

-x

limit listing to file systems not of type TYPE

不要显示指定文件系统类型的磁盘信息

【示例】:

1、基本用法:查看磁盘空间使用情况

[[email protected]
~]# df

Filesystem     1K-blocks    Used Available Use% Mounted on

/dev/sda3        7149156 1494028   5285312  23% /

tmpfs             243320       0    243320   0% /dev/shm

/dev/sda1         194241   40498    143503  23% /boot

[[email protected]
~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       6.9G  1.5G  5.1G  23% /

tmpfs           238M     0  238M   0% /dev/shm

/dev/sda1       190M   40M  141M  23% /boot

 

2、基本用法:查看inode的消耗情况

[[email protected]
~]# df -hi

Filesystem     Inodes IUsed IFree IUse% Mounted on

/dev/sda3        452K   55K  397K   13% /

tmpfs             60K     1   60K    1% /dev/shm

/dev/sda1         50K    39   50K    1% /boot

[[email protected]
~]# df -i

Filesystem     Inodes IUsed  IFree IUse% Mounted on

/dev/sda3      462384 56221 406163   13% /

tmpfs           60830     1  60829    1% /dev/shm

/dev/sda1       51200    39  51161    1% /boot

 

 

再举个栗子

45. 【命令】:dumpe2fs

【功能说明】:

dump ext2/ext3/ext4 filesystem information
 #查看文件系统内部信息(元数据)

【语法格式】:

dumpe2fs [ -bfhixV ] [ -o superblock=superblock ] [ -o
blocksize=block-

       size ] device

【选项参数】:

参数

说明

简解

-b

print the blocks which are reserved as bad in the filesystem

打印文件系统中预留的块信息

-o superblock=superblock

use the block superblock when examining  the  filesystem.   This option  is  not usually needed except by a filesystem wizard who is examining the remains of a very badly corrupted filesystem

指定检查文件系统时使用的超级块

-o blocksize=blocksize

use blocks of blocksize bytes  when  examining  the  filesystem.This  option is not usually needed except by a filesystem wizard who is examining the remains of a very badly corrupted  filesystem

检查文件系统时使用指定的块大小

-f

force  dumpe2fs  to display a filesystem even though it may have some filesystem feature flags which dumpe2fs may not  understand(and  which can cause some of dumpe2fs’s display to be suspect)

 

-h

only display the superblock information and not any of the block group descriptor detail information

仅显示超级块信息

-i

display  the  filesystem  data  from  an  image  file created by e2image, using device as the pathname to the image file

从指定的文件系统映像文件中读取文件系统信息

-x

print the detailed group information block numbers in  hexadecimal format

以16进制格式打印信息块成员

-V

print the version number of dumpe2fs and exit.

 

【示例】:

1、基本用法:查看文件系统内部信息

[[email protected]
~]# dumpe2fs /dev/sda1

dumpe2fs 1.41.12 (17-May-2010)

Filesystem volume name:   <none>

Last mounted on:          /boot

Filesystem UUID:          3e368f7e-0775-4c00-afba-f8880d916366

Filesystem magic number:  0xEF53

Filesystem revision #:    1 (dynamic)

Filesystem features:      has_journal ext_attr resize_inode
dir_index filetype needs_recovery extent flex_bg sparse_super
huge_file uninit_bg dir_nlink extra_isize

Filesystem flags:         signed_directory_hash

Default mount options:    user_xattr acl

……

Group 0: (Blocks 1-8192) [ITABLE_ZEROED]

  Checksum 0x303a, unused inodes 2009

  Primary superblock at 1, Group descriptors at 2-2

  Reserved GDT blocks at 3-258

  Block bitmap at 259 (+258), Inode bitmap at 275 (+274)

  Inode table at 291-546 (+290)

  3785 free blocks, 2009 free inodes, 6 directories, 2009 unused inodes

  Free blocks: 4408-8192

  Free inodes: 40-2048

Group 1: (Blocks 8193-16384) [INODE_UNINIT, ITABLE_ZEROED]

……

 

 

[root@localhost ~]# whereis ifconfig
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[root@localhost ~]# whatis ifconfig
ifconfig (8) – configure a network interface
[root@localhost ~]# man 8 ifconfig

46. 【命令】:tree

【功能说明】:

list contents of directories in a tree-like format
 #以树的形式罗列目录内容

【语法格式】:

tree  [-adfghilnopqrstuvxACDFNS] [-L level [-R]] [-H baseHREF]
[-T title] [-o

       filename]  [–nolinks]  [-P  pattern]  [-I  pattern]
 [–inodes]   [–device]

       [–noreport]  [–dirsfirst]  [–version]  [–help]
[–filelimit #] [directory

       …]

【选项参数】:

参数

说明

简解

-a

All files are printed.  By default tree does not  print  hidden  files(those  beginning  with  a  dot ‘.’).  In no event does tree print the file system constructs ‘.’  (current  directory)  and  ‘..’  (previous directory)

显示所有文件和目录

-A

Turn on ANSI line graphics hack when printing the indentation lines

使用ASNI绘图字符显示树状图而非以ASCII字符组合

-C

Turn colorization on always, using  built-in  color  defaults  if  the LS_COLORS  environment variable is not set.  Useful to colorize output to a pipe

在文件和目录清单加上色彩,便于区分各种类型

-d

List directories only

只罗列目录

-D

Print the date of the last modification time for the file listed

列出文件或目录的更改时间

-L

Max display depth of the directory tree

罗列的层次深度

-f

Prints the full path prefix for each file

在每个文件或目录之前,显示完整的相对路径名称

-F

Append  a  ‘/’ for directories, a ‘=’ for socket files, a ‘*’ for executable files and a ‘|’ for FIFO’s, as per ls -F

在执行文件,目录,Socket,符号连接,管道名称名称,各自加上"*","/","=","@","|"号

-g

Print the group name, or GID # if no group name is available,  of  the file.

列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码

-i

Makes  tree  not print the indentation lines, useful when used in conjunction with the -f option

不以阶梯状列出文件或目录名称

-I

Do not list those files that match the wild-card pattern

不显示符合范本样式的文件或目录名称

-l

Follows symbolic links if they point to directories, as if  they  were directories.  Symbolic links that will result in recursion are avoided when detected

如遇到性质为符号连接的目录,直接列出该连接所指向的原始目录

-n

Turn colorization off always, over-ridden by the -C option

不在文件和目录清单加上色彩

-N

Print  non-printable  characters  as  is  instead of the default caret notation

直接列出文件和目录名称,包括控制字符

-p

Print the file type and permissions for each file (as per ls -l)

列出权限标示

-P

List only those files that match the  wild-card  pattern.   Note:  you must  use  the -a option to also consider those files beginning with a

 dot ‘.’ for matching.  Valid wildcard operators are ‘*’ (any  zero  or more  characters),  ‘?’  (any  single  character), ‘[…]’ (any single character listed between brackets (optional  –  (dash)  for  character range  may be used: ex: [A-Z]), and ‘[^…]’ (any single character not listed in brackets) and ‘|’ separates alternate patterns

只显示符合范本样式的文件或目录名称

-q

Print non-printable characters in filenames as question marks  instead of the default caret notation

用"?"号取代控制字符,列出文件和目录名称

-s

Print the size of each file in bytes along with the name

列出文件或目录大小

-t

Sort the output by last modification time instead of alphabetically

用文件和目录的更改时间排序

-u

Print the username, or UID # if no username is available, of the file

列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码

-x

Stay on the current file-system only.  Ala find -xdev

将范围局限在现行的文件系统中,若指定目录下的某些子目录,其存放于另一个文件系统上,则将该子目录予以排除在寻找范围外

【示例】:

1、基本用法:以树形罗列目录

[[email protected]
~]# tree oldboy

oldboy

├── ext

│   └── oldboy

├── jeacen

├── oldboy

├── test

├── wodi.gz

├── xiaodong

├── xiaofan

├── xingfujie

└── yingsui.gz

 

2、只罗列目录

[[email protected]
~]# tree -d

.

├── data

│   ├── dir1

│   ├── dir2

│   ├── dir3

│   ├── dir4

│   └── dir5

└── oldboy

    ├── ext

    ├── test

    ├── xiaodong

    ├── xiaofan

    └── xingfujie

 

3、只罗列目录,显示深度为1层

[[email protected]
~]# tree -Ld 1

.

├── data

└── oldboy

 

4、显示完整的相对路径

[[email protected]
~]# tree -f data

data

├── data/c.txt

├── data/dir1

├── data/dir2

├── data/dir3

├── data/dir4

├── data/dir5

├── data/d.txt

├── data/e.txt

├── data/oldboy.txt

└── data/test.txt

 

5、不以阶梯状形式显示

[[email protected]
~]# tree -i data

data

c.txt

dir1

dir2

dir3

dir4

dir5

d.txt

e.txt

oldboy.txt

test.txt

 

6、为显示内容添加提示符

[[email protected]
~]# tree -F oldboy

oldboy

├── ext/

│   └── oldboy

├── jeacen

├── oldboy

├── test/

├── wodi.gz

├── xiaodong/

├── xiaofan/

├── xingfujie/

└── yingsui.gz

 

 

 

可以看到ifconfig的等级是8——看一下等级说明,也就是只能是root用户使用的命令,同时看到ifconfig命令在sbin目录下,也就是验证了sbin目录下都是只能root用户使用的命令

47. 【命令】:id

【功能说明】:

print real and effective user and group IDs  #显示用户ID及组ID

【语法格式】:

id [OPTION]… [USERNAME]

【选项参数】:

参数

说明

简解

-a

ignore, for compatibility with other versions

 

-Z

print only the security context of the current user

 

-g

print only the effective group ID

显示用户所属群组的ID

-G

print all group IDs

显示用户所有群组的ID

-n

print a name instead of a number, for -ugG

显示用户所属群组的名称

-r

print the real ID instead of the effective ID, with -ugG

显示实际ID

-u

print only the effective user ID

显示用户ID

【示例】:

1、基本用法:显示用户ID及组ID

[[email protected]
~]$ id oldboy

 

2、显示用户ID

[[email protected]
~]$ id -u oldboy

500

 

 

48. 【命令】:ln

【功能说明】:

make links between files  #创建链接

【语法格式】:

ln [OPTION]… [-T] TARGET LINK_NAME   (1st form)

ln [OPTION]… TARGET                  (2nd form)

ln [OPTION]… TARGET… DIRECTORY     (3rd form)

ln [OPTION]… -t DIRECTORY TARGET…  (4th form)

【选项参数】:

参数

说明

简解

-f

remove existing destination files

链接时先将与dist同档案名的档案删除

-d

allow the superuser to attempt to hard link  directories  (note:  will probably fail due to system restrictions, even for the superuser)

允许系统管理者硬链接自己的目录

-i

prompt whether to remove destinations

在删除与dist同名的档案时先进行询问

-n

treat  destination  that  is  a symlink to a directory as if it were a normal file

在进行软链接时,将dist视为一般的档案

-s

make symbolic links instead of hard links

创建软链接

-v

print name of each linked file

在链接之前显示其档案名

-b

like –backup but does not accept an argument

将在链接时会被覆写或删除的档案进行备份

-S

override the usual backup suffix

将备份的档案都加上SUFFIX的字尾

【示例】:

1、基本用法:创建文件硬链接

[[email protected]
~]# ln oldboy.txt oldboy_hard_link

[[email protected]
~]# ls

anaconda-ks.cfg  install.log         log2.txt  oldboy_hard_link
 oldboy.txt

data             install.log.syslog  oldboy    oldboy.tar.gz
    test.txt

 

2、基本用法:创建文件软链接

[[email protected]
~]# ln -s oldboy.txt oldboy_soft_link

[[email protected]
~]# ls

anaconda-ks.cfg  install.log         log2.txt  oldboy_hard_link
 oldboy.tar.gz  test.txt

data             install.log.syslog  oldboy    oldboy_soft_link
 oldboy.txt

 

 

查看与命令相关的帮助

49. 【命令】:du

【功能说明】:

estimate file space usage  #查看文件和目录大小

【语法格式】:

du [OPTION]… [FILE]…

du [OPTION]… –files0-from=F

【选项参数】:

参数

说明

简解

-a

write counts for all files, not just directories

显示目录中所有文件大小

-b

equivalent to ‘–apparent-size –block-size=1’

显示目录或文件大小时,以byte为单位

-c

produce a grand total

显示一个大小总和

-k

like –block-size=1K

以KB为单位输出

-m

like –block-size=1M

以MB为单位输出

-s

display only a total for each argument

仅显示总计,只列出最后加总的值

-h

print sizes in human readable format (e.g., 1K 234M 2G)

人类可读

-x

skip directories on different file systems

以一开始处理时的文件系统为准,若遇上其它不同的文件系统目录则略过

-L

dereference all symbolic links

显示选项中所指定符号链接的源文件大小

-S

do not include size of subdirectories

显示个别目录的大小时,并不含其子目录的大小

-X

exclude files that match any pattern in FILE

 

–exclude=PATTERN

exclude files that match PATTERN

略过指定的目录或文件

-D

dereference only symlinks that are listed on the command line

显示指定符号链接的源文件大小

-H

equivalent to –dereference-args (-D)

与-h参数相同,但是是以1000为换算单位

-l

count sizes many times if hard linked

重复计算硬链接的文件

【示例】:

1、基本用法:显示目录或文件所占空间

[[email protected]
~]# du

4       ./oldboy/xiaofan

8       ./oldboy/ext

4       ./oldboy/xingfujie

4       ./oldboy/xiaodong

4       ./oldboy/test

40      ./oldboy

4       ./data/dir5

4       ./data/dir1

4       ./data/dir2

4       ./data/dir3

4       ./data/dir4

28      ./data

172     .

 

2、显示指定文件所占空间

[[email protected]
~]# du oldboy.txt

4       oldboy.txt

 

3、显示指定目录所占空间

[[email protected]
~]# du oldboy

4       oldboy/xiaofan

8       oldboy/ext

4       oldboy/xingfujie

4       oldboy/xiaodong

4       oldboy/test

40      oldboy

 

4、以易读模式显示

[[email protected]
~]# du -h

4.0K    ./oldboy/xiaofan

8.0K    ./oldboy/ext

4.0K    ./oldboy/xingfujie

4.0K    ./oldboy/xiaodong

4.0K    ./oldboy/test

40K     ./oldboy

4.0K    ./data/dir5

4.0K    ./data/dir1

4.0K    ./data/dir2

4.0K    ./data/dir3

4.0K    ./data/dir4

28K     ./data

172K    .

 

 

man -k 命令 或者 apropos 命令

50. 【命令】:which

【功能说明】:

shows the full path of (shell) commands
 #从PATH变量所在路径查找二进制命令

【语法格式】:

which [options] [–] programname […]

【选项参数】:

参数

说明

简解

 

 

 

 

 

 

 

 

 

 

 

 

【示例】:

1、基本用法:查找命令

[[email protected]
~]# which cat

/bin/cat

 

1. 【命令】:
cat 【功能说明】: concatenate files and print on the standard output
#连接文件并打印到标准输出,…

[root@localhost ~]# apropos passwd
chpasswd (8) – update passwords in batch mode
fgetpwent_r [getpwent_r] (3) – get passwd file entry reentrantly
getpwent_r (3) – get passwd file entry reentrantly
gpasswd (1) – administer /etc/group and /etc/gshadow
kpasswd (1) – change a user’s Kerberos password
lpasswd (1) – Change group or user password
lppasswd (1) – add, change, or delete digest passwords
pam_localuser (8) – require users to be listed in /etc/passwd
pam_passwdqc (8) – Password quality-control PAM module
passwd (1) – update user’s authentication tokens
passwd2des [xcrypt] (3) – RFS password encryption
passwd (5) – password file
passwd [sslpasswd] (1ssl) – compute password hashes
saslpasswd2 (8) – set a user’s sasl password
smbpasswd (5) – The Samba encrypted password file
smbpasswd (8) – change a users SMB password
userpasswd (1) – A graphical tool to allow users to change their
passwords
ypchfn [yppasswd] (1) – change your password in the NIS database
ypchsh [yppasswd] (1) – change your password in the NIS database
yppasswd (1) – change your password in the NIS database

4.2 其他帮助命令

–选项帮助

help 命令

命令 –help ,可以查看命令的所有选项

[root@localhost ~]# ls –help
–shell

内部(内核)命令帮助

凡是whereis,有命令的执行位置(文件),就是外部命令,比如

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

[root@localhost ~]# whereis cd
cd: /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz

对比以上两个就能很好理解。

对于外部命令,用man;对于内部命令,用help。

–详细命令帮助info

但是,太过于详细了。。。不太实用,操作不方便。

它会把所用的帮助文档调出来,然后找到你要查询的命令。

4.3 总结

man ls

ls –help

whereis cd

help cd

 

5、压缩命令

5.1 格式

这里提到5种linux常用的压缩格式.zip .gz .bz2 .tar.gz .tar.bz2

5.2 .zip格式

这个格式可以windows和linux的通用

–命令格式

zip 压缩名.zip 源文件

注意,写压缩名一定要添加所谓的后缀——虽然不加也可以,但是通用的做法就是加上后缀,方便你我

举个栗子

[root@localhost ~]# ll
总用量 48
-rw——-. 1 root root 1272 9月 24 21:31 anaconda-ks.cfg
-rw-r–r–. 1 root root 26420 9月 24 21:31 install.log
-rw-r–r–. 1 root root 7572 9月 24 21:29 install.log.syslog
drwxr-xr-x. 2 root root 4096 11月 25 07:20 japan
[root@localhost ~]# zip sex_text.zip japan
adding: japan/ (stored 0%)
[root@localhost ~]# ll
总用量 52
-rw——-. 1 root root 1272 9月 24 21:31 anaconda-ks.cfg
-rw-r–r–. 1 root root 26420 9月 24 21:31 install.log
-rw-r–r–. 1 root root 7572 9月 24 21:29 install.log.syslog
drwxr-xr-x. 2 root root 4096 11月 25 07:20 japan
-rw-r–r–. 1 root root 162 11月 25 07:20 sex_text.zip

压缩了一个空文件japan,然后显示出来,会发现压缩文件更大一点

–压缩目录

zip -r 名字 源文件

[root@localhost ~]# mkdir japan2
[root@localhost ~]# touch japan2/cangls
[root@localhost ~]# touch japan2/longls
[root@localhost ~]# touch japan2/boduols
[root@localhost ~]# zip -r jp.zip japan2
adding: japan2/ (stored 0%)
adding: japan2/longls (stored 0%)
adding: japan2/cangls (stored 0%)
adding: japan2/boduols (stored 0%)
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog japan japan2 jp.zip
sex_text.zip

你懂的。

–解压缩

unzip 压缩文件

[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog japan japan2 jp.zip
sex_text.zip
[root@localhost ~]# rm -rf japan
[root@localhost ~]# rm -rf japan2
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog jp.zip sex_text.zip
[root@localhost ~]# unzip jp.zip
Archive: jp.zip
creating: japan2/
extracting: japan2/longls
extracting: japan2/cangls
extracting: japan2/boduols
[root@localhost ~]# unzip sex_text.zip
Archive: sex_text.zip
creating: japan/
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog japan japan2 jp.zip
sex_text.zip

5.3 .gz格式

是可以实现在windows中解压缩的

–压缩

gzip 源文件

压缩完,直接生成一个.gz文件,然后源文件被删除,当然可以保存下来源文件。

压缩目录:gzip -r 

[root@localhost ~]# gzip japan2
gzip: japan2 is a directory — ignored
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog japan japan2
[root@localhost ~]# gzip -r japan
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog japan japan2

会发现没变化,实际上gzip压缩目录的命令,是对目录下的子文件都进行压缩。所以很麻烦。。。

–解压缩

gzip -d 压缩文件

gunzip 压缩文件

5.4 .bz2格式

不能压缩目录

–压缩

bzip2 源文件

同样不保留源文件

–解压缩

bzip2 -d 压缩文件

bunzip2 压缩文件

-k 保留源文件

5.5 打包命令tar&.tar.bz2&tar.gz

tar -cvf 打包文件名 源文件

-c 打包;-v 显示过程;-f 指定打包名

举个栗子

[root@localhost ~]# tar -cvf japan.tar japan
japan/
japan/japan.tar

然后再用相关的压缩命令即可

[root@localhost ~]# bzip2 japan.tar
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog japan japan2
japan.tar.bz2

总结一下就是先打包后压缩——完美解决无法压缩目录的问题——但是麻烦(一会儿说)

–解压缩

同样以栗子来说

[root@localhost ~]# bunzip2 japan.tar.bz2
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog japan japan2 japan.tar
[root@localhost ~]# tar -xvf japan.tar
japan/
japan/japan.tar

解压缩-》解打包——tar -xvf tar文件

–直接实现

tar -zcvf 压缩文件.tar.gz 源文件

tar -jcvf 压缩文件.tar.bz2 源文件

[root@localhost ~]# rm -f japan.tar
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog japan japan2
[root@localhost ~]# tar -zcvf jp.tar.gz japan
japan/
japan/japan.tar
[root@localhost ~]# tar -jcvf jp.tar.gz japan
japan/
japan/japan.tar
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog japan japan2 jp.tar.gz
[root@localhost ~]# tar -jcvf jp.tar.bz2 japan
japan/
japan/japan.tar
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog japan japan2 jp.tar.bz2
jp.tar.gz

解压缩的时候还可以选择解压缩位置

[root@localhost ~]# tar -jxvf jp.tar.bz2 -C /tmp/
japan/
japan/japan.tar

注意位置选项书写的位置

–高级压缩

tar -jcvf /tmp/jp.tar.bz2 japan japan2

这里把压缩文件存到指定目录,而且压缩的是两个目录的文件

5.6 总结

最常用的是最后两个.tar.gz和.tar.bz2

 

6、关机与重启(服务器)

6.1 shutdown

shutdown [] 时间

-c 取消

-h 关机

-r 重启

举个栗子

[root@localhost ~]# date
2015年 11月 25日 星期三 08:37:52 CST
[root@localhost ~]# shutdown -r 12:20

Broadcast message from root@localhost.localdomain
(/dev/pts/0) at 8:38 …

The system is going down for reboot in 222 minutes!
shuit^H^H^H^H^H^H^H^H^H^Cshutdown: Shutdown cancelled
[root@localhost ~]# shutdown -r 12:20 &
[1] 25959
[root@localhost ~]#
Broadcast message from root@localhost.localdomain
(/dev/pts/0) at 8:39 …

The system is going down for reboot in 221 minutes!

这个例子是说:显示当前时间,然后设定关机时间,然后ctrl+c退出,然后同样的命令加&,让这条命令不影响以后的使用,两次enter回来

注意,虚拟机的时间是不确定的

[root@localhost ~]# shutdown -c
shutdown: Shutdown cancelled
[1]+ Done shutdown -r 12:20

然后shutdown -c 取消上一个关机的命令

标准的关机命令是shutdown -r now

注意,一般不要关机,而是用-r重启比较好——服务器在远端

而且,鉴于该命令的安全和日期保存等,一般关机重启都是用shutdown命令

6.2 其他关机命令

halt,poweroff,init 0

不是很安全,一般不要用

6.3 其他重启命令

reboot 可以用

init 6

6.4 退出登录命令

logout

使用完远程工具,最好不要直接关闭——不然用户会卡住,造成不必要的问题

这里的logout就是注销

 

7、其他常用命令

7.1 挂载命令

简单的理解就是分配盘符

7.1.1 查询与自动挂载

mount

[root@localhost ~]# mount
/dev/sda5 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)
/dev/sda2 on /home type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

很简单,就是目录+类型+权限。就看懂根分区,home和boot就行了。

mount -a 实现自动挂载,依据是/etc/fstab这个文件的配置

用vi命令看一下

#
# /etc/fstab
# Created by anaconda on Thu Sep 24 21:12:39 2015
#
# Accessible filesystems, by reference, are maintained under
‘/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more
info
#
UUID=81a087d8-5e03-442d-a138-a0a5b16a0e4c / ext4 defaults 1 1
UUID=e27af68c-c4a8-4982-b81e-7c1ca4ef5eaa /boot ext4 defaults 1 2
UUID=04f853b6-845f-459f-a858-1f84d6a98ce7 /home ext4 defaults 1 2
UUID=8315579c-295e-4254-9c27-1c30894b4544 swap 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
大概看看就行,暂时不要动。

假设想自动挂载U盘,不建议,因为系统每次都要自动挂载,如果没有找到设备,就会系统崩溃。

7.1.2 挂载命令格式

mount [-t 文件系统] [-o 特殊选项] 设备 文件名 挂载点

文件系统-ext4,没写的话,默认为iso9660

特殊选项比较多,但每一个都有默认值

[root@localhost ~]# cd /home
[root@localhost home]# vi hello.sh
[root@localhost home]# chmod 755 hello.sh
[root@localhost home]# ll
总用量 20
-rwxr-xr-x. 1 root root 36 11月 25 09:19 hello.sh
drwx——. 2 root root 16384 9月 24 21:09 lost+found
[root@localhost home]# ./hello.sh
hello cangls!!!
[root@localhost home]# hello.
-bash: hello.: command not found
[root@localhost home]# /home/hello.sh
hello cangls!!!

可以拿这个文件去做权限等的实验

7.1.3 挂载光盘

第一步:在虚拟机中放一个iso镜像,相当于放个光盘先。记得在虚拟机中的已连接打勾

第二步:建立挂载点(盘符)

任何空目录就可以作为盘符

[root@localhost home]# ls /
bin cgroup etc lib media mnt opt root selinux sys usr
boot dev home lost+found misc net proc sbin srv tmp var

这里的mnt常作为U盘光盘等的挂载点。

[root@localhost ~]# mkdir /mnt/cdrom

第三步:执行挂载命令

[root@localhost ~]# mount -t iso9660 /dev/sr0 /mnt/cdrom
mount: block device /dev/sr0 is write-protected, mounting read-only

这样其实就ok了

–读取

[root@localhost ~]# cd /mnt/cdrom
[root@localhost cdrom]# ls
AUTHORS ChangeLog COPYING isolinux user_guide.html user_guide.tex

7.1.4 卸载命令

用于弹出光盘

umount 设备名或者挂载点

[root@localhost cdrom]# umount /mnt/cdrom/
umount: /mnt/cdrom: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))

发现设备正忙。。。因为当前用户就在当前目录下。。。。。。。

[root@localhost cdrom]# cd
[root@localhost ~]# umount /mnt/cdrom/

注意,卸载命令是必须有的。

7.1.5 U盘的挂载

光盘的设备是一直都在/dev的,sr0

[root@localhost ~]# ls /dev/
agpgart dvd loop1 net ram13 scd0 systty tty21 tty36 tty50 tty8 vcs6
autofs dvdrw loop2 network_latency ram14 sda tty tty22 tty37 tty51 tty9
vcsa
block fb loop3 network_throughput ram15 sda1 tty0 tty23 tty38 tty52
ttyS0 vcsa1
bsg fb0 loop4 null ram2 sda2 tty1 tty24 tty39 tty53 ttyS1 vcsa2
btrfs-control fd loop5 nvram ram3 sda3 tty10 tty25 tty4 tty54 ttyS2
vcsa3
bus full loop6 oldmem ram4 sda4 tty11 tty26 tty40 tty55 ttyS3 vcsa4
cdrom fuse loop7 parport0 ram5 sda5 tty12 tty27 tty41 tty56 urandom
vcsa5
cdrw hidraw0 lp0 port ram6 sg0 tty13 tty28 tty42 tty57 usbmon0 vcsa6
char hidraw1 lp1 ppp ram7 sg1 tty14 tty29 tty43 tty58 usbmon1
vga_arbiter
console hpet lp2 ptmx ram8 shm tty15 tty3 tty44 tty59 usbmon2 zero
core hugepages lp3 pts ram9 snapshot tty16 tty30 tty45 tty6 vcs
cpu hvc0 MAKEDEV ram0 random snd tty17 tty31 tty46 tty60 vcs1
cpu_dma_latency input mapper ram1 raw sr0 tty18 tty32 tty47 tty61
vcs2
crash kmsg mcelog ram10 root stderr tty19 tty33 tty48 tty62 vcs3
disk log mem ram11 rtc stdin tty2 tty34 tty49 tty63 vcs4
dmmidi loop0 midi ram12 rtc0 stdout tty20 tty35 tty5 tty7 vcs5

U盘则是看成是一个硬盘。插入后,被认为是新的一块硬盘。要查看一下

[root@localhost ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000018cb

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 281 2048000 83 Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3 281 409 1024000 82 Linux swap / Solaris
Partition 3 does not end on cylinder boundary.
/dev/sda4 409 1306 7207936 5 Extended
/dev/sda5 409 1306 7206912 83 Linux

fdisk -l 看一下已经识别的硬盘。

一般来说,U盘是sdb1

挂载

mount -t vfat /dev/sdb1 /mnt/usb/

注意,vfat类型就是fat32,linux系统默认不支持ntfs。所以,移动硬盘和ntfs的U盘是无法直接挂载的。好在大多数U盘是fat32的文件系统。

解决办法是安装驱动——NTFS 3G,但是只能是只读的。想写,只能修改内核。

实际上,都直接网络传输了。

7.2 用户登录查看命令

–w

查看用户的登录信息

[root@localhost ~]# w
10:04:45 up 1:10, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.2.111 08:59 0.00s 0.56s 0.21s w

分别是系统时间,使用的时间,已登录用户,三次时间点的平均负载。

然后是,用户名,登录地点(/0是远程),

–who

[root@localhost ~]# who
root pts/0 2015-11-25 08:59 (192.168.2.111)

不用解释了吧,只是w要多看一点负载

–last

[root@localhost ~]# who
root pts/0 2015-11-25 08:59 (192.168.2.111)
[root@localhost ~]# last
root pts/0 192.168.2.111 Wed Nov 25 08:59 still logged in
root pts/0 192.168.2.111 Wed Nov 25 08:55 – 08:59 (00:03)
reboot system boot 2.6.32-504.el6.i Wed Nov 25 08:54 – 10:11 (01:16)
root pts/0 192.168.2.111 Wed Nov 25 06:26 – down (02:26)
root pts/0 192.168.1.101 Wed Nov 25 06:09 – 06:22 (00:13)
root tty4 Wed Nov 25 06:02 – down (02:51)
root tty1 Wed Nov 25 05:09 – down (03:44)
reboot system boot 2.6.32-504.el6.i Wed Nov 25 05:05 – 08:53 (03:48)
root pts/0 192.168.0.235 Wed Nov 25 05:00 – down (00:03)
root tty1 Wed Nov 25 04:43 – down (00:20)
reboot system boot 2.6.32-504.el6.i Wed Nov 25 04:42 – 05:04 (00:21)
root tty1 Thu Sep 24 22:24 – down (00:01)
reboot system boot 2.6.32-504.el6.i Thu Sep 24 22:24 – 22:26 (00:02)

wtmp begins Thu Sep 24 22:24:03 2015

查看过去的登录用户信息

–lastlog

[root@localhost ~]# lastlog
用户名 端口 来自 最后登陆时间
root pts/0 192.168.2.111 三 11月 25 08:59:28 +0800 2015
bin **从未登录过**
daemon **从未登录过**
adm **从未登录过**
lp **从未登录过**
sync **从未登录过**
shutdown **从未登录过**
halt **从未登录过**
mail **从未登录过**
uucp **从未登录过**
operator **从未登录过**
games **从未登录过**
gopher **从未登录过**
ftp **从未登录过**
nobody **从未登录过**
dbus **从未登录过**
vcsa **从未登录过**
rpc **从未登录过**
haldaemon **从未登录过**
ntp **从未登录过**
saslauth **从未登录过**
postfix **从未登录过**
abrt **从未登录过**
rpcuser **从未登录过**
nfsnobody **从未登录过**
sshd **从未登录过**
tcpdump **从未登录过**
oprofile **从未登录过**

列出所有用户,并显示他们的状态


果然没睡好是不行的,回去休息了。明天还要改报告和综述,不知道有没机会学习了。bye

相关文章

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注