Linux 中常用的基础命令一

1、目录相关命令的使用
  pwd(printing working directory) 显示当前工作目录
   pwd命令相关的环境变量:
    PWD  保存了当前工作目录路径
    OLDPWD 保存了上一次工作目录路径
    每次执行pwd命令就是去读取PWD的值

  cd(change directory) 改变工作目录
  语法格式  cd [/PATH/TO/SOMEDIR]
    cd或cd~ 切换回当前用户的家目录
    cd ~USERNAME 切换至指定用户的家目录(管理员能执行)
    cd - 在上一次所在目录与当前目录之间来回切换
    cd .. 返回上一级目录

  ls(list) 显示指定路径下的文件列表
  语法格式:ls [OPTION]... [FILE]...
  选项:
    -a  显示所有文件,包括隐藏文件
    -A 显示除 . 和 .. 之外的所有文件
    -l 长格式列表,即显示文件的详细属性信息

  --color  显示颜色(不同类型文件显示为不同颜色)
  -d  显示目录自身的属性,通常与-l同时使用
  -i  显示文件的inode号(index node)
  -h  文件大小以人类易读格式显示
    -r  reverse逆序显示;默认是以升序显示
    -R  recursive递归显示;如果一个目录里有子目录,则同时也显示其子目录中的文件
    -Sl  文件由大到小排序
    -Slr 文件由小到大排序 配合 l
    -t   按修改时间排序,-Slt 等配合l,S,r等排序

2、文件查看类命令
  cat:文件查看类工具
  格式:cat [OPTION]... [FILE]...
  选项:
    -A 显示所有非正常的所有符号
    -E 显示行结束符$
    -T 显示制表符
    -v 显示其他的非打印字符
    -e 等于-vE
    -n 对显示的行进行编号,与文件内容没有联系
    -b 打印出行号(空白行不标行号)
    -s 将多个连续的空白行合并显示一个空白行

  tac 逆序显示文件内容(逆序行)
  rev 逆序显示每行字符(逆序列)
  file 查看文件内容类型格式

演示:
cat、tac、rev命令的区别
[root@centos7 ~]# echo "123456789" > /testdir/f1
[root@centos7 ~]# ll /testdir/f1
-rw-r--r-- 1 root root 10 2月 11 16:29 /testdir/f1

[root@centos7 ~]# cat /testdir/f1
123456789
[root@centos7 ~]# tac /testdir/f1
123456789
[root@centos7 ~]# rev /testdir/f1
987654321

[root@centos7 ~]# ls > /testdir/f2
[root@centos7 ~]# cat /testdir/f2
anaconda-ks.cfg
Desktop
Discuz
Documents
Downloads
lnamp
MHA
Music
nginx
Pictures
Public
Templates
Videos
zabbix-agent
zabbix-proxy

[root@centos7 ~]# tac /testdir/f2
zabbix-proxy
zabbix-agent
Videos
Templates
Public
Pictures
nginx
Music
MHA
lnamp
Downloads
Documents
Discuz
Desktop
anaconda-ks.cfg

[root@centos7 ~]# rev /testdir/f2
gfc.sk-adnocana
potkseD
zucsiD
stnemucoD
sdaolnwoD
pmanl
AHM
cisuM
xnign
serutciP
cilbuP
setalpmeT
soediV
tnega-xibbaz
yxorp-xibbaz

file命令
[root@centos7 ~]# file /etc/fstab
/etc/fstab: ASCII text

[root@centos7 ~]# file /bin/ls
/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]=aa7ff68f13de25936a098016243ce57c3c982e06, stripped

3、echo回显命令
  功能:显示字符
  语法:echo [-neE] [字符串]
  说明:echo会将输入的字符串输出到标准输出,并在最后加上换行号
    -n 不为显示内容自动换行,默认都会自动换行显示
  -e 参数来使转义字符生效(字符串一定要加引号)
    \ 输出本身
    a 输出警告音
     退格键,也就是向左删除键
    c 取消输出行末的换行符,和“-n”选项一致
    e ESC(换码符)
    f 换页符
     换行符
     回车键
     制表符,也是Tab
    v 垂直制表符,换行但光标仍旧停留在原来的位置
    nnn 按八进制ASCII码表输出字符。其中0为数字零,nnn为三位八进制数
    xhh  按十六进制ASCII码表输出字符,其中hh为十六进制数

  改变输出内容颜色和格式:e也可以用33替代因为都是表示的换码符
    echo -e "e[1;5;31;47m abc e[0m"
  输出abc的颜色e[表示颜色输出开始;后跟多个格式参数用分号隔开(1表示输出为粗体,5表示文字闪烁,31表示前景色为红色,47表示背景色为白色);abc表示要输出的信息;e[表示颜色输出结束;0m表示恢复为默认输出格式(如果后面不加0m,命令行所有显示会一直以定义的格式显示)

  输出颜色格式定义:3开头表示前景颜色,4开头表示背景颜色:
    30m=黑色 31m=红色 32m=绿色 33m=黄色 34m=蓝色 35m=洋色 36m=青色 37m=白色
   40m=黑色 41m=红色 42m=绿色 43m=黄色 44m=蓝色 45m=洋色 46m=青色 47m=白色

4、关机或重启命令
  管理类命令
    关机:halt、poweroff、shutdown、init 0
    重启:reboot、shutdown、init 6
    用户登录信息:who、whoami、w

  halt和poweroff:两者都是关机命令
    halt   关机但不断电
    poweroff 关机后并断电    
    -f 强制关机,相当于切断电源(不会调用shutdown进行安全关机操作)
  reboot:一般情况下是调用shutdown进行关机重启
    -f 强制(不会调用shutdown进行安全关机操作)
    -p 切断电源

  shutdown:关机或重启
  格式:shutdown [OPTION...] [TIME] [WALL...]
    -r  reboot 重启
    -h halt 关机
    -c cancle 取消关机
  -k 发行警告但是不执行关机操作

  时间格式:
  now  立刻
  +#   相对时间表示法,#分钟后执行操作,例如:+3分钟
  hh:mm  绝对时间表示,到具体时间后关机

  wall:在所有终端显示其输入的内容,例如:wall "shutdown now"

演示:
# 在5分钟后重启,会通知给所有人
[root@centos7 ~]# shutdown -r +5
Shutdown scheduled for 三 2017-02-08 09:35:05 CST, use 'shutdown -c' to cancel.
[root@centos7 ~]#
Broadcast message from root@centos7 (Wed 2017-02-08 09:30:05 CST):

The system is going down for reboot at Wed 2017-02-08 09:35:05 CST!

#取消关机,也会通知给所有人
[root@centos7 ~]# shutdown -c
[root@centos7 ~]#
Broadcast message from root@centos7 (Wed 2017-02-08 09:30:45 CST):

The system shutdown has been cancelled at Wed 2017-02-08 09:31:45 CST!

# 加wall可显示自定义的语句
[root@centos7 ~]# shutdown -r +5 "hello everyone"
Shutdown scheduled for 三 2017-02-08 09:41:27 CST, use 'shutdown -c' to cancel.
[root@centos7 ~]#
Broadcast message from root@centos7 (Wed 2017-02-08 09:36:27 CST):

hello everyone
The system is going down for reboot at Wed 2017-02-08 09:41:27 CST!

5、与登录相关的命令
  whoami 显示当前登录用户的用户名
 w    系统当前所有的登录会话及所做的操作
 tty  显示当前终端对应终端设备文件
 who   显示当前系统所有登录用户名、终端类型、登录时间;如果是远程登录会显示登录IP地址
   -b  系统此次启动的时间
   -r 显示运行级别

演示:
[root@centos7 ~]# whoami
root

[root@centos7 ~]# who
root tty1 2017-02-08 09:26
root pts/0 2017-02-08 08:57 (192.168.1.104)
root pts/1 2017-02-08 09:30 (192.168.1.104)
root pts/2 2017-02-08 10:15 (192.168.1.104)
root pts/3 2017-02-08 10:18 (192.168.43.174)
root pts/4 2018-07-07 10:10 (192.168.1.104)
[root@centos7 ~]# who -b
系统引导 2017-02-08 15:48
[root@centos7 ~]# who -r
运行级别 3 2017-02-08 08:29

[root@centos7 ~]# w
11:02:39 up 2:32, 6 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN   @ IDLE JCPU PCPU WHAT
root tty1 09:26 1:02m 0.08s 0.08s -bash
root pts/0 192.168.1.104 08:57 1:25m 0.17s 0.17s -bash
root pts/1 192.168.1.104 09:30 1:32m 0.04s 0.04s -bash
root pts/2 192.168.1.104 10:15 47:11 0.05s 0.05s -bash
root pts/3 192.168.43.174 10:18 ? 0.26s 0.26s -bash
root pts/4 192.168.1.104 10:10 ? 0.10s 0.01s w

6、与日期相关的命令
  date  显示时间和日期
 格式:date[OPTION]...[+FORMAT]

 按指定格式显示时间
 date[+FORMAT]
  %D  直接显示日期 mm/dd/yy ("Y"长格式年,"y"短格式年)
  %F 直接显示日期(YY-mm-dd)
  %T 直接显示时间(hh:mm:ss24小时制)
  %Y 完整年份(0000-9999)
  %m 显示月份
  %d 日
  %A 星期几(Sunday-Saturday)
  %a 星期几(Sun-Sat)
  %w 一周中的第几天
  %H 显示小时
  %M 显示分钟
  %S 显示秒
  %s 现在距离1970年1月1号0点0分(unix元年)的秒数,timestamp(时间戳)


  设置时间
  格式:date MMDDhhmm[CC]YY[.ss]
    MM  月份
    DD  日
    hh  小时
    mm  分钟
    CC  两位年份
    CCYY 四位年份
    ss  秒
 -s 通过指定字符串设置时间

  显示当前时间的时间戳
  date +%s
  将指定时间转换为时间戳的格式
  date --date "Thu Nov 18 08:07:21 IST 2010" +%s
  获取指定时间是星期几
  date --date "Jan 20 2001" +%A

  Linux的两个时钟
    系统时钟:由Linux内核通过cpu的工作频率进行计时
    硬件时钟:就是主板bios提供的时钟,由主板电池供电来维持运行

    date 显示的是系统时间;
    hwclock或clock 显示的是硬件时钟
    -s --hctosys以硬件时钟为准,将硬件时钟写入到系统时钟(校正系统时钟)
    -w  --systohc以系统时钟为准,将软件时钟写入到硬件时钟(校正硬件时钟)

演示:
常用格式的日期显示
[root@centos7 ~]# date +%D
02/08/17
[root@centos7 ~]# date +%F
2017-02-08
[root@centos7 ~]# date +%T
10:20:00
[root@centos7 ~]# date +%Y
2017
[root@centos7 ~]# date +%m
02
[root@centos7 ~]# date +%d
08
[root@centos7 ~]# date +%A
星期三
[root@centos7 ~]# date +%a

[root@centos7 ~]# date +%w
3
[root@centos7 ~]# date +%H
10
[root@centos7 ~]# date +%M
21
[root@centos7 ~]# date +%S
24
[root@centos7 ~]# date +%s
1486520492

完整格式的日期显示:
[root@centos7 ~]# date "+现在的时间是:%Y-%m-%d %H:%M:%S"
现在的时间是:2017-02-08 10:23:29
[root@centos7 ~]# date "+现在的时间是:%F %H:%M:%S"
现在的时间是:2017-02-08 10:24:33
[root@centos7 ~]# date "+现在的时间是:%F %H:%M:%S %A"
现在的时间是:2017-02-08 10:24:51 星期三
如果要显示的时间不是当前时间,而是经过运算的时间,则可以使用-d选项,如下:
# 显示3年前的时间
[root@centos7 ~]# date "+%F %H:%M:%S" -d "-3 year"
2014-02-08 10:26:08
# 显示3个月后的时间
[root@centos7 ~]# date "+%F %H:%M:%S" -d "+3 month"
2017-05-08 10:26:24
# 显示10天后的时间
[root@centos7 ~]# date "+%F %H:%M:%S" -d "+10 day"
2017-02-18 10:26:42

有时候需要获取当前时间距离1970年0时0分0秒经历的秒数,保存在变量中,如下:
[root@centos7 ~]# time=`date "+%s"`
[root@centos7 ~]# echo $time
1486520847

设置时间
[root@centos7 ~]# date -s "20180707 10:10:10"
2018年 07月 07日 星期六 10:10:10 CST
[root@centos7 ~]# date -s "2018-07-07 10:10:10"
2018年 07月 07日 星期六 10:10:10 CST
[root@centos7 ~]# date -s "2018/07/07 10:10:10"
2018年 07月 07日 星期六 10:10:10 CST

系统时钟和硬件时钟
# 系统时钟
[root@centos7 ~]# date
2018年 07月 07日 星期六 10:26:42 CST
#硬件时钟
[root@centos7 ~]# clock
2017年02月08日 星期三 10时47分46秒 -0.292560 秒
[root@centos7 ~]# hwclock
2017年02月08日 星期三 10时47分53秒 -0.479196 秒

# 硬件时钟校正系统时钟
[root@centos7 ~]# clock -s
[root@centos7 ~]# date
2017年 02月 08日 星期三 10:48:24 CST

  cal 显示当前日历,默认显示当前月份的日历
  cal [YY]  显示指定年份的日历如:cal 09 2017
演示:
[root@centos7 ~]# cal
二月 2017
日 一 二 三 四 五 六
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

7、type、alias、which和whereis命令
  type
  格式:type COMMAND
  如果是内部命令显示为"builtin"
  如果是外部命令显示命令文件的路径
  注意:命令可以有别名,别名可以与原名相同,此时原名被隐藏,此时如果要运行原命令,则使用 COMMAND

  alias 命令别名
  alias 显示所有定义的别名
  alias NAME='COMMAND' 定义别名(仅对当前shell有效)
  unalias NAME 撤销别名

  which 显示应用程序文件位置
  格式:which COMMAND
  注意:有时候一个命令还有其他的别名,但我们先看命令本身,而非别名,此时可以使用:
    which --skip-alias 禁止使用别名,直接使用原始命令

  whereis
  作用:whereis - locate the binary, source, and manual page files for a command
  格式:whereis [options] name...
  选项:
    -b 仅搜索二进制程序路径
    -m 仅搜索使用手册文件路径

演示:
type
[root@centos7 ~]# type cd
cd 是 shell 内嵌
[root@centos7 ~]# type date
date 已被哈希 (/usr/bin/date)
[root@centos7 ~]# type tree
tree 是 /usr/bin/tree
[root@centos7 ~]# type ls
ls 是 `ls --color=auto' 的别名
wKioL1iazijg7shYAAAse_IY3-U276.png

which
[root@centos7 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@centos7 ~]# which --skip-alias ls
/usr/bin/ls

whereis
[root@centos7 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

alias
[root@centos7 ~]# 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'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

8、screen命令远程控制
  一个终端下打开多个屏幕,并剥离屏幕与终端的关系,终端关闭也不会影响屏幕下的进程
  使用方法:
   screen -S [session 会话] 打开screen
   screen -x [session 会话] 加入screen
  exit 退出并关闭screen
    Ctrl+a,d 剥离当前screen
  screen -ls 显示所有已经打开的screen
  screen -r SCREEN_ID 连接指定screen

原文地址:https://www.cnblogs.com/Link-Luck/p/9849069.html