Ubuntu Debian Linux useful commands

Updated: 2015-09-08

  1. Change keyboard layout to support both English (US) and Greek (GR)
1
setxkbmap -option grp:alt_shift_toggle us,gr
  1. Audjust monitor brightness and gamma
1
2
3
4
5
# 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 /
1
2
3
4
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
1
netstat -n -t | awk '{print $4}' | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" |grep -v "127.0.0.1" | sort -u
  1. Run logrotate
1
logrotate -f /etc/logrotate.conf
  1. Curl get http code and store it to Bash variable
1
bob=`curl -L -s -f -w "%{http_code}" --output /dev/null "http://www.google.gr"`
  1. Curl GET with url encode in BASH
1
curl -G "$urlGet" --data-urlencode "date=$datos" --data-urlencode "diskdata=$diskuse"
  1. Curl basic authentication
1
curl -u "myusername:mypass" "http://www.google.com"
  1. Url Encode String with Perl from Bash
1
adaUrlEncoded=`perl -e ' use URI::Escape; print uri_escape("$row");'`
  1. Cron Job every five minutes with sterr redirection
1
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:

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

Edit your locale variable values using:

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

1
pdfunite source1.pdf source2.pdf source3.pdf alltogether.pdf
  1. Sort Nginx access log by Response Codes
1
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
1
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
1
time find $i/*.tmp -mmin +10 -type f -delete

Source: slashroot

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

or more hackish

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

Source: StackOverflow

  1. View Apache requests per minute (from logs)
1
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)

    1
    
    0 1,9,17 * * * myscript.sh &2>1 | /usr/bin/logger -t myscript
    
  2. Grep Solr logs for errors

    1
    
    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

    1
    2
    
    # 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
    
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy