Linux

Allikas: TMK Wiki

DRAFT VERSION!!

Fall 2016

Sisukord

Help and documentation

https://tldr.ostera.io/tar 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?

https://stackoverflow.com/questions/1358540/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'

SEARCH FILE, FIND FILE

find -iname inc_func.php


Autojump

Autojump: a cd command that learns

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

Usage:

  • 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.



History

Top 10 commands used:

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

Execute Previous Command

history
!43
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


Locate

locate
updated


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

Rename

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
foo
bar
foobar
FOO
$ echo !(foo\|bar)
foobar FOO


Rename many files at once

Example:

$ ls
this_has_text_to_find_1.txt
this_has_text_to_find_2.txt
this_has_text_to_find_3.txt
this_has_text_to_find_4.txt

$ rename \s/text_to_find/been_renamed/\ *.txt
$ ls
this_has_been_renamed_1.txt
this_has_been_renamed_2.txt
this_has_been_renamed_3.txt
this_has_been_renamed_4.txt

Copy

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-3.0.5.4p9.tar.gz root@vps.magic.ee:/root/
#copy other way around
set PATH="C:\Program Files (x86)\PuTTY"
pscp -v root@192.168.100.110:/var/games/minecraft/servers/Paradise.tar.gz c:/lan

File manipulation

Eliminate duplicate lines from a file

sort -u filename > filename.new



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


Repeate

!!

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


#removedir
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:

#!/bin/sh
counter=1
root=mypict
resolution=400x300
for i in `ls -1 $1/*.jpg`; do
        echo "Now working on $i"
        convert -resize $resolution $i ${root}_${counter}.jpg
        counter=`expr $counter + 1`
done

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

chmod u+x picturename.sh

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

picturename.sh /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.



Backup

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/issue.net
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`



Hostname

nano /etc/hostname

The file will contain something along the lines of this:

NETWORKING="yes"
GATEWAY="10.0.0.1"
HOSTNAME="www.example.com"

Find IP aadress

lynx -dump [2]


curl ipinfo.io

{
  "ip": "213.25.102.124",
  "hostname": "12324.s.t123vps.eu",
  "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

DHCPD

vi /etc/dhcp/dhcpd.conf

Named (DNS)

vi /var/named/tmk.zone

.BASHRC

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

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


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



# 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 http://checkip.dyndns.org:8245/ | 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 http://whatismyip.org/
 }



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

#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 "http://www.google.com/search?hl=en&q=define%3A+${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
					do
					echo "${response}"
					done < /tmp/templookup.txt
				else
					echo "Sorry $USER, I can't find the term \"${1} \""				
			fi	
\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
        c=''
        case $i in
          *.t@(gz|lz|xz|b@(2|z?(2))|a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz)))))
                 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 ;;
        esac
        [[ $c ]] && command $c "$i"
    else
        echo "$0: cannot extract \`$i': File is unreadable" >&2; e=2
    fi
  done
  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// /_}" 
	done
} 

clear




Alias

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/autojump.sh

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

Bash shell scripts

Creafind

#!/bin/sh

if test -z $1
then
	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
fi

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

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"

Creasize

#!/bin/sh

if test -z $1
then
	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
fi

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


While

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

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

Programs

Mediawiki

Config

/var/www/wiki.tmk.edu.ee/public_html/LocalSettings.php


Short URLs

WIKI Short Url
https://www.mediawiki.org/wiki/Manual:Short_URL/Apache

vi /var/log/apache2/wiki.magic.ee-error_log

[Wed Oct 05 00:05:08.636815 2016] [core:alert] [pid 11720] [client 90.191.17.88:55310] /var/www/wiki.magic.ee/.htaccess: 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 90.191.17.88:49882] 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/wiki.magic.ee/

vi w/LocalSettings.php
## https://www.mediawiki.org/wiki/Manual:Short_URL
$wgScriptPath = "/w";
$wgArticlePath = "/$1";
$wgUsePathInfo = true;
$wgLogo = "$wgScriptPath/resources/assets/wiki.png"


vi /etc/apache2/sites-enabled/wiki.magic.ee.conf
<VirtualHost *>
DocumentRoot "/var/www/wiki.magic.ee"
ServerName wiki.magic.ee
ErrorLog /var/log/apache2/wiki.magic.ee-error_log
CustomLog /var/log/apache2/wiki.magic.ee-access_log common
<Directory "/var/www/wiki.magic.ee">
allow from all
Options None
Require all granted
AllowOverride All
Options +FollowSymLinks
</Directory>
</VirtualHost>

vi /var/www/wiki.magic.ee/.htaccess

## http://www.mediawiki.org/wiki/Manual:Short_URL/Apache

# 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]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/w/index.php [L]



VI, VIM – text editor

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

http://www.cyberciti.biz/faq/vim-text-editor-find-and-replace-all-text/

:%s/kass/xn--pder-0qa/g


#duplicate line in editor
yy
p


GIT

Windows client

Repositiry


Git commands

GITHUB

Luua kasutaja github.com keskkonda Luua repository nimega "phpkursus2016"

#GIT reposse laadimine
git init
git add *
git status
git remote add origin https://github.com/kasutajanimi/phpkursus2016.git
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 https://github.com/kasutajanimi/phpkursus2016.git
#tõmmatakse alla repo saab muudatused (teha iga päev)
git pull
<pre>

#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:
 Checkout
 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

Email

echo test | mail -s "test message" root


WGET

wget examples http://www.labnol.org/software/wget-command-examples/28750/-http://www.labnol.org/software/wget-command-examples/28750/]


Apache

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

VIRTUAL HOST http://kimbriggs.com/computers/computer-notes/linux-notes/apache2-public_html-virtual-directories.file

cd /etc/apache2/sites-available/
cp kass.kellu.eu.conf xn--pder-0qa.kellu.eu.conf
a2ensite xn--pder-0qa.kellu.eu.conf
service apache2 restart


https://bensmann.no/restrict-sftp-users-to-home-folder/ [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/wiki.magic.ee/extensions

gzip -d file.gz tar xvf file.tar

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

Wordpress

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



PhpMyAdmin

  1. PMA login timeout

[4]



Applications

IPS

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


Payment

[5]


Tmux

tmux - linux screen multiplexer alternative to screen

apt-get install postfix apt-cache search gogs

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



APT

Upgrade system

apt upgrade


YUM

Add new repository

http://wiki.centos.org/AdditionalResources/Repositories/RPMForge

i386 
http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.i386.rpm

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 http://apt.sw.be/RPM-GPG-KEY.dag.txt

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



Tetris

Ascii Tetris game

bastet


WEBMIN

(echo "[Webmin]
name=Webmin Distribution Neutral
baseurl=http://download.webmin.com/download/yum
enabled=1" >/etc/yum.repos.d/webmin.repo
rpm --import http://www.webmin.com/jcameron-key.asc
yum -y install webmin)
https://server:10000/



VMware, Virtualbox

VMware

Multiple TTY windows

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


Caca-utils

Play videos in asci art

  • caca-utils
  • cacademo
  • cacaview
  • cacafire