[LINUX] Shell memo


if statement

IF[ $0-eq 0]
echo "success"
ELIF[ $0-eq 1]
echo "failed,code is 1"

for statement

for i in $@
	echo $i

for((i =0; i <10; i++))
	echo $i

while statement

read -p "please input yes to exit loop:" ans

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

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


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


#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


#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


#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[]Show all columns including(「!~Does not include)
cat file | awk '{print $1~/{print $0}}' 



#Display with duplicate content excluded
uniq file
#Show the number of duplicates
uniq -c file
#Show only duplicate lines
uniq -d file


#ascending order
sort file 
#descending order
sort -r file 
#「:Divided by ", descending order of the first row
cat file | sort -t: -k1 -r


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

Shell memo
Shell script @ study memo
gzip memo
Pandas memo
Shell type
HackerRank memo
Python memo
python memo
graphene memo
Flask memo
pyenv memo
pytest memo
sed memo
Install Memo
BeautifulSoup4 memo
networkx memo
python memo
tomcat memo
command memo
Generator memo.
Python memo
SSH memo
Command memo
Memo: rtl8812
pandas memo
Python memo
Pycharm memo
Shell programming
Python memo
AtCoder devotion memo (11/12)
[Python] Memo dictionary
PyPI push memo
tensorflow-gpu introduction memo
LPIC201 learning memo
Jupyter Notebook memo
LPIC304 virtualization memo
Shell script basics # 2
python beginner memo (9.2-10)
youtube download memo
Linux x memo
Django Learning Memo
ARC # 016 Participation memo
Beautiful Soup memo
LPIC101 study memo
linux (ubuntu) memo
scp command memo
Flask Primer Memo
celery / kombu memo
who command memo
django tutorial memo
Flask basic memo
Linux # Command Memo 1
★ Memo ★ Python Iroha
Gender recognition memo
Linux [shell command]
Image reading memo
[MEMO] [TERMINAL] Alacritty
3D rotation memo (1)
[Python] EDA memo
Python 3 operator memo
H2O.ai Introduction memo