1. Give execution permission , specify file execution through a specific file path;
2. Run the interpreter directly and run the script as a parameter of the interpreter program;
1. Data storage format;
2. Participating operations;
0 && 1 = 0
0 && 0 = 0
or:
1 || 1 = 1
1 || 0 = 1
0 || 1 = 1
0 || 0 = 0
Not:
! 1 = 0
! 0 = 1
##Short circuit operation:
And:
The first one is 0, the result must be 0;
The first one is 1, the second one must be Participate in the operation;
or:
The first one is 1, the result must be 1;
The first one is 0, and the second one must participate in the operation;
grep:
Text Processing Three Musketeers on Linux
grep: text filtering (pattern) tool;
grep, egrep, fgrep
sed: stream editor, text editing tool;
awk: implementation of gawk on Linux, text report generator;
grep: Global search REgular expression and Print out the line.
Function: text search tool, based on the user-specified "pattern" to match the target text line by line; print out the match to the line;
Pattern: filter conditions written by regular expression characters and text characters;
REGEXP: composed of a special type Patterns written by characters and text characters, some of which do not represent the literal meaning of the characters, but represent control or wildcard functions;
is divided into two categories:
Basic regular expression: BRE
Extended regular expression: ERE
grep -E, egrep
Regular expression engine
##grep [OPTIONS] PATTERN [FILE...]
Options:
--color=auto: Color the matched text;
-v: Display lines that cannot be matched by pattern;
-i: Ignore character case;
-o: Only display the matched string;
-q: Silent mode, no information is output;
-A #: after, after # lines
-B #: before, before # lines
-C #: context, before and after # lines
-E: Use ERE;
##Basic regular expression metacharacters:
Character matching:
.: Matches any single character;
[]: Matches within the specified range Any single character
[^]: Matches any single character outside the specified range
[:digit:], [:lower: ], [:upper:], [:alpha:], [:alnum:], [:punct:], [:space:]
##Number of matches: Used after the character to be specified a number of times, used to specify the number of times the previous character is to appear;
*: Match the previous character any number of times;
For example: grep "x*y"
abxy
xay
xxxxxxy
Greedy mode
.*: Any character of any length;
\?: Matches the preceding character 0 or 1 times; that is, the preceding character is optional;
\+: Matches the preceding character characters at least once;
\{m\}: Match the previous character m times;
\{m,n\}: Match the previous character at least m times, At most n times;
\{0,n\}: Match the previous character at most n times;
\{m,\} : Match the previous character at least m times;
Position anchoring:
^: Beginning of line anchoring; Used for the far left side of the pattern;
$: end-of-line anchoring; used for the far right side of the pattern;
^PATTERN$ : Used to pattern match the entire line;
^$: Empty line;
^[[:space:]]*$
\< or \b: word-initial anchor; used on the left side of the word pattern;
\> ; or \b: word ending anchor; used for the right side of the word pattern;
\
: matches the entire word;
Grouping:
\(\): Bundle one or more characters together and process them as a whole;
\(xy\)*ab
Note: The content matched by the pattern in the grouping brackets will be recorded by the regular expression engine Among the internal variables, the naming method of these variables is: \1, \2, \3, ...
\1: starting from the left, the first opening bracket And the characters matched by the pattern between the right brackets;
\(ab\+\(xy\)*\):
\1: ab\+\(xy\)*
\2: xy
Back reference: Reference the characters matched by the pattern in the preceding grouping brackets (not the pattern itself)
If user root exists, display its default shell Program;
id root &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7
Find the two or three digits in /etc/passwd;
grep "\<[0-9]\ {2,3\}\>" /etc/passwd
Display /etc/rc.d/rc.sysinit file, starting with at least one blank Lines starting with characters and followed by non-blank characters;
# grep "^[[:space:]]\+[^[:space:]]" /etc/grub2 .cfg
Find the lines ending with 'LISTEN' followed by 0, 1 or more blank characters in the results of the "netstat -tan" command;
# netstat -tan | grep "LISTEN[[:space:]]*$"
Add user bash, testbash, basher and nologin (its shell is /sbin/nologin); then find the line in the /etc/passwd file where the user name is the same as the shell name;
grep "^\([ [:alnum:]]\+\>\).*\1$" /etc/passwd
Write a script to implement the following functions
If user1 exists, display its existence, otherwise add it;
Display the ID number and other information of the added user;
#!/bin/bash
##id user1 &> /dev/null && echo "user1 exists." || useradd user1
id user1
egrep and extended regular expressions
##egrep = grep - E
egrep [OPTIONS] PATTERN [FILE...]
##Extended regular expression Metacharacters of the formula:
Character matching:
.
[]
[^]
Number of matches:
##*
?: 0 or 1 times;
+: 1 or more times;
{m}: Match m times;
{m,n}: at least m, at most n times;
Anchor:
^
##$
\<, \b
\>, \b
##Group:
()
Backreferences: \1, \2, ...
Or:
a|b
##C|cat: C or cat
1. Display the current The default shell and UID of the system root, centos or user1 user;
# grep -E '^(root|centos|user1)\>' /etc/passwd | cut -d : -f1,3,7
2. Find a word in the /etc/rc.d/init.d/functions file (centos6) followed by A line of parentheses;
# grep -E -o "^[_[:alpha:]]+\(\)" /etc/rc.d/init.d /functions
3. Use echo to output an absolute path, and use egrep to get its base name;
# echo "/mnt/sdc" | grep -E -o "[^/]+/?$" | cut -d"/" -f1
further Ground: Use egrep to get the directory name of the path, similar to the result of the dirname command;
4. Find the value between 1-255 in the ifconfig command result ;
5. Find the IP address in the ifconfig command result;
fgrep : Does not support regular expression search;
Basic features of bash (4)
Variable type:
Data storage format, storage space size, participating operation types;
Character type
Numeric type:
Integer type
Floating point type
Strong typing: The type must be specified when defining a variable, and the type must meet the type requirements when participating in operations; calling undeclared variables will generate an error;
Weak type: No need to specify the type, the default is character type; implicit type conversion will be automatically performed when participating in the operation; variables can be called directly without being defined in advance;
bash
Types of variables in bash:
According to standards such as the effective range of variables:
Local variables: the effective scope is the current shell process; it is invalid for other shell processes other than the current shell, including subshell processes of the current shell;
Environment variables: effective The scope is the current shell process and its sub-processes;
Local variables: the effective scope is a certain code fragment (usually a function) in the current shell process;
Position variables: $1, $2, ... are used to allow the script to call the parameters passed to it through the command line in the script code;
Special variables: $ ?, $0, $*, $@, $
#Local variable:
Variable assignment: name= 'value'
## can be quoted:
value:
(1) can be Direct string; name="username"
(2) Variable reference: name="$username"
(3) Command reference : name=`COMMAND`, name=$(COMMAND)
Variable reference: ${name}, $name
"": Weak reference, the variable reference will be replaced with the variable value;
'': Strong reference, the variable reference will not be replaced with the variable value, but the original string will be retained;
Display all defined variables
set
Destroy variables:
unset name
Environment variables:
Variable declaration and assignment:
export name=VALUE
##declare -x name=VALUE
Variable reference: $name, ${name}
Display all environment variables:
export
env
printenv
Destroy:
unset name
bash has many built-in environment variables: PATH, SHELL, UID, HISTSIZE, HOME, PWD, OLD, HISTFILE, PS1
Variable naming rules:
1. Do not use reserved words in the program: such as if, for;
2. Only numbers, letters and underscores can be used, and cannot start with a number;
3. Know the name by name,
Read-only variable:
readonly name
declare -r name
Position variable:
Call the parameters passed to the script through the command line in the script code;
$1, $2, ...: correspond to the first and second parameters of the call;
shift [n]
$0: The command itself;
##$*: All parameters passed to the script;
$@: All parameters passed to the script;
$#: The number of parameters passed to the script;
Example: Determine the number of lines in the given file
#!/bin/bash
linecount="$(wc -l $1| cut -d' ' -f1)"
echo "$1 has $linecount lines."
bash configuration file:
##Divided according to the effective scope, there are two categories:
Global configuration:
/etc/profile
##/etc/profile.d/*.sh
/etc/bashrc
Personal configuration:
~/.bash_profile
~/.bashrc
Divided by function, there are two categories:
profile class: Provide configuration for interactive login shell
Global:/etc/profile, /etc/profile.d/*.sh
Personal: ~/.bash_profile
Function:
(1) Used to define environment variables;
(2) Run a command or script;
bashrc class: Provide configuration for non-interactive login shell
Global:/etc/bashrc
Personal:~/.bashrc
##Function:
(1) Define command alias;
(2) Define local variable;
Shell login:
Interactive login:
Log in directly through the terminal by entering the account and password;
Use "su - UserName" or "su -l UserName" to switch users
##/etc/profile --> / etc/profile.d/*.sh --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc
Non-interactive login:
su UserName
Terminal opened under the graphical interface
Execute script
~/.bashrc --> /etc/bashrc --> /etc/profile.d/*.sh
How the new configuration defined by editing the configuration file takes effect:
(1) Restart the shell process;
(2) Use source or .command process;
Question:
1. Define an alias that is effective for all users?
2. Add one more path to the user's PATH environment variable, for example, /usr/local/apache2/bin
administrator?
all?
Arithmetic operations in bash
##+, -, *, /, %, **
Implement arithmetic operations:
(1) let var=arithmetic expression
(2) var=$[arithmetic expression]
(3) var=$((arithmetic expression))
(4) var=$(expr arg1 arg2 arg3...)
Multiplication symbols need to be escaped in some scenarios;
bash has a built-in random number generator: $RANDOM
Enhanced assignment:
+=, -=, *=, /=, %=
##let varOPERvalue
For example: let count+=1
Increase and decrement:
let var+=1
let var++
let var-=1
let var- -
Calculate the sum of the IDs of the 10th user and the 20th user in the /etc/passwd file;
#!/bin/bash
##userid1=$(head -n 10 /etc/passwd | tail -n 1 | cut -d: -f3 )
userid2=$(head -n 20 /etc/passwd | tail -n 1 | cut -d: -f3)
useridsum=$[$userid1+$userid2]
##echo "uid sum: $useridsum"
Pass two file paths as parameters to the script and calculate the sum of all blank lines in the two files;
# !/bin/bash
##spaceline1=$(grep "^[[:space:]]*$" $1 |
wc - l)
spaceline2=$(grep "^[[:space:]]*$" $2 |
wc -l)
echo "The sum of space line: $[$spaceline1+$spaceline2]"
Statistics/etc, / How many first-level subdirectories and files are there in the var, /usr directories;
Conditional test:
Determining whether a certain requirement is met needs to be implemented by the testing mechanism;
Note: Special test expressions need to be assisted by test commands to complete the testing process. ;
Test command:
test EXPRESSION
##[ EXPRESSION ]
[[ EXPRESSION ]]
##Note: There must be blank characters before and after EXPRESSION;
bash test type:
Numerical test:
-gt: Is it greater than ;
-ge: Whether it is greater than or equal to;
-eq: Whether it is equal to;
-ne: whether it is not equal to;
-lt: whether it is less than;
-le: whether it is less than or equal to;
String test:
==: whether it is equal to;
>: Whether it is greater than;
<: Whether it is less than;
!=: Whether it is not Equal;
=~: Whether the string on the left can be matched by the PATTERN on the right;
Note: This expression is generally used In [[ ]];
-z "STRING": Test whether the string is empty, if it is empty, it is true, if it is not empty, it is false;
-n "STRING": Test whether the string is not empty. If it is not empty, it is true, if it is empty, it is false;
Note: use All operands used in string comparisons should be quoted;
File Test
bash custom exit status code
exit [n]: Custom exit status code;
Note: Once in the script When encountering the exit command, the script will terminate immediately; the termination exit status depends on the number after the exit command;
Note: If no exit status code is specified for the script, The exit status code of the entire script depends on the status code of the last command executed in the script;
Accepts a file path as a parameter;
If the number of parameters is less than 1, the user will be prompted "at least one parameter should be given" and exit immediately;
If the number of parameters is not less than 1, the user will be prompted The number of blank lines in the file pointed to by the first parameter;
egrep, extended regular expression, variable, configuration file, arithmetic operation, conditional test, exit status code
ERE:
Character matching: ., [], [^]
Number of matches: *, ?, +, {m,n}
Position anchoring: ^, $, \<, \>, \b
Group: (), \1, \2, ..
or: a|b
##Variables:
Local variables
Environment variables
Local variables
Position variables
Special variables
Reserved words cannot be used;
Commands: unset, export, declare -x, set, env, printenv, readonly
Profile:
profile, bashrc
##Arithmetic operations:
let, $[], $(()), expr
##+=, -=, *=, /=
++, --
Conditional test:
test, [], [[]]
Three types:
Value: -lt, - le, -gt, -ge, -ne, -eq
Strings: ==, !=, >, <, =~, -z, -n
File
##Custom exit code:
exit [n]
##[ $# -lt 1 ] && echo "At least one argument." && exit 1
vim editor
Introduction
vi: Visual Interface, text editor
Text: ASCII, Unicode
##Text editing type:
Line editor: sed
Full screen editor: nano, vi
##VIM - Vi IMproved
Using
vim: Modal editing
Basic mode:Edit mode, command mode
Input mode
Last line mode:
Built-in command line interface
Open the file:
# vim [OPTION]. .. FILE...
+#: After opening the file, place the cursor directly at the beginning of line #;
##+/ PATTERN: After opening the file, place the cursor directly at the beginning of the first line matched by PATTERN;
Mode conversion:
Edit mode--> Input mode
i: insert, input at the cursor position;
a: append, enter after the cursor position;
o: open a new line below the current cursor position;
I: in Enter at the beginning of the line where the current cursor is;
A: Enter at the end of the line where the current cursor is;
O: Enter at the end of the line where the current cursor is Open a new line above the current line;
c
C
Input mode--> Edit mode
ESC
##Edit mode--> Last line mode
:
Last line mode --> Edit mode
ESC
##Close file:
:q Exit
:q! Force quit and discard the changes;
:wq Save and exit
:x Save and exit
:w /PATH/TO/SOMEWHERE
##ZZ: Save and exit;
Cursor jump:
Jump between characters:
h, j, k, l
h: left
#l: right
j : Down
##k: Up
COMMAND: Jump to the number of characters specified by #;
Jump between words:
w: The beginning of the next word
e: The ending of the current or next word
b: The beginning of the current or previous word
#COMMAND: The number of words to jump at one time is specified by
#Jump at the beginning and end of the line:
^: jump to the first non-blank character at the beginning of the line;
0: jump to the beginning of the line;
##$ : Jump to the end of the line;
Move between lines:
#G: Jump to the point specified by # Row;
##G: the last row;
1G, gg: the first row;
Moving between sentences:
)
(
Moving between paragraphs:
}
{
vim editing command:
Character editing:
x: Delete the character at the cursor;
#x: Delete the # characters starting at the cursor;
xp: Swap the characters at the cursor and the position of the following characters;
Replacement command (r, replace)
r: Replace the character where the cursor is
Delete command:
d: Delete command, which can be combined with cursor jump characters to achieve range deletion;
d$:
d^:
d0:
dw
de
db
#COMMAND
dd: Delete the line where the cursor is located ;
dd: Multi-line deletion;
##Paste command (p, put, paste):
p: If the buffer stores a whole line, paste it below the line where the current cursor is; otherwise, paste it behind the current cursor position;
P: If the buffer contains a whole line, paste it above the line where the current cursor is; otherwise, paste it in front of the current cursor position;
Copy command (y, yank):
y: Copy, the working behavior is similar to the d command;
y$
y0
y^
##ye
yw
yb
COMMAND
yy: Copy rows
#yy: Copy multiple rows;
Change command (c, change)
c: Modify
edit mode--> input mode
c$
c^
##c0
cb
ce
cw
#COMMAND
cc: Delete and enter new content
#cc:
Other editing operations
Visual mode:
v: Select by character
V: Line by line
Note: Often combined with editing commands;
d, c, y
Undo the previous edit:
u(undo): Undo the previous edit Operation;
#u: Undo the specified number of operations;
Undo the previous undo:
Ctrl+r
Repeat the previous editing operation:
.
Screen flip operation:
Ctrl+f: Scroll to the end of the file;
Ctrl+b: Scroll one screen to the beginning of the file;
##Ctrl+d: Scroll half a screen to the end of the file;
Ctrl+u: Turn half the screen to the beginning of the file;
Last line mode in vim:
Built-in command line interface
(1) Address delimitation
:start_pos ,end_pos
#: The specific # line, for example, 2 means line 2;
#,#: # means the line from the left Starting, to the right # represents the end of the line;
#,+#: Starting from the line represented by # on the left, plus the number of lines represented by # on the right;
.: Current line
$: Last line
.,$-1
%: Full text, equivalent to 1,$
##/pat1/,/pat2/:
Start from the line matched by pat1 pattern for the first time to the end of the line matched by pat2 for the first time;
#,/pat/
/pat/,$
Usage:
followed by An edit command
d
##y
w /PATH/TO/SOMEWHERE: Change the range Save the lines in the specified file to the specified file;
r /PATH/FROM/SOMEFILE: Insert all the contents of the specified file at the specified location;
(2) Search
/PATTERN: Search from the current cursor position to the end of the file;
? PATTERN: Search from the current cursor position to the beginning of the file;
n: The same direction as the command;
N: The opposite direction to the command;
(3) Find and replace
s: Complete the find and replace operation in the last line mode
s/What to find/What to replace with/Modifiers
What to find: You can use the pattern
Replaced content: Patterns cannot be used, but backward reference symbols such as \1, \2, ... can be used; "&" can also be used to refer to the entire content found during the previous search;
Modifiers:
i: Ignore case
g: Global replacement; by default , only replace the first occurrence in each line;
Find the delimiter in the replacement/can be replaced with other characters, such as
s@@@
s
##1. Copy /etc/grub2. cfg to the /tmp/ directory, and use the search and replace command to delete the blank characters at the beginning of the line in the /tmp/grub2.cfg file;
%s/^[[:space:]] \+//g
2. Copy the /etc/rc.d/init.d/functions file to the /tmp directory, and use the find and replace command to /tmp Add a # sign at the beginning of each line of /functions that starts with a blank character;
:%s/^[[:space:]]/#&/
Multiple file mode:
vim FILE1 FILE2 FILE3 ...
:next next
:prev previous
:first first
: last the last
:wall save all
:qall exit all
Window separation mode:
vim -o|-O FILE1 FILE2 ...
-o : Horizontal split
-O: Vertical split
## Switch between windows: Ctrl+w, Arrow
Single file window split:
Ctrl+w,s: split, horizontal split
Ctrl+w,v: vertical, vertical split
Customize the working characteristics of vim:
Configuration file: permanently valid
Global:/etc/vimrc
##Personal:~/.vimrc
Last line: The current vim process is valid
(1) Line number
Display: set number, abbreviated as set nu
Cancel display: set nonumber, abbreviated as set nonu
(2) Bracket matching
Match: set showmatch, abbreviated as set sm
Cancel: set nosm
(3) Automatic indentation
Enable: set ai
Disable: set noai
(4) Highlight search
Enable: set hlsearch
Disable: set nohlsearch
(5) Syntax highlighting
Enable: syntax on
Disable: syntax off
(6) Ignore the case of characters
Enable: set ic
Do not ignore: set noic
Get help:
:help
:help subject
bash condition test:
## Test method:
test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]
Categories of test expressions:
Numerical comparison
String test
File test:
Existence test
-a FILE
- e FILE: File existence test, if it exists, it is true, otherwise it is false;
Existence and category test
-b FILE: Whether Exists and is a block device file;
-c FILE: Whether it exists and is a character device file;
-d FILE: Whether it exists and It is a directory file;
-f FILE: exists and is an ordinary file;
-h FILE or -L FILE: exists and is Symbolic link file;
-p FILE: whether it exists and is a named pipe file;
-S FILE: whether it exists and is a socket Word file;
File permission test:
-r FILE: Whether it exists and is readable
-w FILE: Whether it exists and is writable
-x FILE: Whether it exists and is executable
File special permission test:
-g FILE: Whether it exists and has sgid permission;
-u FILE: Whether it exists and has suid permission;
-k FILE: Whether it exists and has sticky permission;
File size test:
-s FILE: Whether it is saved and not empty;
Whether the file is open:
-t fd: fd indicates whether the file descriptor is open and connected to a terminal Related
-N FILE: Whether the file has been modified since it was last read;
-O FILE: Whether the current valid user is the file owner;
-G FILE: Whether the current valid user is the file owner;
Binocular test:
FILE1 -ef FILE2: Whether FILE1 and FILE2 point to the same inode on the same device
FILE1 -nt FILE2: Whether FILE1 is newer than FILE2;
FILE1 -ot FILE2: Whether FILE1 is older than FILE2;
Combined test conditions:
Logical operation:
First way:
COMMAND1 && COMMAND2
COMMAND1 || COMMAND2
! COMMAND
[ -e FILE ] && [ -r FILE ]
##Second way:
EXPRESSION1 -a EXPRESSION2
EXPRESSION1 -o EXPRESSION2
! EXPRESSION
Must use the test command;
# [ -z "$hostName" -o "$hostName"= ="localhost.localdomain" ] && hostname www.madu.com
# [ -f /bin/cat -a -x /bin/cat ] && cat /etc/fstab
vim:
Edit mode, input mode, last line mode, visualization mode
Jump: h,j,k,l,w,b,e,),(,},{, G, ^, 0, $
Edit: x, r, c, d, y, p, u, ctrl+r
Search: /, ?, n, N
Find and replace: s
s///
g:
i:
Last line demarcation:
#m,n
m,+
#.
$
/pat1/
/pat1/,/pat2/
%
File test:
Single eye: -e, -f, -d, -b, -c, -L, - P, -S, -r, -w, -x, -s
Binary: -nt, -ot
Combination test: -a, -o, !
File search:
In the file system Find files that meet the conditions;
File search: locate, find
Non-real-time search (database search) :locate
Real-time search: find
##locate:
Relies on a pre-built index; the index is built automatically when the system is idle (periodic tasks); the database is updated manually (updatedb);
Index The build process needs to traverse the entire root file system, which consumes a lot of resources;
Working features:
The search speed is fast;
Fuzzy search;
Non-real-time search;
locate KEYWORD
find:
Real-time search tool, completes file search by traversing the file system under the specified path;
Working characteristics:
The search speed is slightly slow;
Accurate Search;
Real-time search;
Syntax:
find [OPTION]... [Search path] [Search conditions] [Processing action]
Search path: specify the specific target path; the default is the current directory;
Search conditions: The specified search criteria can be based on file name, size, type, permissions and other criteria; the default is to find all files under the specified path;
Processing Action: What to do on files that meet the conditions; output to the screen by default;
Search conditions:
Based on File name search:
-name "file name": supports the use of glob
##*, ?, [], [^]
-iname "File name": Insensitive to case letters
##-regex "PATTERN": Match the entire PATTERN File path string, not just file name;
Search based on owner and group:
-user USERNAME: Search for files whose owner is the specified user;
group GRPNAME: Search for files whose owner is the specified group;
-uid UserID: Find the file whose owner is the specified UID number;
#-gid GroupID: Find the file whose owner is the specified UID number Files with GID numbers;
-nouser: Find files without owners;
-nogroup: Find There are no files belonging to the group;
Search based on file type:
-type TYPE:
f: Ordinary file
d: Directory file
l: Symbolic link file
s: Socket file
b: Block device file
c: Character device file
p: Pipe file
Combined conditions:
and: -a
or: -o
Non: -not, !
## !A -a !B = !(A -o B)
!A -o !B = !(A -a B)
Find the files in the /tmp directory whose owner is not root and whose file name is not fstab;
find /tmp \( -not -user root -a -not -name 'fstab' \) -ls
find /tmp -not \( -user root -o -name 'fstab' \) -ls
Search based on file size:
-size [+|-]#UNIT
Commonly used units: k, M, G
UNIT: (#-1, #]
## -#UNIT:[0,#-1]
+#UNIT: (#,oo)
According to the timestamp:
## in "days";
-atime [+|-]#,
#: [#,#+1)
+#: [#+1,oo]
- #: [0,#)
-mtime
-ctime
In "minutes":
##-amin
-mmin
## -cmin
Search based on permissions:
-perm [/|-]MODE
MODE: Exact permission matching
/MODE: As long as one of the permissions of any type (u, g, o) object can be matched;
-MODE: Each type of object must also have the permission standards specified for it;
Processing action:
-print: the default processing action, displayed on the screen;
-ls: similar to executing "ls -l" on the found file "Command;
-delete: delete the found file;
-fls /path/to/somefile: all the found files Save the long format information to the specified file;
##-ok COMMAND {} \; Execute the command specified by COMMAND for each file found;
Before executing the command for each file, the user will be interactively asked for confirmation;
-exec COMMAND {} \; Execute the command specified by COMMAND for each file found. Command;
{}: used to reference the found file name itself;
Note: find is passed When the found files are passed to the later specified command, all the found files that meet the conditions are passed to the subsequent commands at once;
Some commands cannot accept too many parameters, and the command execution may fail; another way to avoid this problem is:
find | xargs COMMAND
1. Find all files or directories under the /var directory whose owner is root and whose group is mail;
# find /var -user root -group mail
2. Find all files or directories in the /usr directory that do not belong to 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 the week in the /etc directory. The content has been modified, and the owner is not root, nor is it a hadoop file or directory;
# 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 or directories that have no owner or group on the current system and have been accessed in the last week;
# find / -nouser -a -nogroup -a -atime -7
5. Find all files in the /etc directory that are larger than 1M and are of normal file type;
# find /etc -size +1M -type f
6. Find the /etc directory All users under the directory do not have write permissions for files;
# find /etc -not -perm /222
7. Find at least one type of file in the /etc directory that the user does not have execution permissions;
# find /etc -not -perm -111
8. Find files in the /etc/init.d directory that all users have execution permissions and other users have write permissions;
# find / etc/init.d -perm -113
Special permissions on Linux file systems
##SUID, SGID, Sticky
1 Permissions
r, w, x
##user, group, other
2 Security context
Prerequisite: The process has an owner and Belongs to a group; a file has an owner and a group;
(1) Whether any executable program file can be started as a process depends on whether the initiator has execution permissions on the program file;
(2) After it is started as a process, the owner of the process is the initiator; the group of the process is the group to which the initiator belongs;
(3) The permissions of a process when accessing a file depend on the initiator of the process:
#(a) If the initiator of the process is the same as the owner of the file: the file owner permissions apply ;
(b) The initiator of the process belongs to the file's group; then the file group permissions apply;
(c) Application File "Other" permissions;
3 SUID
(1) Whether any executable program file can be started As a process: It depends on whether the initiator has execution permission on the program file;
(2) After starting as a process, the owner of the process is the owner of the original program file;
Permission settings:
chmod u+s FILE...
chmod u-s FILE...
4 SGID
By default, when a user creates a file, its attributes The group is the basic group to which this user belongs;
Once a directory is set with an SGID, the group to which the files created in this directory by users with write permissions belong. The group of this directory;
Permission settings:
chmod g+s DIR...
chmod g-s DIR...
5 Sticky
For a directory writable by multiple people, if sticky is set, each user can only delete his or her own files;
Permission settings:
chmod o+t DIR...
##chmod o-t DIR...
SUID SGID STICKY
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7
##chmod 4777 /tmp/ a.txt
Several permission bit mappings:
SUID: user, occupies the owner’s execution permission bit ;
s: The owner has x permissions
S: The owner does not have x permissions
SGID: group, occupies the execution permission bit of group;
s: group has x permissions
S: group does not have x permissions
Sticky: other, occupies the execution permission bit of ohter;
t: other has x permissions
T: other does not have x permission
bash script programming:
Procedural programming Language:
Sequential execution
Selective execution
Loop execution
Select execution:
if Judgment condition
then
The condition is true branch code
fi
##if judgment condition; then
The branch code where the condition is true
else
The branch code where the condition is false
fi
The above is the detailed content of Basics of Linux System (1). For more information, please follow other related articles on the PHP Chinese website!