Check whether the remote port is open to bash:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
Transfer the process to the background:
Ctrl + z
Switch the process to the foreground:
fg
Generate a random hexadecimal 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
Extract the first five 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 capture 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 a time:
mkdir -p /home/user/{test,test1,test2}
List the process trees that contain sub-processes:
ps axwef
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 read speed:
hdparm -Tt /dev/sda
Obtain the md5 hash of text:
echo -n "text" | md5sum
Check xml format:
xmllint --noout file.xml
Extract tar.gz to the new directory:
tar zxvf package.tar.gz -C new_dir
Use curl to obtain HTTP header information:
curl -I http://www.example.com
Modify the timestamp of a file or directory (YYMMDDhhmm ):
touch -t 0712250000 file
Run the wget command to download ftp:
wget -m ftp://username:password@hostname
Generate a random password (in this example, it is 16 characters long ):
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 the 100th line ):
!100
Decompress:
unzip package_name.zip -d dir_name
Enter multiple lines of text (CTRL + d to exit ):
cat > test.txt
Create an empty file or clear 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 and display its output (the default value is 2 seconds ):
watch ps -ef
List of all users:
getent passwd
Mount root in read/write mode:
mount -o remount,rw /
Mount a directory (this is the case where the link cannot be used ):
mount --bind /source /destination
Dynamically update the DNS server:
nsupdate < <EOFupdate add $HOST 86400 A $IPsendEOF
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 (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
Switch Git to another branch (develop ):
git checkout develop
Myfeature ):
git branch -d myfeature
Git deletes a 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
The last cat command in the running history:
!cat
Find all empty sub-directories under/home/user:
find /home/user -maxdepth 1 -type d -empty
Line 50-60 in the test.txt file:
< test.txt sed -n '50,60p'
Run the last command (if the last command is mkdir/root/test, run: sudo mkdir/root/test ):
sudo !!
Create a temporary RAM File System-ramdisk (first create the/tmpram directory ):
mount -t tmpfs tmpfs /tmpram -o size=512m
Grep whole words:
grep -w "name" test.txt
Append the text to a file when you need to escalate permissions:
echo "some text" | sudo tee -a /path/file
List all kill signal parameters:
kill -l
Do not record the last session in the bash history:
kill -9 $$
Scan the network to find 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
Move all files whose names contain "txt" to the/home/user directory:
find -iname "*txt*" -exec mv -v {} /home/user \;
Display files in parallel by row:
paste test.txt test1.txt
Shell progress bar:
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 a space:
expand test.txt > test1.txt
Skip bash history:
< space >cmd
Go to the previous working directory:
cd -
Split the tar.gz file (100 MB each) and merge it back:
split –b 100m /path/to/large/archive /path/to/output/filescat files* > archive
Use curl to obtain the HTTP status code:
curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null
Set the root password to enhance the secure installation of MySQL:
/usr/bin/mysql_secure_installation
When Ctrl + c is not easy to use:
Ctrl + \
Get the file owner:
stat -c %U file.txt
Block device list:
lsblk -f
Find the file with spaces at the end of the file name:
find . -type f -exec egrep -l " +$" {} \;
Find the file with tab indentions in the file name.
find . -type f -exec egrep -l $'\t' {} \;
Print the horizontal line with "=:
printf '%100s\n' | tr ' ' =
[Original article: Linux shell tips and tricks]