Linux Trix

Here are some interesting Linux tricks that might be not very obvious for everyone. Some of them may work with Ubuntu only.

    • Disable hard disk caching (Warning: This will probably reduce your system performance)

Edit /etc/hdparm.conf, search for the following two lines. Uncomment the second line and restart your system.

 # -W Disable/enable the IDE drive's write-caching feature
 #write_cache = off
    • Clear caching from memory (Warning: This will probably reduce your system performance)

Run the following command to flush the cache. For more details, check Drop Caches

 sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
    • Compress a directory /X/Y to be file abc.tar.gz
 tar -zcvf abc.tar.gz /X/Y
    • Extract a compressed file abc.tar.gz
 tar -zxvf abc.tar.gz /X/Y

-z: Compress using gzip program
-c: Create
-v: Verbose (display progress)
-f: File name
-x: eXtract file

    • Find a job that match string “abc”, then kill it
 ps aux | grep -i "abc" | awk {'print $2'} | xargs kill
    • How to Run Long processes using ssh?

Using “screen” command help running long commands on remote machines without worrying about ssh connection.

Run command on a screen:

 screen ./My-long-process

De-attach screen:

 Ctrl+a d

OR run this from another terminal

 screen -d

Once you de-attach the screen it is safe to disconnect your ssh connection and the command will continue in its screen.
List all available screens:

 screen -ls

Re-attach a screen

 screen -r
  • Count number of lines in a file “abc”
 wc -l abc
  • How to run a command from a remote terminal and ensure that job will not stop if the terminal stopped or if the connection lost?
 ssh (user)@(host) "nohup sh (your shell script file).sh" 

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s