1. man 查询帮助
[root@oldboy60service01 ~]# man cp
CP(1) User Commands
NAME
cp - copy files and directories
SYNOPSIS
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
2. pwd 显示当前路径
[root@oldboy60service01 ~]# pwd
/root
[root@oldboy60service01 ~]#
3. cd 切换目录
[root@oldboy60service01 /]# cd /etc
[root@oldboy60service01 etc]#
[root@oldboy60service01 etc]#
cd .. 切换上一级目录
[root@oldboy60service01 /]#
[root@oldboy60service01 /]# cd /etc/sysconfig
[root@oldboy60service01 sysconfig]# cd ..
[root@oldboy60service01 etc]# cd ..
[root@oldboy60service01 /]# cd ..
[root@oldboy60service01 /]#
cd . 保持当前目录不变
[root@oldboy60service01 /]#
[root@oldboy60service01 /]# cd /etc/sysconfig
[root@oldboy60service01 sysconfig]# cd .
[root@oldboy60service01 sysconfig]# cd ..
[root@oldboy60service01 etc]# cd .
[root@oldboy60service01 etc]#
cd ~=== cd 返回当前用户老家
[root@oldboy60service01 etc]#
[root@oldboy60service01 etc]# cd ~
[root@oldboy60service01 ~]#
cd ~ === cd
[root@oldboy60service01 ~]#
[root@oldboy60service01 ~]# cd /etc/sysconfig
[root@oldboy60service01 sysconfig]# cd ~
[root@oldboy60service01 ~]# cd /etc/sysconfig
[root@oldboy60service01 sysconfig]# cd
[root@oldboy60service01 ~]#
cd - 返回到上一次的目录
[root@oldboy60service01 ~]#
[root@oldboy60service01 ~]#
[root@oldboy60service01 ~]# cd /etc/sysconfig/network-scripts
[root@oldboy60service01 network-scripts]# cd -
/root
[root@oldboy60service01 ~]#
[root@oldboy60service01 ~]# cd -
/etc/sysconfig/network-scripts
[root@oldboy60service01 network-scripts]#
[root@oldboy60service01 network-scripts]# cd -
/root
[root@oldboy60service01 ~]#
[root@oldboy60service01 ~]#
可以来回切换上一次的目录
4. tree 以树形结构显示目录
[root@oldboy60service01 ~]# tree -L 1 /
/
├── bin -> usr/bin
├── boot
├── dev
├── etc
├── home
├── lib -> usr/lib
├── lib64 -> usr/lib64
├── media
├── mnt
├── opt
├── proc
├── root
├── run
├── sbin -> usr/sbin
├── srv
├── sys
├── tmp
├── usr
└── var
19 directories, 0 files
[root@oldboy60service01 ~]#
显示根下第一层目录
tree -L 1 -d /
[root@oldboy60service01 ~]# tree -L 1 -d /
/
├── bin -> usr/bin
├── boot
├── dev
├── etc
├── home
├── lib -> usr/lib
├── lib64 -> usr/lib64
├── media
├── mnt
├── opt
├── proc
├── root
├── run
├── sbin -> usr/sbin
├── srv
├── sys
├── tmp
├── usr
└── var
19 directories
[root@oldboy60service01 ~]#
只显示根下的第一层目录 -L 1
5. mkdir 创建目录
[root@oldboy60service01 ~]# mkdir /oldboy
[root@oldboy60service01 ~]# ls
anaconda-ks.cfg
[root@oldboy60service01 ~]#
[root@oldboy60service01 ~]# ls /
bin dev home lib64 mnt opt root sbin sys usr
boot etc lib media oldboy proc run srv tmp var
[root@oldboy60service01 ~]#
mkdir -p /oldboy/lidao/a/b/c/e/f/g
[root@oldboy60service01 ~]# tree /oldboy
/oldboy
└── lidao
└── a
└── b
└── c
└── d
└── e
└── f
└── g
8 directories, 0 files
[root@oldboy60service01 ~]# ls -l /oldboy
total 0
drwxr-xr-x. 3 root root 15 Apr 7 13:33 lidao
[root@oldboy60service01 ~]#
创建多层目录 -p
mkdir -p /oldboy/alex/lidao /alex/oldboy/dir
[root@oldboy60service01 ~]# mkdir -p /oldboy/alex/lidao /alex/oldboy/dir
[root@oldboy60service01 ~]# ls -l /oldboy/alex
total 0
drwxr-xr-x. 2 root root 6 Apr 7 14:22 lidao
[root@oldboy60service01 ~]# ls -l /alex/oldboy
total 0
drwxr-xr-x. 2 root root 6 Apr 7 14:22 dir
[root@oldboy60service01 ~]#
创建多个目录用 ‘ 空格 ’ 分开
6. ls 显示目录
[root@oldboy60service01 ~]#
[root@oldboy60service01 ~]# ls /
bin dev home lib64 mnt opt root sbin sys usr
boot etc lib media oldboy proc run srv tmp var
[root@oldboy60service01 ~]#
ls -l /
[root@oldboy60service01 ~]# ls -l /
total 16
lrwxrwxrwx. 1 root root 7 Mar 26 15:31 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 Mar 26 15:37 boot
drwxr-xr-x. 19 root root 3200 Apr 7 10:10 dev
drwxr-xr-x. 79 root root 8192 Apr 7 12:11 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Mar 26 15:31 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 26 15:31 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 3 root root 35 Apr 7 14:09 oldboy
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 110 root root 0 Apr 7 10:09 proc
dr-xr-x---. 2 root root 114 Mar 26 15:37 root
drwxr-xr-x. 25 root root 660 Apr 7 12:11 run
lrwxrwxrwx. 1 root root 8 Mar 26 15:31 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Apr 7 10:09 sys
drwxrwxrwt. 8 root root 128 Apr 7 12:11 tmp
drwxr-xr-x. 13 root root 155 Mar 26 15:31 usr
drwxr-xr-x. 19 root root 267 Mar 26 15:37 var
[root@oldboy60service01 ~]#
显示目录下的详细信息 -l
ls -lt /
[root@oldboy60service01 ~]# ls -lt /
total 16
drwxr-xr-x. 3 root root 19 Apr 7 13:33 oldboy
drwxr-xr-x. 25 root root 660 Apr 7 12:11 run
drwxrwxrwt. 8 root root 128 Apr 7 12:11 tmp
drwxr-xr-x. 79 root root 8192 Apr 7 12:11 etc
drwxr-xr-x. 19 root root 3200 Apr 7 10:10 dev
dr-xr-xr-x. 13 root root 0 Apr 7 10:09 sys
dr-xr-xr-x. 112 root root 0 Apr 7 10:09 proc
drwxr-xr-x. 19 root root 267 Mar 26 15:37 var
dr-xr-x---. 2 root root 114 Mar 26 15:37 root
dr-xr-xr-x. 5 root root 4096 Mar 26 15:37 boot
drwxr-xr-x. 13 root root 155 Mar 26 15:31 usr
lrwxrwxrwx. 1 root root 7 Mar 26 15:31 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 26 15:31 lib64 -> usr/lib64
lrwxrwxrwx. 1 root root 8 Mar 26 15:31 sbin -> usr/sbin
lrwxrwxrwx. 1 root root 7 Mar 26 15:31 bin -> usr/bin
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
[root@oldboy60service01 ~]#
-t 按照修改时间进行排序,ls命令默认按照字母排序
-r逆序排序
[root@oldboy60service01 ~]# ls -r /
var tmp srv run proc oldboy media lib etc boot
usr sys sbin root opt mnt lib64 home dev bin
[root@oldboy60service01 ~]# ls -t /
oldboy tmp dev proc root usr lib64 bin media opt
run etc sys var boot lib sbin home mnt srv
[root@oldboy60service01 ~]#
ls -lrt 显示目录下面最近被修改的文件
[root@oldboy60service01 ~]# ls -ltr /
total 16
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Mar 26 15:31 bin -> usr/bin
lrwxrwxrwx. 1 root root 8 Mar 26 15:31 sbin -> usr/sbin
lrwxrwxrwx. 1 root root 9 Mar 26 15:31 lib64 -> usr/lib64
lrwxrwxrwx. 1 root root 7 Mar 26 15:31 lib -> usr/lib
drwxr-xr-x. 13 root root 155 Mar 26 15:31 usr
dr-xr-xr-x. 5 root root 4096 Mar 26 15:37 boot
dr-xr-x---. 2 root root 114 Mar 26 15:37 root
drwxr-xr-x. 19 root root 267 Mar 26 15:37 var
dr-xr-xr-x. 110 root root 0 Apr 7 10:09 proc
dr-xr-xr-x. 13 root root 0 Apr 7 10:09 sys
drwxr-xr-x. 19 root root 3200 Apr 7 10:10 dev
drwxr-xr-x. 79 root root 8192 Apr 7 12:11 etc
drwxrwxrwt. 8 root root 128 Apr 7 12:11 tmp
drwxr-xr-x. 25 root root 660 Apr 7 12:11 run
drwxr-xr-x. 3 root root 19 Apr 7 13:33 oldboy
[root@oldboy60service01 ~]#
查找目录下面最新的修改过的文件
ls -a 显示目录下所有文件包含隐藏文件
[root@oldboy60service01 ~]# ll
total 4
-rw-------. 1 root root 1507 Mar 26 15:37 anaconda-ks.cfg
drwxr-xr-x. 2 root root 24 Apr 7 19:15 oldboy
[root@oldboy60service01 ~]# ls -la
total 32
dr-xr-x---. 3 root root 165 Apr 8 17:50 .
dr-xr-xr-x. 19 root root 249 Apr 7 17:27 ..
-rw-------. 1 root root 1507 Mar 26 15:37 anaconda-ks.cfg
-rw-------. 1 root root 1651 Apr 7 23:20 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
drwxr-xr-x. 2 root root 24 Apr 7 19:15 oldboy
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
-rw-------. 1 root root 623 Apr 8 17:50 .viminfo
[root@oldboy60service01 ~]#
ls -d 只显示目录本身的信息(把目录当做文件)
[root@oldboy60service01 ~]# ls -l /tmp
total 0
drwx------. 2 root root 6 Apr 8 17:46 vmware-root_6143-1983851947
drwx------. 2 root root 6 Apr 7 10:10 vmware-root_6237-1723208785
[root@oldboy60service01 ~]# ls -ld /tmp
drwxrwxrwt. 9 root root 163 Apr 8 18:48 /tmp
[root@oldboy60service01 ~]#
7.touch 创建文件,修改文件的时间戳
[root@oldboy60service01 ~]# touch /oldboy/alex.txt
[root@oldboy60service01 ~]# ls -l /oldboy
total 0
-rw-r--r--. 1 root root 0 Apr 7 14:09 alex.txt
drwxr-xr-x. 3 root root 15 Apr 7 13:33 lidao
[root@oldboy60service01 ~]#
touch /oldboy/lidao.txt /alex/lidao.txt
[root@oldboy60service01 ~]# touch /oldboy/lidao.txt /alex/lidao.txt
[root@oldboy60service01 ~]# ls -l /oldboy
total 0
drwxr-xr-x. 3 root root 19 Apr 7 14:22 alex
-rw-r--r--. 1 root root 0 Apr 7 14:09 alex.txt
drwxr-xr-x. 3 root root 15 Apr 7 13:33 lidao
-rw-r--r--. 1 root root 0 Apr 7 14:28 lidao.txt
[root@oldboy60service01 ~]# ls -l /alex/lidao.txt
-rw-r--r--. 1 root root 0 Apr 7 14:28 /alex/lidao.txt
[root@oldboy60service01 ~]#
创建多个文件用 ‘ 空格 ’ 分开
8. { .. }生成序列
[root@oldboy60service01 ~]# cd /oldboy
[root@oldboy60service01 oldboy]# ls
alex alex.txt lidao lidao.txt
[root@oldboy60service01 oldboy]# touch {a..z}
[root@oldboy60service01 oldboy]# ls
a alex.txt c e g i k lidao m o q s u w y
alex b d f h j l lidao.txt n p r t v x z
[root@oldboy60service01 oldboy]#
touch /oldboy/oldboy{01..10}txt
[root@oldboy60service01 oldboy]# touch /oldboy/oldboy{01..10}txt
[root@oldboy60service01 oldboy]# ls /oldboy
a c g k m oldboy02txt oldboy06txt oldboy10txt s w
alex d h l n oldboy03txt oldboy07txt p t x
alex.txt e i lidao o oldboy04txt oldboy08txt q u y
b f j lidao.txt oldboy01txt oldboy05txt oldboy09txt r v z
[root@oldboy60service01 oldboy]#
9.echo 与{ .. }配合使用方便测试,显示到屏幕
[root@oldboy60service01 oldboy]# echo oldboy{01..10}.txt
oldboy01.txt oldboy02.txt oldboy03.txt oldboy04.txt oldboy05.txt oldboy06.txt oldboy07.txt oldboy08.txt oldboy09.txt oldboy10.txt
[root@oldboy60service01 oldboy]#
生成有序的,有规律的文件
(1). { , ,} 例:echo lidao.{a,c}.bak
[root@oldboy60service01 oldboy]# echo lidao.{a,c}.bak
lidao.a.bak lidao.c.bak
[root@oldboy60service01 oldboy]#
生成无序的,有相同的文件
(2). >
重定向符合 标准输出重定向 先清空,再写入内容
[root@oldboy60service01 ~]# cat /oldboy
1
23
456
7890
[root@oldboy60service01 ~]# echo oldboy >/oldboy
[root@oldboy60service01 ~]# cat /oldboy
oldboy
[root@oldboy60service01 ~]#
(3). >>
追加重定向 标准输出追加重定向 不清空,把信息写入到文件结尾
[root@oldboy60service01 ~]# echo oldboy123oldboy123 >>/oldboy
[root@oldboy60service01 ~]# cat /oldboy
oldboyoldboy
oldboy123oldboy123
[root@oldboy60service01 ~]#
(4). echo -n 不显示回车,用于编写脚本
[root@oldboy60service01 ~]# echo -n /oldboy
/oldboy[root@oldboy60service01 ~]#
[root@oldboy60service01 ~]#
10. cp 复制,备份文件
[root@oldboy60service01 oldboy]# cd /alex
[root@oldboy60service01 alex]# ls
lidao.txt oldboy
[root@oldboy60service01 alex]# cp /oldboy/oldboy02txt /alex/
[root@oldboy60service01 alex]# ls
lidao.txt oldboy oldboy02txt
[root@oldboy60service01 alex]# cd /oldboy
[root@oldboy60service01 oldboy]# ls
a c g k m oldboy02txt oldboy06txt oldboy10txt s w
alex d h l n oldboy03txt oldboy07txt p t x
alex.txt e i lidao o oldboy04txt oldboy08txt q u y
b f j lidao.txt oldboy01txt oldboy05txt oldboy09txt r v z
[root@oldboy60service01 oldboy]#
[root@oldboy60service01 oldboy]# ls
a c g k m oldboy02txt oldboy06txt oldboy10txt s w
alex d h l n oldboy03txt oldboy07txt p t x
alex.txt e i lidao o oldboy04txt oldboy08txt q u y
b f j lidao.txt oldboy01txt oldboy05txt oldboy09txt r v z
[root@oldboy60service01 oldboy]# cp /oldboy/lidao.txt /oldboy/lidao.txt.bak
[root@oldboy60service01 oldboy]# ls
a c g k lidao.txt.bak oldboy01txt oldboy05txt oldboy09txt r v z
alex d h l m oldboy02txt oldboy06txt oldboy10txt s w
alex.txt e i lidao n oldboy03txt oldboy07txt p t x
b f j lidao.txt o oldboy04txt oldboy08txt q u y
[root@oldboy60service01 oldboy]#
cp -r 复制目录(默认无法复制目录)
[root@oldboy60service01 ~]# cp -r /oldboy/ /zhang/
[root@oldboy60service01 ~]# cd zhang
[root@oldboy60service01 zhang]# ls -l
total 4
drwxr-xr-x. 4 root root 4096 Apr 7 16:55 oldboy
[root@oldboy60service01 zhang]# cd /zhang/oldboy
[root@oldboy60service01 oldboy]# ls
a c g k lidao.txt.bak oldboy01txt oldboy05txt oldboy09txt r v z
alex d h l m oldboy02txt oldboy06txt oldboy10txt s w
alex.txt e i lidao n oldboy03txt oldboy07txt p t x
b f j lidao.txt o oldboy04txt oldboy08txt q u y
cp -a 复制的时候可以复制目录(-a === -pdr)
[root@oldboy60service01 oldboy]# cp -a /zhang/ /alex/
[root@oldboy60service01 oldboy]# cd /alex
[root@oldboy60service01 alex]# ls
lidao.txt oldboy oldboy02txt zhang
[root@oldboy60service01 alex]#
-p 复制的时候保持属性不变
-d 与软连接有关
11. mv 移动,重命名
[root@oldboy60service01 alex]# mv /alex/zhang /etc/
[root@oldboy60service01 alex]# ls
lidao.txt oldboy oldboy02txt
[root@oldboy60service01 alex]# cd /etc/
[root@oldboy60service01 etc]# ls
abrt GeoIP.conf.default motd securetty
adjtime gnupg mtab security
aliases GREP_COLORS my.cnf selinux
aliases.db groff my.cnf.d services
alternatives group NetworkManager
gcrypt man_db.conf rsyslog.d zhang
gdbinit mke2fs.conf rwtab
gdbinit.d modprobe.d rwtab.d
GeoIP.conf modules-load.d sasl2
[root@oldboy60service01 etc]#
[root@oldboy60service01 etc]# cd /oldboy
[root@oldboy60service01 oldboy]# ls
a c g k lidao.txt.bak oldboy01txt oldboy05txt oldboy09txt r v z
alex d h l m oldboy02txt oldboy06txt oldboy10txt s w
alex.txt e i lidao n oldboy03txt oldboy07txt p t x
b f j lidao.txt o oldboy04txt oldboy08txt q u y
[root@oldboy60service01 oldboy]# mv a aaa
[root@oldboy60service01 oldboy]# ls
aaa c g k lidao.txt.bak oldboy01txt oldboy05txt oldboy09txt r v z
alex d h l m oldboy02txt oldboy06txt oldboy10txt s w
alex.txt e i lidao n oldboy03txt oldboy07txt p t x
b f j lidao.txt o oldboy04txt oldboy08txt q u y
[root@oldboy60service01 oldboy]#
12.rm 删除一个空目录或文件
[root@oldboy60service01 zhang]# cd oldboy
[root@oldboy60service01 oldboy]# ls
a c g k lidao.txt.bak oldboy01txt oldboy05txt oldboy09txt r v z
alex d h l m oldboy02txt oldboy06txt oldboy10txt s w
alex.txt e i lidao n oldboy03txt oldboy07txt p t x
b f j lidao.txt o oldboy04txt oldboy08txt q u y
[root@oldboy60service01 oldboy]# rm a
rm: remove regular empty file ‘a’? y
[root@oldboy60service01 oldboy]# ls
alex d h l m oldboy02txt oldboy06txt oldboy10txt s w
alex.txt e i lidao n oldboy03txt oldboy07txt p t x
b f j lidao.txt o oldboy04txt oldboy08txt q u y
c g k lidao.txt.bak oldboy01txt oldboy05txt oldboy09txt r v z
[root@oldboy60service01 oldboy]#
rm -f 强制删除不提示
[root@oldboy60service01 oldboy]# rm -f oldboy01txt
[root@oldboy60service01 oldboy]# ls
alex d h l m oldboy03txt oldboy07txt p t x
alex.txt e i lidao n oldboy04txt oldboy08txt q u y
b f j lidao.txt o oldboy05txt oldboy09txt r v z
c g k lidao.txt.bak oldboy02txt oldboy06txt oldboy10txt s w
[root@oldboy60service01 oldboy]#
rm -r 删除目录
[root@oldboy60service01 oldboy]# rm -fr oldboy
[root@oldboy60service01 oldboy]# ls
alex d h l m oldboy03txt oldboy07txt p t x
alex.txt e i lidao n oldboy04txt oldboy08txt q u y
b f j lidao.txt o oldboy05txt oldboy09txt r v z
c g k lidao.txt.bak oldboy02txt oldboy06txt oldboy10txt s w
[root@oldboy60service01 oldboy]# cd ..
[root@oldboy60service01 zhang]# rm -fr oldboy
[root@oldboy60service01 zhang]# ls
[root@oldboy60service01 zhang]# ls -l
total 0
[root@oldboy60service01 zhang]#
删除文件不要加 -r
13.关机
shutdown -h now === halt
shutdown -r === reboot
shutdown -c === ctrl+c
pweroff
14. 网络操作命令
ip a
[root@oldboy60service01 ~]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:df:d8:36 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::57bd:42c3:792:d30/64 scope link noprefixroute
valid_lft forever preferred_lft forever
15.vi 编辑命令
[root@oldboy60service01 oldboy]# vi oldboy.txt
~
~
~
~
~
"oldboy.txt" 0L, 0C
按 i 进入编辑模式
~
~
~
~
-- INSERT --
按esc 退出编辑模式
I am studing Linux 60 week
~
~
~
~
:wq 保存退出(冒号必须是英文)
I am studing Linux 60 week
~
~
~
~
:wq
:q 不保存退出
I am studing Linux 60 week
~
~
~
~
:q
:q! 强制退出
I am studing Linux 60 week
~
~
~
:q
:wq! 强制保存退出
I am studing Linux 60 week
~
~
~
~
~
:wq!
与 vim 的区别是 vi 的升级,相当于word
vi 文件编辑中断开连接故障
[root@oldboy60service01 ~]# vi /oldboy
突然断电后,连接重新打开
E325: ATTENTION
Found a swap file by the name "/.oldboy.swp"
owned by: root dated: Tue Apr 9 23:33:36 2019
file name: /oldboy
modified: YES
user name: root host name: oldboy60service01
process ID: 7753
While opening file "/oldboy"
dated: Thu Apr 11 21:48:01 2019
NEWER than swap file!
(1) Another program may be editing the same file. If this is the case,
be careful not to end up with two different instances of the same
file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r /oldboy"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file "/.oldboy.swp"
to avoid this message.
"/oldboy" 0L, 0C
Press ENTER or type command to continue
E325: ATTENTION(请注意,有故障)
Found a swap file by the name "/.oldboy.swp"
找到一个临时文件,临时文件叫 .oldboy.swp
原因1:编辑文件的时候突然断开,重新编辑出现的故障
原因2:多个窗口同时编辑1个文件
***解决方法***:rm -f .oldboy.swp (删除临时文件)
vi/vim执行过程
1.vi 打开文件oldboy
2.进入编辑模式编辑文件
3.编辑文件的时候,vi 会自动创建一个临时文件(oldboy)
4.编辑完成,vi会自动删除临时文件
5.退出 修改成功
再次打开要编辑的文件
[root@oldboy60service01 oldboy]# vi oldboy.txt
恢复编辑的文件直接按 回车
[root@oldboy60service01 oldboy]# vi oldboy.txt
E325: ATTENTION
Found a swap file by the name ".oldboy.txt.swp"
owned by: root dated: Thu Apr 11 22:15:06 2019
file name: ~root/oldboy/oldboy.txt
modified: YES
user name: root host name: oldboy60service01
process ID: 8299
While opening file "oldboy.txt"
dated: Sun Apr 7 19:15:08 2019
(1) Another program may be editing the same file. If this is the case,
be careful not to end up with two different instances of the same
file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r oldboy.txt"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".oldboy.txt.swp"
to avoid this message.
"oldboy.txt" 1L, 27C
Press ENTER or type command to continue
直接按 回车 恢复
I am studing Linux 60 week
~
~
~
~
~
直接 :q 退出
根据临时文件恢复,vim -r
[root@oldboy60service01 oldboy]# vim -r oldboy.txt
Using swap file ".oldboy.txt.swp"
Original file "~/oldboy/oldboy.txt"
Recovery completed. You should check if everything is OK.
(You might want to write out this file under another name
and run diff with the original file to check for changes)
You may want to delete the .swp file now.
Press ENTER or type command to continue
回车(恢复)
,I am studeing Linux 60 week
sdf
asdf
~
~
~
~
保存退出 :wq 恢复成功
实际工作当中不建议恢复,不知道是谁改了什么
最后,还是要删除临时文件,否则一直会在
rm -r .oldboy.txt.swp
16. cat 查看文件内容
[root@oldboy60service01 oldboy]# cat oldboy.txt
I am studing Linux 60 week
[root@oldboy60service01 oldboy]#
(1). cat -n 显示所有行编号(包括空格)
[root@oldboy60service01 ~]# 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@oldboy60service01 ~]#
[root@oldboy60service01 ~]# cat -n /etc/inittab
1 # inittab is no longer used when using systemd.
2 #
3 # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
4 #
5 # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
6 #
7 # systemd uses 'targets' instead of runlevels. By default, there are two main targets:
8 #
9 # multi-user.target: analogous to runlevel 3
10 # graphical.target: analogous to runlevel 5
11 #
12 # To view current default target, run:
13 # systemctl get-default
14 #
15 # To set a default target, run:
16 # systemctl set-default TARGET.target
17 #
[root@oldboy60service01 ~]#
(2). cat >>向文件中追加多好内容
[root@oldboy60service01 ~]# cat >>/root/oldboy/oldboy.txt< I
> am
> studing
> linux
> at
> oldboyedu.com
> EOF
[root@oldboy60service01 ~]# cat oldboy/oldboy.txt
I am studing Linux 60 week
I
am
studing
linux
at
oldboyedu.com
[root@oldboy60service01 ~]#
合并文件
root@oldboy60service01 ~]# cat 201906.txt 201906.txt >11.txt
[root@oldboy60service01 ~]# ls
11.txt 201906.txt anaconda-ks.cfg oldboy
[root@oldboy60service01 ~]# cat 11.txt
1
2
3
4
5
1
2
3
4
5
17.查看日志命令
(1). head -5 显示头部前5行(默认前10行)
[root@oldboy60service01 ~]# head -5 oldboy/oldboy.txt
I am studing Linux 60 week
I
am
studing
linux
[root@oldboy60service01 ~]#
(2).tail -5 显示文件的最后几行(默认最后10行)
[root@oldboy60service01 ~]# tail -5 oldboy/oldboy.txt
am
studing
linux
at
oldboyedu.com
[root@oldboy60service01 ~]#
(3).less 按页查看文件内容 (适用于打开大文件)
[root@oldboy60service01 ~]# less /etc/services
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994). Not all ports
# are included, only the more common ones.
#
# The latest IANA port assignments can be gotten from
# http://www.iana.org/assignments/port-numbers
# The Well Known Ports are those from 0 through 1023.
# The Registered Ports are those from 1024 through 49151
# The Dynamic and/or Private Ports are those from 49152 through 65535
#
# Each line describes one service, and is of the form:
#
# service-name port/protocol [aliases ...] [# comment]
tcpmux 1/tcp # TCP port service multiplexer
tcpmux 1/udp # TCP port service multiplexer
rje 5/tcp # Remote Job Entry
rje 5/udp # Remote Job Entry
echo 7/tcp
echo 7/udp
discard 9/tcp sink null
:
按 Q 退出
(4). more 按页查看文件内容(查看到最后一行之后自动退出)
[root@oldboy60service01 ~]# more /etc/services
18.Linux下面的设备必须要有挂载才能使用
mount /dev/cdrom /mnt/
挂载 给光盘 设置了个入口叫/mnt
[root@oldboy60service01 dev]# cd cdrom
-bash: cd: cdrom: Not a directory
[root@oldboy60service01 dev]# mount /dev/cdrom /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@oldboy60service01 dev]# ls /mnt
CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7
EFI images Packages RPM-GPG-KEY-CentOS-Testing-7
EULA isolinux repodata TRANS.TBL
19. rpm -ivh
-i 安装 -v显示 -h以人类可读形式显示
rpm -qa 查询软件是否安装
[root@oldboy60service01 dev]# rpm -qa yum
yum-3.4.3-161.el7.centos.noarch
[root@oldboy60service01 dev]#
rpm -ql telnet(查看软件包)
[root@oldboy60service01 dev]# rpm -ql yum
/usr/share/locale/cs/LC_MESSAGES/yum.mo
/usr/share/locale/da/LC_MESSAGES/yum.mo
/usr/share/locale/de/LC_MESSAGES/yum.mo
/usr/share/locale/el/LC_MESSAGES/yum.mo
/usr/share/locale/nl/LC_MESSAGES/yum.mo
/var/lib/yum/yumdb
[root@oldboy60service01 dev]#
20.查看Linux系统版本
[root@oldboy60service01 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@oldboy60service01 ~]# uname
Linux
[root@oldboy60service01 ~]# uname -r
3.10.0-957.el7.x86_64
重启网卡:systemctl restart network(生效)
[root@oldboy60service01 ~]# systemctl restart network
21.添加用户
useradd
[root@oldboy60service01 ~]# useradd zgy
22.设置用户密码
[root@oldboy60service01 ~]# passwd zgy
Changing password for user zgy.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
23.切换用户
[root@oldboy60service01 ~]# su - zgy
[zgy@oldboy60service01 ~]$
[oldboy@oldboy60service01 ~]$ su - root
Password:
Last login: Tue Jul 2 21:39:23 CST 2019 from 10.0.0.1 on pts/0
24.查看用户
[zgy@oldboy60service01 ~]$ whoami
zgy
25.退出当前用户
[zgy@oldboy60service01 ~]$ logout
[root@oldboy60service01 ~]#
26.设置运行级别
[root@oldboy60service01 ~]# systemctl get-default
graphical.target
[root@oldboy60service01 ~]# systemctl set-default multi-user.target
27.关闭selinux(永久)SELINUX=disabled 重启服务器生效
[root@oldboy60service01 ~]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
检查是否配置成功
root@oldboy60service01 ~]# grep 'disabled' /etc/selinux/config
# disabled - No SELinux policy is loaded.
SELINUX=disabled
临时关闭selinux (重启失效)
查看当前状态
[root@oldboy60service01 ~]# getenforce
Disabled
设置 0 关闭
[root@oldboy60service01 ~]# setenforce 0
usage: setenforce [ Enforcing | Permissive | 1 | 0 ]
28.防火墙firewalld(CentOS7),iptables(CentOS6)
查看防火墙状态
[root@oldboy60service01 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
临时关闭防火墙
[root@oldboy60service01 ~]# systemctl stop firewalld.service
开启防火墙
[root@oldboy60service01 ~]# systemctl start firewalld.service
linux7里要安装bash-completion(tab命令补全)
关闭firewalld.service开机自启动
[root@oldboy60service01 ~]# systemctl disable firewalld.service
检查是否运行
[root@oldboy60service01 ~]# systemctl is-active firewalld.service
unknown
检查开机是否自动运行
[root@oldboy60service01 ~]# systemctl is-enabled firewalld.service
disabled
29.修改字符集
[root@oldboy60service01 ~]# echo $LANG
en_US.UTF-8
临时修改
[root@oldboy60service01 ~]# export LANG=zh_CN.UTF-8
永久修改字符集
[root@oldboy60service01 ~]# cat /etc/locale.conf
LANG="en_US.UTF-8"
[root@oldboy60service01 ~]# vim /etc/locale.conf
修改完成后要生效
[root@oldboy60service01 ~]# source /etc/locale.conf
当字符集变成乱码修改
设置
[root@oldboy60service01 ~]# localectl set-locale LANG=en_US.UTF-8
检查
[root@oldboy60service01 ~]# echo $LANG
en_US.UTF-8
生效
[root@oldboy60service01 ~]# source /etc/locale.conf
30.生成数字序列
[root@oldboy60service01 ~]# seq 1 5 >201906.txt
1
2
3
4
5
31.查看日志显示文件内容
head 显示文件前3行
[root@sky ~]# head -3 /var/log/secure
Jun 29 18:53:27 oldboy60service01 sshd[6813]: Received signal 15; terminating.
Jun 30 22:20:57 oldboy60service01 polkitd[5392]: Loading rules from directory /etc/polkit-1/rules.d
Jun 30 22:20:57 oldboy60service01 polkitd[5392]: Loading rules from directory /usr/share/polkit-1/rules.d
tail 显示文件最后一行
[root@sky ~]# tail - 1/var/log/secure
==> standard input <==
less 分页显示文件内容
[root@sky ~]# less /var/log/secure
more 分页显示文件内容
[root@sky ~]# more /var/log/secure
32.tr 1对1替换
[root@zgy ~]# tr 'I' '@'
33.sed 取行,修改,替换
替换
[root@zgy ~/oldboy]# sed 's#I#*#g' oldboy.txt
* am studing Linux 60 week
*
am
studing
linux
at
oldboyedu.com
修改文件内容
[root@zgy ~/oldboy]# sed -i 's#I#*#g' oldboy.txt
[root@zgy ~/oldboy]# cat oldboy.txt
* am studing Linux 60 week
*
am
studing
linux
at
oldboyedu.com
34.创建链接
硬链接(备份)
[root@zgy ~/oldboy]# ln oldboy.txt oldboy.txt.bak
[root@zgy ~/oldboy]# ls
oldboy.txt oldboy.txt.bak
软链接(可以跨文件系统,磁盘分区)
[root@zgy ~/oldboy]# ln -s oldboy.txt oldboy.txt.soft
[root@zgy ~/oldboy]# ll
total 8
-rw-r--r-- 2 root root 64 Jul 3 21:26 oldboy.txt
-rw-r--r-- 2 root root 64 Jul 3 21:26 oldboy.txt.bak
lrwxrwxrwx 1 root root 10 Jul 3 21:34 oldboy.txt.soft -> oldboy.txt
35.file查看文件详细的类型
[root@zgy ~/oldboy]# file /bin/ls /etc/hosts
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=ceaf496f3aec08afced234f4f36330d3d13a657b, stripped
/etc/hosts: ASCII text
36.tar 打包压缩
[root@zgy ~/oldboy]# tar zcf /tmp/etc.tar.gz /etc
tar: Removing leading `/' from member names
[root@zgy ~/oldboy]# ll /tmp
total 9972
-rw-r--r-- 1 root root 10208595 Jul 3 21:57 etc.tar.gz
drwx------ 2 root root 6 Jun 30 22:20 vmware-root_5441-3888029643
drwx------ 2 root root 6 Jul 3 21:05 vmware-root_5558-2857042586
drwx------ 2 root root 6 Jul 1 22:04 vmware-root_5572-3100259664
查看压缩包
[root@zgy ~/oldboy]# tar tf /tmp/etc.tar.gz
etc/latrace.d/headers/syslog.h
etc/latrace.d/headers/term.h
etc/latrace.d/headers/termios.h
etc/latrace.d/headers/time.h
etc/latrace.d/headers/typedefs.h
解压
[root@zgy ~/oldboy]# tar xf /tmp/etc.tar.gz
[root@zgy ~/oldboy]# ls
etc oldboy.txt oldboy.txt.bak oldboy.txt.soft
[root@zgy ~/oldboy]# ll etc
total 1068
drwxr-xr-x 3 root root 101 Mar 26 15:32 abrt
-rw-r--r-- 1 root root 16 Mar 26 15:36 adjtime
-rw-r--r-- 1 root root 1518 Jun 7 2013 aliases
-rw-r--r-- 1 root root 12288 Mar 26 15:37 aliases.db
drwxr-xr-x 2 root root 236 Mar 26 15:33 alterna
解压到指定目录
[root@sky /etc]# find / -type f -name etc.tar.gz
/tmp/etc.tar.gz
[root@sky /etc]# ls /opt
[root@sky /etc]# tar xf /tmp/etc.tar.gz -C /opt
[root@sky /etc]# ls /opt
etc
解压文件当中某个文件
[root@sky /opt]# tar xf /tmp/etc.tar.gz etc/hosts etc/fstab
[root@sky /opt]# cd etc
[root@sky /opt/etc]# ls
fstab hosts
37.which显示命令绝对路径
[root@zgy ~/oldboy]# which awk sed
/usr/bin/awk
/usr/bin/sed
38.whereis显示命令及其相关路径
[root@zgy ~/oldboy]# whereis awk
awk: /usr/bin/awk /usr/libexec/awk /usr/share/awk /usr/share/man/man1/awk.1.gz
39.find查找文件
[root@zgy ~/oldboy]# find /etc -type f -name "hosts"
/etc/hosts
查找目录第一层
[root@zgy ~/oldboy]# find -maxdepth 1 -type f
./oldboy.txt
./oldboy.txt.bak
查找文件大小
[root@zgy ~/oldboy]# find -maxdepth 1 -type f -size +1M
[root@zgy ~/oldboy]# find -maxdepth 1 -type f -size -100k
./oldboy.txt
./oldboy.txt.bak
根据inode号查找文件
[root@sky ~/oldboy]# find /root/oldboy/ -inum 26375122
/root/oldboy/oldboy.txt
/root/oldboy/oldboy.txt.bak
找出inode号一样的文件
[root@sky ~/oldboy]# find /root/oldboy/ -samefile /root/oldboy/oldboy.txt
/root/oldboy/oldboy.txt
/root/oldboy/oldboy.txt.bak
找出7天之前的文件
[root@sky /etc]# find -type f -mtime +7 -size +1M -name "*.txt"
40.查看block使用情况
[root@sky ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 8.8G 1.6G 7.3G 18% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 197M 105M 93M 54% /boot
tmpfs 98M 0 98M 0% /run/user/0
41.查看inode使用情况
[root@sky ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda3 4615680 61853 4553827 2% /
devtmpfs 121762 377 121385 1% /dev
tmpfs 124487 1 124486 1% /dev/shm
tmpfs 124487 708 123779 1% /run
tmpfs 124487 16 124471 1% /sys/fs/cgroup
/dev/sda1 102400 326 102074 1% /boot
tmpfs 124487 1 124486 1% /run/user/0
42.date命令
显示日期
[root@sky /etc]# date +%F
2019-07-04
[root@sky /etc]# date +%Y-%m-%d
2019-07-04
显示时分秒
[root@sky /etc]# date +%T
22:16:53
[root@sky /etc]# date +%H:%M:%S
22:17:29
显示周
[root@sky /etc]# date +%w
4
显示年月日时间周几
[root@sky /etc]# date +%F_%H-%w
2019-07-04_22-4
根据描述显示
[root@sky /opt/etc]# date -d "-1day"
Wed Jul 3 22:34:24 CST 2019
[root@sky /opt/etc]# date -d "1day"
Fri Jul 5 22:37:22 CST 2019
设置时间
[root@sky /opt/etc]# date -s "20111111 11:11:11"
Fri Nov 11 11:11:11 CST 2011
[root@sky /opt/etc]# date
Fri Nov 11 11:11:19 CST 2011
自动同步时间
[root@sky /opt/etc]# ntpdate ntp1.aliyun.com
4 Jul 22:42:20 ntpdate[9061]: step time server 120.25.115.20 offset 241270045.406462 sec
[root@sky /opt/etc]# date
Thu Jul 4 22:42:25 CST 2019
43.别名
[root@sky ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
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'
临时配置别名(断开后失效)
[root@sky ~]# echo command bny
command bny
[root@sky ~]# alias rm='echo command bny'
[root@sky ~]# ls
11.txt 201906.txt anaconda-ks.cfg oldboy
[root@sky ~]# rm oldboy/*
command bny oldboy/etc oldboy/oldboy.txt oldboy/oldboy.txt.bak oldboy/oldboy.txt.soft
永久生效
[root@sky ~]# vim /etc/profile (最后一行生效)
38 pathmunge /usr/sbin
39 pathmunge /usr/local/sbin
40 else
41 pathmunge /usr/local/sbin after
42 pathmunge /usr/sbin after
43 fi
44
45 HOSTNAME=`/usr/bin/hostname 2>/dev/null`
46 HISTSIZE=1000
47 if [ "$HISTCONTROL" = "ignorespace" ] ; then
48 export HISTCONTROL=ignoreboth
...
75 unset i
76 unset -f pathmunge
77 export PS1='[\u@\h \w]\$ '
78 alias rm='echo command bny'
79
[root@sky ~]# source /etc/profile (生效)
[root@sky ~]# vim ~/.bashrc cp,mv,rm需要注释掉
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
44.修改文件权限
[root@sky ~/oldboy]# ls -l oldboy.txt
-rw-r--r-- 2 root root 64 Jul 3 21:26 oldboy.txt
[root@sky ~/oldboy]# chmod 755 oldboy.txt
[root@sky ~/oldboy]# ls -l oldboy.txt
-rwxr-xr-x 2 root root 64 Jul 3 21:26 oldboy.txt
[root@sky ~/oldboy]# ls -l oldboy.txt
-rw-r-xr-x 2 root root 64 Jul 3 21:26 oldboy.txt
[root@sky ~/oldboy]# chmod u+x,g-x,o-x oldboy.txt
[root@sky ~/oldboy]# ls -l oldboy.txt
-rwxr--r-- 2 root root 64 Jul 3 21:26 oldboy.txt
[root@sky ~/oldboy]# chmod +x oldboy.txt(x权限可以一起加,其他不能)
[root@sky ~/oldboy]# ls -l oldboy.txt
-rwxr-xr-x 2 root root 64 Jul 3 21:26 oldboy.txt