【Linux】Re02

一、运行启动级别

0 关机
1 单用户
2 多用户状态没有网络服务
3 多用户状态存在网络服务
4 系统未使用保留给用户
5 图形界面
6 重启

命令:

init [0 - 6]

图形化界面级别需要对应的程序支持

在Centos7中进行了简化处理

[root@localhost ~]# cat /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#

查看当前级别:

[root@localhost ~]# systemctl get-default
multi-user.target

二、找回密码

centos7

重启Linux时,进入这个界面按下TAB键取消时间限制,

选择第一项按E进入

找到linux16的这一行,UTF-8后面追加一段:

init=/bin/sh

然后使用快捷键进入单用户模式

Ctrl + X

进入单用户终端之后:

输入mount命令

mount -o remount,rw /

再输入密码更改命令进行密码重置

passwd

密码重置之后执行这个命令

touch /.authrelabel

最后执行

exec /sbin/init

等待系统加载后重启

三、帮助指令

man获取帮助信息

[root@localhost ~]# man
您需要什么手册页?

- - - - - - - - - - - - - - - - - - - - 
语法:
man [命令 / 配置文件]

- - - - - - - - - - - - - - - - - - - - 
演示案例:
查看 ls命令
man ls

help获取帮助信息【shell内置命令帮助】

[root@localhost ~]# help
GNU bash, 版本 4.2.46(2)-release (x86_64-redhat-linux-gnu)
这些 shell 命令是内部定义的。请输入 `help' 以获取一个列表.
输入 `help 名称' 以得到有关函数`名称'的更多信息.
使用 `info bash' 来获得关于 shell 的更多一般性信息
使用 `man -k' 或 `info' 来获取不在列表中的命令的更多信息.

名称旁边的星号 (*) 意味着该命令被禁用.

 job_spec [&]                                         history [-c] [-d 偏移量] [n] 或 history -anrw>
 (( 表达式 ))                                      if 命令; then 命令; [ elif 命令; then 命乾
 . 文件名 [参数]                                 jobs [-lnprs] [任务声明 ...] 或 jobs -x 命
 :                                                    kill [-s 信号声明 | -n 信号编号 | -信号>
 [ 参数... ]                                        let 参数 [参数 ...]
 [[ 表达式 ]]                                      local [option] 名称[=值] ...
 alias [-p] [名称[=值] ... ]                       logout [n]
 bg [任务声明 ...]                                mapfile [-n 计数] [-O 起始序号] [-s 计数]>
 bind [-lpvsPVS] [-m 键映射] [-f 文件名] [-q >  popd [-n] [+N | -N]
 break [n]                                            printf [-v var] 格式 [参数]
 builtin [shell 内嵌 [参数 ...]]                  pushd [-n] [+N | -N | 目录]
 caller [表达式]                                   pwd [-LP]
 casein [模式 [| 模式]...) 命令 ;;]... e>  read [-ers] [-a 数组] [-d 分隔符] [-i 缓冲>
 cd [-L|[-P [-e]]] [dir]                              readarray [-n 计数] [-O 起始序号] [-s 计擾
 command [-pVv] 命令 [参数 ...]                   readonly [-aAf] [name[=value] ...] or readonly -p>
 compgen [-abcdefgjksuv] [-o 选项]  [-A 动作] [>  return [n]
 complete [-abcdefgjksuv] [-pr] [-DE] [-o 选项] [>  select NAME [in 词语 ... ;] do 命令; done
 compopt [-o|+o 选项] [-DE] [名称 ...]            set [-abefhkmnptuvxBCHP] [-o option-name] [--] [a>
 continue [n]                                         shift [n]
 coproc [名称] 命令 [重定向]                   shopt [-pqsu] [-o] [选项名 ...]
 declare [-aAfFgilrtux] [-p] [name[=value] ...]       source 文件名 [参数]
 dirs [-clpv] [+N] [-N]                               suspend [-f]
 disown [-h] [-ar] [任务声明 ...]                 test [表达式]
 echo [-neE] [参数 ...]                             time [-p] 管道
 enable [-a] [-dnps] [-f 文件名] [名称 ...]      times
 eval [参数 ...]                                    trap [-lp] [[参数] 信号声明 ...]
 exec [-cl] [-a 名称] [命令 [参数 ...]] [重㼠 真
 exit [n]                                             type [-afptP] 名称 [名称 ...]
 export [-fn] [名称[=值] ...] 或 export -p        typeset [-aAfFgilrtux] [-p] name[=value] ...
 伪                                                  ulimit [-SHacdefilmnpqrstuvx] [限制]
 fc [-e 编辑器名] [-lnr] [起始] [终结] 或 >  umask [-p] [-S] [模式]
 fg [任务声明]                                    unalias [-a] 名称 [名称 ...]
 for 名称 [in 词语 ... ] ; do 命令; done        unset [-f] [-v] [名称 ...]
 for (( 表达式1; 表达式2; 表达式3 )); dountil 命令; do 命令; done
 function 名称 { 命令 ; } 或 name () { 命令 >  variables - 一些 shell 变量的名称和含义>
 getopts 选项字符串 名称 [参数]              wait [编号]
 hash [-lr] [-p 路径名] [-dt] [名称 ...]         while 命令; do 命令; done
 help [-dms] [模式 ...]                             { 命令 ; }

获取cd的帮助信息

[root@localhost ~]# help cd
cd: cd [-L|[-P [-e]]] [dir]
    Change the shell working directory.
    
    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.
    
    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.
    
    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.
    
    Options:
        -L    force symbolic links to be followed
        -P    use the physical directory structure without following symbolic
        links
        -e    if the -P option is supplied, and the current working directory
        cannot be determined successfully, exit with a non-zero status
    
    The default is to follow symbolic links, as if `-L' were specified.
    
    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.

四、文件目录操作

显示完整的当前工作路径

[root@localhost ~]# pwd
/root

显示当前路径下的所有目录和文件

[root@localhost /]# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
-a 参数将显示所有文件目录【包含隐藏的】

[root@localhost /]# ls -a
.   bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
..  boot  etc  lib   media  opt  root  sbin  sys  usr
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
-l 以列表形式展开

[root@localhost /]# ls -l
总用量 16
lrwxrwxrwx.   1 root root    7 11月 29 17:06 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 11月 29 17:12 boot
drwxr-xr-x.  20 root root 3240 11月 29 17:11 dev
drwxr-xr-x.  82 root root 8192 11月 29 17:12 etc
drwxr-xr-x.   2 root root    6 4月  11 2018 home
lrwxrwxrwx.   1 root root    7 11月 29 17:06 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 11月 29 17:06 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 4月  11 2018 media
drwxr-xr-x.   2 root root    6 4月  11 2018 mnt
drwxr-xr-x.   2 root root    6 4月  11 2018 opt
dr-xr-xr-x. 139 root root    0 11月 29 17:11 proc
dr-xr-x---.   2 root root  114 11月 29 17:10 root
drwxr-xr-x.  28 root root  800 11月 29 17:12 run
lrwxrwxrwx.   1 root root    8 11月 29 17:06 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 4月  11 2018 srv
dr-xr-xr-x.  13 root root    0 11月 29 17:11 sys
drwxrwxrwt.   9 root root  246 11月 29 17:12 tmp
drwxr-xr-x.  13 root root  155 11月 29 17:06 usr
drwxr-xr-x.  19 root root  267 11月 29 17:11 var
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
支持多参数合并命令

[root@localhost /]# ls -al
总用量 16
dr-xr-xr-x.  17 root root  224 11月 29 17:09 .
dr-xr-xr-x.  17 root root  224 11月 29 17:09 ..
lrwxrwxrwx.   1 root root    7 11月 29 17:06 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 11月 29 17:12 boot
drwxr-xr-x.  20 root root 3240 11月 29 17:11 dev
drwxr-xr-x.  82 root root 8192 11月 29 17:12 etc
drwxr-xr-x.   2 root root    6 4月  11 2018 home
lrwxrwxrwx.   1 root root    7 11月 29 17:06 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 11月 29 17:06 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 4月  11 2018 media
drwxr-xr-x.   2 root root    6 4月  11 2018 mnt
drwxr-xr-x.   2 root root    6 4月  11 2018 opt
dr-xr-xr-x. 139 root root    0 11月 29 17:11 proc
dr-xr-x---.   2 root root  114 11月 29 17:10 root
drwxr-xr-x.  28 root root  800 11月 29 17:12 run
lrwxrwxrwx.   1 root root    8 11月 29 17:06 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 4月  11 2018 srv
dr-xr-xr-x.  13 root root    0 11月 29 17:11 sys
drwxrwxrwt.   9 root root  246 11月 29 17:12 tmp
drwxr-xr-x.  13 root root  155 11月 29 17:06 usr
drwxr-xr-x.  19 root root  267 11月 29 17:11 var

变更当前的工作路径到指定位置

cd 位置

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

- - - - - - - - - - - - - - - - - - - - - - - - 
要注意 ~ 表示的是当前用户的目录
比如root用户是 /root
如果是其他用户,则是/home/用户名

[root@localhost ~]# pwd
/root
[root@localhost ~]# su user-01
[user-01@localhost root]$ cd ~
[user-01@localhost ~]$ pwd
/home/user-01
- - - - - - - - - - - - - - - - - - - - - - - - 
. 和 ..的意义
.表示当前目录
..表示上一级

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

目录创建

mkdir 文件目录名称或者完整路径名称

[root@localhost ~]# mkdir aaa
[root@localhost ~]# ls
aaa  anaconda-ks.cfg
[root@localhost ~]# mkdir /root/bbb
[root@localhost ~]# ls
aaa  anaconda-ks.cfg  bbb

- - - - - - - - - - - - - - - - - - - - - - - - - - - - 
-p 参数用于越级创建目录

[root@localhost ~]# mkdir aaa/bbb/ccc/ddd
mkdir: 无法创建目录"aaa/bbb/ccc/ddd": 没有那个文件或目录
[root@localhost ~]# mkdir -p aaa/bbb/ccc/ddd
[root@localhost ~]#

目录移除

rmdir 目录名称
该命令只能删除空目录,如果目录包含子目录或者文件将不会删除

- - - - - - - - - - - - - - - - - - - - - - - - - - 
[root@localhost ~]# rmdir aaa
rmdir: 删除 "aaa" 失败: 目录非空

[root@localhost ~]# rmdir aaa/bbb/ccc/ddd
[root@localhost ~]# cd aaa/bbb/ccc
[root@localhost ccc]# ls
[root@localhost ccc]# 

- - - - - - - - - - - - - - - - - - - - - - - - - - 

文件创建:

touch 文件名称  指定位置的文件名称

[root@localhost ~]# touch file1.txt
[root@localhost ~]# touch /root/file2.txt
[root@localhost ~]# ls
aaa  anaconda-ks.cfg  bbb  file1.txt  file2.txt

文件拷贝【复制粘贴】

cp 目标文件或者目录 指定目录

- - - - - - - - - - - - - - - - - - - - - - 
演示案例环境准备:
[root@localhost ~]# mkdir a
[root@localhost ~]# mkdir b
[root@localhost ~]# ls
a  anaconda-ks.cfg  b
[root@localhost ~]# touch a/c.txt
[root@localhost ~]# ls
a  anaconda-ks.cfg  b

- - - - - - - - - - - - - - - - - - - - - - 
将a目录下的c.txt复制到b目录中
[root@localhost ~]# cp a/c.txt b/
[root@localhost ~]# ll b
总用量 0
-rw-r--r--. 1 root root 0 11月 29 17:47 c.txt

- - - - - - - - - - - - - - - - - - - - - - 
复制整个a目录到b中去
[root@localhost ~]# cp -r a b/
[root@localhost ~]# ls
a  anaconda-ks.cfg  b
[root@localhost ~]# ll b
总用量 0
drwxr-xr-x. 2 root root 19 11月 29 17:48 a
-rw-r--r--. 1 root root  0 11月 29 17:47 c.txt
[root@localhost ~]# ll b/a/
总用量 0
-rw-r--r--. 1 root root 0 11月 29 17:48 c.txt

- - - - - - - - - - - - - - - - - - - - - - 
复制目录尽量使用完整路径和斜杠标注目录

文件移除

rm 移除文件或者目录

-r 递归删除
-f 不提示询问直接删除
- - - - - - - - - - - - - - - - 
[root@localhost ~]# ls
a  anaconda-ks.cfg  b  c

[root@localhost ~]# rm -rf a
[root@localhost ~]# rm -rf b
[root@localhost ~]# rm -rf c
[root@localhost ~]# ls
anaconda-ks.cfg

移动文件与重命名

mv 源文件名称 新文件名称
mv 目标文件 新位置

[root@localhost ~]# touch aaa.txt
[root@localhost ~]# ls
aaa.txt  anaconda-ks.cfg
[root@localhost ~]# mv aaa.txt bbb.txt
[root@localhost ~]# mkdir ccc
[root@localhost ~]# mv bbb.txt ccc/
[root@localhost ~]# ls
anaconda-ks.cfg  ccc
[root@localhost ~]# ls ccc
bbb.txt

移动目录注意的操作:
[root@localhost ~]# mkdir ddd
[root@localhost ~]# mv bbb ddd/
[root@localhost ~]# ls
anaconda-ks.cfg ddd
[root@localhost ~]# mkdir eee
[root@localhost ~]# mv ddd eee/fff
[root@localhost ~]# ls
anaconda-ks.cfg eee
[root@localhost ~]# ll eee
总用量 0
drwxr-xr-x. 3 root root 17 11月 29 18:03 fff

cat查看文件内容

[root@localhost ~]# cat aa.txt
hello

sadasd

dasdas

dasda
s
dasdas


dasdasdas
[root@localhost ~]# cat -n aa.txt
     1    hello
     2    
     3    sadasd
     4    
     5    dasdas
     6    
     7    dasda
     8    s
     9    dasdas
    10    
    11    
    12    dasdasdas

less查看文件内容

less 文件

向下翻页
pagedown键  space键

向上翻页
pageup键

向上一行 
↑键
向下一行
↓键

查找指定字符
/查找的字符 【先向下,按n向下继续查找,按N向上查找】
?查找的字符 【先向上,按n向下继续查找,按N向上查找】

退出浏览
q键

查看文件头部信息

head 文件名
查看文件前10行内容

head -n 行数 文件名
查看文件前指定行数的行内容

查看文件尾部信息

tail 文件名
查看文件尾部前10行的内容

tail -n 行数 文件名
查看文件尾部前指定行数的内容

写入命令

> 覆盖写入
>> 追加写入

一些用法

ls -l  > 文件
将展示的列表信息覆盖写入该文件中

ls -al >> 文件
将展示的列表所有信息追加写入该文件中

cat 文件1 > 文件2 
把文件1的内容覆写到文件2中

echo "内容" >> 文件
把输出的内容追加写入文件中

日历查看:

[root@localhost ~]# cal
     十一月 2020    
日 一 二 三 四 五 六
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
原文地址:https://www.cnblogs.com/mindzone/p/14055986.html