Wednesday, December 19, 2012

30 Aliases

Man, I added a bunch of these already: http://www.cyberciti.biz/tips/bash-aliases-mac-centos-linux-unix.html

I also discovered the column command from this, to format things into columns, for instance:

mount | column -t
I also found from the comments on this page that BASH has functions that can be used like aliases:

function f {
 arg_path=$1 && shift
 find $arg_path -wholename "*/path-to-ignore/*" -prune -o $* -print
}
Can be called with
~$ f

Monday, December 17, 2012

Find changes for a subversion commit

In subversion it can be surprisingly difficult to figure out what actually changed for a given revision.

This line will show you the files that were modified for a revision:
svn log -qv -r r101
Compare differences between two versions (show only files changed):
svn diff -r 101:102 --summarize
Where  "r101" is whatever revision you are interested in.

Importing a database (.sql file) into MySQL

No real trick, but I don't do this often enough to remember:

First log in to mysql and create the database named DB-NAME, then run the following from command line:
$ mysql -u root DB-NAME < filename.sql

Thursday, December 13, 2012

Highlighting grep results

Speaking of highlighting, to highlight grep results add "--color".

To preserve the color when piping through less, two things: To grep add "--color=always" and to less add "-R"

I added aliases to .bashrc
alias less='less -R'
alias grep='grep --color=always'

$ grep if *java|less


Colorizing man pages


This is awesome, just stumbled across it at http://www.tuxarena.com/2012/04/tutorial-colored-man-pages-how-it-works/.

Add these lines to .bashrc and man pages take on syntax style highlighting:


export LESS_TERMCAP_mb=$(printf '\e[01;31m') # enter blinking mode - red
export LESS_TERMCAP_md=$(printf '\e[01;35m') # enter double-bright mode - bold, magenta
export LESS_TERMCAP_me=$(printf '\e[0m') # turn off all appearance modes (mb, md, so, us)
export LESS_TERMCAP_se=$(printf '\e[0m') # leave standout mode
export LESS_TERMCAP_so=$(printf '\e[01;33m') # enter standout mode - yellow
export LESS_TERMCAP_ue=$(printf '\e[0m') # leave underline mode
export LESS_TERMCAP_us=$(printf '\e[04;36m') # enter underline mode - cyan
The linked article offers a second color scheme as well.


Monday, December 10, 2012

Revert bad SVN commit

Found this, which works. seems like a strange way to revert to a previous version, but I guess that's how SVN works.
svn merge -r [current_version]:[previous_version] [repository_url]
svn commit -m "Reverting back to revision [previous_version]."
Thought it looks a bit scary, I tried it and it worked.

Thursday, December 6, 2012

Find and delete empty files/folders

Find can find empty files and/or folders and even remove them (scary).

# Find all empty files and/or folders recursively
find -depth  -empty 
# Find all empty folders, only recurse two levels  
find -maxdepth 2 -depth  -empty -type d 
# Delete what you find 
find -maxdepth 2 -depth  -empty -type d  -delete

Friday, November 30, 2012

Arbitrary screen capture in KDE


A nice post at floatingsun, explains how to get screen capture functionality in KDE similar to what you get with the Compiz screenshot plugin. Basically you set up shortcut and have it run the previously unknown to me application
kbackgroundsnapshot -region 

If you copy and paste from the linked website you will need to replace the hyphen with a "minus sign". Anyway, this command lets you select any screen region and puts a .png on your desktop.

In arch I had to install the kdegraphics-ksnapshot package.

It also locked up on me when I tried to press escape to exit without selecting anything, I had to CTRL+ALT+F1 and kill it from command line.

But that's one of the key things I've been missing from Compiz.

Monday, November 26, 2012

Sorting "find" results by date

I have been meaning to look this up for a while now. Sometimes I save a file, but I'm not sure in which folder. I use "ls -ltrh" all the time to sort contents of a folder by reverse time so the most recently modified items appear at the bottom (very useful example is ls -ltrh /var/log to find most recently modified log files).

The following command will use the results of  the find command to do the same thing over multiple folders. Its too complex to type all the time, so I put it in a script in my personal script folder. Another good idea would be to alias it.


    find . -type f -printf '%T@ %p\n' | sort -k 1nr | sed 's/^[^ ]* //' | head -n 10

This example shows only the 10 most recent files (head -n 10) and doesn't filter filenames in any way. It also only examines normal files, not folders or other.

MPRIS players in KDE


I've switched to Arch Linux and a KDE desktop. I love cairo-dock/compiz but it seems like its hard to get compiz to work consistently any more due to gnome etc... loading their own compositing tools that keep trying to take control.

So far I like Arch a lot, and I'm surprised to find that applications tend to run significantly quicker and use less RAM. Notably Windows in VirtualBox and Office 2010 under WINE which were both pretty slug like in Ubuntu.

Anyway, one of the issues is getting the play/pause button on my keyboard to work with Pithos, its assigned only for Amarok by default under KDE. This post gives a method of creating scripts for play/pause, stop, next, previous and reassigning multimedia keys to run them.

Sunday, November 25, 2012

Kile's 1024 character line limit

For some reason Kile (and Kate?)has a configurable line length limit.
When editing .tex documents its easy to exceed the default of 1024 characters and if you open a file with lines longer than the default it gives an error and sets the document to read only.

Go into settings->Configure Kile and select Editor->Open/Save set the line length limit to 0 and there will be no limit.

Wednesday, November 14, 2012

Maximize Minimize button discovery

I just found something by accident after all these years (well it may be a new feature...).

Right clicking on the maximize button maximizes a window only in the width direction.
Middle click maximizes in only the height direction.
Clicking either a second time restores back to original.

Tuesday, November 13, 2012

Using the script command

For years I've wondered why the script command gives no output in Ubuntu. I still don't know the answer, but I just found that by using the "-q" option it works!

-q is supposed to be for "quiet" mode, which I guess turns off the little messages like "script started". But it causes script to actually produce a script.
$ script -q <filename>

Thursday, November 8, 2012

Reset USB ports in Ubuntu 12.10

From http://davidjb.com/blog/2012/06/restartreset-usb-in-ubuntu-12-04-without-rebooting

If your USB port gets mangled and a device isn't working as it should:

  tward $ lspci | grep USB 
  00:1a.0 USB controller: ...
  00:1d.0 USB controller: ...

Then do the following:

  tward $ echo -n "00:1a.0" | \
          sudo tee /sys/bus/pci/drivers/ehci_hcd/unbind
  tward $ echo -n "00:1a.0" | \
          sudo tee /sys/bus/pci/drivers/ehci_hcd/bind

For each of the above controllers. 

Be aware that after you write to unbind, anything connected to that particular USB controller will be unavailable until you write to bind on the next command. So if your keyboard is connected to one of those, you may be in trouble.

Note: See the comment below for an updated procedure.

Thursday, November 1, 2012

Disable overlay scrollbars in Ubuntu 12.10 (Quantal)

Since I upgraded to 12.10, (Quantal) I have have all sorts of issues because apparently gnome is moving away from compiz and using something of their own, but I'm using cairo-session which uses gnome-session, ... Whatever anyway ubuntu-tweak isn't working completely for me and the stupid overlay tool bars kept reappearing. The command below disables them:
gsettings set com.canonical.desktop.interface scrollbar-mode normal
Another thing that's not persisting is window focus mode:
dconf write /org/gnome/desktop/wm/preferences/focus-mode \'sloppy\'
dconf write /org/gnome/desktop/wm/preferences/auto-raise false

Thursday, October 25, 2012

Branching with GIT

Yann Esposito compares BZR and GIT Distributed Version Control Systems (Source control).

I like this example of branching and merging for GIT though.

Cheap branching

You always work into the same main directory. For example, you can work on two fix in the same time. Say fix1 require you to work on file1 and fix2to work on file2. You can work in any order on file1 and file2 in themaster branch. And then go to branch fix1, commit file1 into it. Then go to branch fix2 and commit file2 into it. And finally merge the two branchesfix1 and fix2 into master.
> vim file1
> vim file2
> git br fix1
> git add file1 
> git commit -m 'fix1'
> git br fix2
> git add file2
> git commit -m 'fix2'
> git commit master
> git merge fix1
> git merge fix2
And this is great not to worry about working in the good branch and coding in the same time. You just worry about your code and then about the versionning system.
And I use this possibilities a lot. Working with bazaar, I often made the error to begin a change in the bad branch. then I have to copy my modifications, then revert. In short it was tiedous.
This is why I prefer using git on an every day usage. If Bazaar implement the same way of cheap branching than git. I should switch again.

File associations

There is a lot of information at libre-software.net

Associate all office documents to LibreOffice instead of OpenOffice.org

Display all the MIME types associated to OpenOffice.org documents with the following command:

cat /usr/share/applications/defaults.list | grep openoffice.org
Append all the concerned lines to the local file:
cat /usr/share/applications/defaults.list | grep openoffice.org >> ~/.local/share/applications/mimeapps.list
Open the defaults.list description file with gedit:
gedit ~/.local/share/applications/mimeapps.list
And replace all occurences of "openoffice.org" with "libreoffice" 
 
Finally, save the file. No need to restart, you're all set!

Friday, October 19, 2012

Disable overlay scrollbars in Ubuntu

Some nice tricks from noobslab for Ubuntu 12.10

Disable overlay scroll bars in Ubuntu

gsettings set com.canonical.desktop.interface scrollbar-mode normal

Disable crash reports

I have had a lot of issues with crash reports that just keep on occurring, even when I say "don't tell me again" and when nothing has apparently happened.
sudo gedit /etc/default/apport  
change "enabled=1" to "enabled=0"
sudo service apport stop

Add "open as administrator" as a right click option in nautilus

wget http://dl.dropbox.com/u/53319850/NoobsLab.com/libnautilus-gksu.so 
sudo cp libnautilus-gksu.so /usr/lib/nautilus/extensions-3.0/ 
sudo rm libnautilus-gksu.so

Thursday, September 27, 2012

Choosing random lines from a file

I have needed to do this more than once. The GNU shuf command will generate a random permutation of its input.

The following command will choose every 1/100ths of the lines from the given file. In other words if the file has 1000 lines, this command will randomly choose 10 of them.
file="some-file.txt"
shuf -n $(( $(wc -l $file | cut -d' ' -f1) / 100)) $file

Friday, July 20, 2012

Fixing skype

I use Ubuntu but I use cairo-dock and gnome-session.
 One issue has been that skype doesn't leave any way to bring itself back up if you click the "x" after launching it. It should leave an icon in the system tray but doesn't.

So I found a script that when relaunching skype instead of starting a second instance simply raises the existing instance. Simply replace the regular skype launcher with this.

#!/usr/bin/env python
import dbus
import sys
import os

try:
    # Try and set skype window to normal
    remote_bus = dbus.SessionBus()
    out_connection = remote_bus.get_object('com.Skype.API', '/com/Skype')
    out_connection.Invoke('NAME single-instance')
    out_connection.Invoke('PROTOCOL 5')
    #out_connection.Invoke('SET WINDOWSTATE MAXIMIZED')
    out_connection.Invoke('SET WINDOWSTATE NORMAL')
    out_connection.Invoke('FOCUS')
except:
    os.system("skype")
    sys.exit()

Wednesday, July 11, 2012

Find recently modified files

Something new I just learned. When using find, -mmin -N will find only files modified in the last N minutes.

~$ find . -type f -mmin -30
Will find only files (-type f) modified in the last 30 minutes.

Friday, June 29, 2012

Get rid of annoying Apache warning

For years I put up with Apaches annoying warning on startup about not being able to determine a fully qualified domain name:

Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
I finally looked up how to get rid of it, and despite a few pieces of incorrect advice in forums the solution is to add the following:
ServerName localhost
to /etc/apache2/apache2.conf, or replace localhost with whatever makes sense.

Wednesday, June 27, 2012

Disable upstart/non-upstart services on startup

To disable non-upstart services:
$ sudo update-rc.d apache2 disable

To disable upstart services, create file named <service>.override in /etc/init/ folder that contains the word "manual"


$ sudo sh -c 'echo manual >> /etc/init/mysql.override'

Friday, June 15, 2012

Resolving SVN conflict after directory rename



Renaming a folder with "svn ren oldname newname" causes a conflict error to occur. I was able to resolve the conflict by issuing the following commands found at http://stackoverflow.com/questions/3941291/a-sane-way-to-rename-a-directory-in-subversion-working-copy

svn resolve --accept working -R .
svn commit

Wednesday, May 9, 2012

Protecting local web folders

This posting is something I had put on my server at some point: 

I maintain a multi-user Linux server and the users can put content up on this server under their own public_html folders.

 I set up a script so users could password protect their folders from the web, which worked but other local users could still see each others work when they logged in locally via ssh.

 I found that by setting the permissions for public_html to 750 I could protect their work from other local users. To allow Apache to still see into their folders I added the apache group (www-data on my system) as the group owner of the public_html folder.

Fortunately, you can use wildcards for directory names in Linux. So in the end it was a simple as doing the following (as root user):

~$ chmod 750 /home/*/public_html 
~$ chown :www-data /home/*/public_html

Tuesday, April 10, 2012

Fixing screen resolution issues in Ubuntu

About a year ago I got a new Dell laptop with integrated Intel video card. Beginning with Ubuntu 11.10 there was an issue. It would boot up and set the screen resolution to the maximum (1920x1080) but it would give me no options to reduce it. I use my laptop with projectors all the time, but I was unable to mirror my output because all I could get was 1920x1080. So I've been using Mint, thinking the next version would fix it. No dice though. So I dug in and figured out my issue. First of all, Ubuntu no longer creates an xorg.conf file in the /etc/X11 folder. However you can put a config file in there and Ubuntu will use it. So in order to get Ubuntu to generate an xorg.conf file, you run:
tward $ X :1 -configure
:1 specifies the display to use, :0 is default but you are (probably) already using that one. This should put xorg.conf.new in your home folder. Now you need to add modeline options to it, these tell it what to do with the monitor at various resolutions. To create these modelines use cvt (the etc.. part is my own).
tward $ cvt 800 600
# 800x600 59.86 Hz (CVT 0.48M3) hsync: 37.35 kHz; pclk: 38.25 MHz
Modeline "800x600_60.00" 38.25 <...etc...> 624 -hsync +vsync
Do this for each resolution you want to have and copy the lines into xorg.conf in the Monitor section for Monitor0.
Section "Monitor"
  Identifier   "Monitor0"
  VendorName   "Monitor Vendor"
  ModelName    "Monitor Model"

  Modeline "1280x1024_60.00" 109.00 <...etc...> 1063 -hsync +vsync
  Modeline "1024x768_60.00" 63.50 <...etc...> 798 -hsync +vsync
EndSection
Finally, you need to (maybe not??) add the resolution strings to the Screen section, Display SubSections where applicable:
Section "Screen"
        Identifier "Screen0"
        Device     "Card0"
        Monitor    "Monitor0"
....
        SubSection "Display"
                Viewport   0 0
                Depth     1
                Modes "1920x1080" "1280x1024" "1152x865" "1024x768"
        EndSubSection
 EndSection
Reboot, or otherwise restart X and everything is now working.

Friday, April 6, 2012

19 ffmpeg commands for various things

This site has a bunch of simple, cool things ffmpeg can do, though as with everything I've ever tried in ffmpeg, they don't all work as given...

  • Turn a video in to a series of image files or vice-versa
  • Encode for various devices: PSP/iPod etc..
  • Rip audio
  • And of course, all kinds of conversions


http://www.catswhocode.com/blog/19-ffmpeg-commands-for-all-needs

Wednesday, April 4, 2012

Miles Davis, Kind of Blue

I don't normally post non-Linux related stuff on here, but I had to post this...
I tried to create a "radio station" on Pandora by typing in "Kind of Blue" and got two results for composers I have never heard of, but NO Miles Davis!

Are you kidding me? Arguably the most famous Jazz album ever created. Possibly the best selling Jazz album of all time. Considered by many to be the best Jazz album of all time. Considered to be one of Jazz' masters, Miles Davis's masterpiece. And Pandora doesn't know it?

Wow, maybe their servers are being worked on...

Wednesday, March 14, 2012

Force removal of broken packages

If you try to install a package and are missing dependencies, apt-get will not let you remove it because it is an an inconsistent state.

tward $ sudo apt-get remove tinyos-2.1.1
Reading package lists... Done
Building dependency tree       
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 avr-libc-tinyos : Depends: avr-gcc-tinyos but it is not going to be installed
 avr-tinyos : Depends: avr-binutils-tinyos but it is not going to be installed
              Depends: avr-gcc-tinyos but it is not going to be installed
              Depends: avrdude-tinyos but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

Apt-get -f install does not fix the problem.
A solution is to remove the package with dpkg, then do the repair.

tward $ sudo dpkg --force-depends --purge tinyos-2.1.1 

tward $ sudo apt-get -f install

Tuesday, February 28, 2012

Find what apt-get installs

Some packages install programs whose names are not obvious, such as air-crack and some put files in non-obvious locations such as apache2.
After installing, you can determine what files are installed and where with the following command:
dpkg -L <packagename>


tward $ sudo dpkg -L afflib-tools | head -5
/.
/usr
/usr/bin
/usr/bin/afcat
/usr/bin/afcompare

Friday, January 27, 2012

Using -exec instead of xargs

Using xargs to pipe results of find into grep has problems if find returns files/folders with spaces in the name.

find ./ -name "*html" | xargs grep "something"


Ends up giving me lots of "file doesn't exist" errors.

Using exec circumvents this problem, although using exec is apparently a less efficient way of doing things.

find ./ -name "*html" -exec grep -H "something" "{}" \;

-H tells grep to always show the filename, otherwise you only get the match.
"{}" represents the filename returned by find.
\; I'm not sure what this is, but it has to be there...

Update:
I describe a slightly simpler solution in this posting.

Friday, January 20, 2012

Open MS Office documents with MS Office through nautilus

See reply below for updated solution.

The general form is: wine "C:\path\to\irfanview" '""Z:%f""'

So for MS word for instance:
  Right click on .docx file and select open with-> custom
  Enter the following: 


wine "c:\Program Files\Microsoft Office\Office12\WINWORD.EXE" '""Z:%f""'


The Z and %f have to do with the fact that wine apps locate Linux files through the virtual z: drive, and %f inserts a filename.