Shell
IF[ $0-eq 0]
THEN
echo "success"
ELIF[ $0-eq 1]
THEN
echo "failed,code is 1"
FI
#FOREACH
for i in $@
do
echo $i
done
#FOR
for((i =0; i <10; i++))
do
echo $i
done
while["$ans"!="yes"]
do
read -p "please input yes to exit loop:" ans
done
head | tail
#Get the contents before the Nth line from the top
head +20 notes.log
#Get the contents before the Nth line from the bottom
head -20 notes.log
#Surveillance
tail -f notes.log
#Get the contents after the Nth line from the top
tail +20 notes.log
#Get the contents after the Nth line from the bottom
tail -20 notes.log
grep
#Get the line containing the character you want to search
grep "string" *.txt
#Get the line that does not contain the character you want to search
grep -V "string" *.txt
#Use regular expressions
grep -E "string[0-9]" *.txt
#Other use cases
cat file | grep "string"
find
#Search files and folders commands
#Search files and folders by name
find path -name "*.txt"
find path -name "*.txt" -type f #Search only files
find path -name "*.txt" -type d #Search only folders
#Search by permission specification
find path -perm 777
#Search by user
find path -user root
#Search by change time
find path -mtime -5 #Things within 5 days
find path -mtime +3 #3 days or older
#Search by size
find path -size +1024k #1M or more
find path -size -1024k #1M or less
#Add other processing to what you searched for
find path -name "*.txt"|xargs rm -rf #Delete
find path -name "*.txt"|xargs ls -l #View permissions
sed
#Display the second line ($Is the last line)
sed -n '2'p file
#Display the 1st to 4th lines
sed -n '1,4'p file
#Use regular expression, show lines containing los
sed -n '/los/'p file
#Use regular expression, display the contents from the 4th line to the line including los
sed -n '4,/los/'p file
#Do not display the first and second lines
sed -n '1,2'd file
awk
#Show all columns in file
awk '{print $0}' file.txt
#Show columns 1 and 7 in the file
awk '{print $1,$7}' file.txt
#Show columns 1 and 7 in the file(Columns separated by ":")
awk -F: '{print $1”\t”$7}' file.txt
#Convert the first column of the third row in the file to an integer and display it(NR means row, NF means column)
awk '{if(NR==3){print int($1)}}' file.txt
#In the file[192.168.100.1]Show all columns including(「!~Does not include)
cat file | awk '{print $1~/192.168.100.1/{print $0}}'
crul
uniq
#Display with duplicate content excluded
uniq file
#Show the number of duplicates
uniq -c file
#Show only duplicate lines
uniq -d file
sort
#ascending order
sort file
#descending order
sort -r file
#「:Divided by ", descending order of the first row
cat file | sort -t: -k1 -r
less
command | Operation content | |
---|---|---|
Read | r | Redraw the screen. |
Read | R | Redraw the screen.(If there is a change while browsing, the content will be reflected.) |
Read | F | Continue reading the last line of the file.(「tail -Similar to the "f" command) |
Move | SPACE | Advance one screen. |
Move | d | Advance half screen. |
Move | RETURN | Advance one line. |
Move | b | Go back one screen. |
Move | u | Go back half screen. |
Move | y | Go back one line. |
Move | g, < | Moves to the beginning of the file. |
Move | G, > | Move to the end of the file. |
Search | /pattern | "Pattern" Performs a forward search that matches. |
Search | ?pattern | "Pattern" Performs a backward search that matches. |
Search | n | Repeat the previous search. |
Search | N | Repeat the previous search in the opposite direction. |
help | h, H | Display help. |
End | q, Q, :q, :Q, ZZ | I'm done. |
#Examine the process
ps -ef | grep ssh
#Statistics the number of rows
xxxx | wc -l
#Folder copy (if dir2 does not exist)
cp -r dir1 dir2
#Split the file every 5 lines
split -5 file newfile
Recommended Posts