Allikas: TMK Wiki


Fall 2016


Help and documentation This is a web client for a project called tldr-pages; they are a community effort to simplify the beloved man pages with practical examples.

Linux commands

Keyboard shortcuts, shortcut keys

Keyboard shortcuts that can drastically speed up your command line usage once they become second nature.

  • Alt+Backspace: Deletes the previous word.
  • Ctrl+A: Moves the cursor to the start of line.
  • Ctrl+E: Moves the cursor to the end of line.
  • Ctrl+L: Clear Screen (same as "clear")
  • CTRL+D: quits the shell.
  • Ctrl + Z: Puts whatever you are running into a suspended background process. fg restores it.
  • Tab: Auto-complete files and folder names

Search, find, locate

To find files and directories your are looking for.

How to count all the lines of code in a directory recursively?

Find from everywhere

grep -rnw '/' -e "bastet"
grep -Ril "bastet" /

Find from current dir

find . -name '*.gz'


find -iname inc_func.php


Autojump: a cd command that learns

  • apt install autojump
  • add the following line to your ~/.bashrc
. /usr/share/autojump/


  • j <dirspec>
  • j --stat

^R, Ctrl+R reverse search

  • ^R reverse search. Hit ^R, type a fragment of a previous command you want to match, and hit ^R until you find the one you want. Then I don't have to remember recently used commands that are still in my history.
  • CTRL-R and type a few first letters of iptables', like ipt\'. That will display the last iptables command you executed.


Top 10 commands used:

history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -nr | head

Execute Previous Command

history \| grep nologin

Bash history: "ignoredups" and "erasedups" setting conflict with common history across sessions [1]

Find big files

How to Find Biggest Files and Directories in Linux

du -a /home | sort -n -r | head -n 5

A common problem with computers is when you have a number of large files (such as audio/video clips) that you may want to get rid of. You can find the biggest files in the current directory with:

ls -lSrh

The "r" causes the large files to be listed at the end and the "h" gives human readable output (MB and such).

You can also look for the largest directories with:

du -kx | egrep -v "\./.+/" | sort –n



Find and make playlist

find /root/ -name \*.mp3 -print

How to Count Number of Files and Subdirectories inside a Given Directory

tree -iLf 1

List all files modified in last 5 minutes excluding .svn directories

find . -type d -name .svn -prune -o -mmin -5 -type f -print


Renaming/moving files with suffixes quickly

cp /home/foo/realllylongname.cpp{,-old\}

This expands to:

cp /home/foo/realllylongname.cpp /home/foo/realllylongname.cpp-old

Just to point out that to do the reverse (going from .cpp-old to .cpp) you\d do

cp /home/foo/realllylongname.cpp{-old,\}

Without some files

rm !(foo|bar)

expands like * without foo or bar:

$ ls
$ echo !(foo\|bar)
foobar FOO

Rename many files at once


$ ls

$ rename \s/text_to_find/been_renamed/\ *.txt
$ ls


network copy with ssh and tar

you can use ssh in conjunction with tar to pull an entire directory tree from a remote machine into your current directory:

ssh <username@sourcehost> tar cf - -C <sourcedir> . \| tar xvf - 

For example, let\s say you have a "bsmith" account on a host called "apple". You want to copy those files into your "bobsmith" account on a host called "pear". You\d log into your "bobsmith@pear" account and type the following:

ssh bsmith@apple tar cf - -C /home/bsmith . \| tar xvf - 

This technique is useful when you have insufficient disk space on the source machine to make an intermediate tarball.

tar pipe

One of my favorites tricks with bash is the "tar pipe". When you have a monstrous quantity of files to copy from one directory to another, doing

cp * /an/other/dir

doesn't work if the number of files is too high and explode the bash globber, so, the tar pipe :

(cd /path/to/source/dir/ ; tar cf - * ) | (cd /path/to/destination/ ; tar xf - )

Copy with permissions

cp -rp /home/my_home /media/backup/my_home

Copy from Windows to Linux

set PATH="C:\Program Files (x86)\PuTTY"
pscp ISPConfig-
#copy other way around
set PATH="C:\Program Files (x86)\PuTTY"
pscp -v root@ c:/lan

File manipulation

Eliminate duplicate lines from a file

sort -u filename >

Cycles, loop

echo "I can count to a thousand" ...{000..999\}
for f in *.txt; do mv $f ${f/txt/doc\}; done
touch {1,2\}.txt



Repeats your last command. Most useful in the form:

sudo !!

Change Directory, CD

cd - It's the command-line equivalent of the back button (takes you to the previous directory you were in).

It's worth mentioning that 'cd' takes you to your home directory.

Make directory, folder (mkdir)

Make a whole directory tree with one command Tags: mkdir Use the -p option to mkdir and make all parent directories along with their children in a single command.

mkdir -p tmp/a/b/c

rmdir foldername

Replacing same text in multiple files

If you have text you want to replace in multiple locations, there are several ways to do this. To replace the text Windows with Linux in all files in current directory called test\[something\] you can run this:

perl -i -pe \s/Windows/Linux/;\ test*

To replace the text Windows with Linux in all text files in current directory and down you can run this:

find . -name '*.txt' -print | xargs perl -pi -e's/Windows/Linux/ig' *.txt

Rename and resize images

Fond of your new camera but can\t put up with the terrible names? Do you want also to prepare them for publishing on the web? No problem, a simple bash script is what you need:

for i in `ls -1 $1/*.jpg`; do
        echo "Now working on $i"
        convert -resize $resolution $i ${root}_${counter}.jpg
        counter=`expr $counter + 1`

Save the script in a file called and make it executable with

chmod u+x

and store it somewhere in your path. Now, if you have a bunch of .jpg files in the directory /path/to/pictdir, all you have to do is to execute /path/to/pictdir

and in the current directory you'll find mypict_1.jpg, mypict_2.jpg etc, which are the resized versions of your original ones. You can change the script according to your needs, or, if you're just looking for super-simple image resizing, try looking at the mogrify command with its -geometry parameter.


tar -cjf bk-etc-`date +%Y-%m-%d`.tar.bz2 /location/

Listing – ls

How to list only subdirectories in the current one.

ls -d */

Listing today's files only ls -al --time-style=+%D \| grep `date +%D`

Shell color

a splash of colour in prompts:

PS1="\[\033[06;32m\] \h \[\033[42;30m\] \W \[\033[00;31m\] \$ \[\e[m\]"

Machine information, hardware

uname -a
cat /etc/
cat /etc/readhat-release

Login banner

cat /etc/motd

Becoming human

Pass the -h or -H (and other options) command line option to GNU or BSD utilities to get output of command commands like ls, df, du, in human-understandable formats:

ls -lh
# print sizes in human readable format (e.g., 1K 234M 2G)
df -h
df -k
# show output in bytes, KB, MB, or GB
free -b
free -k
free -m
free -g
# print sizes in human readable format (e.g., 1K 234M 2G)
du -h
# get file system perms in human readable format
stat -c %A /boot
# compare human readable numbers
sort -h -a file
# Show the  size of each file but in a more human readable way
tree -h
tree -h /boot

Processes, ps

Or, to be a little more clever, why not just list the top ten time-wasters:

ps aux  --sort=-%cpu | grep -m 11 -v `whoami`


nano /etc/hostname

The file will contain something along the lines of this:


Find IP aadress

lynx -dump [2]


  "ip": "",
  "hostname": "",
  "city": "",
  "region": "",
  "country": "LT",
  "loc": "56.0000,24.0000",
  "org": "AS62282 UAB Rakrejus"

Version check

Debian version

lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 8.4 (jessie)
Release:        8.4
Codename:       Jessie


vi /etc/dhcp/dhcpd.conf

Named (DNS)

vi /var/named/


# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)

# enable color support of ls and also add handy aliases
alias grep='grep --color=auto'
alias ls='ls --color=auto'

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

#netinfo - shows network information for your system
netinfo ()
echo "--------------- Network Information ---------------"
/sbin/ifconfig | awk /'inet addr/ {print $2}'
/sbin/ifconfig | awk /'Bcast/ {print $3}'
/sbin/ifconfig | awk /'inet addr/ {print $4}'
/sbin/ifconfig | awk /'HWaddr/ {print $4,$5}'
myip=`lynx -dump -hiddenlinks=ignore -nolist | sed '/^$/d; s/^[ ]*//g; s/[ ]*$//g' `
echo "${myip}"
echo "---------------------------------------------------"

### Functions ###
function mktar()
 tar czf "${1%%/}.tar.gz" "${1%%/}/"; 
function rot13()
 echo "$@" | tr a-zA-Z n-za-mN-ZA-M; 
function getip()
 lynx -dump

# Usage: repeat PERIOD COMMAND
function repeat() {
    local period
    period=$1; shift;
    while (true); do
        eval "$@";
    sleep $period;

#Dirsize - finds directory sizes and lists them for the current directory
dirsize ()
du -shx * .[a-zA-Z0-9_]* 2> /dev/null | \
egrep '^ *[0-9.]*[MG]' | sort -n > /tmp/list
egrep '^ *[0-9.]*M' /tmp/list
egrep '^ *[0-9.]*G' /tmp/list
rm -rf /tmp/list

# Define a word - USAGE: define dog
define ()
lynx -dump "${1}&btnG=Google+Search" | grep -m 3 -w "*"  | sed 's/;/ -/g' | cut -d- -f1 > /tmp/templookup.txt
			if [[ -s  /tmp/templookup.txt ]] ;then	
				until ! read response
					echo "${response}"
					done < /tmp/templookup.txt
					echo "Sorry $USER, I can't find the term \"${1} \""				
\rm -f /tmp/templookup.txt

##### Extract - extract most common compression types

extract() {
  local e=0 i c
  for i; do
    if [[ -f $i && -r $i ]]; then
        case $i in
                 c='bsdtar xvf' ;;
          *.7z)  c='7z x'       ;;
          *.Z)   c='uncompress' ;;
          *.bz2) c='bunzip2'    ;;
          *.exe) c='cabextract' ;;
          *.gz)  c='gunzip'     ;;
          *.rar) c='unrar x'    ;;
          *.xz)  c='unxz'       ;;
          *.zip) c='unzip'      ;;
          *)     echo "$0: cannot extract \`$i': Unrecognized file extension" >&2; e=1 ;;
        [[ $c ]] && command $c "$i"
        echo "$0: cannot extract \`$i': File is unreadable" >&2; e=2
  return $e

clock ()
while true;do clear;echo "===========";date +"%r";echo "===========";sleep 1;done

screenshot ()
import -frame -strip -quality 75 "$HOME/$(date +%s).png"

# Switch two files (comes in handy)		 #

function switchfile() {
mv $1 ${1}.tmp && $2 $1 && mv ${1}.tmp $2

# Changes spaces to underscores in names	 #

function underscore()
	for f in * ; do
    	[ "${f}" != "${f// /_}" ]
		mv -- "${f}" "${f// /_}" 



alias lsnew=" ls -al --time-style=+%D \| grep `date +%D` "
alias cd..="cd .."
alias ls="ls -la"
alias l=\ls -la

Creature alises

# vi /root/.bashrc

alias are='service apache2 restart'
alias cdsa='cd /etc/apache2/sites-available'
alias cdse='cd /etc/apache2/sites-enabled'
alias cdwww='cd /var/www/'

# Autojump: a cd command that learns; apt install autojump
. /usr/share/autojump/

#a splash of colour in prompts:
PS1="\[\033[06;32m\] \h \[\033[42;30m\] \W \[\033[00;31m\] \$ \[\e[m\]"

Bash shell scripts



if test -z $1
	echo "Usage: creafind <path> <searchword>"
	echo "creafind looks searchword from files in selected directoty."
	echo "error: 1st file argument is missing!"
	echo "Insert search path. Example: /etc or /home"
	echo "Program by (C)reature 2004."
	exit 2

if test -z $2
	echo "error: 2nd file argument is missing!"
	echo "Insert search word. Example: localhost"
	exit 2

find $1 -type f -exec grep -l $2 {} \;

#If you only want the filenames that have a matching line without showing the matching line:
find . -type f -print0 | xargs -0 grep -l "some string"



if test -z $1
	echo "Usage: creasize <path>"
	echo "creasize reports directory sizes."
	echo "error: 1st file argument is missing!"
	echo "Insert path. Example: /etc or /home"
	echo "Program by (C)reature 2006."
	exit 2

echo "200000=200MB"
du --max-depth=1 $1 | sort -n -r

Monitor, tail, while, watch

Want to get the last few lines of a log file?

tail /var/log/syslog

Want to quickly read over a file from the start?

more /var/log/syslog

Want to quickly find if a file contains some text?

grep "find this text" /var/log/syslog


When downloading a large file I quite often do:

while ls -la <filename>; do sleep 5; done

And then just ctrl+c when I'm done


watch - execute a program periodically, showing output fullscreen
watch --interval=10 lynx -dump http://dslrouter/stats.html





Short URLs

WIKI Short Url

vi /var/log/apache2/

[Wed Oct 05 00:05:08.636815 2016] [core:alert] [pid 11720] [client] /var/www/ Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

#enable modrewrite
a2enmod rewrite

ls /etc/apache2/mods-enabled/rewrite.load

[Wed Oct 05 00:11:52.107152 2016] [rewrite:error] [pid 12259] [client] AH00670: Options FollowSymLinks and SymLinksIfOwnerMatch are both off, so the RewriteRule directive is also forbidden due to its similar ability to circumvent directory restrictions : /var/www/

vi w/LocalSettings.php
$wgScriptPath = "/w";
$wgArticlePath = "/$1";
$wgUsePathInfo = true;
$wgLogo = "$wgScriptPath/resources/assets/wiki.png"

vi /etc/apache2/sites-enabled/
<VirtualHost *>
DocumentRoot "/var/www/"
ErrorLog /var/log/apache2/
CustomLog /var/log/apache2/ common
<Directory "/var/www/">
allow from all
Options None
Require all granted
AllowOverride All
Options +FollowSymLinks

vi /var/www/


# Enable the rewrite engine
RewriteEngine On

# Short url for wiki pages
#RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/index.php [L]

# Redirect / to Main Page
#RewriteRule ^/*$ %{DOCUMENT_ROOT}/index.php [L]

RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/w/index.php [L]

VI, VIM – text editor

  • Ctrl+G: Current line number
  • Shift+G: Go to end of file


#duplicate line in editor


Windows client


Git commands


Luua kasutaja keskkonda Luua repository nimega "phpkursus2016"

#GIT reposse laadimine
git init
git add *
git status
git remote add origin
git commit -m "minu kommentaar"
git push -u origin master

#uue faili üleslaadimine
touch foo.php 
git add * 
git commit -m "minu teine kommentaar"
git push -u origin master

#kogu repo endale masinasse tõmbamine (teha üks kord)
cd /var/www/html
git clone
#tõmmatakse alla repo saab muudatused (teha iga päev)
git pull

#Codelight ja GIT
LAMP all saab sama teha CodeLight tekstiredaktoriga määrates esimesel korral Plugins->Git->Set GIT repository path /var/www/html
Edaspidi klikkides parema nupuga failile ja valides alguses "+ Add file" ning hiljem
Plugins->Git->Commit local Changes
Plugins->Git->Push local Commits

#how to delete all commit history in github?
Deleting the .git folder may cause problems in your git repository. If you want to delete all your commit history but keep the code in its current state, it is very safe to do it as in the following:
 git checkout --orphan latest_branch

 Add all the files
 git add -A

 Commit the changes
 git commit -am "commit message"

 Delete the branch
 git branch -D master

 Rename the current branch to master
 git branch -m master

 Finally, force update your repository
 git push -f origin master

PS: this will not keep your old commit history around


echo test | mail -s "test message" root


wget examples]


Find Top 10 IP Addresses Accessing Your Apache Web Server

awk '{ print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -nr | head -n 10

Virtual host, subdomain


cd /etc/apache2/sites-available/
service apache2 restart [3]

CHECK apache2 confg syntax

apachectl -t

Unzip, tar

\#Unzip to right directory tar -xzf SyntaxHighlight_GeSHi-REL1_27-20c4ad5.tar.gz -C /var/www/

gzip -d file.gz tar xvf file.tar

#Create tar Archive File
tar -cvf tecmint-14-09-12.tar /home/tecmint/


#WP wp-config.php
#install updates and plugins
define(\FS_METHOD\, \direct\);


  1. PMA login timeout




  • ISPconfig
  • free cpanel
  • Webmin (enda serveri haldus)
  • Ajenti (enda serveri haldus, moodsam mui Webmin)
  • Cloudmin (cpanel)




tmux - linux screen multiplexer alternative to screen

apt-get install postfix apt-cache search gogs

  • CTRL+b
    • % - spilt vertical
    • " - spilt horisontal
    • Arrows – move focus


Upgrade system

apt upgrade


Add new repository


The preferred rpmforge-release package to retrieve and to install in order to enable that repository is one of the two listed above.

Install DAG's GPG key

rpm --import

Verify the package you have downloaded

rpm -K rpmforge-release-0.5.2-2.el5.rf.*.rpm

Install the package

rpm -i rpmforge-release-0.5.2-2.el5.rf.*.rpm

This will add a yum repository config file and import the appropriate GPG keys.

Then try to install something like this

yum install htop


Ascii Tetris game



(echo "[Webmin]
name=Webmin Distribution Neutral
enabled=1" >/etc/yum.repos.d/webmin.repo
rpm --import
yum -y install webmin)

VMware, Virtualbox


Multiple TTY windows

  • Edit-Preferences->Hot Keys->Ctrl+Alt+Shift
  • In X-Windows press
  • Ctrl+Alt F1-F6
  • Ctrl+Alt F7 = X windows


Play videos in asci art

  • caca-utils
  • cacademo
  • cacaview
  • cacafire