Tips and tricks

Created Monday 18 November 2013

  1. Check after which line of code begins overload:
    1. curl www.ilist.co.il
  2. Connect to incoming mail server by telnet:
    1. telnet 127.0.0.1 110
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
+OK Hello there.
user nameit@domain.com
+OK Password required.
pass 1234567
-ERR Login failed
  1. Setting "system-wide" Emacs key bindings:
    1. gsettings set org.gnome.desktop.interface gtk-key-theme "Emacs"
  2. Emacs replace rectangle contents:
    1. C-x r t string <RET>
  3. Emacs replace-regex with backreference:
    1. http://www.gnu.org/software/emacs/manual/html_node/emacs/Regexp-Replace.html
In replace-regexp, the newstring need not be constant: it can refer to all or part of what is matched by the regexp. ‘\&’ in newstring stands for the entire match being replaced. ‘\d’ in newstring, where d is a digit, stands for whatever matched the dth parenthesized grouping in regexp. (This is called a “back reference”.) ‘\#’ refers to the count of replacements already made in this command, as a decimal number. In the first replacement, ‘\#’ stands for ‘0’; in the second, for ‘1’; and so on. For example,

M-x replace-regexp <RET> c[ad]+r <RET> \&-safe <RET>

replaces (for example) ‘cadr’ with ‘cadr-safe’ and ‘cddr’ with ‘cddr-safe’.

M-x replace-regexp <RET> \(c[ad]+r\)-safe <RET> \1 <RET>

performs the inverse transformation. To include a ‘\’ in the text to replace with, you must enter ‘\\’.

If you want to enter part of the replacement string by hand each time, use ‘\?’ in the replacement string. Each replacement will ask you to edit the replacement string in the minibuffer, putting point where the ‘\?’ was.
EXAMPLE 1
C-M-s \(^.*\)\(linear-gradient(\)to \b\w*\b\(.*$\) - just to be sure that the selection works
C-M-% \(^.*\)\(linear-gradient(\)to \b\w*\b\(.*$\) <RET>
\& C-q C-j
\1-prefix-\2\?\3
(use anything-query-replace-regexp for dynamic highlighting)
EXAMPLE 2 (perfection)
C-M-% \(^.*\)\(linear-gradient(\)\(to right\|to bottom\)\(.*$\) <RET>
\& C-q C-j
\1-prefix-\2\,(if (equal "to right" \3) "left" "top")\4
  1. Emacs regex to replace existing radiuses with 5 px:
    1. C-M-% \(border-radius:\)\( ?\)\([0-9]+px\|0\)\( ?\)\([0-9]+px\|0\)\( ?\)\([0-9]+px\|0\)\( ?\)\([0-9]+px\|0\) <RET>

\1\2\,(if (not (equal "0" \3)) "5px" "0")\4\,(if (not (equal "0" \5)) "5px" "0")\6\,(if (not (equal "0" \7)) "5px" "0")\8\,(if (not (equal "0" \9)) "5px" "0")
  1. Emacs html-mode jump to closing/opening tag:
    1. C-c C-f
C-c C-b
  1. Emacs delete matching lines:
    1. M-x delete-matching-lines - an alias for flush-lines
  2. Emacs regex multiple lines:
    1. http://www.emacswiki.org/emacs/MultilineRegexp
\(.|\n\)
\(.*\n?\)*
[\0-\377[:nonascii:]]*
For incremental search use C-q C-j
^.*background.*:linear.*^J.*^J.*^J.*^J.*$ - ^J states for C-q C-j
\(^.*background.*:linear.*^J\)\(.*^J\)\(.*^J\)\(.*^J\)\(.*$\) - OR:
\(^.*background.*:linear.*\)\(^J.*\)\(^J.*\)\(^J.*\)\(^J.*$\)
  1. Emacs 5 space delimited columns regex:
    1. ^\([^ ]* \)\{5\}
  2. Emacs regex find '.' then numbers and ' ' and everything till ' ':
    1. \.[0-9]* [^ ]*
  3. Tricky find:
    1. find -type f -mtime -40 -exec ls -l --full-time {} \; | sort -t ' ' -k 6.1,6.10 -k 7.1,7.8 | grep -v '.*\.png$\| .*\.gif$\|.*resellerclubmods.*$\|.*cust1he.*$\|.*CallBack.*$\|.*~$'
    2. find /home/test/ -path ./.git -prune -o -type f -mtime 5 -exec ls {} \; | grep -En '.*'
  4. Create automated ssh connection to a server:
    1. ssh-keygen -t rsa
    2. **scp /home/user/.ssh/id_rsa.pub root@serveraddr:.ssh/authorized_keys2**
  5. Update Ubuntu with aptitude:
    1. aptitude safe-upgrade --full-resolver
  6. Get text file encoding:
    1. file -bi filename
  7. Find php.ini:
    1. find / -name "*php.ini*"
  8. Rewrite recipient address of incoming mail in postfix:
    1. + Added to /etc/postfix/main.cf:
    remote_header_rewrite_domain = domain.invalid
recipient_canonical_maps = hash:/etc/postfix/canonical
+ Created file:
    touch /etc/postfix/canonical
+ Contents in /etc/postfix/canonical:
support@domain.co.il support@domain.net
supp@domain.co.il support@domain.net
support@domain.com support@domain.net
+ Commands after:
    postmap /etc/postfix/canonical
    postfix reload
  1. Add user for postfix and WHMCS:
    1. + Create user billing:
adduser billing
+ Add following lines to /etc/postfix/canonical
accountingsupport@domain.net billing@domain.net
accounting@domain.net billing@domain.net
+ Run:
postmap /etc/postfix/canonical
postfix reload
+ Add following line to /etc/aliases
billing: "| php -q /var/www/whmcs/pipe/pipe.php"
OR (to set default permissions for file creation)
billing: "| (umask 022; php -q /var/www/whmcs/pipe/pipe.php)"
http://www.cyberciti.biz/tips/understanding-linux-unix-umask-value-usage.html
+ Run:
postalias /etc/aliases
  1. Count all lines in a file (may add more filenames after the first with spaces) that don't begin with #:
    1. sed -e 's/^#.*//' index.org | grep -v '^$' | wc -l
  2. Regex for finding the diff sentences:
    1. grep -R '^[0-9]*,\?[0-9]*[a|c|d][0-9]*,\?[0-9]*'
  3. Useful Firebug extensions:
    1. Firecookie
    2. Firestarter - ?
    3. Firediff
    4. Firefocus
    5. Formbug
    6. Omnibug
    7. ConsoleExport - ?
    8. Selectbug - ?
    9. Acebug
    10. CodeBurner
    11. Firebug Autocompleter
    12. Firefinder
    13. FireRainbow
    14. Firepicker
    15. FireStorage Plus!
    16. Arboreal - Not available for FF 25
    17. FirePath
    18. Fireformat
    19. Chromebug - ?
    20. Eventbug - ???
    21. Firebug Code Coverage
    22. FireFlow
    23. FireQuery
    24. Javascript Deminifier
    25. FireAtlas - for ASP.NET AJAX
    26. FireLinkReport - ?
    27. Closure Inspector - ?
    28. Illuminations for Developers
  4. LaTeX tables
    1. \begin{tabular}{ l | c || r }
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
  1. \begin{center}
\begin{tabular}{ l | c || r }
\hline
1 & 2 & 3 \\ \hline
4 & 5 & 6 \\ \hline
7 & 8 & 9 \\
\hline
\end{tabular}
\end{center}
  1. \begin{tabular}{|r|l|}
\hline
7C0 & hexadecimal \\
3700 & octal \\ \cline{2-2}
11111000000 & binary \\
\hline \hline
1984 & decimal \\
\hline
\end{tabular}

  1. Setting default theme in Emacs:
    1. Open .emacs.d/init.el or .emacs
    2. Add:
      1. (load-theme 'manoj-dark)
  2. Javascript: count string occurence:
    1. var temp = "This is a string";
    2. var count = temp.match(/is/g);
    3. alert(count.length);
    4. http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string
  3. Connect to Windows VPS:
    1. rdesktop -u username -p 'password' -f 127.0.0.1
    2. rdesktop -u username -p 'password' -g 1280x1024 127.0.0.1 - custom resolution
    3. rdesktop -u username -p 'password' -f 127.0.0.1 -r disk:shrName='/path/to/host/dir' - share a host dir
  4. Connect to FreeBSD with rdesktop:
    1. xrdp
  5. Check if the port is open:
    1. nmap 127.0.0.1 -p 443
  6. Access remote zim:
Install sshfs. Use your local desktop's file manager to mount ssh:/USER@REMOTE/home/USER. Or mount the remote folder from your terminal:
mkdir -p ~/mnt/REMOTE
sshfs USER@REMOTE:/home/USER ~/mnt/REMOTE
Browse into the mounted path, find your Notebook's folder, and double-click the .zim file.
-o reconnect -C -o workaround=all
  1. Tricky PS:
root@ws-alexey:~# ps -e --forest | grep -B 5 -A 5 ssh
kill -9 1980
  1. Unmount sshfs:
regular: fusermount -u /mnt/somefolder
force: fusermount -uz /media/somefolder
  1. VPS — Windows — high memory usage:
    1. http://support.microsoft.com/kb/321363
    2. http://www.nzgn.co.nz/sqlservr-exe-causing-high-memory-usage-sbs-server-2008/
  2. VPS — Windows — PLESK — ASP.NET 4.0 vs 2.0:
    1. http://blog.aytacengin.com/plesk-panel-asp-net-4-0-nasil-calistirilir
  3. ssh:
    1. ssh -X -l user 127.0.0.1 - with X
    2. **ssh user@127.0.0.1** - regular
  4. Calculate string's length:
    1. echo -n 'some string' | wc -m - Linux
  5. Linux disk usage:
    1. du -h
    2. du -h some/path
    3. du -ach | grep -e '^[0-9]\{3\}M\|^[0-9]\{1,2\}\.\?[0-9]*G' | sort -n > curfolderdu.txt
    4. du -hcs
    5. df -h
    6. http://www.codecoffee.com/tipsforlinux/articles/22.html
  6. Create custom sized file:
    1. dd if=/dev/zero of=output.dat bs=1024 count=10240 - 10M
    2. dd if=/dev/zero of=create-file bs=1M count=200 - 200M
    3. http://linuxcommando.blogspot.co.il/2008/02/create-file-of-given-size.html
    4. http://forum.linuxcareer.com/threads/1639-How-to-create-file-in-linux
  7. Connect to SMTP by telnet:
    1. http://technet.microsoft.com/en-us/library/aa995718%28v=exchg.65%29.aspx
    2. http://www.opinionatedgeek.com/dotnet/tools/base64encode/
  8. Generate Machine Key:
    1. http://aspnetresources.com/tools/machineKey
    2. http://msdn.microsoft.com/en-us/library/ff649308.aspx
  9. List all opened windows on all workspaces sorted by the number of workspace
    1. wmctrl -lG | sort +1 -2 -n > my_open_windows.txt - won't work in i3wm
  10. How to see headers on Outlook 2003/2007/2010
    1. http://answers.vt.edu/kb/entry/2659
  11. Test page load speed:
    1. http://tools.pingdom.com/fpt/
  12. Redirect 301:
    1. http://www.htaccessredirect.net/
    2. http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php
    3. http://www.askapache.com/htaccess/modrewrite-tips-tricks.html
    4. http://en.wikipedia.org/wiki/URL_redirection
  13. Scan websites:
    1. http://www.urlvoid.com/
    2. http://hackertarget.com/whatweb-scan/
    3. http://hackertarget.com/nmap-online-port-scanner/
    4. http://virusscan.jotti.org/en
    5. http://onlinelinkscan.com/
    6. https://www.virustotal.com/
    7. http://sitecheck.sucuri.net/scanner/
  14. 64bit encoder online:
    1. http://www.opinionatedgeek.com/dotnet/tools/base64encode/
  15. Listen eth1 for TCP to specific IP:
    1. tcpflow -c -s -i eth1 host 127.0.0.1
  16. IIS 64 bit to 32 bit mode:
    1. http://blogs.msdn.com/b/rakkimk/archive/2007/11/03/iis7-running-32-bit-and-64-bit-asp-net-versions-at-the-same-time-on-different-worker-processes.aspx
  17. Restart Plesk service:
    1. /etc/init.d/psa restart — or:
    2. service psa restart
  18. Check mail password on Plesk server:
    1. history | grep mysql - copy the appropriate command grepping its output with email name:
    2. mysql -uadmin -p`cat /etc/psa/.psa.shadow` -Dpsa -e"select m.mail_name, d.name, a.type, a.password from domains d, mail m, accounts a where m.dom_id = d.id and m.account_id = a.id" | grep emailname
  19. Adding input languages - Linux:
    1. setxkbmap -layout 'us,il,ru' -option 'grp:alt_shift_toggle,grp_led:scroll'
  20. Enter long url in smartphone:
    • Install barscanner program on phone
    • Look for "qr code generator" on PC (http://qru.nu/index.php for example)
    • Enter URL there and generate a barcode
  21. Remote connection program (userful for changing client resolution):
    1. remmina
  22. Cancel error log on VPS + PLESK:
    1. Create file vhost.conf in conf folder
      1. /var/www/vhosts/DOMAIN_NAME/conf
    2. Add to vhost.conf line:
      1. ErrorLog /dev/null
    3. Run domain reconfiguration
      1. /usr/local/psa/admin/sbin/httpdmng --reconfigure-domain DOMAIN_NAME
    4. Reload apache
      1. /etc/init.d/httpd reload
  23. Google Apps: CNAMEs and Naked Domains:
    1. https://sites.google.com/site/gsunhelp/step-by-step-guides/cnamealiasmappinghowdoesitallwork/mapping-site-custom-domain/cnames-and-naked-domains
  24. Google Apps: Google Apps Naked Domain Redirect:
    1. https://sites.google.com/site/gsunhelp/step-by-step-guides/google-apps-naked-domain-redirect
  25. ASP redirect example:
<%
If InStr( UCase(Request.ServerVariables("SERVER_NAME")), UCase("somedomain.org") ) > 0 Then
Response.Redirect("/idso/index.html")
End If
%>
  1. Option +FollowSymLinks:
    1. Options +SymLinksIfOwnerMatch
  2. Find rules files in Thunderbird:
    1. find /home/$LOGNAME/.thunderbird/ -type f -name msgFilterRules.dat
  3. Apache module: run html files as php (.htaccess):
    1. AddHandler application/x-httpd-php .html
  4. Connect mysql with SSH to root (if there is PLESK):
    1. mysql -uadmin -p`cat /etc/psa/.psa.shadow`
  5. Lock screen Ubuntu:
    1. gnome-screensaver — turn on
    2. gnome-screensaver-command -l - lock
  6. Configure dual head:
    1. xrandr --output VGA1 --mode 1920x1080 --rate 60
    2. xrandr --output HDMI1 --mode 1280x1024 --rate 60
    3. xrandr --output VGA1 --auto --left-of HDMI1
  7. Recursive list directories only:
    1. find dir/path -type d -ls
    2. ls -Rp dir/path | grep "/"
  8. Mount:
    1. EXT4:
      1. mount -t ext4 /dev/sdb1 /media/extdisk
    2. FAT32:
      1. mount -t vfat /dev/hda1 /mnt/d
      2. http://www.cyberciti.biz/faq/how-to-access-the-fat32-files-or-filesystem-from-linux-system/
    3. NTFS:
      1. mount -t ntfs /dev/sdb1 /mnt/ntfs
      2. http://linuxconfig.org/how-to-mount-partition-with-ntfs-file-system-and-read-write-access
    4. NTFS with write permissions:
      1. mount -t ntfs-3g /dev/sdb1 /mnt/ntfs/
        1. To install ntfs-3g:
          1. sudo apt-get install ntfs-3g
    5. NTFS auto-mount:
      1. http://viewsby.wordpress.com/2011/10/05/mount-ntfs-partition-in-linuxubuntu-with-readwrite-permissions/
    6. Before mounting:
      1. Check your kernel release version:
        1. uname -r
      2. Verify if FS modules installed:
        1. ls /lib/modules/3.8.0-26-generic/kernel/fs
      3. Identify your FS partition:
        1. sudo fdisk -l
  9. Emacs Prelude (and classic emacs):
    1. Disable flyspell:
      1. Add to init.el (or .emacs):
        1. (setq prelude-flyspell nil)
    2. Disable tabs indentation:
      1. (setq-default indent-tabs-mode nil)
      2. http://emacsblog.org/2007/09/30/quick-tip-spaces-instead-of-tabs/
    3. Disable whitespace highlighting:
      1. https://github.com/bbatsov/prelude/issues/52
        1. Redefine prelude-prog-mode-hook:
(defun prelude-prog-mode-hook ()
"Default coding hook, useful with any programming language."
(flyspell-prog-mode)
(prelude-local-comment-auto-fill)
(prelude-turn-off-whitespace)
(prelude-turn-on-abbrev)
(prelude-add-watchwords)
(linum-mode +1)
;; keep the whitespace decent all the time
(add-hook 'before-save-hook 'whitespace-cleanup nil t))
      1. (setq prelude-whitespace nil)
  1. Highlight more than 80 columns:
    1. Edit core/prelude-editor.el:
      1. (setq whitespace-line-column 80)
...
  1. deb packages:
    1. Install:
      1. sudo dpkg -i package-name.deb
      2. http://www.cyberciti.biz/faq/ubuntu-linux-how-do-i-install-deb-packages/
    2. Uninstall:
      1. sudo apt-get remove package-name
      2. http://www.cyberciti.biz/faq/ubuntu-uninstall-deb-howto/
  2. Track directory changes:
    1. sudo apt-get install inotify-tools
    2. inotifywait -rme modify,attrib,move,close_write,create,delete,delete_self /path/to/dir
    3. http://www.infoq.com/articles/inotify-linux-file-system-event-monitoring
  3. Monitor system activity:
    1. sudo apt-get install auditd
    2. Start, stop, restart:
      1. service auditd start
      2. service auditd stop
      3. service auditd restart
    3. Audit directory:
      1. auditctl -w /var/lib/php5/ -p war -k mycurrentsessionwatch
    4. http://www.cyberciti.biz/tips/linux-audit-files-to-see-who-made-changes-to-a-file.html
    5. http://manpages.ubuntu.com/manpages/lucid/man8/auditd.8.html
  4. Find Linux distribution name and version:
    1. cat /etc/*-release
    2. lsb_release -a
    3. uname -a
    4. uname -mrs
    5. cat /proc/version
    6. http://www.cyberciti.biz/faq/find-linux-distribution-name-version-number/
  5. Smarty - get template vars with PHP:
    1. In php file:
      1. http://www.smarty.net/docsv2/en/api.get.template.vars.tpl
    2. In tpl file:
      1. {php}$all_tpl_vars = $this -> get_template_vars();{/php}
  6. Bash history:
    1. http://lifehacker.com/278888/ctrl%252Br-to-search-and-other-terminal-history-tricks
    2. http://lifehacker.com/202712/review-your-most-oft+used-unix-commands
    3. history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort -r - sort based on usage frequency
  7. Firefox profiles:
    1. Start custom profile (use -no-remote if there is FF instance already):
      1. firefox -P -no-remote profileName
    2. Start profile manager:
      1. firefox -ProfileManager -no-remote
  8. Set NTP date ISOC:
    1. https://help.ubuntu.com/10.04/serverguide/NTP.html
    2. https://www.isoc.org.il/iix/2x_ntp.html
    3. crontab -e
      1. 1 1 * * * ntpdate timeserver.iix.net.il
  9. Screencasts:
    1. recordmydesktop --width 1920 --height 1200 --full-shots --fps 15 --channels 1 --device hw:1,0 --delay 10
      1. http://askubuntu.com/questions/4428/how-to-create-a-screencast
    2. recordmydesktop -x 0 -y 0 --width 1920 --height 1080
  10. Handy tree:
    1. tree | grep -E '\.php$|\.js$|\.css$|^[^\.]+$' > tree.txt
  11. Zip recursively a directory:
    1. http://www.seanbehan.com/recursively-zip-a-directory-and-files-on-linux
    2. zip -r name_of_your_directory.zip name_of_your_directory
  12. Set default browser
    1. http://www.howtogeek.com/howto/ubuntu/set-the-default-browser-on-ubuntu-from-the-command-line/
      1. sudo update-alternatives --config x-www-browser
  13. Steam reset
    1. https://support.steampowered.com/kb_article.php?ref=3134-TIAL-4638
      1. steam --reset
      2. sudo shutdown -r now
  14. List installed packages
    1. http://askubuntu.com/questions/17823/how-to-list-all-installed-packages
    2. dpkg --get-selections | grep -v deinstalld
  15. List open ports
    1. nmap 127.0.0.1 -P0



Backlinks: