Day 3 shell系统命令基础 Basic system commands of Shell
一 shell 介绍 What is Unix Shell
A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system to control the execution of the system using shell scripts.
Unix shell,通常被称作“命令行”,为Unix和类Unix操作系统提供了传统的用户界面。用户通过输入shell所执行的命令,引导计算机的操作。
二 shell 交互式环境 Interactive environment
[root@xxx ~]# #represents the command line for the admin user
[root@xxx ~]$ $represents the command line for a normal user
三 shell 命令语法 Grammar
组成Composition
Command 命令
Option 选项 format(-,--,+)
Parameter 参数
快捷键 shortcuts
Shortcut | Function |
---|---|
Ctrl + A | Go to the beginning of the line you are currently typing on |
Ctrl + E | Go to the end of the line you are currently typing on |
Ctrl + L | Clears the Screen, similar to the clear command |
Ctrl + U | Clears the line before the cursor position. If you are at the end of the line, clears the entire line. |
Ctrl + H | Same as backspace |
Ctrl + R | Let's you search through previously used commands |
Ctrl + C | Kill whatever you are running |
Ctrl + D | Exit the current shell |
Ctrl + Z | Puts whatever you are running into a suspended background process. fg restores it. |
Ctrl + W | Delete the word before the cursor |
Ctrl + K | Clear the line after the cursor |
Ctrl + T | Swap the last two characters before the cursor |
Esc + T | Swap the last two words before the cursor |
Alt + F | Move cursor forward one word on the current line |
Alt + B | Move cursor backward one word on the current line |
Tab | Auto-complete files and folder names |
历史命令 history commands
[root@xxx ~]# history //View history commands
[root@xxx ~]# history -c //Clear history commands
[root@xxx ~]# cat ~/.bash_history //view the history command saves files
ctrl+R //Find the history command and run it (It has to be continuous)
!220 //find the 220th command
!(character string) //find the command starts with the character string
!$ //quote the last parameter of the previous command
别名 alias
# alias //查看系统当前的别名 ll='ls -l --color=tty'
# alias egon='ls /etc/sysconfig/network- scripts/' //建立别名
# unalias egon
[root@xxx ~]# grep root /etc/passwd // 默 认有颜色
[root@xxx ~]# alias grep
alias grep='grep --color=auto'
四 命令查找优先级 How BASH Shell Command Search Sequence Works
-
Before a command is executed REDIRECTION is done. Then following sequence used by SHELL
-
ALIASES 别名
-
Parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable
-
Shell FUNCTION 函数
-
BUILTIN command 内置命令
-
HASH tables
-
PATH variable
-
If everything fails, you see command not found error message.
五 查看帮助信息 View the help information
man + command
--help
help + command
A useful website (In Chinese)
六 常用命令 common commands
change hostname
[root@iZm5e59rizbgmmp4net6zbZ ~]#
hostnamectl set-hostname aliyun
\#修改主机名称为aliyun
// 退出重新进入即可看到
change default runlevel
[root@iZm5e59rizbgmmp4net6zbZ ~]# systemctl set-default graphical.target // 图形界面 #设置系统默认启动级别为init 5,图形界面
[root@iZm5e59rizbgmmp4net6zbZ ~]# systemctl set-default multi-user.target // 字符终端 #设置系统默认启动级别为init 3,多用户界面
view ip address
# 查看 ifconfig #ifconfig为centos6版本使用,centos7 以上的版本执行ip address或者简写ip a
ifconfig eth0 #ifconfig为centos6版本使用, centos7以上的版本执行ip a show eth0
#查看网卡信息 ip a s ens33
set time
查看时间 # date "+%Y_%m_%d %H-%M-%S"
设置时间 #date -s "2018-05-17 09:51:50" 或者# timedatectl set-time '16:10:40 2022- 11-12 13:14:15'
Set the time zone
查看timedatectl status
列出可用时区 timedatectl list-timezones
设置# timedatectl set-timezone "Asia/Shanghai"
更新当前系统时间 # ntpdate ntp1.aliyun.com
将更新的时间保存到系统始终 # hwclock -w
Reboot
shutdown -r 10 // 10分钟后重启 shutdown -r 0 // 立即重启 shutdown -r now // 立即重启 shutdown -r 11:30 // 定点重启 init 6 // 立即重启 reboot // 立即重启
Close down
shutdown -h 10 // 10分钟后关机 shutdown -h 0 // 立刻关机 shutdown -h now // 立刻关机 shutdown -h 11:30 // 定点关机 halt // 立即停止系统,需要人工关闭电源 poweroff // 立即停止系统,并且关闭电源
Log out
exit logout ctrl+d
Day3 Exercise
1) 要求以root用户登录系统,右击桌面打开终端,查看当前登陆Linux系统所使用的用户名
whoami
2) 查看哪些用户在系统上工作
who
3) 修改当前时间为2018年8月26号11:28
date 082611282018
4) 查看2015年10月份日历
cal 10 2015
5) 使用两种方法查看ls命令的使用说明
man ls
ls --help
6) 清除屏幕
clear
Ctrl+L
7) 使用“useradd tom”命令新建tom用户,为tom用户设置密码“123”
useradd tom
123
8) 切换当前用户为tom
su tom