批量创建用户的简单方法
1、借助notepad++或excel创建
在notepad++内新建一个文件,按住alt选中你需要添加用户数量的列数;
输入useradd user,然后按alt+c进行列编辑,选择插入数字,初始值、增量都是1,勾选开头补零,确定后即可生成批量创建用户脚本;
2、用shell脚本的循环语句(简单方法)
for num in {01..20}; do useradd user$num; done
验证:
for num in {01..20}; do id user$num; done
免交互式设置密码,可用notepad++批量设置,需要先创建用户:
echo 123456| passwd --stdin user01
可将创建用户和设置密码两步结合,';'和'&&'都可以表示前一命令执行后再执行后面的命令:
for num in {01..20}; do useradd user$num; echo 123456 |passwd --stdin user$num; done
文件属性介绍
文件类型查看工具file
参数:
- -b 列出辨识结果时,不显示文件名称。
- -c 详细显示指令执行过程,便于排错或分析程序执行的情形。
- -f<名称文件> 指定名称文件,其内容有一个或多个文件名称时,让file依序辨识这些文件,格式为每列一个文件名称。
- -L 直接显示符号连接所指向的文件的类别。
- -m<魔法数字文件> 指定魔法数字文件。
- -v 显示版本信息。
- -z 尝试去解读压缩文件的内容。
- [文件或目录...] 要确定类型的文件列表,多个文件之间使用空格分开,可以使用shell通配符匹配多个文件。
linux系统中常见文件类型
'd' --- 目录文件类型
'-' --- 普通文件
①.纯文本文件 text /etc/hosts --- 文本文件(可以直接编辑查看)
②.二进制文件 binary /bin/ls --- 命令文件(不可编辑可执行)
③.数据型文件 data /tmp/etc.tar.gz --- 数据文件(压缩文件)
'l' --- 链接文件
'c/b' --- 块文件(block)/字符文件(设备文件character)
磁盘设备文件 /dev/sda
随机设备文件 /dev/urandom
输出设备文件 /dev/zero
输入设备文件 /dev/null
's' --- socket文件(网络编程)
Linux扩展命令简述:
a 显示文件信息类型命令:file
b 显示命令绝对路径信息:which (which只会去指定的环境变量查找$PATH)
[root@krist /]# which cat
/usr/bin/cat
c 显示命令路径以及文件:whereis(和which一样,区别只是会显示命令的帮助文件)
whereis命令主要用于查找程序文件,并提供这个文件的二进制可执行文件、源代码文件和使用手册页存放的位置。例如,查找find命令:
[root@krist /]# whereis find
find: /usr/bin/find /usr/share/man/man1/find.1.gz
可以使用-b参数让whereis命令只查找这个程序的二进制可执行文件:
[root@krist /]# whereis -b find
find: /usr/bin/find
d 登录用户信息查看:who(有哪些人登录,以及都工作在哪个控制台上)
[root@krist /]# who
root tty1 2020-08-12 22:19
root pts/2 2020-08-12 13:01 (192.168.123.1)
whoami(查看自己是以什么身份登录系统的)
e 查看命令帮助手册:man
man命令在显示手册页时实际调用的是less程序,man手册一般被分成以下9节:
目 录 | 内 容 |
---|---|
/usr/share/man/man1 | 普通命令和应用程序 |
/usr/share/man/man2 | 系统调用 |
/usr/share/man/man3 | 库调用,主要是libc()函数的使用文档 |
/usr/share/man/man4 | 设备驱动和网络协议 |
/usr/share/man/man5 | 文件的详细格式信息 |
/usr/share/man/man6 | 游戏 |
/usr/share/man/man7 | 文档使用说明. |
/usr/share/man/man8 | 系统管理命令 |
/usr/share/man/man9 | 内核源代码或模块的技术指标 |
f 获取命令简介:whatis(程序的简单介绍,帮助用户了解这个程序的大致用途)
[root@krist /]# whatis find
find (1) - search for files in a directory hierarchy
g 搜索对应功能的命令:apropos(可以通过使用手册反查到某个命令)
[root@krist /]# apropos search //查找命令介绍中有search的命令
abrt-action-analyze-ccpp-local (1) - Generates backtrace and search for duplicates
apropos (1) - search the manual page names and descriptions
ausearch (8) - a tool to query audit daemon logs
ausearch-expression (5) - audit search expression format
badblocks (8) - search a device for bad blocks
bzgrep (1) - search possibly bzip2 compressed files for a regular expression
find (1) - search for files in a directory hierarchy
git-bisect (1) - Find by binary search the change that introduced a bug
manpath (1) - determine search path for manual pages
oldfind (1) - search for files in a directory hierarchy
Pod::Simple::Search (3pm) - find POD documents in directory trees
probe::nfs.proc.lookup (3stap) - NFS client opens/searches a file on server
probe::nfsd.lookup (3stap) - NFS server opening or searching file for a file for cl...
probe::nfsd.proc.lookup (3stap) - NFS server opening or searching for a file for cl...
Search::Dict (3pm) - look - search for key in dictionary file
vgscan (8) - Search for all volume groups
xzegrep (1) - search compressed files for a regular expression
xzfgrep (1) - search compressed files for a regular expression
xzgrep (1) - search compressed files for a regular expression
zgrep (1) - search possibly compressed files for a regular expression
zipgrep (1) - search files in a ZIP archive for lines matching a pattern
h 快速定位文件的所在路径:locate
比find更快,locate并没有进入子目录搜索,而是通过检索文件名数据库来确定文件的位置。locate自动建立整个文件名数据库,如果希望立刻生成该数据库文件的最新版本,可以使用updatedb命令,更新整个数据库大概耗时1分钟,需要root用户权限。
由于locate默认未安装,通过yum provides命令可以查看locate命令属于哪个软件包,然后安装locate命令
[root@krist ~]# yum provides locate
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base/7/x86_64/filelists_db | 7.1 MB 00:00:32
epel/x86_64/filelists_db | 11 MB 00:00:41
mlocate-0.26-8.el7.x86_64 : An utility for finding files by name
Repo : base
Matched from:
Filename : /usr/bin/locate
[root@krist ~]# yum install -y mlocate
[root@krist ~]# locate ifcfg-eth0 //首次使用会报错,需要使用updatedb建立索引
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
[root@krist ~]# updatedb
[root@krist ~]# locate ifcfg-eth0 //可以正确定位文件
/etc/sysconfig/network-scripts/ifcfg-eth0
i 指定范围搜索工具:find
find命令基本语法:
find path -option [-print] [-exec -ok |xargs |grep] [command {} \;]
find命令格式参数:
1)path: 要查找的目录路径。
- ~ 表示$HOME目录
- . 表示当前目录
- / 表示根目录
2)print: 表示将结果输出到标准输出。
3)exec: 对匹配的文件执行该参数所给出的shell命令。
形式为command {} \;,注意{}与\;之间有空格
4)ok: 与exec作用相同,
区别在于,在执行命令之前,都会给出提示,让用户确认是否执行
5)|xargs: 与exec作用相同 ,起承接作用
区别在于 |xargs 主要用于承接删除操作 ,而 -exec 都可用 如复制、移动、重命名等
6)options: 表示查找方式
options常用选项:
-name name,-iname name: 文件名称符号name的文件,iname会忽略大小写
-size n : 文件大小是n单位,b代表512位元组的区块,c表示字节数,k表示kilo bytes,w是二个位元组
-type xxx: 文件类型是xxx的文件。xxx的参数有:
d:目录文件 c:字符设备文件 b:块设备文件 f:一般文件 l:链接文件 p:命名管道 s:socket网络编程文件
-mount, -xdev : 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件
-anewer file : 比文件 file 更晚被读取过的文件
-amin n : 在过去 n 分钟内被读取过
-cmin n : 在过去 n 分钟内被修改过
-atime n : 按文件访问时间来查找文件
-mtime n: 按文件更改时间来查找文件
-ctime n : 按文件创建时间来查找文件
-cnewer file : 比文件 file 更新的文件
-empty : 空的文件-gid n or -group name : gid 是 n 或是 group 名称是 name
-ipath p, -path p : 路径名称符合 p 的文件,ipath 会忽略大小写
-pid n : process id 是 n 的文件
-perm: 按执行权限来查找
-user username: 按文件属主来查找
-group groupname : 按组来查找
-nogroup: 查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser: 查无有效属主的文件,即文件的属主在/etc/passwd中不存
-follow: 如果遇到符号链接文件,就跟踪链接所指的文件
-prune: 忽略某个目录
1、查找指定路径中指定文件名的文件
[root@krist ~]# find /etc/ -name passwd
/etc/pam.d/passwd
/etc/passwd
实时监控当前目录
watch -n 1 ls -l .
2、表示与的连接参数查找(-a/-o/not)
默认为-a并且。-o表或者。-not表示非,即反向选择。
[root@krist www]# find . -user root -a -group root
3、查找最近100天内没有使用过的命令
[root@krist /]# find /usr/bin/ -type f -atime +100 -print
4、按文件所在的深度(层次)查找
-maxdepth表示最大深度
[root@krist /]# find /etc/ -maxdepth 1 -name passwd
/etc/passwd
[root@krist /]# find /etc/ -maxdepth 2 -name passwd
/etc/pam.d/passwd
/etc/passwd
-mindepth表示最小深度
[root@krist /]# find /etc/ -mindepth 2 -name passwd
/etc/pam.d/passwd
[root@krist /]# find /etc/ -mindepth 1 -name passwd
/etc/pam.d/passwd
/etc/passwd
5、按文件大小查找
可以使用dd命令复制和转换出一个指定块大小的文件
[root@krist www]# dd if=/dev/zero of=file1 bs=1 count=10240
10240+0 records in
10240+0 records out
10240 bytes (10 kB) copied, 0.0144327 s, 710 kB/s
使用du命令查看文件所占磁盘的大小
[root@krist www]# du -sh *
12K file1
[root@krist www]# dd if=/dev/zero of=file2 bs=1 count=20480
[root@krist www]# dd if=/dev/zero of=file3 bs=1 count=40960
使用find命令查找
[root@krist www]# find /www/ -size 20k
/www/file2
[root@krist www]# find /www/ -size +20k
/www/file3
[root@krist www]# find /www/ -size -20k
/www/
/www/file1
6、按文件类型来查找
find /dev/ -type f //表普通文件
find /dev/ -type b //表块设备
find /dev/ -type s //表套接字文件
find /dev/ -type p //表示命名管道
find /dev/ -type d //表目录
7、按文件权限查找
find /www -perm 404 //查找文件权限为404的文件
8、按文件更新时间查找
ctime 与 cmin 都表示按照时间查找被篡改的文件
ctime 以天为单位
cmin 以分钟为单位
-cmin 10 查找文件更新距离现在10分钟的文件
-cmin +10 查找文件更新距离现在超过10分钟的文件
-cmin -10 查找文件更新距离现在10分钟以内的文件
-ctime +/-10 查找文件更新距离现在超过10天/10天以内的文件
9、对查找到的文件执行某些操作
[root@krist www]# ll
total 72
-rw-r--r--. 1 root root 10240 Aug 16 20:33 file1
-rw-r--r--. 1 root root 20480 Aug 16 20:41 file2
-rw-r--r--. 1 root root 40960 Aug 16 20:41 file3
[root@krist www]# find /www/ -size 20k -exec chmod g+w {} \;
[root@krist www]# ll
total 72
-rw-r--r--. 1 root root 10240 Aug 16 20:33 file1
-rw-rw-r--. 1 root root 20480 Aug 16 20:41 file2
-rw-r--r--. 1 root root 40960 Aug 16 20:41 file3
将系统中属于mail组的文件备份到/www下
[root@krist www]# find / -group mail -exec cp {} /www \;
j 显示目录详细结构:tree
tree基本语法:
tree [-aACdDfFgilnNpqstux][-I <范本样式>][-P <范本样式>][目录...]
参数:
-a 显示所有文件和目录。
-A 使用ASNI绘图字符显示树状图而非以ASCII字符组合。
-C 在文件和目录清单加上色彩,便于区分各种类型。
-d 显示目录名称而非内容。
-D 列出文件或目录的更改时间。
-f 在每个文件或目录之前,显示完整的相对路径名称。
-F 在执行文件,目录,Socket,符号连接,管道名称名称,各自加上"*","/","=","@","|"号。
-g 列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码。
-i 不以阶梯状列出文件或目录名称。
-L level 限制目录显示层级。
[root@krist /]# tree -L 1 /
/
├── bin -> usr/bin
├── boot
├── dev
├── etc
├── home
├── lib -> usr/lib
├── lib64 -> usr/lib64
├── lost+found
├── media
├── mnt
├── opt
├── proc
├── root
├── run
├── sbin -> usr/sbin
├── srv
├── sys
├── tmp
├── usr
├── var
└── www
21 directories, 0 files
-l 如遇到性质为符号连接的目录,直接列出该连接所指向的原始目录。
-n 不在文件和目录清单加上色彩。
-N 直接列出文件和目录名称,包括控制字符。
-p 列出权限标示。
-P <范本样式> 只显示符合范本样式的文件或目录名称。
-q 用"?"号取代控制字符,列出文件和目录名称。
-s 列出文件或目录大小。
-t 用文件和目录的更改时间排序。
-u 列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码。
-x 将范围局限在现行的文件系统中,若指定目录下的某些子目录,其存放于另一个文件系统上,则将该子目录予以排除在寻找范围外。
评论前必须登录!
立即登录 注册