Linux常用命令-基础命令

Linux基础命令 

  用户登陆

  • root 用户

    • 超级管理员

    • 对系统有完全操作的权限

    • 误操作对系统的损害无限大

    • 尽量不要使用root登录

  • 普通用户

    • 对系统的操作权限很小

    • 损害有限

    • 需要用普通用户登录

  终端terminal分类

  • 设备终端
  • 物理终端
  • 虚拟终端(通过ctrl+alt+f[1-6]) /dev/tty#
  • 图形终端 /dev/tty7
  • 串行终端
  • 伪终端(ssh远程连接) /dev/pts/#
  • 查看当前的设备终端:tty

  查看ip

ifconfig  查看ip地址
ip addr 
ip a

 

  交互式接口

交互式接口:启动终端后,在终端设备附加一个交互式的应用程序

  • GUI(图形)
  • CLI(命令行)
    • sh
    • csh
    • tcsh
    • ksh
    • bash(linux、mac上的shell)
    • zsh

  shell

shell 是linux系统的用户界面,提供了用户和内核进行交互操作的一种接口,它接受用户输入的 命令并将它送到内核去执行

  bash shell

  • 目前linux和mac上标准的shell
  • centos默认使用
  • 显示当前使用的shell echo ${SHELL}
  • 显示当前系统所有的shell cat /etc/shells
  • 切换shell chsh -s 指定的shell

  Xshell连接

  方式一:

  方式二:

 

  连接成功:

  命令提示符

[root@localhost ~]#
[xiaoxu@localhost ~]$
其中 # 表示管理员  $表示普通用户
[用户@主机名 目录]命令提示符

  显示提示符格式

[root@centos ~]# echo $PS1
[\u@\h \W]\$
[用户@主机名 当前目录] 命令提示符

 

  修改提示符格式

PS1="\[\e[1;7;47;30m\][\u@\h \W]\\$\[\e[0m\]"
\e 
\h 主机名简称
\w 当前工作目录 \t 24小时时间格式 \! 命令历史数
\u 当前用户
\H 主机名
\W 当前工作目录基名 \T 12小时时间格式
\# 开机后命令历史数
1表示字体加粗, 0表示默认字体。4表示给字体加上下划线。5表示字体闪烁。7表示用亮色突出显示,来让你的文字更加醒目
31表示字符颜色。
可选颜色:红色、绿色、黄色、蓝色、洋红、青色和白色。他们对应的颜色代码是:30(黑色)、31(红色)、32(绿色)、 33(黄色)、34(蓝色)、35(洋红)、36(青色)、37(白色)
40表示字符背景色。可选颜色 40、41、42、43、44、45、46、47

 

写到配置文件中

永久生效
echo 'PS1="\[\e[1;7;47;30m\][\u@\h \W]\\$\[\e[0m\]"' >> /etc/profile.d/ps.sh 

  执行命令

输入命令后回车

在shell中可以执行的命令分为两类

  • 内部命令 shell自带的命令 安装完系统以后自带的命令

    • help 内容命令列表
  • 外部命令

    • 第三方提供的,在文件系统路径下可以有对应的文件
  • 如何区分内部还是外部命令

type 查看命令的类型
which 查找命令的路径

[root@centos ~]#type cd
cd is a shell builtin
cd 是 shell 内嵌 [root@centos ~]#type cp
cp is aliased to `cp -i'
cp 是 `cp -i' 的别名 [root@centos ~]#which cp alias cp='cp -i' /usr/bin/cp

  命令别名 alias

 显示当前shell进程所有可用的命令别名 alias

 

定义别名name,其实就是相当于执行命令value

 

  • 在命令行中定义的别名,仅对当前shell进程有效

  • 如果想永久有效,要定义在配置文件中

    • 仅对当前用户有效:~/.bashrc    eg:[root@localhost ~]#echo "alias cdetc='cd /etc'" >> .bashrc
    • 对所有用户有效:/etc/bashrc                                            echo "alias cdetc='cd /etc'" >> /etc/bashrc
  • bash进程重新读取配置文件 source /path/to/config_file

  • 撤销别名 unalias -a 取消所有别名

[root@centos etc]#unalias cdetc
[root@centos etc]#cdetc
-bash: cdetc: 未找到命令

如果别名同原命令同名,如果要执行原命令,可以用

  • \aliasname
  • "aliasname"
  • 'aliasname'
  • /path/command
[root@centos ~]#ls
anaconda-ks.cfg  a.retry  a.txt  b.yml    shellinabox
ansible2         a.sh     a.yml  GateOne  syncmysql
[root@centos ~]#"ls"
anaconda-ks.cfg  a.retry  a.txt  b.yml      shellinabox
ansible2     a.sh      a.yml  GateOne  syncmysql
[root@centos ~]#'ls'
anaconda-ks.cfg  a.retry  a.txt  b.yml      shellinabox
ansible2     a.sh      a.yml  GateOne  syncmysql
[root@centos ~]#\ls
anaconda-ks.cfg  a.retry  a.txt  b.yml      shellinabox
ansible2     a.sh      a.yml  GateOne  syncmysql

  命令格式

command [OPTIONS…][ARGS….]

  • 选项:用于启用或者关闭命令的某个或者某些功能
    • 短选项:例如 -l -h -a
    • 长选项:例如 - - all - - help
  • 参数:命令的作用队形,比如文件名,用户名等
  • 注意:
    • 多个选项以及参数和命令之间使用空格分割
    • 取消和结束命令执行 ctrl+c
    • 多个命令可以用;隔开
    • 一个命令可以用\分成多行

  日期和时间 

    date

  命令格式

[root@localhost ~]#date --help
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

  修改时间

[root@localhost ~]#date 073117322019
Wed Jul 31 17:32:00 CST 2019

ntpdate time.windows.com  自动与时间服务器同步时间

  显示不同的时间格式

  [root@localhost ~]#date
  Wed Jul 31 17:33:41 CST 2019

1.显示年月日
[root@centos ~]#date +%F
2019-07-05
2.显示小时(24小时制)
[root@centos ~]#date +%H
10
3.显示小时(12小时制)
[root@centos ~]#date +%I
10
4.显示年
[root@centos ~]#date +%y
19
5.显示月
[root@centos ~]#date +%m
07
6.显示日
[root@centos ~]#date +%d
05
7.显示分钟
[root@centos ~]#date +%M
13
8.显示星期
[root@centos ~]#date +%a
五
[root@centos ~]#date +%A
星期五
9 显示全部时间
[root@centos ~]#date +%c
2019年07月05日 星期五 10时14分39秒
10.显示时分秒
[root@centos ~]#date +%T
10:15:08
11.自定义时间格式
[root@centos ~]#date +%y-%M-%d
19-16-05
12.显示自UTC 时间 1970-01-01 00:00:00 以来所经过的秒数
[root@centos ~]#date +%s
1562292991
13.一年中的第几周
[root@centos ~]#date +%W
26

  时区

[root@localhost ~]#timedatectl

      Local time: Wed 2019-07-31 17:34:46 CST
  Universal time: Wed 2019-07-31 09:34:46 UTC
        RTC time: Wed 2019-07-31 09:35:19
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

[root@localhost ~]#timedatectl set-timezone  Asia/Shanghai
[root@localhost ~]#time
time         timedatectl  timeout      times        
[root@localhost ~]#timedatectl set-timezone 
Display all 425 possibilities? (y or n)
Africa/Abidjan                  America/Menominee               Asia/Ulaanbaatar
Africa/Accra                    America/Merida                  Asia/Urumqi
Africa/Addis_Ababa              America/Metlakatla              Asia/Ust-Nera
Africa/Algiers                  America/Mexico_City             Asia/Vientiane
Africa/Asmara                   America/Miquelon                Asia/Vladivostok
Africa/Bamako                   America/Moncton                 Asia/Yakutsk
Africa/Bangui                   America/Monterrey               Asia/Yangon
Africa/Banjul                   America/Montevideo              Asia/Yekaterinburg
Africa/Bissau                   America/Montserrat              Asia/Yerevan
Africa/Blantyre                 America/Nassau                  Atlantic/Azores
Africa/Brazzaville              America/New_York                Atlantic/Bermuda
Africa/Bujumbura                America/Nipigon                 Atlantic/Canary
Africa/Cairo                    America/Nome                    Atlantic/Cape_Verde
Africa/Casablanca               America/Noronha                 Atlantic/Faroe
Africa/Ceuta                    America/North_Dakota/Beulah     Atlantic/Madeira
Africa/Conakry                  America/North_Dakota/Center     Atlantic/Reykjavik
Africa/Dakar                    America/North_Dakota/New_Salem  Atlantic/South_Georgia
Africa/Dar_es_Salaam            America/Ojinaga                 Atlantic/Stanley
Africa/Djibouti                 America/Panama                  Atlantic/St_Helena
Africa/Douala                   America/Pangnirtung             Australia/Adelaide
Africa/El_Aaiun                 America/Paramaribo              Australia/Brisbane
Africa/Freetown                 America/Phoenix                 Australia/Broken_Hill
Africa/Gaborone                 America/Port-au-Prince          Australia/Currie
Africa/Harare                   America/Port_of_Spain           Australia/Darwin
Africa/Johannesburg             America/Porto_Velho             Australia/Eucla
Africa/Juba                     America/Puerto_Rico             Australia/Hobart
Africa/Kampala                  America/Punta_Arenas            Australia/Lindeman
Africa/Khartoum                 America/Rainy_River             Australia/Lord_Howe
Africa/Kigali                   America/Rankin_Inlet            Australia/Melbourne
--More--
View Code

  日历

[root@localhost ~]#cal
      July 2019     
Su Mo Tu We Th Fr Sa
    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 31

cal -y 展示当年的日历
cal -y # 显示#年的日历

 

  关机重启

  重启命令

  • reboot
    • -f 强制,不调用shutdown
    • -p 切断电源
  • init 6

  关机命令

  • poweroff
  • halt
  • init 0

  关机或者重启

  • shutdown   默认是一分钟之后关机
    • -r reboot 重启
    • -h halt 关机
    • -c 取消
    • TIME 无指定,默认相当于+1
      • now 立刻
      • +m: 相当于多长会时间以后
      • hh:mm 绝对时间表示,知名具体的时间

  查看用户登陆信息

    • whoami: 显示当前登陆有效用户
    • who: 系统当前所有的登陆会话
    • w:系统当前所有的灯了会话及所做的事
[root@localhost ~]#whoami
root
[root@localhost ~]#who am i
root     pts/0        2019-07-31 17:00 (192.168.110.1)
[root@localhost ~]#who
root     :0           2019-07-31 16:44 (:0)
root     pts/0        2019-07-31 17:00 (192.168.110.1)
root     pts/1        2019-07-31 17:08 (:0)
[root@localhost ~]#w
 17:53:12 up  1:10,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       :0               16:44   ?xdm?  60.83s  0.93s /usr/libexec/gnome-session-binary
root     pts/0    192.168.110.1    17:00    0.00s  0.20s  0.03s w
root     pts/1    :0               17:08   45:02   0.02s  0.02s bash

  echo回显

功能:显示字符

说明:echo会将输入的字符串显示在标准屏幕上,输出的字符串间以空白字符隔开,并在最后加上换行号

[root@centos ~]#echo qwqe
qwqe
[root@centos ~]#echo da\
> sadasd\
> sadad\
> \dadadad
dasadasdsadaddadadad

显示变量

[root@centos ~]#name=alex
[root@centos ~]#echo '$name'
$name
[root@centos ~]#echo "$name"
alex

"" 可以直接打印变量的值

'' 引号里面写什么就打印什么

  获取环境变量

[root@localhost ~]#echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

  命令行扩展、集合

命令行扩展 把一个命令输出打印给另外一个命令的参数

  • $()
  • ``
[root@centos ~]#echo "i am `whoami`"
i am root

 

括号扩展,命令展开

[root@centos ~]#echo file{1,2,3}
file1 file2 file3
[root@centos ~]#echo file{1..10} 命令展开
file1 file2 file3 file4 file5 file6 file7 file8 file9 file10
[root@centos ~]#echo file{a..z}
filea fileb filec filed filee filef fileg fileh filei filej filek filel filem filen fileo filep fileq filer files filet fileu filev filew filex filey filez
[root@centos ~]#echo file{00..20..2} 指定步长
file00 file02 file04 file06 file08 file10 file12 file14 file16 file18 file20
[root@centos ~]#echo `seq 1 2 10`
1 3 5 7 9 

  命令引用

`date`
$(date)

[root@localhost ~]#echo `date`
Wed Jul 31 20:38:51 CST 2019
[root@localhost ~]#echo $(date)
Wed Jul 31 20:39:10 CST 2019

 

  tab键  命令补全

  • 命令补全

    • 内部命令:
    • 外部命令:bash根据PATH环境变量定义的路径,自左向右在每个路径搜索以给定命令命名的文件,第一次找到的命令就是要执行的命令
      • 用户给定的字符串只有一条唯一对应的命令,直接补全
      • 否则,再次tab会给出列表(要按两次tab键)
  • 路径补全

    • 把用户给出的字符串当做路径开头,并在其指定上级目录下搜索以指定的字 符串开头的文件名

      如果惟一:则直接补全 否则:再次Tab给出列表

  命令行历史

  • 保存你输入的命令历史,可以用来重复执行命令
  • 登录shell时,会读取历史文件中记录的命令~/.bash_history
  • 登录进shell后新执行的命令只会记录在缓存中;这些命令会用户退出时“追加”至命令历史文件中(正常退出才会有)
  • 重复前一个命令
    • 键盘的上下方向键,找到之后回车
    • 按!!并回车执行
    • 输入!-1并回车执行
    • 按ctrl+p并回车执行
  • !:0 执行前一条命令(去除参数)
  • !n 执行history命令输出对应序号n的命令
[root@localhost ~]#!123
which cp
alias cp='cp -i'
    /usr/bin/cp

 

  • !-n 执行history命令中倒数第n个命令
  • !string 重复前一个以“string”开头的命令
  • !?string 重复前一个包含“string”的命令
  • ctrl+r来在命令历史中搜索命令:
    • (reverse-i-search)`':
  • ctrl+g:从历史搜索模式退出
  • 重新调用前一个命令的最后一个参数
    • !$
    • esc,.(点击Esc键后松开,然后点击 . 键)
  • history
    • -c 清空命令历史
    • n 显示最近的n条记录

  bash的快捷键

  • ctrl+l 清屏,相当于clear命令
  • ctrl+o 执行当前命令,并重新显示本命令
  • ctrl+s 阻止屏幕输出,锁定
  • ctrl+q 允许屏幕输出
  • ctrl+c 终止命令
  • ctrl+z 挂起命令
  • ctrl+a 光标移动到行首,相当于home
  • ctrl+e 光标移动到行尾,相当于end
  • ctrl+xx光标在命令行首和光标之间移动
  • ctrl+u 从光标处删除至命令行首
  • ctrl+k 从光标处删除至命令行尾
  • alt+r 删除当前正行
  • alt+f 光标向右移动一个单词尾
  • alt+b 光标向左移动一个单词首
  • 需要注意: alt组合键经常和其他软件冲突

  获取帮助

  • 内部命令
    • help command 或者man bash
  • 外部命令
    • command --help
    • command -h
    • man command    q退出
    • 官方文档
    • google
  • - - help和-h选项
    • 显示用法总结和参数列表
    • 适用大多数,但并非所有
[root@centos ~]#date --help
用法:date [选项]... [+格式]
 或:date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
 []表示可选项
 <>表示变化的数据
 ...表示一个列表
 x|y|z 表示或者
 -abc 表示-a -b -c
 {}表示分组

  man章节

  • 1:用户命令                           Executable programs or shell commands
  • 2:系统调用                           System calls (functions provided by the kernel)
  • 3:c库调用                             Library calls (functions within program libraries)
  • 4:设备文件与特殊文件         Special files (usually found in /dev)
  • 5:配置文件格式                   File formats and conventions eg /etc/passwd
  • 6:游戏                                  Games
  • 7:杂项                                  Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
  • 8:管理类的命令                   System administration commands (usually only for root)
  • 9:Linux内核API                   Kernel routines [Non standard]

      箭头来控制输出

      回车输出下一行

      空格切换到下一屏

        q 退出man

 

转载于:https://www.cnblogs.com/Xiao_Xu/p/11266921.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值