Linux操作系统常用命令

目录

 

日常操作命令

文件目录操作

查找命令

文件权限操作


日常操作命令

查看当前所在的工作目录的全路径,完整路径。

[root@xinxin001 dubbo-admin-tomcat]# pwd
/app/runtime/dubbo-admin-tomcat

查看当前系统的时间

[root@xinxin001 /]# date
2018年 10月 27日 星期六 14:18:04 CST
[root@xinxin001 /]# date +%Y-%m-%d
2018-10-27

显示当前时间减去一天的时间、减去一个月的时间、减去一年的时间。

[root@xinxin001 /]# date +%Y-%m-%d  --date="-1 day" 
2018-10-26
[root@xinxin001 /]# date +%Y-%m-%d  --date="-1 month" 
2018-09-27
[root@xinxin001 /]# date +%Y-%m-%d  --date="-1 year"                                                       
2017-10-27  

修改设置系统时间。

[root@xinxin001 /]# date -s "2017-10-01 11:12:13"
2017年 10月 01日 星期日 11:12:13 CST

查看有哪些用户登录到了服务器,有谁在线。

[root@xinxin001 ~]# who
root     :0           2018-10-27 14:14 (:0)
root     pts/0        2018-10-27 14:14 (:0)
root     pts/1        2018-10-27 14:14 (:0)
admin    pts/2        2017-10-01 11:31 (:1)
admin    :1           2017-10-01 11:31 (:1)
admin    pts/3        2017-10-01 11:31 (:1)

查看最近用户的登录历史记录。

[root@xinxin001 ~]# last
admin    pts/3        :1               Sun Oct  1 11:31   still logged in   
admin    pts/2        :1               Sun Oct  1 11:31   still logged in   
admin    :1           :1               Sun Oct  1 11:31   still logged in   
root     pts/2        :0               Thu Jul 28 16:12 - 16:12  (00:00)    
root     pts/1        :0               Sat Oct 27 14:14   still logged in   
root     pts/0        :0               Sat Oct 27 14:14   still logged in   
root     :0           :0               Sat Oct 27 14:14   still logged in   
reboot   system boot  3.10.0-862.el7.x Sat Oct 27 14:13 - 11:33 (-391+-2:-39
root     pts/1        :0               Thu Oct 18 23:26 - down   (01:15)    
root     pts/0        :0               Thu Oct 18 23:26 - down   (01:15)    
root     :0           :0               Thu Oct 18 23:26 - down   (01:16)    
reboot   system boot  3.10.0-862.el7.x Thu Oct 18 23:02 - 00:42  (01:40)    
root     pts/1        :0               Mon Oct 15 22:33 - 00:22  (0

 

使用root用户关闭服务器,关机。

立刻关机命令。

[root@xinxin001 /]# shutdown -h now

 其他关机命令,不安全,不建议使用。

[root@xinxin001 /]# halt
[root@xinxin001 /]# poweroff
[root@xinxin001 /]# init 0

1分钟后关机。

[root@xinxin001 /]# shutdown -h +1
Shutdown scheduled for 六 2018-10-27 15:13:49 CST, use 'shutdown -c' to cancel.
[root@xinxin001 /]# 
Broadcast message from root@xinxin001 (Sat 2018-10-27 15:12:49 CST):

The system is going down for power-off at Sat 2018-10-27 15:13:49 CST

10分钟后关机。

[root@xinxin001 /]# shutdown -h +10
Shutdown scheduled for 六 2018-10-27 15:28:00 CST, use 'shutdown -c' to cancel.
[root@xinxin001 /]# 
Broadcast message from root@xinxin001 (Sat 2018-10-27 15:18:00 CST):

The system is going down for power-off at Sat 2018-10-27 15:28:00 CST

通知在线使用者关机时间,通知10分钟后关机。

[root@xinxin001 /]# shutdown +10 "The machine will shutdown"
Broadcast message from root@xinxin001 (Sat 2018-10-27 15:43:11 CST):

The machine will shutdown
The system is going down for power-off at Sat 2018-10-27 15:51:11 CS

设置在指定时间关机。

[root@xinxin001 /]# shutdown -h 11:12
Shutdown scheduled for 日 2018-10-28 11:12:00 CST, use 'shutdown -c' to cancel

使用crontab定时任务关机。
crontab格式,星号分别代表分、时、日、月、周:
*  *  *  *  *  command
分 时 日 月 周  命令

/etc/crontab
# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

编辑定时任务。
crontab -e
添加保存定时任务内容,每天下午16点25分定时任务执行一条关机命令,关机命令将在16点30分定时关机。

25 16 * * * /sbin/shutdown -h 16:30

查看定时任务列表。

[root@xinxin001 ~]# crontab -l

查看定时任务执行情况。

[root@xinxin001 /]# tail -f /var/log/cron
Oct 27 16:18:50 xinxin001 crontab[3015]: (root) BEGIN EDIT (root)
Oct 27 16:19:34 xinxin001 crontab[3015]: (root) REPLACE (root)
Oct 27 16:19:34 xinxin001 crontab[3015]: (root) END EDIT (root)
Oct 27 16:19:37 xinxin001 crontab[3017]: (root) LIST (root)
Oct 27 16:20:01 xinxin001 crond[1181]: (root) RELOAD (/var/spool/cron/root)
Oct 27 16:20:02 xinxin001 CROND[3023]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Oct 27 16:20:02 xinxin001 CROND[3022]: (root) CMD (/sbin/shutdown -h 16:21)
Oct 27 16:21:00 xinxin001 crond[1181]: (CRON) INFO (Shutting down)
Oct 27 16:30:42 xinxin001 crond[1181]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 1% if used.)
Oct 27 16:30:47 xinxin001 crond[1181]: (CRON) INFO (running with inotify sup

取消定时关机。

[root@xinxin001 /]# sudo shutdown -c
[root@xinxin001 /]# 
Broadcast message from root@xinxin001 (Sat 2018-10-27 16:34:46 CST):

The system shutdown has been cancelled at Sat 2018-10-27 16:35:46 CST

 

使用root用户立刻重启服务器。

[root@xinxin001 ~]# reboot

 或

[root@xinxin001 ~]# shutdown -r now

 清屏,清除终端窗口所有内容。

[root@xinxin001 /]# clear

退出当前进程,ctrl+c快捷键。
挂起当前进程,ctrl+z快捷键,进程会挂起到后台。
让进程在后台继续执行,bg jobid。
让进程回到前台,fg jobid。

使用echo在终端窗口打印输出信息。
定一个变量a,值为hi, linux command。使用echo输出变量a的值。
使用echo输出信息This is an output message。

[root@xinxin001 /]# a="hi, linux command"
[root@xinxin001 /]# echo $a
hi, linux command
[root@xinxin001 /]# echo This is an output message
This is an output message
[root@xinxin001 /]# echo 这是一条输出信息
这是一条输出信息


文件目录操作

查看目录信息。
查看目录下的文件和文件夹,ls。
查看app文件夹下的文件和文件夹,ls /app。
查看隐藏文件,ls -a。
以详细列表方式查看目录下文件和文件夹,ls -l 或者 ll。

[root@xinxin001 /]# ls
app   bin   dev  home  lib64  mnt  proc  run   srv  tmp                  usr  zookeeper.out
app1  boot  etc  lib   media  opt  root  sbin  sys  tomcat-webapps-ROOT  var
[root@xinxin001 /]# ls /
app   bin   dev  home  lib64  mnt  proc  run   srv  tmp                  usr  zookeeper.out
app1  boot  etc  lib   media  opt  root  sbin  sys  tomcat-webapps-ROOT  var
[root@xinxin001 /]# ls app
data  logs  runtime  services  soft
[root@xinxin001 /]# 
[root@xinxin001 /]# 
[root@xinxin001 /]# ls -a
.   app   bin   dev  home  lib64  mnt  proc  run   srv  tmp                  usr  zookeeper.out
..  app1  boot  etc  lib   media  opt  root  sbin  sys  tomcat-webapps-ROOT  var
[root@xinxin001 /]# ls -al
总用量 48
dr-xr-xr-x.  20 root root 4096 10月 15 23:35 .
dr-xr-xr-x.  20 root root 4096 10月 15 23:35 ..
drwxr-xr-x.   7 root root   73 10月 14 20:19 app
drwxr-xr-x.   6 root root   61 9月  25 01:00 app1
lrwxrwxrwx.   1 root root    7 9月  24 05:41 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 9月  24 06:05 boot
drwxr-xr-x.  20 root root 3300 10月 27 16:30 dev
drwxr-xr-x. 135 root root 8192 10月 27 16:46 etc
drwxr-xr-x.   3 root root   19 9月  24 05:52 home
lrwxrwxrwx.   1 root root    7 9月  24 05:41 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 9月  24 05:41 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.   3 root root   16 9月  24 05:46 opt
dr-xr-xr-x. 190 root root    0 10月 27 16:30 proc
dr-xr-x---.  22 root root 4096 10月 27 16:33 root
drwxr-xr-x.  40 root root 1180 10月 27 16:30 run
lrwxrwxrwx.   1 root root    8 9月  24 05:41 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 4月  11 2018 srv
dr-xr-xr-x.  13 root root    0 10月 27 16:30 sys
drwxrwxrwt.  24 root root 4096 10月 27 16:33 tmp
drwxr-xr-x.   3 root root 4096 10月 15 23:35 tomcat-webapps-ROOT
drwxr-xr-x.  13 root root  155 9月  24 05:41 usr
drwxr-xr-x.  21 root root 4096 9月  24 06:05 var
-rw-r--r--.   1 root root 4331 10月 27 16:30 zookeeper.out
[root@xinxin001 /]# ls -l
总用量 40
drwxr-xr-x.   7 root root   73 10月 14 20:19 app
drwxr-xr-x.   6 root root   61 9月  25 01:00 app1
lrwxrwxrwx.   1 root root    7 9月  24 05:41 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 9月  24 06:05 boot
drwxr-xr-x.  20 root root 3300 10月 27 16:30 dev
drwxr-xr-x. 135 root root 8192 10月 27 16:46 etc
drwxr-xr-x.   3 root root   19 9月  24 05:52 home
lrwxrwxrwx.   1 root root    7 9月  24 05:41 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 9月  24 05:41 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.   3 root root   16 9月  24 05:46 opt
dr-xr-xr-x. 190 root root    0 10月 27 16:30 proc
dr-xr-x---.  22 root root 4096 10月 27 16:33 root
drwxr-xr-x.  40 root root 1180 10月 27 16:30 run
lrwxrwxrwx.   1 root root    8 9月  24 05:41 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 4月  11 2018 srv
dr-xr-xr-x.  13 root root    0 10月 27 16:30 sys
drwxrwxrwt.  24 root root 4096 10月 27 16:33 tmp
drwxr-xr-x.   3 root root 4096 10月 15 23:35 tomcat-webapps-ROOT
drwxr-xr-x.  13 root root  155 9月  24 05:41 usr
drwxr-xr-x.  21 root root 4096 9月  24 06:05 var
-rw-r--r--.   1 root root 4331 10月 27 16:30 zookeeper.out
[root@xinxin001 /]# ll
总用量 40
drwxr-xr-x.   7 root root   73 10月 14 20:19 app
drwxr-xr-x.   6 root root   61 9月  25 01:00 app1
lrwxrwxrwx.   1 root root    7 9月  24 05:41 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 9月  24 06:05 boot
drwxr-xr-x.  20 root root 3300 10月 27 16:30 dev
drwxr-xr-x. 135 root root 8192 10月 27 16:46 etc
drwxr-xr-x.   3 root root   19 9月  24 05:52 home
lrwxrwxrwx.   1 root root    7 9月  24 05:41 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 9月  24 05:41 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.   3 root root   16 9月  24 05:46 opt
dr-xr-xr-x. 190 root root    0 10月 27 16:30 proc
dr-xr-x---.  22 root root 4096 10月 27 16:33 root
drwxr-xr-x.  40 root root 1180 10月 27 16:30 run
lrwxrwxrwx.   1 root root    8 9月  24 05:41 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 4月  11 2018 srv
dr-xr-xr-x.  13 root root    0 10月 27 16:30 sys
drwxrwxrwt.  24 root root 4096 10月 27 16:33 tmp
drwxr-xr-x.   3 root root 4096 10月 15 23:35 tomcat-webapps-ROOT
drwxr-xr-x.  13 root root  155 9月  24 05:41 usr
drwxr-xr-x.  21 root root 4096 9月  24 06:05 var
-rw-r--r--.   1 root root 4331 10月 27 16:30 zookeeper.out

切换文件夹目录,进入某个文件目录。
进入runtime文件夹。
cd /app/runtime
进入上一层级文件夹目录。
cd ..
进入当前用户主目录。
cd ~ 
返回到上次命令所在的文件夹目录。
cd - 
进入根目录。 
cd  /

[root@xinxin001 /]# cd /app/runtime
[root@xinxin001 runtime]# cd ..
[root@xinxin001 app]# cd ~
[root@xinxin001 ~]# cd -
/app
[root@xinxin001 app]# cd
[root@xinxin001 ~]# cd /
[root@xinxin001 /]# 

创建文件夹。
创建名称为files的目录。
mkdir files。
创建层级目录,文件夹层级不存在也可以创建。
mkdir -p /files/doc/01
一次创建多个目录02、03、04、05、06。
mkdir 02 03 04 05 06

[root@xinxin001 app]# ls
data  logs  runtime  services  soft
[root@xinxin001 app]# mkdir files
[root@xinxin001 app]# ls
data  files  logs  runtime  services  soft
[root@xinxin001 app]# mkdir -p /files/doc/01
[root@xinxin001 01]# cd /files
[root@xinxin001 files]# ls
doc
[root@xinxin001 files]# cd doc
[root@xinxin001 doc]# ls
01
[root@xinxin001 doc]# mkdir 02 03 04 05 06
[root@xinxin001 doc]# ls
01  02  03  04  05  06
[root@xinxin001 doc]# 

删除文件夹。
删除文件夹目录01。
rmdir  01
删除doc目录下所有文件夹和文件,文件夹中的所有子节点。需要输入yes或y确认删除。
rm -r doc
强制删除文件夹或目录,不需要确认删除。
rm  -rf  doc

[root@xinxin001 doc]# ls
01  02  03  04  05  06
[root@xinxin001 doc]# rmdir 01
[root@xinxin001 doc]# ls
02  03  04  05  06
[root@xinxin001 doc]# cd ..
[root@xinxin001 files]# rm -r doc
rm:是否进入目录"doc"? y
rm:是否删除目录 "doc/02"?y
rm:是否删除目录 "doc/03"?y
rm:是否删除目录 "doc/04"?y
rm:是否删除目录 "doc/05"?y
rm:是否删除目录 "doc/06"?y
rm:是否删除目录 "doc"?n
[root@xinxin001 files]# ls
doc
[root@xinxin001 files]# rm -rf doc
[root@xinxin001 files]# ls
[root@xinxin001 files]

修改文件夹名称、移动文件、复制文件、删除文件。
mv修改文件名称。
mv 1.jpg  001.jpg
rename修改文件名称。
rename 2.jpg 02.jpg *
移动文件。
mv 001.jpg  images/
复制文件到images目录。
cp 3.jpg  images/
删除文件3.jpg。
rm -f 3.jpg

[root@xinxin001 files]# ls
1.jpg  2.jpg  3.jpg  images
[root@xinxin001 files]# mv 1.jpg  001.jpg
[root@xinxin001 files]# ls
001.jpg  2.jpg  3.jpg  images
[root@xinxin001 files]# mv 001.jpg  images/
[root@xinxin001 files]# ls
2.jpg  3.jpg  images
[root@xinxin001 files]# cd images
[root@xinxin001 images]# ls
001.jpg
[root@xinxin001 images]# cd ..
[root@xinxin001 files]# ls
2.jpg  3.jpg  images
[root@xinxin001 files]# rename 2.jpg 02.jpg *
[root@xinxin001 files]# ls
02.jpg  3.jpg  images
[root@xinxin001 files]# cp 3.jpg  images/
[root@xinxin001 files]# cd images
[root@xinxin001 images]# ls
001.jpg  3.jpg
[root@xinxin001 images]# cd ..
[root@xinxin001 files]# ls
02.jpg  3.jpg  images
[root@xinxin001 files]# rm -f 3.jpg 
[root@xinxin001 files]# ls
02.jpg  images

创建文件。
使用touch创建一个空的文件read.md。
touch read.md
使用vi创建一个空的文件version.md。
vi version.md
使用echo >重定向功能,将一条指令的输出结果内容写入到一个文件中,覆盖原文件内容。指定的文件不存在,将会创建文件。
echo "hello,ladies and gentlemen" > read.md
使用echo >>追加功能,将一条指令的输出结果内容追加到一个文件中,不会覆盖原文件内容。
echo "This is a doc." >> read.md

[root@xinxin001 files]# ls
02.jpg  3.jpg  images
[root@xinxin001 files]# touch read.md
[root@xinxin001 files]# ls
02.jpg  3.jpg  images  read.md
[root@xinxin001 files]# vi version.md 
[root@xinxin001 files]# ls
02.jpg  3.jpg  images  read.md  version.md
[root@xinxin001 files]# echo "hello,ladies and gentlemen" > read.md
[root@xinxin001 files]# vi read.md 
[8]+  已停止               vi read.md

[root@xinxin001 files]# echo "This is a doc." >> read.md
[root@xinxin001 files]# vi read.md 
[9]+  已停止               vi read.md

编辑文件。
vi编辑文件、或vim编辑文件。
vi是系统自带的编辑器,vim系统不一定会自带,可能需要安装,vim会对编辑的内容关键字高亮显示,用不同颜色区分,适合修改配置文件使用。

[root@xinxin001 files]# vi read.md 
[root@xinxin001 files]# vim read.md 

最基本用法。
1、使用vi编辑文件时,先会进入只读模式。只读模式可以接受快捷键操作,但不能编辑文件内容。
2、按Insert插入键,编辑器左下角显示“-- 插入 -- ”或“-- Insert -- ”,从只读模式变成编辑模式。编辑模式下,可以编辑文件内容。
3、编辑完成之后,按Esc键退出编辑模式,返回只读模式。
4、按“Shift + :”进入命令模式,在编辑器底部输入wq或wq!,保存编辑内容。不想保存文件内容,按Ctrl + z退出编辑文件内容。

只读模式下常用快捷键。
a          在光标后一位开始插入,进入编辑模式。
A         在光标所在行的最后面插入,进入编辑模式。
I          在光标所在行的最前面插入,进入编辑模式。
gg      直接跳到文件的首行,光标定位到首行第一个字符。
G        直接跳到文件的末行,光标定位到末行第一个字符。
dd      删除一行,删除光标当前所在行。
3dd    删除3行,删除从光标所在行开始往下数3行,删除包括光标所在行。ndd,n需要删除几行的数字,2dd,3dd,4dd,5dd。
yy      复制一行,复制光标当前所在行。
3yy    复制3行,复制从光标所在行开始往下数3行,复制包括光标所在行。ndd,n需要复制几行的数字,3yy,4yy,5yy。
p       粘贴,复制后粘贴。
u       undo,撤销、取消修改的文件内容,文件内容还原到修改之前的。
v       进入字符选择模式,按上下左右箭头选择文件内容,选择完成后,按y复制,按p粘贴。取消选择再按一次v。
ctrl+v    进入内容块选择模式,按上下左右箭头选择文件内容,选择完成后,按y复制,按p粘贴。取消选择再按一次ctrl+v。
shift+v  进入行选择模式,按上下左右箭头选择文件内容,选择完成后,按y复制,按p粘贴。取消选择再按一次shift+v。

只读模式下查找并替换文件内容。
显示行号。

:set nu

隐藏行号,不显示行号。

:set nonu

查找关键字。查找文件中出现的hello,并定位到第一个找到的地方,按n可以定位到下一个匹配位置,按N定位到上一个匹配位置。

:/hello

修改替换文件内容。
查找光标所在行的第一个hello,替换为hi。

:s/hello/hi   

 查找文件中所有hello,全部替换为hi。

:%s/hello/hi 

查看文件内容。
一次性将read.md文件内容全部输出到控制台。
cat    read.md
可以翻页查看,下翻一页(空格),上翻一页(b),退出(q)。
more   somefile
可以翻页查看,下翻一页(空格),上翻一页(b),上翻一行(↑) ,下翻一行(↓),可以搜索关键字(/keyword)。
less   somefile      
跳到文件末尾: G
跳到文件首行: gg
退出less :  q

[root@xinxin001 files]# cat read.md 
15This is a doc.
14This is a doc.
13This is a doc.
12hi,ladies and gentlemen
11This is a doc.
10This is a doc.
9This is a doc.
8hi,ladies and gentlemen
7hi,ladies and gentlemen
6This is a doc.
5This is a doc.
4This is a doc.
3hi,ladies and gentlemen
2This is a doc.
1This is a doc.

查看文件尾部的3行。
tail -3 read.md   

[root@xinxin001 files]# tail -3 read.md 
3hi,ladies and gentlemen
2This is a doc.
1This is a doc.

显示文件内容,文件内容改变会显示出来。小f跟踪文件的唯一inode号,就算文件改名后,还是跟踪原来这个inode表示的文件。
tail -f  read.md

[root@xinxin001 files]# tail -f  read.md 
10This is a doc.
9This is a doc.
8hi,ladies and gentlemen
7hi,ladies and gentlemen
6This is a doc.
5This is a doc.
4This is a doc.
3hi,ladies and gentlemen
2This is a doc.
1This is a doc.

 显示文件内容,文件内容改变会显示出来。大F按照文件名来跟踪。
 tail -F read.md

[root@xinxin001 files]# tail -F  read.md 
10This is a doc.
9This is a doc.
8hi,ladies and gentlemen
7hi,ladies and gentlemen
6This is a doc.
5This is a doc.
4This is a doc.
3hi,ladies and gentlemen
2This is a doc.
1This is a doc.

查看文件头部的5行。
head -5  read.md  

[root@xinxin001 files]# head -5 read.md 
15This is a doc.
14This is a doc.
13This is a doc.
12hi,ladies and gentlemen
11This is a doc.

打包压缩文件、解压文件。

.gz后缀文件。
压缩文件生成.gz压缩包。
gzip read.md 
解压.gz压缩包。
gzip -d read.md.gz
gunzip read.md.gz

[root@xinxin001 files]# ls
02.jpg  images  read.md  version.md
[root@xinxin001 files]# gzip read.md 
[root@xinxin001 files]# ls
02.jpg  images  read.md.gz  version.md
[root@xinxin001 files]# gzip -d read.md.gz
[root@xinxin001 files]# ls
02.jpg  images  read.md  version.md
[root@xinxin001 files]# gzip read.md 
[root@xinxin001 files]# ls
02.jpg  images  read.md.gz  version.md
[root@xinxin001 files]# gunzip read.md.gz 
[root@xinxin001 files]# ls
02.jpg  images  read.md  version.md

 .bz2后缀文件。
压缩文件生成.bz2压缩包。
bzip2 read.md
解压.bz2压缩包。
bunzip2 read.md.bz2 
bzip2 -d read.md.bz2 

[root@xinxin001 files]# ls
02.jpg  images  read.md  version.md
[root@xinxin001 files]# bzip2 read.md 
[root@xinxin001 files]# ls
02.jpg  images  read.md.bz2  version.md
[root@xinxin001 files]# bzip2 images/
bzip2: Input file images/ is a directory.
[root@xinxin001 files]# ls
02.jpg  images  read.md.bz2  version.md
[root@xinxin001 files]# bunzip2 read.md.bz2 
[root@xinxin001 files]# ls
02.jpg  images  read.md  version.md
[root@xinxin001 files]# bzip2 read.md 
[root@xinxin001 files]# ls
02.jpg  images  read.md.bz2  version.md
[root@xinxin001 files]# bzip2 -d read.md.bz2 
[root@xinxin001 files]# ls
02.jpg  images  read.md  version.md

 .tar后缀文件。
压缩文件生成.tar压缩包,可指定文件或文件夹。
tar -cvf read.tar read.md
压缩文件夹。
tar -cvf images.tar images/
解压.tar后缀压缩包。
tar -xvf read.tar

[root@xinxin001 files]# ls
02.jpg  images  read.md  version.md
[root@xinxin001 files]# tar -cvf read.tar read.md
read.md
[root@xinxin001 files]# ls
02.jpg  images  read.md  read.tar  version.md
[root@xinxin001 files]# tar -cvf read.tar version.md 
version.md
[root@xinxin001 files]# tar -cvf images.tar images/
images/
images/001.jpg
images/3.jpg
images/.001.jpg.swp
[root@xinxin001 files]# ls
02.jpg  images  images.tar  read.md  read.tar  version.md
[root@xinxin001 files]# tar -xvf read.tar 

 .tar.gz后缀文件。
压缩文件生成.tar.gz压缩包,可指定文件或文件夹。
tar -zcvf read.tar.gz read.md 
tar -zcvf images.tar.gz images
解压.tar.gz后缀压缩包。
tar -zxvf read.tar.gz
tar -zxvf images.tar.gz 
查看.tar.gz压缩包中包含哪些文件。
tar -ztvf read.tar.gz
tar -ztvf images.tar.gz 

[root@xinxin001 files]# ls
02.jpg  images  read.md  version.md
[root@xinxin001 files]# 
[root@xinxin001 files]# tar -zcvf read.tar.gz read.md 
read.md
[root@xinxin001 files]# ls
02.jpg  images  read.md  read.tar.gz  version.md
[root@xinxin001 files]# tar -zcvf images.tar.gz images
images/
images/001.jpg
images/3.jpg
images/.001.jpg.swp
[root@xinxin001 files]# ls
02.jpg  images  images.tar.gz  read.md  read.tar.gz  version.md
[root@xinxin001 files]# tar -zxvf read.tar.gz
read.md
[root@xinxin001 files]# tar -zxvf images.tar.gz 
images/
images/001.jpg
images/3.jpg
images/.001.jpg.swp
[root@xinxin001 files]# tar -ztvf read.tar.gz
-rw-r--r-- root/root       282 2018-10-29 00:55 read.md
[root@xinxin001 files]# tar -ztvf images.tar.gz 
drwxr-xr-x root/root         0 2018-10-29 00:37 images/
-rw-r--r-- root/root         2 2018-10-27 18:04 images/001.jpg
-rw-r--r-- root/root         4 2018-10-27 19:14 images/3.jpg
-rw-r--r-- root/root      4096 2018-10-29 00:31 images/.001.jpg.swp

 .tar.bz2后缀文件。
压缩文件生成.tar.bz2压缩包,可指定文件或文件夹。
tar -jcvf read.tar.bz2 read.md
tar -jcvf images.tar.bz2 images
解压.tar.bz2后缀压缩包。
tar -jxvf read.tar.bz2 
tar -jxvf images.tar.bz2 

[root@xinxin001 files]# ls
02.jpg  images  read2.md  read.md  version.md
[root@xinxin001 files]# tar -jcvf read.tar.bz2 read.md
read.md
[root@xinxin001 files]# ls
02.jpg  images  read2.md  read.md  read.tar.bz2  version.md
[root@xinxin001 files]# tar -jxvf read.tar.bz2 
read.md
[root@xinxin001 files]# ls
02.jpg  images  read2.md  read.md  read.tar.bz2  version.md
[root@xinxin001 files]# rm -rf read.md 
[root@xinxin001 files]# ls
02.jpg  images  read2.md  read.tar.bz2  version.md
[root@xinxin001 files]# tar -jxvf read.tar.bz2 
read.md
[root@xinxin001 files]# ls
02.jpg  images  read2.md  read.md  read.tar.bz2  version.md
[root@xinxin001 files]# tar -jcvf images.tar.bz2 images
images/
images/001.jpg
images/3.jpg
images/.001.jpg.swp
[root@xinxin001 files]# ls
02.jpg  images  images.tar.bz2  read2.md  read.md  read.tar.bz2  version.md
[root@xinxin001 files]# tar -jxvf images.tar.bz2 
images/
images/001.jpg
images/3.jpg
images/.001.jpg.swp

 .zip后缀文件。
压缩文件生成.zip压缩包。
zip  read.zip read.md
解压.zip后缀压缩包。
unzip news.zip
把文件解压到指定的images目录下
unzip -d images news.zip
查看zip压缩包中包含哪些文件。
unzip -l news.zip
查看显示的文件列表,包含压缩比率。
unzip -v news.zip 

[root@xinxin001 files]# ls
02.jpg  images  news.zip  read2.md  read.md  version.md
[root@xinxin001 files]# unzip news.zip 
Archive:  news.zip
  inflating: news.txt                
[root@xinxin001 files]# ls
02.jpg  images  news.txt  news.zip  read2.md  read.md  version.md
[root@xinxin001 files]# unzip -d images news.zip
Archive:  news.zip
  inflating: images/news.txt 
[root@xinxin001 files]# zip  read.zip read.md
  adding: read.md (deflated 71%)
[root@xinxin001 files]# ls
02.jpg    images    news.zip  read.md   version.md
[root@xinxin001 files]# unzip -l news.zip
[root@xinxin001 files]#  unzip -v news.zip 

查找命令

常用查找命令的使用。
查找可执行的命令所在的路径。
which ls
which ll
which java
查找可执行的命令和帮助的位置。
whereis ls
whereis java

从某个文件夹开始查找文件。
find / -name "java"
find / -name "zookeeper*" -ls
查找并删除。
find / -name "test*" -ok rm {} \;
find / -name "test*" -exec rm {} \;
查找用户为admin的文件。
find  /  -user  admin  -ls
查找用户为admin的文件夹。
find /home -user admin -type d -ls

查找权限为777的文件。
find / -perm -777 -type d -ls
显示命令历史,会显示所有输入过的命令历史记录,所有在Linux上操作的命令记录。
history

[root@xinxin001 files]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@xinxin001 files]# which ll
alias ll='ls -l --color=auto'
	/usr/bin/ls
[root@xinxin001 files]# which java
/app/runtime/jdk1.8.0_181/bin/java
[root@xinxin001 files]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@xinxin001 files]# whereis java
java: /usr/bin/java /usr/lib/java /etc/java /usr/share/java /app/runtime/jdk1.8.0_181/bin/java /usr/share/man/man1/java.1.gz
[root@xinxin001 /]# history
    1  ls
    2  cd root
    3  cd /
    4  ls
    5  ll
    6  cd /
    7  /etc/hosts
    8  vim /etc/hosts
    9  hostname
   10  ls
......

基本使用。
查询包含hi的行。
grep hi read.md
grep hi *.md

[root@xinxin001 files]# grep hi read.md
15This is a doc.
14This is a doc.
13This is a doc.
12hi,ladies and gentlemen
11This is a doc.
10This is a doc.
9This is a doc.
8hi,ladies and gentlemen
7hi,ladies and gentlemen
6This is a doc.
5This is a doc.
4This is a doc.
3hi,ladies and gentlemen
2This is a doc.
1This is a doc.
[root@xinxin001 files]# grep hi *.md
read2.md:15This is a doc.
read2.md:14This is a doc.
read2.md:13This is a doc.
read2.md:12hi,ladies and gentlemen
read2.md:11This is a doc.
read2.md:10This is a doc.
read2.md:9This is a doc.
read2.md:8hi,ladies and gentlemen
read2.md:7hi,ladies and gentlemen
read2.md:6This is a doc.
read2.md:5This is a doc.
read2.md:4This is a doc.
read2.md:3hi,ladies and gentlemen
read2.md:2This is a doc.
read2.md:1This is a doc.
read.md:15This is a doc.
read.md:14This is a doc.
read.md:13This is a doc.
read.md:12hi,ladies and gentlemen
read.md:11This is a doc.
read.md:10This is a doc.
read.md:9This is a doc.
read.md:8hi,ladies and gentlemen
read.md:7hi,ladies and gentlemen
read.md:6This is a doc.
read.md:5This is a doc.
read.md:4This is a doc.
read.md:3hi,ladies and gentlemen
read.md:2This is a doc.
read.md:1This is a doc.

cut截取以.分割保留第7段
grep hi read.md | cut -d. -f7

[root@xinxin001 files]# grep hi read.md | cut -d. -f7
12hi,ladies and gentlemen
8hi,ladies and gentlemen
7hi,ladies and gentlemen
3hi,ladies and gentlemen

查询不包含This的行。
grep -v This read.md

[root@xinxin001 files]# grep -v This read.md
12hi,ladies and gentlemen
8hi,ladies and gentlemen
7hi,ladies and gentlemen
3hi,ladies and gentlemen

使用正则表达式查询包含This的行。
grep 'This' read.md

[root@xinxin001 files]# grep 'This' read.md
15This is a doc.
14This is a doc.
13This is a doc.
11This is a doc.
10This is a doc.
9This is a doc.
6This is a doc.
5This is a doc.
4This is a doc.
2This is a doc.
1This is a doc.

使用正则表达式匹配,点代表任意一个字符。
grep 'T.*i*' read.md

[root@xinxin001 files]# grep 'T.*i*' read.md
15This is a doc.
14This is a doc.
13This is a doc.
11This is a doc.
10This is a doc.
9This is a doc.
6This is a doc.
5This is a doc.
4This is a doc.
2This is a doc.
1This is a doc.

正则表达式以15This开头。
grep '^15This' read.md

[root@xinxin001 files]# grep '^15This' read.md
15This is a doc.

正则表达式以doc.结尾。
grep 'doc\.$' read.md

[root@xinxin001 files]# grep 'doc\.$' read.md
15This is a doc.
14This is a doc.
13This is a doc.
11This is a doc.
10This is a doc.
9This is a doc.
6This is a doc.
5This is a doc.
4This is a doc.
2This is a doc.
1This is a doc.

正则表达式匹配规则。
.  : 任意一个字符
a* : 任意多个a(零个或多个a)
a? : 零个或一个a
a+ : 一个或多个a
.* : 任意多个任意字符
\. : 转义.
o\{2\} : o重复两次

查找不是以1开头的行。
grep -v '^1' read.md | grep -v '^$' 

[root@xinxin001 files]# grep -v '^1' read.md | grep -v '^$' 
9This is a doc.
8hi,ladies and gentlemen
7hi,ladies and gentlemen
6This is a doc.
5This is a doc.
4This is a doc.
3hi,ladies and gentlemen
2This is a doc.

查找以8或9开头的行。
grep '^[89]' read.md

[root@xinxin001 files]# grep '^[89]' read.md
9This is a doc.
8hi,ladies and gentlemen

查找不是以1和2开头的行。
grep '^[^12]' read.md

[root@xinxin001 files]# grep '^[^12]' read.md
9This is a doc.
8hi,ladies and gentlemen
7hi,ladies and gentlemen
6This is a doc.
5This is a doc.
4This is a doc.
3hi,ladies and gentlemen
[root@xinxin001 files]# grep '^[^1-5]' read.md
9This is a doc.
8hi,ladies and gentlemen
7hi,ladies and gentlemen
6This is a doc.

文件权限操作

linux文件权限的描述格式解读。

[root@xinxin001 bin]# ls -la java
lrwxrwxrwx. 1 root root 22 9月  24 05:43 java -> /etc/alternatives/java
 

基本的用户管理

系统管理操作

SSH免密登陆配置

网络管理

 

重要参考来自Linux爱好者微信公众号(文章来源:李东浩):
https://mp.weixin.qq.com/s/Ye4Rn_Dl58Sb_2PAsch_1g
www.lxx.help/2016/07/26/大数据学习之基础部分-----Linux常用命令/
   

centos服务器如何自动关机自动开机
https://dwz.cn/FpIaLi9m

https://www.cnblogs.com/sxdcgaq8080/p/7498906.html

https://www.cnblogs.com/woshimrf/p/linux-file-properties.html#_caption_1

https://jingyan.baidu.com/article/4853e1e5413b541909f72632.html

  • 5
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值