unusual-linux-commands

Unusual linux commands can be very handy in several cases. Here are some of them.

Repeat the last typed command:

!!

 

Display file-content like cat, but from upside down, can be achieved with tac (reverse cat)

tac myfile.txt

 

Like to lookup in a dictionary file? Use look. Everything whats starts with the passed word will be shown.

look car

 

Display time / date in bash screen on right upper corner. Continously!

while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &

 

Show all buildin commands

compgen -b

 

Show all commands you can run

compgen -c

Linux Tutorial Part2

Read Linux Tutorial Part1, before you start here.

Commands you have head about in Part1 are:
whoami, hostname, uname, uptime, clear, pwd, cd, echo, ~ and others.

 

In the second part of the Linux Tutorial, we will learn about dealing with files and folders (directory).

First move to your home directory with (change directory):

cd

Now let us create a directory, named „garage“, with the „make directory“ command:

mkdir garage

You have created the garage directory, but you are still inside your home directory. Go into the garage directory with:

cd garage

Notice,  that the „change directory“ is by far the most important command. You will see, that you will use this command very often.
In the next step, you go „one directory up“, so that you will be in your home directory again. This can be achieved with:

cd ..

Don’t forget the space between „cd“ and the two dots. The two dots is a sign for the directory above. There is a top-level directory, called the root. There is no directory above the root directory. You can go there with:

cd /

Again, there is a space between „cd“ and the „slash“. If you are curious whats inside the root folder, type:

ls -l

The list command, with the parameter „l“, shows you the directories content. Time to go back into our garage directory inside our home directory. Go there with:

cd ~/garage

The tilde (press Alt-key + tilde-sign) is a sign for our home folder. And we want to go inside the garage folder, which is inside our home folder. We create now a file called „mycar.txt“ and write „bmw“ inside this file. Here we go:

echo "bmw" > mycar.txt

The echo command simply displays the word in quotes, in this case „bmw“. The > character redirects the result („bmw“) into the file, which came behind the „>“ sign. Btw: a single „>“ always create an empty file, even when the file already exists. When you want to attach something to a file simply use „>>“. Let’s look into the file:

cat mycar.txt

We want to copy the file mycar.txt:

cp mycar.txt mybike.txt

And now we want to attach something to our new file:

echo "is not a car" >> mybike.txt

Check the content of our new file with:

cat mybike.txt

And check the contet of our garage folder with:

ls -l

 

Linux Tutorial Part1

These tutorials explain the Linux operating system (os) step by step. Starting with simple examples and getting more advanced each lesson.

The line where we type in the commands is called the prompt, the blinking something right to it is the cursor.

Lets show who is logged in:

whoami

And the hostname of the computer can be shown with:

hostname

As you can see, username and hostname are contained in the prompt.

If you want to know the name of the operating system, you can use:

uname

How long the computer is up (not rebooted since) shows the following command:

uptime

Clear all the stuff on the screen with:

clear

If you using the arrow keys (up and down), you can browse between the commands you already entered.

When you want to know in which folder you are, use:

pwd

It’s short for: print working directory.

Let’s go to your users home directory by typing:

cd

At this location, all the files related to your current user are stored. You home-folder is a special folder. The tilde sign „~“ is a synonym for your home folder. Instead of typing „cd“ you can type „cd ~“ as well. When you’re in your home folder you can see the tilde in the prompt. The tilde sign is a variable. You can output its content to the screen:

echo ~

Create a directory named garage withe this command:

mkdir garage

mkdir is short for: make directory

Most important command of all is „cd“. It means change directory. With this command you can browse in the directory tree.
Let’s switch to the directory „garage“ you recently created by typing:

cd garage

The prompt, by the way, also shows you in which directory you are. (So you don’t have to type pwd all the time)

 

If I want to go back in the directory-tree, like one level up, we use:

cd ..

Don’t forget a space between cd and the two dots. The two dots are a symbol for the upper (superordinate) folder.

If you type the first letters of a command, try to press two times the tab-key, and the command will be completed.
This will save your time. It’s one of the most useful ways to increase your working speed.

Linux Tutorial Part2 >>

Monitoring network traffic

tcpdump

You want to track all incomming http-Headers on your Webserver?

sudo tcpdump -A -s 10240 'tcp port 80' | egrep --line-buffered "^........(GET |HTTP\/|POST |HEAD )|^[A-Za-z0-9-]+: " | sed -r 's/^........(GET |HTTP\/|POST |HEAD )/\n\1/g'

Show the HTTP Requests only:

sudo tcpdump -A -s 10240 'tcp port 80' | egrep "^........(GET |HTTP\/|POST |HEAD )|^[:alnum:]+: " | sed -r 's/^........(GET |HTTP\/|POST |HEAD )/\n\1/g'

tcptrack

A quite handy tool for monitoring tcp traffic is tcptrack. Get it with:

sudo apt-get install tcptrack

and start it with

sudo tcptrack -i eth0 port 80

the interface parameter -i eth0 must be according to your needs, you can check your interfaces with ifconfig.

There is also tcpdump, tcpflow and other nice tools

Netstat

Sometimes one like to figure out what programs listen at a port. Here we can get an overview with:

sudo netstat -tupln

Where t=tcp, u=udp, p=show program name, l=show listening ports, n= numeric (not resolve machine names)
The Output could look something like this:

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1338/mysqld 
tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 1709/dnsmasq 
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1303/sshd 
tcp 0 0 127.0.0.1:3350 0.0.0.0:* LISTEN 1900/xrdp-sesman
tcp 0 0 127.0.0.1:9050 0.0.0.0:* LISTEN 1356/tor 
tcp 0 0 0.0.0.0:3389 0.0.0.0:* LISTEN 1887/xrdp 
tcp6 0 0 :::80 :::* LISTEN 2064/apache2 
tcp6 0 0 :::22 :::* LISTEN 1303/sshd 
tcp6 0 0 :::443 :::* LISTEN 2064/apache2 
udp 0 0 0.0.0.0:35885 0.0.0.0:* 1709/dnsmasq 
udp 0 0 0.0.0.0:5353 0.0.0.0:* 7335/chrome 
udp 0 0 0.0.0.0:5353 0.0.0.0:* 1062/avahi-daemon: 
udp 0 0 0.0.0.0:55935 0.0.0.0:* 1062/avahi-daemon: 
udp 0 0 127.0.1.1:53 0.0.0.0:* 1709/dnsmasq 
udp 0 0 0.0.0.0:68 0.0.0.0:* 1698/dhclient 
udp 0 0 0.0.0.0:631 0.0.0.0:* 4625/cups-browsed
udp6 0 0 :::51764 :::* 1709/dnsmasq 
udp6 0 0 :::45088 :::* 1062/avahi-daemon: 
udp6 0 0 :::5353 :::* 7335/chrome 
udp6 0 0 :::5353 :::* 1062/avahi-daemon:

 

lsof

With lsof you can determine easily which program listens on a port and under which user that program runs:

sudo lsof -i :80

 

mtr

Need Ping  and traceroute combined in a single application? Use mtr (matt’s traceroute)

mtr google.com

 

php packages installation

Let’s have a look which packages are already installed and show only those with „php“ in the name. A fresh system usually shouldn’t give any results.

dpkg --get-selections | grep php

Next you can check what kind of modules are available. Do it like this:

sudo apt-cache search --names-only php7

Now install the php7 base packages with the command:

sudo apt-get install -y php7.0

Now install some other packages for php:

sudo apt-get install -y libapache2-mod-php7.0 php7.0-curl php7.0-xml php7.0-mysql php7.0-mcrypt php7.0-intl php7.0-zip php7.0-bz2

 

send internet traffic over a proxy with tsocks

Sometimes it happens that you can’t use your services cause they deal with blocked ports. For exampel you use mysql on the commandline and it is by some reason denied. A proxy can help you. Tsocks helps you to use a proxy. Tsocks is a library for intercepting outgoing network connections and redirecting them through a SOCKS server.

install tsocks with

sudo apt-get install tsocks

Configure the file /etc/tsocks.conf

# the proxys address
server = 10.100.13.50
# the server type
server_type = 5
# the proxys port
server_port = 3306

and finally call your command like this:

tsocks mysql -h myHost -u myUser -pPassword

 

 

PDF Frontpage to jpg

First find all the files you want to convert and store their filenames in a file

find my-pdf-folder/ -type f -name "*.pdf" > pdffiles.txt

Iterate through that files and make a jpeg from them

while read -r LINE; do pdftoppm -f 1 -singlefile -jpeg "$LINE" "$LINE"; done < pdffiles.txt

Finally move all jpgs to a separate location if necessary

find my-pdf-folder/ -type f -name "*.jpg" -exec mv {} jpgs/ \;

 

perform action on multiple files found with find command

In reality you have quite often perform a certain command on several files. First i detect all files I’m looking for and store them in a filelist.

find . -name "myFilename" > filelist.txt

Second i run through this file, line by line and perform my command on all filename I’ve received one step before.

while read -r LINE; do echo "$LINE"; done < filelist.txt

You need to replace in several files. You can do it with sed. Be aware about masking special chars. In the example below the string „my\folder“ is replaced with „your\folder“.
Replacing with sed works like this: sed -i s#old#new#g
The delimiter (in this case the #) can be changed. The first sign after the s acts as delimiter.

while read -r LINE; do
sed -i s#\"my\/folder\",\"your\/folder\"#g "$LINE";
done < filenames.txt