preface
Using Linux shell is the basic work of some programmers every day, but we often forget some useful shell commands and techniques. Of course, I can remember the command, but I can’t say I can remember how to use it to perform a specific task. It should be noted that there are some uses that require additional software to be installed on your Linux system. Let’s take a look at the details.
Check if the remote port is open to bash:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
Let the process go to the background:
Ctrl + z
Take the process to the foreground:
fg
Generates a random hexadecimal number, where n is the number of characters:
openssl rand -hex n
Execute a command in a file in the current shell:
source /home/user/file.name
Intercept the first 5 characters:
${variable:0:5}
SSH debug mode:
ssh -vvv [email protected]_address
SSH with pem key:
ssh [email protected]_address -i key.pem
Use WGet to grab the complete website directory structure and store it in the local directory
wget -r --no-parent --reject "index.html*" http://hostname/ -P /home/user/dirs
Create multiple directories at once:
mkdir -p /home/user/{test,test1,test2}
List the process trees that include child processes:
ps axwef
To create a war file:
jar -cvf name.war file
Test hard disk write speed:
dd if=/dev/zero of=/tmp/output.img bs=8k count=256k; rm -rf /tmp/output.img
Test hard disk reading speed:
hdparm -Tt /dev/sda
Get the MD5 hash of the text:
echo -n "text" | md5sum
Check the XML format:
xmllint --noout file.xml
take tar.gz Extract to new directory:
tar zxvf package.tar.gz -C new_dir
Use curl to get HTTP header information:
curl -I http://www.example.com
Modify the timestamp of the file or directory (yymmddhhmm):
touch -t 0712250000 file
Use WGet command to download FTP:
wget -m ftp://username:[email protected]
Generate a random password (16 characters in this example)
LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;
Fast backup a file:
cp some_file_name{,.bkp}
To access the windows shared directory:
smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir
Execute the command in history (here is line 100)
!100
Decompression:
unzip package_name.zip -d dir_name
Enter multiline text (Ctrl + D exit)
cat > test.txt
To create an empty file or empty an existing file:
\> test.txt
Synchronization time with Ubuntu NTP server:
ntpdate ntp.ubuntu.com
Use netstat to display all tcp4 listening ports:
netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'
Qcow2 image file conversion:
qemu-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img \precise-server-cloudimg-amd64-disk1.raw
Run the file repeatedly to display its output (the default is once every 2 seconds)
watch ps -ef
All users list:
getent passwd
Mount root in read/write mode:
mount -o remount,rw /
Mount a directory (this is not a case where links can be used): –
mount --bind /source /destination
Dynamic update DNS server:
nsupdate < <EOF update add $HOST 86400 A $IP send EOF
Recursive grep all directories:
grep -r "some_text" /path/to/dir
List the top 10 largest files:
lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB "$9 }' | sort -n -u | tail
Show remaining memory in MB:
free -m | grep cache | awk '/[0-9]/{ print $4" MB" }'
Open vim and jump to the end of the file:
vim + some_file_name
Git clone specified branch (Master)
git clone [email protected]:name/app.git -b master
Git switches to other branches (Development):
git checkout develop
Git delete branch (myfeature)
git branch -d myfeature
Git Delete remote branch
git push origin :branchName
Git pushes the new branch to the remote server:
git push -u origin mynewfeature
Last cat command in print history:
!cat:p
Last cat command in running history:
!cat
Find all empty subdirectories under / home / user:
find /home/user -maxdepth 1 -type d -empty
obtain test.txt Lines 50-60 of the document contain:
< test.txt sed -n '50,60p'
Run the last command (if the last command is MKDIR / root / test, the following will be run: sudo MKDIR / root / test):
sudo !!
Create a temporary ram file system – ramdisk (create the / tmpram directory first)
mount -t tmpfs tmpfs /tmpram -o size=512m
Grep whole words:
grep -w "name" test.txt
Add text to a file when you need to increase permissions:
echo "some text" | sudo tee -a /path/file
List all kill signal parameters:
kill -l
Disable recording the last session in Bash history:
kill -9 $$
Scan the network for open ports:
nmap -p 8081 172.20.0.0/16
Set git email:
git config --global user.email "[email protected]"
To sync with master if you have unpublished commits:
git pull --rebase origin master
Move all files with “TXT” in the file name to the / home / user directory:
find -iname "*txt*" -exec mv -v {} /home/user \;
Display files side by side by line:
paste test.txt test1.txt
Progress bar in shell:
pv data.log
Use netcat to send data to graphite server:
echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000
Convert tabs to spaces:
expand test.txt > test1.txt
Skip bash history:
< space >cmd
Go to the previous working directory:
cd -
Split large volume tar.gz Files (100MB each) and merge back:
split –b 100m /path/to/large/archive /path/to/output/files cat files* > archive
Use curl to get HTTP status code:
curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null
Set the root password to strengthen the security installation of MySQL
/usr/bin/mysql_secure_installation
When Ctrl + C is not working well:
Ctrl + \
Get file owner:
stat -c %U file.txt
Block device list:
lsblk -f
Find the file with a space at the end of the file name:
find . -type f -exec egrep -l " +$" {} \;
Find the file with tab indent in the file name
find . -type f -exec egrep -l $'\t' {} \;
Print out the line with “=”: copy all and put it into the note
printf '%100s\n' | tr ' ' =
summary
The above is the whole content of this article, I hope the content of this article can bring some help and convenience to your study or work, if you have any questions, you can leave a message to exchange.