Tuesday, February 19, 2013

Sort folders by modification times of contents

This is a handy bash script that will take a folder or list of folders, and show you the date of the most recently modified file or folder that exists within it.

I use it to prune users from my server that are no longer active.

#!/bin/bash
# Take a list of folders, search each of 
# them for the most recently modified file
# output the top level folder name and date
while (( "$#" )); do
  FOLDER=$1
  shift

  NEWEST=`find $FOLDER -printf '%T@ %p\n' | 
   sort -k 1nr | sed 's/^[^ ]* //' | 
   head -n 1 `

  DATE=`stat -c %y "$NEWEST" | cut -f 1 -d " "`
  echo "$DATE:  $FOLDER"
done
# ~/scripts/find-inactive.sh `ls /home` | sort
2008-07-28:  lost+found
2012-01-06:  gshaughnessy
2012-01-19:  mwilliams
2012-01-21:  agreenfield
2012-01-22:  zsilva
2012-01-26:  lwomack
2012-01-26:  rmcknight
2012-01-27:  ckyzer
2012-01-29:  crowe
...

Thursday, February 14, 2013

Analyzing the boot process

Something I want to investigate further in the future, so I'm making a note of it here.
Arch uses systemd to initialize the OS, which has some cool features for analyzing the boot process and showing where the time is taken.

Show the hogs:
$ systemd-analyze blame
  7977ms NetworkManager.service
  2796ms colord.service
  2788ms bluetooth.service
  2728ms systemd-logind.service
  1558ms polkit.service
   516ms systemd-tmpfiles-setup.service
   488ms tmp.mount
   486ms systemd-binfmt.service
   484ms dev-mqueue.mount
  ...
Create a plot of processes:
$ systemd-analyze plot > ~/plot.svg
$ kde-open ~/plot.svg
There's also some way to create a "bootchart" but it may required a custom kernel or something, I don't remember.

Install Microsoft Office 2010 in 64 bit Arch Linux

This might actually help someone other than me... I am running 64 bit Arch Linux and last November or so I installed Office2010 using PlayOnLinux with no problems.
Unfortunately I had to re-install Arch about a week ago (Early February) and afterwards my Office applications would no longer start (my home is on a separate partition so I didn't have to reinstall Office). Nothing I tried could get Office 2010 to work properly. I deleted my .wine and .PlayOnLinux folders and tried to reinstall.
I either couldn't get it to install, or could get it to install, but activation over the Internet would not work. I didn't want to call Microsoft even though that would probably have been the easiest solution. I tried exporting WINEARCH="win32" but the installer then crashed.

What I finally had to do was uninstall wine and all its dependencies and then install bin32-wine-snapshot from the AUR. After that everything was just great.

# packer -S bin32-wine-snapshot     # or yaourt or whatever...
$ export WINEPREFIX="${HOME}/.msoffice2010"
$ export WINEARCH="win32"
$ wine my-installer.exe
Done, and it activated with no issue.

Thursday, February 7, 2013

Fixing slow SSH logins

I've noticed recently that logging in to my server over SSH has a delay of 5 to 10 seconds, which gets irritating. The problem has to do with SSHD trying to resolve IP addresses of incoming connections. Add "UseDNS no" to sshd's config file and it all goes away.

# echo "UseDNS no" >> /etc/ssh/sshd_config
Restart your DNS server and it should log in instantly.

Tuesday, February 5, 2013

Copying files or partitions using dd and netcat

Something I need to do every so often, but not enough to remember is copy files across a network when the destination computer has no obvious way to receive the files.

Using netcat on both ends makes it easy.

On recipient computer (whose IP is 192.168.1.5)

netcat -l -p 2000 | dd of=some.file
(-l is for listen, -p is the port number)

On sending computer:

dd if=some.file | netcat 192.168.1.5 2000
I actually used this to copy a partition (hence the dd command). I haven't tried this with a file as shown, but it should work with files just the same.

Tuesday, January 29, 2013

Convert dynamic VirtualBox VDI volume to static

The command below will convert your dynamic storage volume to fixed, which I'm hoping will boost performance.

$ VBoxManage clonehd dynamic.vdi static.vdi --format VDI --variant Fixed
However, in Arch at least this command seems to wreak havoc with normal OS function. Things just kind of quit working, kwin, chromium, they all sort of die. Not just slow due to disk thrashing, but actually die. I'll have to leave it overnight and see if it actually works.

Another issue I found is that I have my virtualbox VM folder soft linked into my home folder, but stored in my Documents. I had to first delete this link otherwise VBoxManage refused to work due to finding a disk with a duplicate UUID.

Tuesday, January 22, 2013

Using tr to delete carriage returns

I wanted to change a files contents from being carriage return delimited to being space delimited, So of course I tried to pipe it through sed, but couldn't get it to do what I needed for whatever reason.

Found the "tr" command for "translate characters".

Can use -d option to delete chracters
tr -d "\n" < file.txt > file-sans-cr.txt
or give two strings to translate between
tr "\n" " " < file.txt > space-delimited-file.txt

Saturday, January 19, 2013

Using fuser to find who's doing what

I just ran across a use of the program fuser which I have never used. It might come in handy for debugging stuff. It is used to identify processes using files or sockets.

It was suggested to use fuser to find what user has access to my sound card.
~ $ fuser -v /dev/snd/*          
                     USER        PID ACCESS COMMAND
/dev/snd/controlC0:  tward       785 F.... cairo-dock
                     tward       791 F.... pulseaudio

Tuesday, January 15, 2013

Arch bluetooth connections fixed?

I have been having issues with bluetooth connections since I switched to Arch. I ran "journalctl" and saw an entry "Unable to connect to SEP" which led me to http://en.gentoo-wiki.com/wiki/Bluetooth_headset#Troubleshooting

After adding
  [General]
  Enable=Socket
to /etc/bluetooth/audio.conf my bluetooth speaker connected right away. Hope it stays fixed.

EDIT: This worked as I said, but a day or two later I was having the issue again and eventually found a message indicating a socket error (I don't remember exactly what it was). I undid this modification and haven't had any trouble since, so... who knows.

Sunday, January 13, 2013

Piping "find" results through xargs

Piping the results of find using xargs doesn't typically work exactly how I want. If a file or folder has a space in its name xargs doesn't handle it correctly.
From the xargs man page:
Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic filenames containing blanks and/or newlines are incorrectly processed by xargs. In these situations it is better to use the -0 option, which prevents such problems. When using this option you will need to ensure that the program which produces the input for xargs also uses a null character as a separator. If that program is GNU find for example, the -print0 option does this for you.
So, long story short: add "-print0" to find, and "-0" to xargs:

find . -name "*txt" -print0 | xargs -0 grep -i poisson

Friday, January 11, 2013

LibreOffice document recovery actually recovers a document

I have been using Linux all day every day for about 8 years now and the application I've used more than any has been OpenOffice/LibreOffice.
Literally hundreds of times I have lost content due to a crash, power failure, command line reboot without saving or whatever. When this happens the next time you start *office it has always asked  me if I want to recover from my previously aborted session.

In all my 8 years and hundreds of times having lost content I have never had it actually recover even so much as a single word of unsaved content.

Well, yesterday I rebooted my machine from command line without saving a document I had been working on. When it came back up, I started LibreOffice Writer, and HOLY CRAP!!! My completely unsaved document was just there!

It didn't ask me to recover or anything, just started in the same state it had been in previously with the unsaved content!!!!!

I'm just amazed at the simple fact that this actually worked. Maybe I can begin to expect the style manager in Impress to actually manage styles now! I'm gonna go check on that right now...


My build

LibreOffice: Version 3.6.4.3 (Build ID: 3.6.4.3 Arch Linux build-3)

Thursday, January 10, 2013

Fix PDF showing inifinity instead of bullets

I've had this problem for a long time, sometimes a PDF was created with a font that doesn't get mapped properly and the bullet points show up as inifinity symbols.

I found this post for Ubuntu: https://bugs.launchpad.net/ubuntu/+source/fontconfig/+bug/551977

$ fc-match Symbol symbol.ttf: "Symbol" "Regular"
A workaround is adding this code to /etc/fonts/local.conf and the bullets would appear:
<match target="pattern">  <test name="family">   <string>Symbol</string>  </test>  <edit name="family" mode="prepend" binding="same">   <string>Standard Symbols L</string>  </edit> </match>
$ fc-match Symbol s050000l.pfb: "Standard Symbols L" "Regular"
This worked perfectly in Arch for me.

Wednesday, January 9, 2013

Regex search/replace to insert newlines

I use Kile and Mendeley. Mendeley generates bibtex references, but with no carriage returns, and the carriage returns are apparenly required.

Example:

@article{Baset_2012, place={New York, New York, USA}, ..., pages={1–2}}

I need each of the x={...}, sections to be on a separate line, and it takes a while to do it by hand.

To search/replace, I enter this search criteria:

\}, +([a-zA-Z])
And this replace criteria:

\}, \ \1

The red block is actually representing a CTRL+V of a newline character. Just select an entire empty line, copy and past into the expression. It will look like a blank space.