File lookup: Find command
grep: Text Search
Find : Text lookup
Locate: (UpdateDB manual production)
1.依赖于数据库 2.非实时查找,结果非精确 3.查找速度快 4.采用的模糊查找
Find
实时查找:速度慢
Format:
find [options] [Find path] [find condition] [processing action]
查找路径:默认为当前目录 查找条件:默认为查找指定路径下的所有文件 处理动作:默认为显示
Search criteria:
-name “文件名称”:支持使用globbing(文件通配) -iname "文件名称" : 根据姓名查找文件,不区分大小写 -user UserName :根据属主查找 -group GroupName : 根据属组查找 -nouser:查找没有属主的文件 -nogroup: 查找没有属组的文件 -type: 根据文件类型查找 f: 普通文件 d: 目录 b: 块设备 c: 字符设备 l: 符号链接文件 p: 命令管道 s: 套节子 -szie:根据文件大小查找 格式: -size[+|-]#Unit #:(#-1)<x<=# 例: -size +2MB 大于2兆的 -size -2MB 小于2兆的 -size 2MB 等于2兆的
Find by Time Pinch:
以天为单位(time): -atime [+|-]# -mtime -ctime -: 刚好# +: ># 以分钟为单位 -amin -mmin -cmin
Search by permissions:
-perm [+|-] MODE MODE : 精确匹配 + MODE:任何一类用户的任何一位权限匹配 (常用于查找某类的某特定权限是否存在) - MODE:每类用户的指定要检查的权限位都匹配
File wildcard:
*:表示任意长度的任意字符 ?:表示任意的单个字符 [] : 表示在指定范围内的单个字符:[a-z] [^]:脱字符,取反的意思
Combination conditions:
-a :与。同事满足 -o : 或,一个满足 -not , !非。取反
Practice:
1. Look for files in the/var directory that belong to the primary root and belong to the group Mail
find /var/ -user root -group mail
2. Find all files that are not root, bin, or Hadoop under the/usr directory
find /usr/ -not -user root -not -user bin -not -user hadoop find /usr/ -not \( -user root -o -user bin -o -user hadoop \)
3. Find files in the/etc directory that have been modified in the last week and that are not root or hadooop
find /etc -mtime -7 -not -user root -not -user hadoop
4. Find files that are not group or group on the current system and have been visited in the last one months
find / \(-nouser -o -nogroup\) -atime -30
5. Find all files that are larger than 1M in the earlier/etc/directory and are of the normal file type
find /etc/ -type f -size +1M
6. Find files with no write permission for all users in the/etc/directory
find /etc/ -not -perm +222 所有都没有;相反:任何一个有 所有都有:相反:只要有一个没有
7. Find at least one type of file without write permission in the/etc/directory
find /etc/ -perm -222
8. Find the/ETC/INIT.D directory where all users have permission to execute files that other users have write access to
find /etc/init.d/ -perm -111 -perm -002find /etc/init.d/ -perm -113
Linux task scheduling one-time task execution:
At
1.交互式 让用户在at>提示符输入多个要执行命令 格式:at time atq =at -L 查看作业 -q 字母 TIme 添加别的队列 at -d job_name 删除一个尚未执行的作业= atrm Ctrl+d :提价任务 at 作业有队列:使用单个字符表示2.批处理:将任务的各命令写入文件由at进行调用 at批模式: at -f 文件路径 TIME TIME: 模糊时间: now noon(中午) midnight(午夜) teatime(下午4点) tomorrow
The execution of the task results in the form of a meeting message sent to the submitter
Using mail
-s 指定主题 mail -s 主题 用户 mail 用户名 主题 内容 可以通过系统文件当作内容 或者用ehco echo -e "hello are you " | mail -s "to" root cat /etc/passwd | mail -s "ss" root
Batch: Cannot specify a time, it automatically selects the system to execute when idle
crond:守护进程,运行在后台,随时监视着进程等待执行 服务进程:阻塞,轮训
Recurring Task Execution
System cron:
文件: /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed
Time notation:
1.每个时间位都应该使用其可用的有效取值范围内的值2.某时间位上的*表示对应位的所有有效取值3. -:连续的相邻时间点取值4. ,:离散的时间点取值5. /# :表示在指定时间范围内每隔#一次 5-45/3 5到45分钟,每3分钟执行一次
Note: Reject messages with output redirection
&> /dev/null 错误 > /dev/null 2>/文件位置 成功的到/dev/null 成功到指定位置
User cron:
文件: /var/spool/sron/UserName
Use the crontab command to implement
-l:查看自己的cron任务列表 -e:通过EDITOR变量中定义的编辑器打开用户自己的cron配置文件: 编辑单独的任务都使用-e选项,无论是删除、修改还是新建 -r:移除crontab文件
If the administrator
-u UserName: 为别的用户指定crontab作业 #crontab -e -u 用户名
Reminder: If you use% in Conrtab user commands, you have to escape to \%
Anacron
Practice
1, every 3 minutes to perform an "echo" How is it? ””
2, weekly 2, 4, 6 back up the/etc/directory to the/backup directory, the file name of the backup is the file name that starts with the ETC_ and the date of the day
3 2 * * 2,4,6 /bin/tar -Jcf /backup/etc_`data ‘+/%F‘.tar.xz ` /etc/*
3.6,9,12,15,18 every day to see all the file systems currently mounted on the system and append the results to the/tmp/mounts.txt file
3 6,9,12,15,18 * * * * * /bin/mount>>/tmp/mounts.txt
4. Take the current system memory headroom every two hours per day and save it to the/stats/memory.txt file
34 */2 * * * /bin/grep "^MemFree:" /proc/meminfo >> /status/memory.txt
How to implement a second-level task
* * * * * * /bin/echo "how are your"; sleep 30; /bin/echo "how are you"
Format of crontab file
空白行会被忽略 #开头的行是注释
Anacron
1. 是crontab的补充,用于检查crontab中某任务在过去的一个周期内是否没有执行, 2. 如果没有执行,则在开机以后的某时间点让其执行一次,无论其周期是否到达
Facl: File access Control List
An ordinary user cannot safely authorize a file to be accessed by another user
Facl:
附加原有权限模型之上另一层权限控制机制,保存至文件扩展属性信息中
Command:
getfacel file... setfacel {-x|-m} 权限 file... -m:设定权限 -m u:UserName:Perms -m g:GroupName:Perms -m m::Perms -x:取消权限 -x u:UserName -x g:GroupName -x m: ll表示为+,有额外的权限属性 -R:递归,对文件夹内的文件也生效
Cyclic control of Bash programming
for varName in List; do 循环体donewhile CONDITION; do 循环体done
Loop control
continue:提前结束本次循环而开始评估下一轮 break [n]:结束当前循环,指定条吹几次循环
Shell Practice
Exercise: Ask for the sum of all even numbers within 100
#!/bin/bash declare -i sum=0 declare -i i=1 while [ $i -le 100 ];do if [ [ $i%2 ] -eq 0 ];then let sum+=$i fi let i++ done
Write a script
1. Let the user enter a block device, if the device file does not exist, prompt the user to re-enter, know that the user entered the correct
2. User can enter quit to quit
#!/bin/bash# while true; do read -p "Enter a dev " devdir if [ "$devdir" == "quit" ];then break fi if [ -b $devdir ];then break else echo "wrong device file" fidone
Extend the previous question
1, display the user input block device, and prompt the user, subsequent actions will damage all files on the device, let the user choose whether to select
2. If the user enters Y. Then continue to the following operation
3. If the user enters N, the user selects terminate and exits the script
4. Enter any other characters, then the user re-selects
#!/bin/bash # while true; do read -p "Enter a dev " devdir if [ "$devdir" == "quit" ];then break fi if [ -b $devdir ];then echo "$devdir is very good !" read -p "Are you suer [y|b]" devop devop=echo $devop | tr ‘A-Z‘ ‘a-z‘ if [[ "$devop" == "y" ]];then echo "no xuanze le yes" elif [[ "$devop" == "n" ]];then break else continue fi else echo "wrong device file" fi done
Exercise: Write a script
1. Prompt the user to enter a directory path
2, display the file name with at least one uppercase letter in the directory
#!/bin/bashwhile true;do read -p "Enter a directory: " dirname [ "$dirname" == "quit" ] && exit 3 [ -d "$dirname" ] && break || echo "wrong directory..."done for filename in $dirname/*;do if [[ "$fileName" =~ .*[[:upper:]]{1,}.* ]]; then echo "$fileName" fidone
Exercise: Write a script
Prerequisite: Configure the Yum source.
1. If the machine does not have a available Yum source, prompt the user and exit the script (4); If the script is not executed as root, then only root is shown to install the package, and then exit (3);
2, prompt the user to enter a package name, and then use the Yum automatic installation, as far as possible, do not output the Yum command execution information;
If the installation is successful, the green is displayed, otherwise the red display fails;
3, if the user input package does not exist, the display error after the user to continue to enter;
4, if the user input quit, the normal exit (0);
5, before the normal exit, display the number of locally installed packages;
#!/bin/bashwhile true;doif [ $UID -ne 0 ]; then echo "`basename $0` must be running as root" exit 3fiyum repolist &> /dev/nullif [[ $? -eq 0 ]];then while true;do read -p "Enter a chengxubao: " baoname if [[ "$baoname" == "quit" ]];then rpm -qa |wc -l exit 0 fi yum list |grep "^$baoname.*" &> /dev/null if [[ $? -eq 0 ]];then yum install $baoname -y &> /dev/null if [[ $? -ne 0 ]];then echo "$baoname not ok" else echo "$baoname is ok!" exit 0 fi else continue fi done else echo "yum is not ok!!" exit 4fidone
Linux Learning log (ix)