I’m using Linux shell (Bash) on daily basis, but I often forgot some useful command or shell tip. Yes, I can remember commands, but I can’t say that if I used it just once for specific task. Then I started to write Linux shell tips in text file on my Dropbox account and now I decided to share that. This list will be updated over time. Also keep in mind that for some tips you will need to install additional software on your Linux distribution.
UPDATE: DEC 30, 2015
Trim http or https from URL variable with echo:
echo ${URL#*//}
Find 10 largest directories and sort them by used space:
du -a /path | sort -n -r | head -n 10
Create empty partition on whole disk:
parted -sa optimal /dev/xvdb mklabel gpt mkpart primary 0% 100%
Find all files starting with dot:
find /path -name ".[^.]*"
Echo as sudo (tee -a to append):
echo "/dev/xvdb1 /data ext4 defaults 0 2" | sudo tee /etc/fstab
Check if remote port is open with bash:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
Suspend process:
Ctrl + z
Move process to foreground:
fg
Generate random hex number where n is number of characters:
openssl rand -hex n
Execute commands from a file in the current shell:
source /home/user/file.name
Substring for 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
Get complete directory listing to local directory with wget:
wget -r --no-parent --reject "index.html*" http://hostname/ -P /home/user/dirs
Create multiple directories:
mkdir -p /home/user/{test,test1,test2}
List processes tree with child processes:
ps axwef
Create war file:
jar -cvf name.war file
Test disk write speed:
dd if=/dev/zero of=/tmp/output.img bs=8k count=256k conv=fdatasync; rm -rf /tmp/output.img
Test disk read speed:
hdparm -Tt /dev/sda
Get md5 hash from text:
echo -n "text" | md5sum
Check xml syntax:
xmllint --noout file.xml
Extract tar.gz in new directory:
tar zxvf package.tar.gz -C new_dir
Get HTTP headers with curl:
curl -I http://www.example.com
Modify timestamp of some file or directory (YYMMDDhhmm):
touch -t 0712250000 file
Download from ftp using wget:
wget -m ftp://username:password@hostname
Generate random password (16 char long in this case):
LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;
Quickly create a backup of a file:
cp some_file_name{,.bkp}
Access Windows share:
smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir
No comments:
Post a Comment