Preface
Using the Linux shell is a basic daily job for some programmers, but we often forget some useful shell commands and techniques. Sure, I can remember the command, but I can't say I can remember how to perform a specific task with it. One thing to note is that some usage requires the installation of additional software in your Linux system. Not much to say below, let’s take a look at the detailed content.
Check whether the remote port is open to bash:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
Let the process transfer Enter the background:
Ctrl + z
Bring the process to the foreground:
fg
Generate random hex number, where n is the number of characters:
openssl rand -hex n
Execute the command in a file in the current shell:
source /home/user /file.name
Truncate the first 5 characters:
${variable:0:5}
SSH debug mode:
ssh -vvv user@ip_address
SSH with pem key:
ssh user@ip_address -i key.pem
Use wget to grab The complete website directory structure is stored 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 including child processes Process tree:
ps axwef
Create war file:
jar -cvf name.war file
Test hard disk writing Speed:
dd if=/dev/zero of=/tmp/output.img bs=8k count=256k; rm -rf /tmp/output.img
Test hard drive Reading speed:
hdparm -Tt /dev/sda
Get md5 hash of text:
echo -n "text" | md5sum
Check xml format:
xmllint --noout file.xml
Extract tar.gz to a 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 a file or directory (YYMMDDhhmm):
touch -t 0712250000 file
Use wget command to perform ftp download:
wget -m ftp:/ /username:password@hostname
Generate a random password (16 characters long in the example):
LANG=c < /dev/urandom tr -dc _A-Z-a-z- 0-9 | head -c${1:-16};echo;
Quickly back up a file:
cp some_file_name{,.bkp}
Access the Windows shared directory:
smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir
Execute the commands in the history (Here is line 100):
!100
Unzip:
unzip package_name.zip -d dir_name
Enter Multi-line text (CTRL + d to exit):
cat > test.txt
Create an empty file or clear an existing file:
\> test.txt
Synchronize 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
Repeatedly run the file, Display its output (default is every 2 seconds):
watch ps -ef
All user list:
getent passwd
Mount root in read/write mode:
mount -o remount,rw /
Mount a directory (this is the case when links cannot be used):
mount --bind /source /destination
Dynamic update DNS server:
nsupdate <
Recursively 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
Display remaining memory (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 the specified branch (master):
git clone git@github.com:name/app.git -b master
Git switch to other branch (develop):
git checkout develop
Git delete branch (myfeature):
git branch -d myfeature
Git deletes the remote branch
git push origin :branchName
Git pushes the new branch to the remote server:
git push -u origin mynewfeature
Print the last cat command in the history:
!cat:p
Run the last cat command in the history:
!cat
Find all empty subdirectories under /home/user:
find /home/user -maxdepth 1 -type d -empty
Get the contents of lines 50-60 in the test.txt file:
< test.txt sed -n '50,60p'
Run the last command (if the last The command is mkdir /root/test, the following will run: sudo mkdir /root/test):
sudo !!
Create a temporary RAM file system – ramdisk (create / first tmpram directory):
mount -t tmpfs tmpfs /tmpram -o size=512m
Grep whole words:
grep -w "name" test.txt
Append text to a file when privileges need to be elevated:
echo "some text" | sudo tee -a /path/file
List all kill Signal parameter:
kill -l
Disable recording of 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 "me@example.com"
To sync with master if you have unpublished commits:
git pull --rebase origin master
will All files containing "txt" in their file names are moved into the /home/user directory:
find -iname "*txt*" -exec mv -v {} /home/user \;
Display files side by side:
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 the large tar.gz files (100MB each) and merge them 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 root password to strengthen MySQL security installation:
/usr/bin/mysql_secure_installation
When Ctrl + c does not work:
Ctrl + \
Get the file owner:
stat -c %U file.txt
block device list:
lsblk -f
Find files with spaces at the end of their file names:
find . -type f -exec egrep -l " +$" {} \;
Find the files whose file names have tab indentation
find . -type f -exec egrep -l $'\t' {} \;
Use "=" to print out horizontal lines: select all and copy them into notes
printf '%100s\n' | tr ' ' =
Summary
The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. And convenient, if you have any questions, you can leave a message to communicate.
For more related articles summarizing the 73 commonly used commands in Linux shell, please pay attention to the PHP Chinese website!