Linux Notes

First published: 13 Nov 2016
Last updated: 13 Nov 2016

Note: Most of these notes apply for all Linux distros but some might be Debian specific.

Determining the operating system, kernel version, and hostname.

Use: hostnamectl
Sample output:

   Static hostname: debian-laptop
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 4fa1ba00ff974ef8bfe6f311541da742
           Boot ID: 242accd57d91f8a75ec3950bd3b2e4ae
    Virtualization: oracle
  Operating System: Debian GNU/Linux 8 (jessie)
            Kernel: Linux 3.16.0-4-amd64
      Architecture: x86-64


Start / Stop / Restart / Reload Apache 2 web server service

To start Apache use: sudo service apache2 start
To stop Apache use: sudo service apache2 stop
To stop and start, i.e., restart Apache use: sudo service apache2 restart
To apply new configuration settings a restart is not required. Instead use reload as follows: sudo service apache2 reload


Configure Apache to host multiple virtual sites

Create configuration files underneath /etc/apache2/sites-available for each virtual web site you need. So if for example, you need to have two websites, you would create two files, and

Each web site configuration file should look similar to the following:

<VirtualHost *:80>
DirectoryIndex index.html
DocumentRoot /var/www/
ErrorLog /var/www/ 
CustomLog /var/www/ combined

Ensure that the paths, for instance, /var/www/ and /var/www/ exist. Finally, you should run sudo a2ensite and sudo a2ensite to create the symbolic links required underneath /etc/apache2/sites-enabled.

Change the default page loaded from Apache directory

To specify that, for instance, index.php should be loaded if no file is specified, i.e., a directory listing, do the following:

  1. Edit the website configuration file, for example, sudo nano /etc/apache2/sites-available/
  2. Add a DirectoryIndex entry to the VirtualHosts element, such as DirectoryIndex index.php to set index.php as the default file to load when none is specified.

Example file.

<VirtualHost *:80>
     DirectoryIndex index.php
     DocumentRoot /var/www/
     ErrorLog /var/www/
     CustomLog /var/www/ combined


Change system time zone

Use sudo dpkg-reconfigure tzdata and follow the instructions.

Compress folder contents

tar -cvzf filename.tar.gz folderName

Compress specific files in specific directories

find . -maxdepth 2 -name "*.txt" -type f \( -path "./subdir01/*" -o -path "./subdir02/*" -o 
	-path "./subdir03/*"  \) | tar -cvz -f text-files.tar.gz -T -

Uncompress archive file

tar -xvzf filename.tar.gz

For zip files use unzip -d destination_folder

Count the lines present in files with specified extension in current folder and sub-folders.

( find ./ -name '*.java' -print0 | xargs -0 cat ) | wc -l

Count number of files in current directory.

ls -1 | wc - l

Lookup for a particular file or directory name and list content

find . -maxdepth 1 -name "frame*" -print0 | xargs -0 ls -l

Get total number of bytes used by all files matching a particular pattern in current path and sub-folders

Example looking for JavaScript files with extension *.js.

find . \( -iname "*.js" \) -type f -ls | awk '{total += $7} END {print total}'

Next example is looking for common image file types.

find . \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" \) -type f -ls | 
	awk '{total += $7} END {print total}'

Restart all network services

sudo /etc/init.d/networking restart

List network interfaces

/sbin/ifconfig -a

Check how much free disk space is available on all drives.

df -h

List directory size including sub-directories

du -sh *

Copy certain file types including directory structure to a new destination

rsync -avm --include='*.ext1' --include="*.ext2" -f 'hide,! */' source destination

To dry run the above process add the n option, like so, -avmn.

Check for hidden paused jobs


To attach to a job and bring to foreground use

fg job_no

To send job to background and stop (pause) it

Ctrl + Z

To set the job running in the background

bg job_no

You can also start a process to run in the background by appending the & sign, for example, sublime_text &

To connect to a server via SSH

ssh -l username

Starting lengthy processes and leaving them running in the background using tmux

If you connect to a remote server through an SSH session and start a lengthy process as a background task, your task will be terminated as soon as you end the session or are accidentally disconnected. This is very frustrating of course, so to avoid this problem use tmux to instantiate another terminal session within which you will run your lengthy process.

To start a session: tmux new -s session_name
To list the running sessions: tmux ls
To attach to a session: tmux a -t session_name
To detach from a session: tmux detach
To kill a session: tmux kill-session -t session_name

Compile SRILM

SRILM - The SRI Language Modeling Toolkit is used to build and evaluate statistical language models, for tasks such as speech recognition, machine translation and other natural language processing tasks.

mkdir /usr/share/srilm
mv srilm.tar.gz /usr/share/srilm
cd /usr/share/srilm
tar xvzf srilm.tar.gz
sudo make NO_TCL=1

Searching command history

To view the list of commands used in the past type history. Each command is prefixed with a number, which you can then use to execute that command once again by typing !n where n is the prefixed number.

A quicker method is to press Ctrl + R and then start typing part of the command to look it up in the history list. Press Ctrl + R again to find other matches. Once you find the required command just press Enter to execute it.

Search within man page

To search within a man page just press the / key and type in your search query, which can include regex style patterns, such as [a-z], [0-9] and so on. To look for the next match press N or to look back press Shift + N. To end the search, press Ctrl + C.

Appending a line to a text file from the command line

Use echo "The line of text you want to append to the text file." >> file.txt

To concatenate commands on the command line

Use double & symbols to separate the commands, for example, clear && ls -l.

Create a symbolic link to a file

Use ln -s name_of_file name_of_link

Installing applications without packages

It is recommended to install third party applications which are not packages into /opt and then create symoblic links to them from /usr/local/bin or /usr/local/lib.

/usr/bin and /usr/lib are the global equivalents if you want to make a program available to all users.

Using cURL to test web RESTful APIs

curl -i -X POST http://localhost:3000/signup -d "u=username&p=password" -b cookies.txt -c cookies.txt

-X is used to specify the request command, such as GET or POST.
-d is used to pass form data.
-b and -c options are required to use cookies and thus allow session authentication.