Linux Tutorials

Linux du – find disk usage of directories or files

Linux du command can be used to find disk usage of files and directories. Here are some handy du commands on Ubuntu Linux (or any read more

Linux – how to create swap partition

Step to create swap partition on Ubuntu Linux. It should work on any other Linux also. Create storage file (say 4GB) using fallocate for swap read more

Linux – how to create a large file like 1GB

Sometimes we need a large file with any content on Linux. This may be needs for benchmarking and testing purpose. Here are some ways to read more

Rsync – show progress and transfer rate

Rsync can be used to sync directories and files from one machine to another. Sometime we want to see the progress and transfer speed. For read more

Linux/Unix – find inode number of a file

To find the inode number of a file on Linux/Unix the following approaches can be used. Find inode using ls $ ls -i file1.txt 412882 read more

Linux/Unix – truncate a large log file without deleting it

Sometimes we need to truncate (make it size 0) a large opened file by a running process (e.h. php error log, apache error logs, custom read more

Linux – command to check swap size

Swap space is the area on a hard disk which is part of the Virtual Memory. Swap space temporarily holds memory pages that are inactive. read more

Linux/Unix – How to go to previous directory

To go to previous working directory is Linux is a frequently occurring need. These are two ways to achieve it: Using dash (-) $ cd read more

Linux/Unix history with date and time

It is useful to run history command with date and time along with each command. Here are instructions on how to do it on Linux/Unix. read more

Jenkins – how to delete old builds

Jenkins supports deleting old builds for a project by days or maxium number of builds to keeps. This is useful to keep disk usage under read more

node-sass quick start tutorial on Linux and Mac

Sass (Syntactically Awesome Stylesheets) is a style sheet language which brings programming features like variables, etc. to css. It makes maintaining and modifications in css read more

Composer – quick start guide on Linux and Mac

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) read more

Install rabbitmq on Ubuntu Linux

RabbitMQ is an open source message broker software that implements the Advanced Message Queuing Protocol (AMQP). It is written in the Erlang programming language. Here read more

WordPress – prevent access to php files in wp-includes

Most wordpress php files are in directory wp-includes in wordpress root directory. This can be used by hackers to try to access these directly. This read more

Linux – list only directories

Sometimes we need to list only directories on Linux (or any other unix). Here are some approaches. List directories using ls and shell wildcard $ read more

Run nginx in docker container

Docker can be used to run nginx in a container. We’ll use Ubuntu Linux (14.04.2) for the purpose of this tutorial. This tutorial assumes that read more

How to get docker container IP

Docker inspect can be used to get information including IP address about a container using its id or name. $ docker inspect [container_name_or_id] To get read more

Docker container – handy commands

Some handy command to work with containers. We’ll assume a docker image ubuntu is available on current machine. Run interactive shell (or any other command) read more

Linux – how to remove user from a group

To remove a user from a group on Linux, one can use gpasswd or deluser. Here are sample commands. Using gpasswd To remove user1 from read more

Docker how to add a user to group docker on Linux

It is a good idea to add a user to docker group. In case user is not added to docker group, you may see the read more

Command line – top IP list from apache access log

Linux (or Unix) command line utilities like awk, sort, uniq, can be used to analyze apache log to get interesting stats. One use case is read more

PHP execute command and capture shell exit status

PHP exec and shell_exec can be used to execute a shell command on server side. exec also populates shell exit return status in one of read more

Linux – find and delete files older than 30 days

Often we need to delete old file on Linux or Unix which are old (say 30 days or more). Here are some commands which can read more

Perl command line – replace multi line comments

Perl command line one liner can be used to replace text in a file using regular expressions (regex). It is very powerful approach for automating read more

Jenkins – how to setup build cron

Jenkins supports setting build triggers using Jenkins crons. This way build will run at regular interval and possibly Jenkins can send email on failure. Here read more

Jenkins – how to setup build failure email

Getting email of a build failure is a common use case in Jenkins. We would want to get notified whenever any build fails and when read more

Bash – how to compare file timestamps

Sometimes we need to perform some action if a file is newer than other file. This can be used to recreate generated file if source read more

Linux – find files containing specific text

Handy command on Linux/Mac to find all files matching a pattern containing a specific text. This will also print file name. $ find . -type read more

Jenkins how to clone a project

Sometimes we need to create a replica of a project or project with similar configuration in Jenkins. This helps in avoiding entering project configuration data read more

Gulp quick start tutorial

Gulp is a toolkit to automate tasks primarily in web development workflow. It can automate things like css/js minification, concatenation, etc. Here is quick tutorial read more

LXC (Linux Containers) – quick start tutorial on Ubuntu

LXC (Linux Containers) is an operating-system-level virtualization environment for running multiple isolated Linux systems (containers) on a single Linux control host. Here is a quick read more

Csslint – quick command-line tutorial and how to use it for pre-deployment sanity check

Csslint can be used to check the syntax of a css file. This can be used in release/deployment scripts to check the syntax of css read more

Svn – how to make file executable on Linux

To make a file executable in svn on Linux, it property svn:executable has to be set as ON. $ svn propset svn:executable ON sync.py $ read more

awk sum examples

Here are some awk examples to sum an integer column from a file or stdin (standard input) stream. Data used in these examples Awk sum read more

awk – if condition examples

Awk is pretty powerful command line utility to filter lines from a stream. Here are some awk if condition examples. Data used in these examples: read more

Dig – list all dns records

To list all DNS records of a domain name use the following dig command (after replacing microsoft.com with desired domain) $ dig microsoft.com any ;; read more

Svn – how to diff last committed change

To see the diff of last committed change follow these steps: Find last revision id $ svn update $ svn log -l 1 r4546 | read more

Svn – short diff without context

Using svn command line option Use the following diff command to avoid diff context $ svn diff –diff-cmd=diff -x -U0 Option -x passes option -U0 read more

Vim – highlight and move to matching bracket

To find and move to matching bracket (curly, square or round) use the following command inside vim once you are in one of the brackets. read more

Rsync – using dryrun to list changed files

Use rsync dryrun (-n) with -i (or –itemize-changes) to list changed files. $ rsync -n -i -a ./dir1 ./destdir/ cd+++++++ dir1/ >f+++++++ dir1/file1.php >f+++++++ dir1/file2.php read more

Install and setup Jenkins on Ubuntu Linux

Jenkins is an open source continuous integration (CI) tool written in java. It can be used to run (on-demand or periodic) build and test jobs read more

NodeJS – npm beginner tutorial

Npm (Node package manager) is used to install/uninstall and manage node packages. Here are some common commands when using npm on command line on Linux read more

How to install Imagemagick on Ubuntu Linux

Use apt-get to install imagemagick $ sudo apt-get install imagemagick List binaries in imagemagick $ dpkg -L imagemagick | grep bin /usr/bin /usr/bin/conjure.im6 /usr/bin/composite.im6 /usr/bin/import.im6 read more

How to install node.js on Ubuntu Linux

Update node source $ curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash – Check which nodejs will be installed $ apt-cache policy nodejs nodejs: Installed: (none) read more

PHP echo – comma (,) vs dot (.) performance benchmarks

PHP echo allow comma (,) for concatenating various values in addition to dot (.). This works only for echo though. It is sometimes discussed comma read more

Bash – how to check if a variable is set

To check if a variable is set or empty, we can use couple of approaches. One approach is to use empty string check (if [ read more

Bash – iterate over array

These are few ways of iterating on bash arrays to read and or modify array’s elements in bash on Linux or Mac. Here are some read more

Bash – set default value if a variable is empty

There are multiple ways to check if a variable is empty and then set it to some default value. Shell parameter expansion gives us an read more

How to setup password less git push for github

You can use user and password based authentication to push git changes to GitHub. But it is cumbersome to type user and password with every read more

How to create ssh public/private keys on command line

Command line utility ssh-keygen can be used to generate rsa or dsa public and private keys. By default it generates rsa keys. Here are commands read more

Generate ssh public key from private key on command line

In case you have lost your ssh public key, you can re-generate it from the private key you have. Here is the command to generate read more

Bash – newline and other escape character in string

To have newline (\n) and other escape characters in bash strings, we can use $’string’ syntax (ANSI-C Quoting). $’string’ expands to string, with backslash-escaped characters read more

Bash – append text to a variable

Variable in string approach Use curly braces {} around variable (${VARNAME}) in case text begins with alphanumeric character. Concatenate variable with string Use =+ operator read more

Curl – how to hide progress bar

Curl by default shows progress bar. To hide it -s option can be used. We can also add -S to show errors when -s option read more

Bash – how to stop at error

To stop a bash script at first error use set -e. To undo it use set +e.

Linux – sending mail from command using mailutils

Linux command line tools to send mails are very handy for sending mails from bash scripts. As an example you can send daily php error read more

PHP – convert dos newline to unix format

Files or strings in DOS format may have \r (carriage return) along with \n (newline). This may appear as ^M in some editors. Here is read more

PHP – empty() vs isset() vs is_null() vs boolean check

PHP isset() and empty() are frequently used to check the values of variables. In some cases is_null is also used. Here is a table covering read more

Imagemagick – how to create animation on command line

Imagemagick is very powerful tool to process images on command line. If you have multiple images, you can use imagemagick convert utility on Mac or read more

Bash – pass all arguments from one script to another

When writing a wrapper bash script, we often need to pass all arguments passed to the wrapper scrip to another script. Here is quick bash read more

How to find python package’s file location

Here is simple python code snippet to see the file location of a python package on command line: $ python -c “import urllib2; print urllib2.__file__;” read more

Curl – follow redirects

Curl is fairly handy command line utility to fetch a web page. It can also be used to automate various tasks in bash shell scripts. read more

How to crop an image using imagemagick convert

Imagemagick convert is pretty handy tool to manipulate images and can be used to crop an image. This can be useful in automation and also read more

How to check if a user has password on Linux

Any user created on Linux may or may not have a password. Linux stores passwords in encrypted format in /etc/shadow. Here is quick command line read more

Linux – providing sudo access to a users – some best practices

Giving sudo to a user is frequently occurring activity on Linux. Here are some scenarios and best practices for providing sudo to users on Linux. read more

Phantomjs – how to check what resources are being requested by a page

Phantomjs lets you add hooks when it fetches a web page. Here is code snippet which can be used to print all resources phantomjs fetches read more

Bash check if file begins with a string

Bash code to check if file begins with a string Note the presence of double square brackets ([[ ]]) Bash code to check if file read more

Get file size in bytes on Linux

Sometime we need to get file size for the purpose of automation in bash shell script. Here are few ways to get a size of read more

Bash shell – check if file or directory exists

Bash code snippet to check if file exists FILE=/etc/passwd if [ -f $FILE ] ; then echo “$FILE exists and is regular file” else echo read more

How to compress screenshot images using imagemagick convert

Imagemagick convert can be used to compress and screenshot images by changing unique colors, image depth etc. Here are some ways to compress images created read more

Find image unique colors and bit depth using imagemagick identify

Print image unique colors and bit depth $ identify -format “UniqueColors: %k, BitDepth:%q\n” img1.png UniqueColors: 1694, BitDepth:16 Note that when can compress an image with read more

Fetch wordpress rss feed as FeedBurner user agent on command line

In case you have setup your wordpress feed to redirect to Feedburner url for regular user-agents, you can use either curl or wget (Mac or read more

Get image width and ppi using imagemagick identify on command line

Imagemagick identify is pretty handy utility to get information about an image. You need to have imagemagick installed on your Mac or Linux for the read more

Vim (vi) – multiline search replace with confirmation

Vi (or Vim) is a very powerful and light weight editor which is available on any Linux or Mac. Even if you are working on read more

How to resize image (including animated gif) using Image magick convert

Imagemagick convert can be used to resize and image (including animated gif). Here are some scenarios where convert can be used to change size of read more

How to edit animated gif speed (FPS) using imagemagick convert

Imagemagick convert can be used to edit animated gif speed (FPS). Here are some scenarios where convert can be used. For these commands to work read more

Git – how to view diff for a given commit id

Diff commit id with its first parent commit id Here are some approaches to view diff between first parent of commit id and commit id. read more

Vim – how to go back to last edited line/context

Sometimes we are editing a certain section of code in vi and then navigate to different section for reference. It is tedious to come back read more

WordPress – write custom php log to separate file

Why write php log to separate file Using default php error log in wordpress php code for informational messages can pollute php error log file. read more

How to open vim at previous location

Vi (or Vim) editor can remember location at the time of exiting a file. This can be fairly convenient while editing files during development. .vimrc read more

Linux – Yesterday’s Date in YYYYMMDD format

Linux Bash code for yesterday’s date in YYYYMMDD format $ date –date=”yesterday” +”%Y%m%d” 20151128 $ date –date=”-1 days” +”%Y%m%d” 20151128 Mac Bash code for yesterday’s read more

php cli – find which ini file is loaded

Command to find which ini file is loaded for php cli $ php -i | grep “Loaded Configuration File” Sample outcome on Ubuntu Linux Loaded read more

Linux – how to run a command as different user

Often we need to run a command as different users. Some of these users may not even have a login shell (e.g. www-data on Ubuntu read more

Linux – how to add a user using useradd

Adding a new user on Linux can be easily done on command line using useradd. These are some scenarios and handy commands to add a read more

Linux how to modify a user using usermod

Modifying a user on Linux can be easily done on command line using usermod. These are some scenarios and handy commands to modify a user read more

Linux – how to enable password login on ssh server

By default password based on login may be disabled on you Linux Server. Even though it is better to use private and public key for read more

Find authentication methods an ssh server supports

It is sometimes handy to know which authentication methods an ssh server supports. This may be useful if you want to enable or disable certain read more

How to install AWStats on Ubuntu Linux

AWStats is a powerful and featureful tool to analyze apache access logs. Using AWStats, you can view various stats 404, etc. Here are steps to read more

Iterate over specific file extension in a dir in shell script

Sometimes we need to iterate over all files of a specific file extension using a bash script. This can be used to convert one type read more

svn – how to undo last commit

Often we need to undo our last commit or an old recent commit in svn. These are quick steps you can follow to undo last read more

How to prevent ssh session freezing due to timeout

It can be quite annoying to kill a frozen ssh session and the restart it. This can happen if you leave an ssh shell idle read more

Why and how to log Content-Type in apache access log

It is very handy to log Content-Type in apache access log. It can be used to grep visit of specific content-type (e.g. text/html). Here is read more

How to customize wordpress rss feed

By defaul WordPress will include all posts sorted by creation time for rss feed. If you want to customize wordpress feed, you can write custom read more

git – how to diff two branches

It is sometimes useful to be able to diff two branches in git. This can be useful to understand the changes in a git repository read more

Find all user created wordpress custom field keys

Sometime we need to find all user created custom field key names (or meta key) in wordpress. This may be needed to find all posts read more

find if a wordpress page is leaf page

Sometime we want to consider pages which have children as browse pages and may treat them separately in plugins etc. For example we may not read more

How to display wordpress top level pages

Displaying wordpress top level pages may be useful for displaying sitemap or browse interface on a wordpress site. Here is the code you can use read more

Mysql – how to enable query log

It may be useful to enable mysql query log on development of even production server for a short duration for debugging or code/flow analysis purpose. read more

How to kill unresponsive ssh session using escape sequence

Sometime ssh shell become unresponsive when left for too long. It is little annoying to close the terminal to kill the ssh session. To kill read more

Apache – add basic auth to a location or directory

In case you want to protect a directory from public access, you can add Apache basic auth to it. This may be needed if you read more

Disable directory listing in apache

Apache 2.4 on Ubuntu Linux has directory listing enabled by default. In case you are upgrading from apache 2.2 or creating a new site, you read more

Block directory access using htaccess 404

RewriteRule in Apache .htaccess is a convenient way to block directory access to a directory in your Apache document root. This can be useful to read more

How to sort using a specific field on Linux

Sometime we need to sort a file on a specific field. This may be useful if a file is in csv or tsv formart (probably read more

Impact on LC_ALL on Linux sort

Linux sort is a handy Linux command line tool to sort text files. It can sort fairly large files without consuming too much memory. The read more

How to show environment variable for a process id (pid)

Sometimes it may be useful to see what environment variables were passed to a process. e.g. you may want to see LC_ALL value for a read more

DIG quick start tutorial for DNS Lookup

Dig (Domain information groper) can be used to do DNS lookup on Linux, Mac or windows machine. It will connect a DNS server (default or read more

How to recover svn lost password on Linux

If you are using simple web auth to connect to svn server, you can choose to store it at client side. This way you don’t read more

How to delete files starting with dash/hyphen

Deleting files starting with dash or hyphen can be little tricky as dash is interpreted as option prefix on Mac and Linux. Here is what read more

Setup xdebug for remote wordpress debugging

Using xdebug is pretty useful for debugging and sometimes understanding existing code. In a typical setup where wordpress in installed remotely on EC2 or any read more

Command line – run python webserver

Sometime we need to run a quick static webserver which can serve various files in a directory. This can be pretty handy to serve html read more

How to upgrade wordpress manually

Recently I upgraded wordpress from version 3.5.1 to 4.3.1. There are couple of ways you can upgrade wordpress and easiest one is do it using read more

Linux rsync tutorial

Rsync is a great tool to copy and sync directories and files across computers. It can also be used to manage source code and data read more

Custom terminal tab title and ssh on mac

In case you are using ssh private keys to connect to remote Linux servers on cloud, it is better to automation connection to various machines read more

How to use custom identity file with rsync on Linux

Rsync uses ssh to sync files between two machines and usually it will use default identity file present in ~/.ssh/ directory on Linux. In case read more

Find which process is listening on a port on Linux

Sometimes we need to find out what process is using or listening on a specific port on Linux machine. This can be fairly useful in read more

Use x2go to access remote Ubuntu Linux

X2Go enables you to access a graphical desktop of a computer over network. It is a remote desktop solution for Linux. In this article we read more

Edit a file without changing its timestamp on Linux

Usually anytime a file a edited, its timestamp is also changed. There are cases when you want to change the content of a file but read more

Linux file timestamps

Linux stores creation, modification and access file timestamp of a file. Here is brief definitions of these three timestamps: Creation, modification and access file timestamp

migrate multiple mysql databases using mysqldump on Linux

Sometimes we need to migrate all mysql databases to a new machine. This can be the case when you are migrating your multiple wordpress blogs read more

prevent wordpress xmlrpc.php attack

Sometimes you may see too many POST requests to xmlrpc.php in your apache access log. Large number of hits to xmlrpc.php can slow down your read more

Find recently modified files on Linux

There are times when we want to find recently modified files in a directory. We can use command line tool find on Linux or Mac read more

How to zip/unzip a directory with password

Zip and unzip utility can be used to compress a file or directory on Mac. These should also work on Ubuntu or other Linux flavours. read more

How to add a user on Linux with sudo access

Sometime we need to add a user on Linux environment from command line or from a shell/bash script and give it sudo access. Linux has read more

How to fix a broken sudoers file on AWS/EC2 Linux

In case you are using Ubuntu or Amazon linux on Amazon EC2 and introduce a syntax error, you won’t be able to run any sudo read more

NFS client and server handy commands

In case you are using NFS server, here are some handy commands you can use while working in production. These were tested in Ubuntu Linux. read more

Mongo – cli quick start guide

Using mongo cli utility can be handy way to query mongo db and automate few simple use cases. This article covers some handy commands which read more

Ngrep – quick start guide

Ngrep is similar to tcpdump with ability to look for regex search in packets playload and show matching packets to screen. This can be very read more

Some handy linux gnu date commands

Using date command is pretty handy way to covert unix timestamp to string format and vice versa. This can also be used to print unix read more

Linux screen – quick start guide

When working on remote Linux terminals its a good idea to use screen manager as it will protect your session from disconnection. Here are some read more

Memcache – how to dump all keys and values on command line

Memcache is a very simple, distributed memory cache used widely with mysql and other databases. Often we want to inspect what is in memcache server read more

How to use pm2 to manage node.js application in production

PM2 can be used to manage and run application in production. For this article we are going to use Ubuntu 14.04.2 LTS. But it should read more

redis cli quick start tutorial

Redis is a fast key value cache and store. It is useful to get familiar with some handy redis command line interface (cli) commands. Here read more

curl – some handy commands

Command line utility curl (on Linux, Mac and other operating systems) is pretty handy tool for web developers and can be used to multiple activities. read more

How to create Linux instance on Amazon AWS/EC2 Classic

[Update: This article is for Amazon classic. For Amazon VPC instructions may be slightly different] One has to make couple of choices while installing Linux read more

How to use phantomjs to create site/url snapshot thumbnail

PhantomJS in a scriptable webkit written by Ariya Hidayat. Chrome and Safari are based on webkit. This is the formal definition as per the official site: PhantomJS is read more

How to Preload cache in wordpress on Linux

I’m using memcache with w3c total cache plugin to cache pages on my wordpress site on Ubuntu Linux. The way it works is whenever someone read more

How to install and monitor memcache for php on Ubuntu Linux

Memcache is one of the most popular distributed memory based cache system. It can give excellent performance results if configured correctly. Its a good idea read more

Ssh automation on Amazon EC2 Ubuntu Linux

When you create Amazon EC2 instance, you are given a RSA private key to access the instance. On Ubuntu Linux the key is for default read more

How to find recursion stack growth direction using C code

In a Linux (or other Operating System) process when a subroutine is called, the memory for local variables comes from stack area of the process. read more

How to install ViewVC for svn repository on Ubuntu Linux

ViewVC is a browser interface for cvs and subversion version control repositories. If you are using svn then its a great tool to browse your read more

How to setup ssl (https) for your site on Ubuntu Linux

You may need ssl for your site for multiple reasons. I needed it for facebook apps. So my requirements are not so strict. For building read more

How to setup svn repository on AWS Ubuntu Linux with apache simple auth

Svn (Subversion) is one of the popular options for maintaining source code versions and revisions on Linux. Here are the steps to install and setup read more

How to use ssh port forwarding to surf a site from different location

Sometimes we want to access a web site from a different location or ip to see how various things appear on the site. There can read more

ApacheBench (ab) – how to load test a web site

ApacheBench (ab) is a very handy webserver benchmarking tool which can be run from command line. It is extremely simple to use. A quick outcome read more

How to use rsync for production release automation

Rsync can be used to automate the production releases for wordpress or any other site. I maintain two wordpress sites – one for development purpose and one read more

How to log page latency and url host in apache log

Apache log can give you lot of insight into the performance of your site. The default configuration of apache does not log the page serving read more

How to automate wordpress sandbox setup on Linux

Maintaining a wordpress site can be pretty involving task specially when you have so many plugins with regular updates. Many time you have to experiment new plugins, read more

How to install wordpress on Amazon AWS-EC2 Classic Ubuntu Linux micro instance

[Update: This post is applicable for Amazon classic infrastructure. Now you should use Amazon VPC infrastructure and steps may be slightly different. We’ll soon write read more

How to fix mysql service stopping intermittently on AWS micro instance

I was having this problem since last few days. My mysql server was getting killed on its own on aws/ec2 micro instance. So far I read more

How to use nc (netcat) to print headers sent by a browser

Unix utility nc (netcat) is a pretty handy utility for many network tasks. One such use is to see what headers are being sent by read more

How to connect to mysql server using ssh port forwarding

When mysql server is hosted on a cloud environment like amazon aws/ec2 or rackspace, connecting to it using mysql GUI tools may not be possible without read more

How to migrate wordpress from root to sub directory

Migrating wordpress from root to subdirectory is not a difficult task but should be done carefully. Here are the steps which can be followed. Terminology read more

Python/Perl/Unix one liners

Here are some cool python one liners. Parse apache log using python: print 5th field python -c “import csv,sys;f=csv.reader(sys.stdin, delimiter=’ ‘); print ‘\n’.join([r[5] for r read more