terminal usage [ubuntu, raspberrypi, debian]

based on:
http://www.heise.de/ct/artikel/On-a-highway-to-shell-287356.html #great shell infos
http://www.explainshell.com #explains shell commands
http://www.commandlinefu.com/commands/browse #code snippets
http://www.grymoire.com/Unix/Sed.html #sed tricks
http://learnxinyminutes.com/ #learn code languages(bash,git,c..)

nano cut more lines at once:
nano mark text at cursor position:

ctrl + F (forward one character)
ctrl + Space (forward one word)
or Page Up or Page Down for next/prev page
Ctrl + K to cut it

run following code as admin:
sudo -s

kill a window in linux:
xkill #then click on the window

upgrade only a special package:
sudo apt-get install --only-upgrade packagename

working with python lists:

get information about your system:
sudo apt install screenfetch

get debian version:
lsb_release -a

usage of the current diretory:
du -sh

size of a folder:
du -sh /* >> size.log

handling modify usage of groups:
grep pi /etc/group #shows all groups of pi
sudo usermod -a -G group1,group2,group3 testuser #add a user to groups
sudo usermod -G "" testuser #to remove a user from all groups

who is logged in:

check failed logins:
sudo lastb

transfer files:
scp pi@192.168.x.x:/home/pi/fileyouwant.txt /home/$USER/Schreibtisch/destinationoffileyouwant.txt

delete all lines beginning with a hashtag or that are empty:
sed '/^#/ d' file.txt | sed '/^\s*$/d'

request superuser rights to run a process:
pkexec apt update && apt upgrade

what system do you use:
more /etc/issue

what build image do you use:
cat /etc/rpi-issue

Get information for domain:
whois wordpress.com

dns information for domain:
dig wordpress.com

Lookup DNS ip address for the name
host wordpress.com

check if you are using 32 or 64 bit:
uname -m #shows your bit version

create a symbolic link:
ln -s /path/to/sourcefile.xyz /path/to/new/target.xyz

add :i386 to 64 bit sources:
sudo dpkg --add-architecture i386 && sudo apt update
sudo apt install..

remove i386 architecture:
based on: https://wiki.debian.org/Multiarch/HOWTO
sudo apt-get purge ".*:i386"
sudo dpkg --remove-architecture i386
sudo apt-get update

what version of debian is equal to ubuntu:

list installed programs:
cd /usr/share/applications/ && ls

list all autostarted programs:
cd /etc/xdg/autostart/ && ls

add a program to autostart:
check installed programs
cd /usr/share/applications/ && ls
then copy it over:
sudo cp /usr/share/applications/firefox.desktop /etc/xdg/autostart/

check wlan passwords:
cd /etc/NetworkManager/system-connections && ls
sudo nano name_of_network

remove all lines from a file that start with “string”:
sed -i '/^string/d' input-file

write a long text in terminal to file
cat << EOF | tee ~/test.txt
hello world

this is a very long story.
this is the end.

use sed to replace string with more lines:

use sed to insert more lines after a string:

use sed to insert more lines before a string:

navigate directorys:
cd .. #to move one directory up
cd ../Documents #go one directory up then move into folder Documents
cd ~ # move to home directory

sort lines with comma seperated arguments:
sort -k2 test.txt

count words and letters:
wc test.txt

update,upgrade,autoremove,clean old packages:
sudo apt-get -qq update && \
sudo apt-get -qq -y upgrade && \
sudo apt-get -qq -y clean && \
sudo apt-get -qq -y autoclean && \
sudo apt-get -qq autoremove

add a keyserver:
sudo apt-key adv --keyserver PGP_KEY_SERVER --recv-keys ID

remove a keyserver:
sudo apt-key list
sudo apt-key del D3D831EF
sudo rm /etc/apt/sources.list.d/mono-xamarin.list


show process tree:

switch to superuser:
sudo su

list apps:
cd /usr/share/applications && ls

some basic operators:
"/" ---at the end of a line: command will be continued in the next line
"|" ---connects the input and output of two commands (pipe)
";" ---seperates commands in one line
"$" ---marks a variable name
"&&"---connects two commands if the first one was successful
"||"---connects two commands second one will only run if the first one was not successful

explaining basic operators another way:
A; B Run A and then B, regardless of success of A
A && B Run B if A succeeded
A || B Run B if A failed
A & Run A in background.

run the last command again:# for example if the last command was “date”

run the last command again as sudo:
sudo !!

change to root user:
sudo -i

bash help:
help for
help return

list all installed packages
dpkg -l | grep ^i
or write it into a file:
dpkg -l | grep ^i > ~/test.txt

show all downloaded .deb files:
cd /var/cache/apt/archives && ls
clean that files:
sudo apt clean

create / add a new user for tests:
sudo adduser testuser
sudo su testuser

remove a user:
sudo passwd -l kontalkuser #lock useraccount
sudo userdel -r -f kontalkuser #home, mail, other users files there
sudo crontab -r -u kontalkuser #remove crontab

add as superuser a new user to the sudoers group:
adduser testuser sudo

install python pip:
sudo apt-get install python-pip
install a pip package:
sudo pip install package_name
upgrade a package:
sudo pip install --upgrade package_name
remove a package:
sudo pip uninstall package_name

read keymap:
xmodmap -pke > ~/.xmodmap

add another line at the end of a file:
echo "another line" >> /path/to/file.txt

search for files inside not-installed packages:
based on:
sudo apt-get install apt-file
sudo apte-file update
sudo apt-file search 'file'
apt-file list packagename

find packages:
sudo apt-file search packagename #first install apt-file and update it:sudo apt-file update
sudo apt-cache search packagename
#shows also the version

show dependencies:
apt-cache showpkg packagename

check if package is installed:
dpkg -l packagename

How to hide computer name and user name in terminal command prompt:
nano ~/.bashrc #and add at the end
export PS1="\W \$"

reinstall a program:
sudo apt-get install --reinstall amor

reinstall a package:
if you get the error:
error while loading shared libraries: libSDL2-2.0.so.0: cannot open shared object file: No such file or directory
sudo apt-get update && sudo apt-get install --reinstall libsdl2-2.0-0

create a gui window from bash:

prepare a long command for better view (works in shellscript):
echo This \
is \
really \
one \
long \

find / search directory:
find . -type d -name '*search_string*'

find / search file:
find . -name "*search_string*"

search / find all txt files in a directory:
ls *.txt

empty trash from terminal:
cd ~/.local/share/Trash/info/
rm -rf *
cd ~/.local/share/Trash/files/
rm -rf *

get partition uuids:
sudo blkid

nano shortcuts:
Ctrl + o :save without exit
Alt + w :search again
Ctrl + g :nano help

open a link from terminal:
press Ctrl while clicking the link (seams not to work in xfce workarround:
sudo apt install lxterminal #in terminal from lxde it works 🙂

mount iso images:
sudo mount -o loop filename.iso ~/tmpmount2/

mount an image to loop:
sudo losetup -f --show 2016-03-18-raspbian-jessie.img

unmount an image from loop:
sudo losetup -d /dev/loop0 /dev/loop1

Install the GRUB structures to a drive:
grub-install /dev/hda

change editor for your user: (for crontab for example)
cat ~/.selected_editor

change editor for root user: (for crontab for example)
sudo select-editor
sudo cat /root/.selected_editor

clean all logs:
sudo rm -r /var/log/*

read logs:
sudo less /var/log/syslog
sudo zless /var/log/syslog.2.gz
sudo tail -f -n 0 /var/log/syslog

If you want to completely silent your application(no output):
myprogram 2>&1 /dev/null

print out file with linenumbers infront of the file:
cat -n

copy all files from a folder to another:
sudo cp -r /path/to/source/folder/* ~/path/to/destination/folder/

give a folder all rights:
cd ~/path/to/folder/
sudo chmod -R 777 ./*

give all things in a folder to a user:
sudo chown -R xxfreddyxx /home/xxfreddyxx/Schreibtisch/folderx/

share files over sftp with another linux pc:
sudo apt install openssh-server
sudo apt install fail2ban
sudo apt install ufw gufw
#then enable port 22 in gufw
now you are able to reach your sftp server from another computer with:

stuck on linux:
install htop
sudo apt install htop
choose the running program you want to stop with with arrow up and down
press f9 to kill it
confirm killing with enter

or Ctrl + Alt +F2
top #lists all process (mostly that with the most cpu useage causes problems)
q #to quit top
pkill processname #kills all process that start with processname
Ctrl + Alt + F7 #to go back to normal screen

check battery:
sudo powertop

check if you are using systemd or upstart or sysv:
based on: http://unix.stackexchange.com/questions/196166/how-to-find-out-if-a-system-uses-sysv-upstart-or-systemd-initsystem
sudo stat /proc/1/exe

usage systemd:
systemd --version
systemd-analyze #how long did it take to boot kernel and system
systemd-analyze blame # to check each process
systemctl list-unit-files #list all services ,static means installed as dependency
systemctl list-units #list only running services

show all partition ids:
sudo blkid

terminal command to download a file from rpi to debian
sftp -P 22 pi@192.168.1.xx
get remote-path [local-path]

terminal command to upload a file from debian to rpi
sftp -P 22 pi@192.168.1.xx
put local-path [remote-path]

clear history:
history -c

search in a pdf file:
pdftotext file-name.pdf && cat file-name.txt | grep 'Search-string'

download a website:
wget --random-wait -r -p -e robots=off -U mozilla http://www.example.com

ping a ip adress every 60 sec and give a beep if it is there:
ping -i 60 -a IP_address

show network activity:
lsof -i

unzip a tar.gz file without saving it first:
wget -qO - http://example.com/path/to/blah.tar.gz | tar xzf -

get all string values from your ram:
sudo dd if=/dev/mem | cat | strings

create an iso image from cd:
readom dev=/dev/scd0 f=/path/to/image.iso

replaces spaces in filenames with underscores:
sudo apt install rename -y
rename 'y/ /_/' *

strip spaces from filenames:
rename 's/ //' *.JPG

rename all .xml files to .txt:
rename 's/\.xml$/.txt/' *xml

binary clock:
watch -n 1 'echo "obase=2;`date +%s`" | bc'

delete the last line from terminalhistory(if you typed a password by mistake or so):
history -d

get current gold price:
sudo apt-get install jq #sed for json
echo Gold price is $(wget "https://rate-exchange-1.appspot.com/currency?from=XAU&to=USD" -q -O - | jq .rate) USD

paste clipboard to terminal:
Ctrl + Shift + V

copy text from terminal:
Ctrl + Shift + C

select rectangle in terminal:
ctrl + alt then select it

pack a password protected zip file:
zip --password MY_SECRET secure.zip doc.pdf doc2.pdf doc3.pdf
or to select all files from a folder:
zip --password MY_SECRET secure.zip *
or maybe it also works with your gui:
rightclick on the folder
then choose as ending zip and at other options write down your password
maybe also install the programs for zipping:
sudo apt install p7zip-full unp

list files from a zip:
unzip -l files.zip

unzip specific file:
unzip -j "zip-archive.zip" "one_file.txt"
unzip -j "myarchive.zip" "in/archive/file.txt" -d "/path/to/unzip/to"

show partition table:

list txt files by time:
ls -lt --time=atime *.txt

clean system from old things:
sudo apt-get autoclean -y
sudo apt-get autoremove -y
sudo apt-get clean -y
dpkg -l |grep linux-image #to list all images same as dpkg --list
sudo apt-get purge linux-image-3.19.0-15-generic #to delete an old image

exit terminal / ssh session:
Exit console:
exit console:
Ctrl + D

kill a process:

kill a process my way:
Ctrl + Alt + F2
xxuserxx xxpasswordxx
Ctrl + Alt + F7
maybe (sudo top works too)

kill a process:
alt+f2 write “xkill” click the window (maybe sudo xkill)

kill a process that has a string “sample” in it:
pkill sample

kill a process called firefox:
ps aux | grep firefox
kill #### (pid) (process id)
sudo kill #### (if the process is not running by the actually user)

kill all processes called firefox:
killall firefox

usage mysql:
delete a database and user:
mysql -u root -p
DROP DATABASE databasename_to_delete;
DROP USER 'databaseuser_to_delete'@'localhost';


check mysql table:
based on:

check tables:
mysqlcheck -c -u root -p --all-databases

autorepair tables:
mysqlcheck -u root -p --auto-repair --all-databases

optimize tables:
mysqlcheck -o -u root -p --all-databases

fix broken dependencies:
sudo apt-get install -f #it is the shortcut for –fix-broken

sudo apt-get update && sudo apt-get upgrade -y
ssh pi@ -p 22 "sudo apt-get update && sudo apt-get upgrade -y"

update, upgrade and shutdown after that:
sudo apt-get update && sudo apt-get upgrade -y && sudo shutdown -h 1

dist-upgrade(normally it causes trouble, so do not use it):
sudo apt-get update && sudo apt-get dist-upgrade

shutdown / reboot:
sudo shutdown -h 0
sudo halt

shutdown the system in 30 minutes:
sudo shutdown -P +30
to quit the shutdown process:
sudo shutdown -c

sudo shutdown -r now
sudo reboot

handling with files:
make a folder structure:
mkdir ‐p language/learn/stuff

print working directory:
pwd #good for bash scripts

list all files in directory(also hidden ones):
ls -la

properties of a file:
ls -lh filename

prints the file to the terminal:
cat path/to/file

give all rights to a file:
sudo chmod 777 filename

change owner and group from root to your user:
chown xfreddyx /path/to/file/filename
chown xfreddyx:xfreddyx /path/to/file/filename

say user:

say home:

change directory:
cd /xx/yy

go to the previous directory:
cd -

go one directory up:

go into next folder:
cd ./next

show linenumber infront of a file:
cat -n song.txt

top ten lines of a file:
head -n 10 file.txt

bottom ten lines of a file:
tail -n 10 file.txt

sort files.txt lines:
sort file.txt

list things in folder:

list all things in folder:
ls -a

autocomplete folder or filename:

remove file:
sudo rm /home/pi/test.txt

cd /home/pi/
rm test.txt

remove all files from a directory:
sudo rm /path/to/directory/*.*

remove all files and folders:
sudo rm -r /path/to/directory/*

clear a folder from inside:
rm -r ./*

remove all the programs,everything:(so only use it to destroy your system)
rm -rf /

make a directory:

remove a directory:

remove folder:
sudo rm -rf home/testuser/files/

move file:
sudo mv /home/pi/Desktop/test.txt /home/user2/

copy file:
sudo cp -r /home/pi/folder /home/user2/ #-r for all subfolders+files

rename files:
rename from c.sh to c
sudo mv /home/pi/test.txt /home/pi/test2.txt

echo blah-date.eps.png |cut -d\. -f1

replace every ok with great:
sed -i 's/okay/great/g' file.txt

replace A with a in patter line 14-17:
sed '14,17 s/A/a/'

print all lines beginning with foo and ending with bar:
grep "^foo.*bar$" file.txt

previous command as input for next one:
ls -l | grep "\.txt" #lists only txt files

calculate in bash:
echo $(( 10 + 5 ))

bash create multiple folders in a line:
for (( i=1; i <= 24; i++ )); do mkdir $i; echo "created folder $i"; done

print variable 3 times:
for Variable in {1..3}
echo "$Variable"

run cat on more files:
for Variable in file1 file2
cat "$Variable"

traditional for loop:
for ((a=1; a <= 3; a++))
echo $a

Deletes all lines starting with #:
sed '/^#/ d'

Delete first line:
sed '1d'

handle terminal history:
see last terminal command:

terminal history:

edit terminal history:
nano .bash_history


reconfigure locales:
sudo dpkg-reconfigure locales

reconfigure timezone:
sudo dpkg-reconfigure tzdata

edit crontab:
crontab -e

list crontab:
crontab -l

cron logs:
grep CRON /var/log/syslog

more info about your linux version:
cat /etc/os-release

using debian or ubuntu or..:
cat /etc/issue

linux kernel:
uname -a

only your linux version number:
uname -r

difference between files:
diff file1.txt file2.txt

print calendar:
cal -h > ThisMonth.txt
cal -y 2016 #shows the whole year
cal -m 4 #shows the fourth month of the year
cal -3 #show previous month, actually month, next month

what happend today in history:

compute and check md5:

get the man help for a program:
man sudo

get the installed python version:
python -V

proof a sha1sum of a file:
cd ./Downloads/
sha1sum filename

or for sha256sum
sha256sum filename

search for a special word in a file or output and paste a part of the line:
based on:
useage awk:
awk '/search pattern1/ {Actions}
/search pattern2/ {Actions}' file

awk '/search_pattern/ { action_to_take_on_matches; another_action; }' file_to_parse
cat filename | awk '/search_pattern/ { action_to_take_on_matches; another_action; }'
^ infront of pattern means exactly that pattern between spaces in line (not part of word)
$NF means last word
$2 means second word ..

search for a special word in a file:
grep key_word /path/to/file/yourfile.txt

search for a special word in more files:
grep -rnw /path/to/folder/ -e katzen

connect to pi from ubuntu:
ssh pi@192.168.1.xx -p 22

if you forgot to write sudo infront of a process:

run same command again:

run last command beginning with foo:

clear screen:

stop running process in console:

cpu info:
cat /proc/cpuinfo

Detailed CPU info:

running programs:

process monitor like top:
sudo htop

network information:
sudo ifconfig -a

wlan info:

scan wlans:
iwlist scanning

show the ip:
hostname -I

show new connected usb devices and more:

live show new connected usb devices and more:
dmesg --follow --human

superuser rights:
sudo su

change user:
su username

list all users:

list all groups:

info last time password was set:
passwd -S

change password:

clear all text in console:

execute the previous command with sudo before:
sudo !!

show free ram:
free -h

how long is the pc up:

show last reboot:
last reboot

show shutdown:
last shutdown

show useable free space:
df -h

crop and rename pictures:
cd ~/Desktop/pics/
convert *.png -crop 795x595+287+121 +repage $((var++)).png
for i in *.png; do mv "$i" "$((var++))".png; done

testing connection to another pc:

testing ping of more websites at the same time:
sudo apt install oping
noping wenzlaff.de wenzlaff.info kleinhirn.eu

open file with nano:
sudo nano /home/pi/test.txt
nano -c /home/pi/test.txt
#shows lines and rows

open file with leafpad:
sudo leafpad /home/pi/test.txt

run bash file:
sudo bash -x /etc/init.d/c start
sudo bash -x /etc/init.d/c status

make a bash script executeable:
(so you can run script.sh instead of sh script.sh)
chmod +x script.sh
sudo chmod +x /etc/init.d/c

make a script not executable:
chmod -x script.sh

usage tmux:
based on: https://gist.github.com/MohamedAlaa/2961058
start new with session name:
tmux new -s myname

tmux a # (or at, or attach)

detach session:
ctrl+b then d

attach to named:
tmux a -t myname

list sessions:
tmux ls

kill session:
ctrl + b then write :kill-session
tmux kill-session -t otherSessionName

tmux split screen horizontally:
Ctrl-b %

navigate to other window:
Ctrl-b arrowkey

split screen vertically:

make pane go full screen:
Ctrl-b z #Hit Ctrl-b z again to shrink it back to its previous size

exit window:
Ctrl-d or write: exit

list all tmux sessions:
tmux ls

useage screen:
start process with screen:
screen -dmS longrun sh -c 'echo hihi;/home/pi/cam/time_lapse/a_record.sh'

get back screen session:
screen -S longrun -x
screen -d -r longrun

detach screen session:
ctrl+a d

quit screen session inside the session:

quit screen session outside the session:
screen -S longrun -X quit
screen -> screen -ls


get back:
screen -x
screen -r 5180

kill a session:

exit (to kill a screen)

setup monit:
sudo mv /etc/monit/monitrc.d/cron /etc/monit/conf.d
end of monitrc : include…/conf.d/*
sudo nano /etc/monit/monitrc //with putty then the monit script over the end over includes
sudo apt-get install monit for install
sudo nano /etc/monit/monitrc for setup
sudo monit -t for checking
sudo monit start all start running all of the monitored programs.
sudo monit status
sudo service monit restart

check all repositorys stable unstable free nonfree:
sudo nano /etc/apt/sources.list

check by hand added repositorys:
cd /etc/apt/sources.list.d

show installed pakets:
dpkg -l | more

list all features/folders of a program:
dpkg -L new_program or dpkg --list PartOfPackagename

info about a package:
dpkg --info packageName

find installed package:
dpkg -l | grep 'your_special_name' #list them

save installed packages into a txt file:
dpkg -l > ~/Schreibtisch/all_installed_packages.txt

remove programs (apt-get):
sudo apt-get remove xxx
sudo apt-get purge xxx # include conf files
sudo apt-get remove –purge xxx -y

install deb packages(dpkg):
sudo dpkg --install packetname.deb

remove deb packages(dpkg):
sudo dpkg --remove packetname

fix dependencies problems:
apt-cache rdepends packagenamethatcausesproblems
sudo apt purge -y ...
sudo apt-get autoclean
sudo apt-get clean
sudo apt-get autoremove

install programs (apt-get):
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y # be carefull with it
sudo apt-cache search xxx
sudo apt-cache show xxx
sudo rpi-update
sudo apt-get install audacity #install audacity

some more programs to install:

login to a pi with password directly [debian]
install sshpass:
sudo apt-get install sshpass -y
sshpass -p 'raspberry' ssh pi@192.168.1.xx
login + update + upgrade:
sshpass -p 'raspberry' ssh pi@192.168.1.xx -p 22 "sudo apt-get update && sudo apt-get upgrade -y"

install programs (pacman):
pacman -S -y

write long text to file from commandline:

get cpu and gpu temperatures:
sudo apt install psensor #nvidia card only working after installing their graphic drivers
psensor > Sensoreinstellungen > Alarm > Obere Grenze 75°C

acpi -t #cpu temperature
acpi -i #battery
acpi -V #high temp, critical temp
sensors #high temp, critical temp
nvidia-smi -l #list all about nvidia graphic card

backup wordpress from inside browser:
Datei->Seite speichern untern->Webseite,komplett->speichern


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.