Monday, October 18, 2010

Automove files

There are a lot of utilities for automatically doing stuff with files. But I've never found any of them too useful. So I wrote this script. This python script will watch any number of directories for matching files and move them to corresponding directories.

Download here


It supports full regex matching of files and can do some basic renaming of target files. This is an example config file.

*~/Downloads*
.*\.(?:txt|docx?|pdf|odt|xlsx?)$ ~/Documents
[0-9]+\.(?:jpg|gif|png) ~/Dropbox/Misc/4chon

I'll go through line by line but it's fairly simple.

* * indicate which directory to watch. So *~/Downloads* watches /home/username/Downloads. I chose * * because regexes can't start with a *.

Next comes an ordered list of regexes and directories. When a file is created in ~/Downloads, the script will check that filename against the regexes that follow. Once it finds a match, it stops and uses that regex. Note: it does a regex MATCH. So the entire regex has to match the filename for it to be a match.

If there are no groups in the regex, it just moves the file from the watched directory to the new directory. So if you saved a file called document.doc in ~/Downloads, this script would move it to ~/Documents/document.doc.

If there IS a group in a regex, it will attempt to replace that group in the new file corresponding to (N). For example, if you saved document.txt in ~/Downloads, it would be moved to ~/Documents/falmarri-document.txt. Therefore you must keep track of which group corresponds with what number. This is standard regex behavior though, so I won't explain it here.

Because of these different formats for move, if you're not using grouping to replace filenames in the new file, you CANNOT have any groups in the regex. You MUST use (?:) to prevent the regex engine from naming the group.

So
[0-9]+\.(?:jpg|gif|png) ~/Dropbox/Misc/4chon
is legal, but
[0-9]+\.(jpg|gif|png) ~/Dropbox/Misc/4chon
is not. In fact, the latter will probably do some damage to your files since it will think 4chon is the new name of the file.

Finally, the default directory for your config file is ~/.move.conf. You can't specify a config file in this release, but it should be trivial to change if you don't want to wait for me.

As always, let me know of any errors

Download here

Monday, October 4, 2010

Stock market index futures

Discover more Android applications


It's fairly basic, but you can select which indices to show. It updates every 30 minutes, but it shouldn't use even a noticeable amount of battery life since it doesn't force the phone to wake up.

Sunday, October 3, 2010

Python enhancement to scp

I was on windows a while ago and found out that WinSCP has this cool feature to keep a remote directory in sync. I didn't really find anything similar on linux. There's rsync, but it's not the same thing.

I wrote a little python script that does just that. You can use it to keep multiple local directories or files in sync with several remote directories. The syntax is just like scp

scpy path/to/directory username@remotehost:/path/to/
This will sync the local directory "directory" to the remote host under the path /path/to/directory. Then, subsequent modifications to any files within the local directory will propagate to the remote one. This fully works with nested folders.

What you can also do is sync multiple local directories

scpy path/to/directory path/to/otherdirectory username@remotehost:/path/to

Now this will sync both directory and otherdirectory into the /path/to remote directory. You can add as many local paths as you want, and you can add as many remote hosts as you want.

There are a couple of options with this. If you want to propagate modifications in a directory but you DON'T want to initially sync the hole directory, run scpy with the -s flag.

scpy -s path/to/directory username@remotehost:/path/to

This will watch the directory, and propagate any file modifications to the remote directory. For now, if you modify a file in a watched directory, and that directory doesn't exist on the remote host, it won't work.

There are more options in the --help section, but they're not implemented yet. Also be aware that this is beta software. I've tested it and I use it myself at work. But with remote file transfers, you should always be careful. I would suggest against using it to send to the root user on a remote host until you've tried it under your work conditions.

Since it uses inotify to watch for directory modifications, it needs linux kernel 2.6.13. I wrote this in python 2.6.5, but it will probably work in any version that supports the following modules:

There's no installation to this, just put the script wherever you want and run it. I suggest putting it in /usr/local/bin and renaming it to scpy (remote the .py extension). Also, don't forget to chmod it to executable (chmod +x scpy)

Download here