Contents

Linux shell tips and tricks

Contents

I’m using Linux shell 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: June 15, 2013

Send process to background:

1
2
3
4
5
6


  
Ctrl + z 

  

Move process to foreground:

1
2
3
4
5
6


  
fg

  

Generate random hex number where n is number of characters:

1
2
3
4
5
6


  
openssl rand -hex n

  

Execute commands from a file in the current shell:

1
2
3
4
5
6


  
source /home/user/file.name

  

Substring for first 5 characters:

1
2
3
4
5
6


  
${variable:0:5}

  

SSH debug mode:

1
2
3
4
5
6


  
ssh -vvv user@ip_address

  

SSH with pem key:

1
2
3
4
5
6


  
ssh user@ip_address -i key.pem

  

Get complete directory listing to local directory with wget:

1
2
3
4
5
6


  
wget -r --no-parent --reject "index.html*" http://hostname/ -P /home/user/dirs

  

Create multiple directories:

1
2
3
4
5
6


  
mkdir -p /home/user/{test,test1,test2}

  

List processes tree with child processes:

1
2
3
4
5
6


  
ps axwef

  

Create war file:

1
2
3
4
5
6


  
jar -cvf name.war file

  

Test disk write speed:

1
2
3
4
5
6


  
dd if=/dev/zero of=/tmp/output.img bs=8k count=256k; rm -rf /tmp/output.img

  

Test disk read speed:

1
2
3
4
5
6


  
hdparm -Tt /dev/sda

  

Get md5 hash from text:

1
2
3
4
5
6


  
echo -n "text" | md5sum

  

Check xml syntax:

1
2
3
4
5
6


  
xmllint --noout file.xml

  

Extract tar.gz in new directory:

1
2
3
4
5
6


  
tar zxvf package.tar.gz -C new_dir

  

Get web headers with curl:

1
2
3
4
5
6


  
curl -v -D - http://hostname -o /dev/null

  

Modify timestamp of some file or directory (YYMMDDhhmm):

1
2
3
4
5
6


  
touch -t 0712250000 file

  

Download from ftp using wget:

1
2
3
4
5
6


  
wget -m ftp://username:password@hostname

  

Generate random password (16 char long in this case):

1
2
3
4
5
6


  
LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;

  

Quickly create a backup of a file:

1
2
3
4
5
6


  
cp some_file_name{,.bkp}

  

Access Windows share:

1
2
3
4
5
6


  
smbclient -U "DOMAINuser" //dc.domain.com/share/test/dir

  

Run command from history (here at line 100):

1
2
3
4
5
6


  
!100

  

Unzip to directory:

1
2
3
4
5
6


  
unzip package_name.zip -d dir_name

  

Multiline text (CTRL + d to exit):

1
2
3
4
5
6


  
cat > test.txt

  

Create empty file or empty existing one:

1
2
3
4
5
6


  
> test.txt

  

Update date from Ubuntu NTP server:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10


  
ntpdate ntp.ubuntu.com  
  
netstat show all tcp4 listening ports:  
  
netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'

  

Convert image from qcow2 to raw:

1
2
3
4
5
6
7
8


  
qemu-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img   
  
precise-server-cloudimg-amd64-disk1.raw

  

Run command repeatedly, displaying it’s output (default every two seconds):

1
2
3
4
5
6


  
watch ps -ef

  

List all users:

1
2
3
4
5
6


  
getent passwd

  

Mount root in read/write mode:

1
2
3
4
5
6


  
mount -o remount,rw /

  

Mount a directory (for cases when symlinking will not work):

1
2
3
4
5
6


  
mount --bind /source /destination

  

Send dynamic update to DNS server:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12


  
nsupdate <<EOF  
  
update add $HOST 86400 A $IP  
  
send  
  
EOF

  

Recursively grep all directories:

1
2
3
4
5
6


  
grep -r "some_text" /path/to/dir

  

List ten largest open files:

1
2
3
4
5
6


  
lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB "$9 }' | sort -n -u | tail

  

Show free RAM in MB:

1
2
3
4
5
6


  
free -m | grep cache | awk '/[0-9]/{ print $4" MB" }'

  

Open Vim and jump to end of file:

1
2
3
4
5
6


  
vim + some_file_name

  

Git clone specific branch (master):

1
2
3
4
5
6


  
git clone git@github.com:name/app.git -b master

  

Git switch to another branch (develop):

1
2
3
4
5
6


  
git checkout develop

  

Git delete branch (myfeature):

1
2
3
4
5
6


  
git branch -d myfeature

  

Git delete remote branch:

1
2
3
4
5
6


  
git push origin :branchName

  

Git push new branch to remote:

1
2
3
4
5
6


  
git push -u origin mynewfeature

  

Print out the last cat command from history:

1
2
3
4
5
6


  
!cat:p

  

Run your last cat command from history:

1
2
3
4
5
6


  
!cat

  

Find all empty subdirectories in /home/user:

1
2
3
4
5
6


  
find /home/user -maxdepth 1 -type d -empty

  

Get all from line 50 to 60 in test.txt:

1
2
3
4
5
6


  
< test.txt sed -n '50,60p'

  

Run last command (if it was: mkdir /root/test, below will run: sudo mkdir /root/test):

1
2
3
4
5
6


  
sudo !!

  

Create temporary RAM filesystem – ramdisk (first create /tmpram directory):

1
2
3
4
5
6


  
mount -t tmpfs tmpfs /tmpram -o size=512m

  

Grep whole words:

1
2
3
4
5
6


  
grep -w "name" test.txt

  

UPDATE: June 15, 2013

Source:http://www.techbar.me/linux-shell-tips/