Ubuntu Debian Linux useful commands

3 minute read

Updated: 2015-09-08

  1. Change keyboard layout to support both English (US) and Greek (GR)
setxkbmap -option grp:alt_shift_toggle us,gr
  1. Audjust monitor brightness and gamma
# make it darker
xrandr --output LVDS1 --brightness 0.4 --gamma 1.6:1.6:1.6

# restore to default brightness and gamma
xrandr --output LVDS1 --brightness 1 --gamma 1:1:1
  1. Find top 10 largest files in /
sudo du -a / | sort -n -r | head -n 10

# or in human readable format
cd / ; sudo du -hsx * | sort -rh | head -10

Source: cybercity

  1. Find IP without using ifconfig
netstat -n -t | awk '{print $4}' | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" |grep -v "" | sort -u
  1. Run logrotate
logrotate -f /etc/logrotate.conf
  1. Curl get http code and store it to Bash variable
bob=`curl -L -s -f -w "%{http_code}" --output /dev/null "http://www.google.gr"`
  1. Curl GET with url encode in BASH
curl -G "$urlGet" --data-urlencode "date=$datos" --data-urlencode "diskdata=$diskuse"
  1. Curl basic authentication
curl -u "myusername:mypass" "http://www.google.com"
  1. Url Encode String with Perl from Bash
adaUrlEncoded=`perl -e ' use URI::Escape; print uri_escape("$row");'`
  1. Cron Job every five minutes with sterr redirection
0,5,10,15,20,25,30,35,40,45,50,50,55 * * * * /home/bob/sendServerData.py >/dev/null 2>&1

More examples in Cyberciti tutorial

  1. Add a GPG key to apt manually

Lets say we want to add MongoDB GPG key to apt in order to install MongoDB in Debian Linux. We download the key from here or copy it to a file, lets say 10gen-gpg-key.asc. Then run:

cat 10gen-gpg-key.asc | sudo apt-key add -
  1. **Change your default locale **

Edit your locale variable values using:

sudo vi /etc/default/locale
  1. Merge PDF files

Append multiple pdf files (source*.pdf) to one (alltogether.pdf) using the following command. NOTE: The last document is the generated destination pdf document where the rest will be appended.

pdfunite source1.pdf source2.pdf source3.pdf alltogether.pdf
  1. Sort Nginx access log by Response Codes
sudo awk '{print $9}' /var/log/nginx/ssl.access.log | sort | uniq -c | sort -r

Source: rtcamp

  1. Count IP requests from Nginx access log
sudo awk '{print $1}' /var/log/nginx/ssl.access.log | sort | uniq -c | sort -nr

Source: Mkyong

  1. Fast Delete files modified before 10 minutes
time find $i/*.tmp -mmin +10 -type f -delete

Source: slashroot

  1. Remove newline character (‘\n’) from a file
tr '\n' ' ' < input_filename

or more hackish

sed ':a;N;$!ba;s/\n/ /g'

Source: StackOverflow

  1. View Apache requests per minute (from logs)
grep "23/Jan/2013:06" example.com | cut -d[ -f2 | cut -d] -f1 | awk -F: '{print $2":"$3}' | sort -nk1 -nk2 | uniq -c | awk '{ if ($1 > 10) print $0}'

Source: inmotionhost

  1. Redirect script output to syslog (while it is in crontab)

     0 1,9,17 * * * myscript.sh &2>1 | /usr/bin/logger -t myscript
  2. Grep Solr logs for errors
     egrep "^(ERROR|[[:blank:]])" solr.log.1 | egrep -v "^[[:blank:]]commit" > 20170515001700-errorlog.txt
  3. Create an SSH tunnel to a remote VM hosting MySQL server
     # This will create a tunnel which attaches to port 3306 of the remote server. To use it, we connect to port 3307 of our local machine.
     ssh -N -L 3307:localhost:3306 mysql.manios.org -p 3022

Leave a Comment