Posted by & filed under Code Tips.

Not really code, but still something I need often enough to write it down.

This command will list all files in a Linux folder sorted by date with the most recently updated files at the bottom.

ls -ltrh

From the ls man page, here’s what each of the choices do.

-l – Uses a Long format which shows more information on each file

-t – Sorts by modification time

-r – Reverse order. Without this the most recently updated files would be at the top of the list instead of the bottom.

-h – Human friendly file sizes. Shows files in KB, MB, or GB instead of just bytes.

Posted by & filed under Code Tips.

This snippet will let you run the same command using each line from a file as a parameter.

cat filename.txt | while read in; do echo "$in"; done

As written it’ll open up filename.txt and echo out every line. Probably not what you want, so replace echo “$in” with whatever command you want to run. The $in variable is the line it’s pulling out of the file.

What I needed this for was to block a bunch of IP addresses that were hitting honeypot files on a server so instead of echo I was running csf to add the IP.

Posted by & filed under Code Tips.

This little batch file came about from needing to run a cron job from within XAMPP. Since it’s Windows I could have used a scheduled task, but I don’t have XAMPP running all the time and I wanted a little more control over when it runs.

And on this specific project, I’m working on a Moodle plugin that needs to run under cron every minute. So the program that’s running is PHP calling the Moodle CLI cron.php file. You’re probably going to want to change the script that’s called.

:loop
php /path/to/your/script.php
@timeout /t 60
@goto loop

You’ll want to change the 2nd line to whatever you want to run. Add as many lines as you want. And then change the 60 on the 3rd line to however many seconds you want it to wait. I needed it to run every minute so I’m using 60 seconds.

Now when I need to have cron active I just drop to the command line and run this batch file and leave it running until I’m done.

And for those that haven’t dealt with the command line in a while, or ever, pressing Ctrl-C will terminate the batch file.

Posted by & filed under Code Tips.

Fighting against an issue with the sendmail queue getting filled with junk I needed a way to delete a folder full of messages, and there were too many for rm to delete.

A post on Stack Overflow had the solution. Only thing I added was the -v option to rm so that it displays the files as they’re being deleted just to double check that something was actually happening.

for f in *.pdf; do rm -v "$f"; done

Posted by & filed under Code Tips.

Found this one on Stack Overflow needing a quick and easy way to create somewhat random strings.

function randomString(len, charSet) {
    charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var randomString = '';
    for (var i = 0; i < len; i++) {
    	var randomPoz = Math.floor(Math.random() * charSet.length);
    	randomString += charSet.substring(randomPoz,randomPoz+1);
    }
    return randomString;
}

If you visit the entire thread where this code was posted there’s a pretty good discussion of how to create random strings using math and date functions.

Posted by & filed under Code Tips.

As a follow up to an earlier post on double braces in Java, came up with a short, but ugly way of filling an ArrayList via an array.

String[] ray = {"Hi", "there", "Bob"};
ArrayList<String> lst = new ArrayList<String>() {{
	for (String s: ray) { add(s); }
}}

Of course, at that point you might as well just do this.

ArrayList<String> lst = Arrays.asList(new String[]{"Hi", "there", "Bob"});

Posted by & filed under Code Tips.

This is one of those that I never remember, but use a lot whenever I want to pull an entire file into a String quickly.

String text = new Scanner(new File("file.txt")).useDelimiter("\\A").next();

\\A is the start of the file, so Scanner is going to try and split on the beginning of the file. Since there’s only one beginning, there’s only one next.

Posted by & filed under Code Tips.

Not sure why, but it looks like sorting on last name and first name is not built into the WordPress get_users method. Seems like it would be a pretty basic need, but it’s not there. So, it’s on to PHP to make it happen.

This bit of code assumes that $user_list has already been filled with users by calling the get_users method.

usort($all_users, function($a, $b) {
	return strnatcmp($a->last_name . ', ' . $a->first_name, $b->last_name . ', ' . $b->first_name);
});

Once it’s done, $user_list will be sorted based on lastname and firstname. It’s actually sorting on “lastname, firstname” which should work out the same.

Posted by & filed under Code Tips.

For a project I’ve been working on I needed to use preg_match quite a bit. But I got really tired of having to punch in several lines, so I cooked up this little snippet to pull out a specific match group.

<?php
/**
 * Shortcut to doing regex without needing to worry about $matches
 * @param type $pattern
 * @param type $search
 * @param type $group
 */
function preg_search($pattern, $search, $group=1) {
    if (preg_match($pattern, $search, $matches)) {
        if (isset($matches[$group])) {
            return $matches[$group];
        }
        return false; 
    }
    return false; 
}

$pattern and $search work just like normal preg_match. The $group parameter tells it which matching group to return, if it exists. If it doesn’t exist, it returns false.

Posted by & filed under Photography.