Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

8. Periodic tasks, find, break and continue study notes

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

find [options] [find path] [find condition] [process action]

Search path: default to current directory

Search condition: default to search all files under the specified path

Processing action: default to display

Search conditions:

-name "File name" Support for globbing regular expressions

-iname "File name" Search case insensitive

-user UserName Search by owner

-group GroupName Find by group

-uid UID Find by uid

-gid GID Find by gid

-nouser Find files without owners

-nogroup Find files that have no group

Combination conditions:

-a and, simultaneously satisfying

-o or, satisfy one

-not, ! Not, negate

-type: Search by file type

f: General documents

d: Contents

b: Block equipment

c: Character device

l: Symbolic link file

p: named pipe

s: socket

-size: Search according to file size (common units: k, M, G)

-#M Find all files smaller than #M

+#M Find all files larger than #M

#M Find all files of size #M

Find by timestamp:

In days (time):

-atime

-mtime

-ctime

+#Find files accessed (#+1) days away

-#Find files accessed in #days

Find time periods shorter than (#+1)> x >=#days visited

In minutes (min):

-amin

-mmin

-cmin

+#Find files accessed outside of (#+1) minutes

-#Find files accessed #Day of the Day

Find time periods shorter than (#+1)> x >=#minutes visited

Search by permission:

-perm [+|-]MODE

MODE Exact matching

+MODE Any permission of any class of users matches; often used to find out whether a specific permission of a certain class of users exists;

-MODE The permission bits specified to be checked match for each type of user;

File permissions: 644

-perm 600 No, because no match 644

-perm +222 Yes, because any bit has 2 permissions

-perm +002 No, because none of the permissions match.

-perm -444 Yes, because every 4 permissions match.

Exercise:

1. Find all files in the/var/directory that belong to root and belong to mail;

# find /var/ -user root -a -group mail

2. Find files in/usr directory that are not root, bin or hadoop;

# find /usr/ -not -user root -a -not -user bin -a -not -user hadoop

# find /usr/ -not \( -user root -o -user bin -o -user hadoop \)

3. Find files in/etc/directory whose contents have been modified in the last week and do not belong to root or hadoop;

# find /etc/ -mtime -7 -a -not -user root -a -not -user hadoop

# find /etc/ -mtime -7 -a -not \( -user root -o -user hadoop \)

4. Find files that have no owner or group on the current system and have been accessed in the last month;

# find / \( -nouser -o -nogroup \) -a -atime -30

5. Find all files in/etc/directory that are larger than 1M and of type ordinary files;

# find /etc/ -size +1M -a -type f

6. Find files in/etc/directory where all users do not have write permissions;

# find /etc/ -not -perm +222

None of them: the opposite: any of them has

All have: Opposite: At least one has not

7. Find at least one type of user in the/etc/directory who does not have write permission;

# find /etc/ -not -perm -222

8. Find files in/etc/init.d/directory where all users have execute permissions and other users have write permissions;

# find /etc/init.d/ -perm -113

Processing actions:

-print By default prints on standard output

-ls outputs information about each file in long format

-exec COMMAND {} \; Executes the specified command on the found file

-ok COMMAND {} \; interactive-exec;

| xargs COMMAND

Find additional material (from the Internet):

find and xargs

When processing matched files using the-exec option of the find command, the find command passes all matched files together to exec for execution. However, some systems limit the length of commands that can be passed to exec, so that overflow errors occur a few minutes after the find command runs.

Error messages are usually "parameter column too long" or "parameter column overflow." This is where the xargs command comes in handy, especially when used with the find command.

The find command passes matching files to xargs, which fetches only a few files at a time, not all of them, unlike the-exec option. This way it can process the first batch of files, then the next batch, and so on.

In some systems, using the-exec option will initiate a corresponding process for processing each matched file, rather than executing all matched files as parameters at once; in some cases, there will be too many processes, and the system performance will decrease, so the efficiency is not high;

With xargs, there is only one process. In addition, when using the xargs command, whether to obtain all parameters at once or obtain parameters in batches, and the number of parameters obtained at each time will be determined according to the options of the command and the corresponding adjustable parameters in the system kernel.

Special permissions for files:

Executable file: suid

Any user executing this executable will no longer be treated as the owner of the process as the user himself, but rather as the owner of the file

suid represents s or S on the file belongs to the main execution permission bit, s for those with execution permission and S for those without execution permission

How to set suid permissions:

chmod u+s FILE

Catalog file: sgid

Directory with sgid, where when a user creates a file, the group of the new file is no longer the base group to which the user belongs, but the group of the directory

sgid is represented as s or S on the file belongs to group execution permission bit, s for those with execution permission and S for those without execution permission

How to set sgid permissions:

chmod g+s FILE ...

Sticky: sticky

For publicly writable directories, users can create files and delete their own files, but they cannot delete other users 'files.

sticky indicates t or T on the execution permission bit for other users of the file, t for those with execution permission and T for those without execution permission

How to set sticky permissions:

chmod o+t FILE ...

Exercise:

1. Copy the cat command to the/tmp directory. Ordinary users can use the/tmp/cat command to view all files that root has permission to view.

cp `which cat` /tmp/

cp /etc/shadow /tmp/

chmod u+s cat

2. Create a new/tmp/test directory:

Openstack and docker users are required to have write permissions on them, and all files created in the directory belong to cloud groups.

Ask each user not to delete someone else's files, but to edit them

mkdir /tmp/test

groupadd cloud

useradd -G cloud openstack

useradd -G cloud docker

chmod g+w /tmp/test/

chown :cloud /tmp/test

chmod g+s test/

Linux Mission Plan:

One-time task execution:at

Periodic task execution: crontab

One-time mission execution:

at:

interactive: let that user ent multiple commands to execute at the at> prompt;

at TIME

at>

Ctrl+d: Submit the task;

Batch processing: write the commands of the task into files and call them by at;

at -f /path/to/at_job_file TIME

at Job has queue: use a single letter to indicate

View jobs: at -l = atq

Delete an unexecuted job:

at -d job_num

atrm job_num

The execution result of the task plan will be sent to the task submitter by mail;

Mail command:

interactive mode receiving mail;

Interactive mode Send mail:

-s "Subject"

< /path/to/somefile 周期性任务计划:cron crond: 守护进程 服务进程: 阻塞,轮询 系统cron: 文件:/etc/crontab 用户cron: /var/spool/cron/UserName /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 时间表示法: 1、每个时间位都应该使用其可用的有效取值范围内的值; 2、某时间位上的*表示对应位的所有有效取值; 3、-: 连续的时间相邻点取值; 4、,: 离散的时间点取值; 5、/#:表示在指定时间范围内每隔#一次; 注意:通过输出重定向而拒收邮件: 将执行结果重定向到/dev/null &>

/dev/null

Send mail to empty user

MAILTO=""

User cron:

Use the crontab command to do this

-l: View your cron task list;

-e: Open the user's own cron profile via the editor defined in the EDITOR variable;

Edit individual tasks using the-e option, whether deleted, modified, or created;

-r: Remove crontab file

If it is an administrator:

-u UserName: Configure crontab jobs for other users;

# crontab -e -u docker

Reminder: If you use % in a crontab user command, the escape is\%

5 3 * * * /bin/touch ~/testfile_`date +\%Y-\%m-\%d`.txt

After using single quotes, % can also be escaped

5 3 * * * /bin/touch ~/testfile_`date +'%Y-%m-%d'`.txt

Exercise:

1) How are you? ";

2. Backup the/etc/directory to the/backup directory every 2, 4, and 6 weeks. The file name of the backup starts with the date of the day and follows the date of the day as the file name;

3. Check all file systems mounted on the current system every 6, 9, 12, 15, and 18 days, and append the results to the/tmp/mounts.txt file;

4. Take the current system memory space balance every two hours every day and save it to the/stats/memory.txt file;

crontab file format:

Blank lines are ignored

#The first line is a comment;

1、*/3 * * * * /bin/echo "how are you? "

2、3 2 * * 2,4,6 /bin/tar -Jcf /backup/etc_`date '+%F'`.tar.xz /etc/*

3、17 6,9,12,15,18 * * * /bin/mount >> /tmp/mounts.txt

4、34 */2 * * * /bin/grep "^MemFree:" /proc/meminfo >> /stats/memory.txt

How to accomplish second-level missions:

* * * * * for i in {0.. 4}; do /bin/echo "how are you? "; sleep 10; done

facl: file access control list

facl: Attach another layer of permission control mechanism above the original permission model and save it to the file extension attribute information;

getfacl FILE Get permission information for a file

setfacl {-x|-m} permissions FILE sets permissions for files

-m: Set permissions

-m u:UserName:Perms

-m g:GroupName:Perms

-m m::Perms

-x: Cancel permissions

-x u:UserName

-x g:GroupName

-x m:

-R: Recursive

bash programming cycle control:

for varName in LIST; do

loop body

done

while CONDITION; do

loop body

done

until CONDITION; do

loop body

done

Cycle control:

continue: End the cycle early and start evaluating the next round;

break [n]: Jump out of the current loop

Exercise: prompt user for username, display user ID number; until user enters quit;

#!/ bin/bash

#

if [ $UID -ne 0 ]; then

echo "`basename $0` must be running as root"

exit 1

fi

while true; do

read -p "Enter a user name: " userName

if [ "$userName" == 'quit' ]; then

break

fi

id -u $userName

done

Exercise: Writing a Script

1. Prompt the user to enter a directory path;

2. Show the file name containing at least one capital letter under the directory;

#!/ bin/bash

#

while 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"

fi

done

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report