Linux shell 基本命令

shell的基本命令

shell提供用户与内核、硬件交互,将command转化为内核指令控制硬件。

关机、重启

commanddescription
sudo shutdown -h <+time>关机或<定时关机>
sudo shoutdown -r <+time>重启或<定时重启>

shell命令格式

Command [-options] Argument Argument
指令 选项 参数1 参数2
1、一条命令的三要素之间用空格隔开
2、多个命令在一行书写,用 ; 将各命令隔开
3、如果命令不能再一行写完,在行尾用 \ 标明该命令未结束
4、可用tab命令补齐,按两下命令补齐,一下文件名补齐
5、echo ¥HISTSIZE查看历史记录容量,HISTSIZE=可直接赋值临时改变历史记录容量(.bashrc里可修改历史记录容量)

shell中的特殊字符

shell中的通配符

通配符含义实例
星号(*)匹配任意长度的字符串用file_*.txt, 匹配file_wang.txt、file_jie.txt、file_jird.txt
问号(?)匹配一个长度的字符用file_?.txt, 匹配file_r.txt、file_2.txt、file_t.txt
方括号([…])匹配其中指定的一个字符用file_[qwe].txt, 匹配file_q.txt、file_w.txt、file_e.txt
方括号([ - ])匹配指定的一个字符范围用file_[a-c].txt, 匹配file_a.txt、file_b.txt、file_c.txt
方括号([^…])除了其中指定的字符,均可匹配用file_[^ab].txt, 除了file_a.txt、file_b.txt的其他文件

ls -l file_*.txt
(~为家目录)

shell中的管道

管道可以把命令连起来使用,前一个命令的输出作为后一个命令的输入
jet@ubuntu:~$ ls ~ | wc -w
在这里插入图片描述
通过 | 得到家目录里的文件个数
grep 可以和 | 配合使用
grep为筛选
cat ~ | grep text.c

输入输出重定向

重定向符mainexample
>file将file文件重定向为输出源,新建模式ls /etc >file,新建file文件夹保存/etc目录下文件
>>file将file文件重定向为输出源,追加模式ls /etc >>file 将ls /etc执行的结果追加到file中已有内容后
<file将file文件重定向为输入源wc >file,将file中的内容作为输入传给wc
2>或&>将由命令产生的错误信息传入到文件中ls 1 2>file,将错误信息保存到file中

命令置换

命令置换就是将command2的输出作为command1的参数
command1 ·command2·
将command2的输出作为command1的参数

shell常用命令

man commandname
commandname的帮助页面

passwd username
修改密码

su
临时改变用户身份, -c表示执行一个命令后结束,-m保存环境变量不变,-环境变量更改

echo [-n] information
在显示器上显示information。-n表示不换行

date [-d | -s | -u] datestr
选项-s表示按照datestr日期显示格式设置日期;单独使用date命令,显示系统时钟中当前日期。时间格式为:“hh: mm: ss”,日期格式为:“mm/dd/yy”。改时间需要sudo权限

clear
clear用于清除屏幕信息(crtl+l)

df
df用于查看磁盘空间
常用参数:-a : 列出所有文件系统
-k : 列出磁盘的分配情况(KB)
-h : 同-k, 但大小以G、M、K单位显示
-l : 仅列出本地文件系统

du
列出目录和文件所使用的磁盘块数,每块占521字节 du -h /etc
常用参数:-a : 仅列出空闲的文件数
-h : 列出磁盘的使用情况(KB)
-s : 列出总的空闲空间(kb)

用户管理相关文件介绍

/etc/passwd文件是系统能够识别的用户清单

man 5 passwd
/etc/passwd contains one line for each user account, with seven fields delimited by colons (“:”). These fields are:

   ·   login name

   ·   optional encrypted password

   ·   numerical user ID

   ·   numerical group ID

   ·   user name or comment field

   ·   user home directory

   ·   optional user command interpreter

Linux@ubuntu:~$ vi /etc/passwd

Linux: x:1000:1000:fs:/home/fs:/bin/bash

/etc/group

NAME
group - user group file

DESCRIPTION
The /etc/group file is a text file that defines the groups on the system. There is one entry per line, with the fol‐
lowing format:

          group_name:password:GID:user_list

   The fields are as follows:

   group_name  the name of the group.

   password    the (encrypted) group password.  If this field is empty, no password is needed.

   GID         the numeric group ID.

   user_list   a list of the usernames that are members of this group, separated by commas.

adduser添加用户

Linux@ubuntu:/etc$ sudo adduser jiangtao
[sudo] password for fs:
Adding user jiangtao' ... Adding new groupjiangtao’ (1002) …
Adding new user jiangtao' (1001) with groupjiangtao’ …
Creating home directory /home/jiangtao' ... Copying files from/etc/skel’ …
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for jiangtao
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []: 123456
Home Phone []: 123456
Other []: 555
Is the information correct? [Y/n] y

adduser配置文件/etc/adduser.conf

skel模板

/etc/skel目录是被/uer/sbin/usersdd使用
把想要新用户拥有的配置文件中/etc/skel目录拷被

修改用户属性

usermod -option name
[-u uid] [-g group] [-G group,…] [-d home [-m]] [-s shell] [-c comment]
[-l new_name] [-f inactive] [-e expire] [-p passwd] [-L] [-U] name
linux@ubuntu:/home$ sudo username -d /home/jiangtao1 -m -l jiangtao1 jiangtao
修改家目录及用户名

deluser

语法: deluser
使用方法:
deluser --remove-home user
删除用户user的同时删除用户的工作目录

添加删除用户组

addgroup/delgroup
添加/删除组
sudo addgroup group1
sudo usermod -g group1 u1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值