No dots in cron scripts?

I added a little script to /etc/cron.daily, sayhello.sh, while I sorted out how to integrate my backup script (tresync) into the list of cron jobs run daily.

The odd thing is, the script never ran. I could run it as “sudo /etc/cron.daily/sayhello.sh“, but it didn’t run with the rest of the cron jobs.

Lo and behold, I found the answer, which is actually on the man page for run-parts:

If neither the –lsbsysinit option nor the –regex option is given then the names must consist entirely of ASCII upper- and lower-case letters, ASCII digits, ASCII underscores, and ASCII minus-hyphens.

Which means: you cannot have a dot in the file name.

That seems quite counterintuitive, but perhaps there is a reason that I just don’t understand.

Advertisements

Printing the middle N lines of a file in Linux

While working on my “e” alias in my aforalias project, I want the show only the entries of zip files, that is, without the header and footer sections. The “e” alias also shows the entries sorted by name, unlike the behavior of “unzip -l“, which displays:

Archive:  gradle-1.12-all.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
<entries>
---------                     -------
102761855                     9503 files

Thus I wanted to print lines[3 .. lines.length – 3].

Lo and behold, there’s a way to do that with “head” and “tail”, as follows:

unzip -l $1 | tail -n +4 | head --lines=-2

tail -n +X” means “start printing at line X”, where 1 is the first line of the file (not 0).

And “head --lines=-X” means “print all but the last X lines”.

Not the most intuitive pair of commands in this context, but they can be quite helpful.

A for Alias

In Zsh (and other shells), aliases may be the most important feature for increased productivity.

A simple rule is this: the more often you run a command, the shorter it should be. I’ve written about this before, in Efficient Way to Improve Console Productivity.

I have a number of one-character aliases:

G='| glark'
H='| head'
L='| less'
M='| more'
S='| sort'
T='| tail'
a=alias
c=clear
h='history -5000'
l='ls -CF'
t=cat

I’ll write later about those nifty uppercase ones (and yes, there is the alias “a” for “alias”, but for now, I’ll just show them with examples:

alias G -g '^.='

That was the command that I ran to generate the list above. It expands to “alias | glark -g ‘^.='”.

find src -type d S

That finds all directories and pipes them to sort.

There are three “families” of commands that I run the most often:

  • diff – diff files
  • eye – look at/in files on the command line
  • find – list all files/directories matching a pattern
  • open – open files in editor, browser
  • search – look in files

So I have been writing one-character scripts/functions (in Bash) for each of the above, where the script understands context as well as the targets.

For example, “s pluralize” run from a directory that contains a “Rakefile” will expand to search (using glark) only for Ruby files.

e program.tar.gz” is the equivalent of “tar ztvf program.tar.gz“, and “e program.zip” lists the zip file, with entries sorted by name.

d dir1 dir2” runs a recursive diff on the directories, and “d path/to/file ../dir2” will diff “path/to/file” against “../dir2/path/to/file”, if the latter file exists. I do this to avoid the tedious repetition of comparing one code base to another (such as two branches, when merging them): “diff path/to/file1 ../dir2/path/to/file1“, “diff path/to/file2 ../dir2/path/to/file2“, etc.

Although this project is in its early stage, and the output is still rough (i.e., containing debugging output), it already has been quite useful, so I thought I would share.

The project is at Github here.