krypted.com

Tiny Deathstars of Foulness

Earlier, we looked at creating thousands of empty directories. Today, we’re going to get rid of them. But we need to get rid of only empty directories. To do so, we’ll use the find command:

find . -depth -type d -empty -exec rmdir {} \;

Now, we can put both into a script:

mkdir $(printf '%05d\n' {1..10000})
find . -depth -type d -empty -exec rmdir {} \;

July 29th, 2015

Posted In: Mac OS X, Ubuntu, Unix

Tags: , , , ,

Leave a Comment

You can easily accept user provided input in bash by using the read command in bash for Linux and OS X. Here, we’ll echo out a choice to a user in a script, read the output into a variable called yn and then echo out the response:

echo "Please enter y or n: "
read yn
echo "You chose wrong: $yn"

Here, we used echo to simply write out what was chosen in the input. But we could also take this a little further and leverage a case statement to then run an action based on the choice selected:

read -p "Should the file extension change warning be disabled (y/n)? " yn
case ${yn:0:1} in
y|Y )
defaults write com.apple.finder FXEnableExtensionChangeWarning -bool false
echo "The warning has been disabled"
;;
* )
defaults write com.apple.finder FXEnableExtensionChangeWarning -bool true
echo "The warning has been enabled"
;;
esac

The options when scripting are pretty much infinite and chances are, if you’ve written any scripts, you’ll know of a better way to do this than how I’ve always done it. One of the great things about scripting is the fact that there’s always a better way. So feel free to throw any of your examples into the comments!

July 28th, 2015

Posted In: Mac OS X, Mac OS X Server, Mac Security, Ubuntu, Unix

Tags: , , , , , , , ,

Leave a Comment

I mess computers up a lot. And that means I have to reload operating systems a lot. I’ve also been having terrible issues caused by autocorrect. So… Let’s disable it. By sending the NSAutomaticSpellingCorrectionEnabled key as a false boolean into NSGlobalDomain:

defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool false

July 27th, 2015

Posted In: Mac OS X, Mac OS X Server, Mac Security

Tags: , , , , ,

Leave a Comment

Sometimes when I’m writing a script, I need something to phone home to something in the script. For example, this can tell another daemon where to ssh into when I invoke it remotely. So, let’s say I want to grab my WAN address in a script. I can use curl with a number of 3rd party sites (sites that often change. But, one that we can use here is ipecho.net. Here, we’ll look at their plain output page here:

curl ipecho.net/plain

This can then get output into a variable or file for processing in other parts of a script. For example, the output here is basically the same thing but the command is in backticks, as you might put it in when scripting:

echo `curl ipecho.net/plain`

July 26th, 2015

Posted In: Mac OS X, Mac OS X Server, Mac Security, Ubuntu, Unix

Tags: , ,

Leave a Comment

There’s an excellent tool that can be used to grab a heap dump from a Java process. It’s called jmap. To do so, run the jmap command, followed by a format and a file path as the format and file operators. Also, provide the PID, as follows:

jmap -dump:format=b,file=~/memdump.hprof 80446

Once dumped, you can view the dump file in the Memory Analyzer Tool (MAP) and find objects that use use too much memory and/or have memory leaks, as part of your troubleshooting. You can also replace the pid with a name of an executable or a core. Run the map tool along with a -h option for a help summary.

A sister tool is jps, which can be used to just list running processes by pid and then path. To run, assuming the same pid as earlier:

jps 80446

You can also run a java debugger daemon using jsadebugd, which attaches a process as a debug server. Then stack, map and info can attach via RMI. Finally, not everyone has access to every path on a file system. So jinfo can be used to view a configuration for a Java process or core. To run, simply run jinfo followed by a pid, executable or core name, as follows (assuming 80446 is the pid for the java process in question:

jinfo 80446

July 25th, 2015

Posted In: Java, Mac OS X

Tags: , , , , , ,

Leave a Comment

The caffeinate command is pretty cool. It keeps your computer from going to sleep. It can run in a couple of different ways. There’s a timer that prevents sleep for a little while. You can also run another command from within caffeinate that keeps the system awake until the other command is finished. Here, we’ll scp a file called source file to a host called servername and keep the system from going to sleep until the process is finished:

caffeinate -s scp sourcefile me:servername/targetfile

Here, we’ll just use the boring command to tell the computer not to go to sleep for an hour:

caffeinate -t 3600 &

July 24th, 2015

Posted In: Mac OS X, Mac OS X Server, Mac Security

Tags: , , , , ,

Leave a Comment

July 24th, 2015

Posted In: Mac OS X, Mac OS X Server, Mac Security, personal

Leave a Comment

The at command can be used to schedule jobs to be run at certain times. I have a hard time getting up in the morning. Here, we’re going to echo a command that we want to be run at a certain time. In this case, we’re going to open a song to make into our alarm clock:

echo 'open ~/Desktop/bangbang.m4v 2>/dev/null' | at 07:00 tomorrow

The job will then output. You can see jobs waiting to be run, along with when they’ll be run using the at command with the -l option:

at -l

In this case, the job is 2. You can then remove a job using the atrm command:

atrm job 2

July 23rd, 2015

Posted In: Mac OS X, Ubuntu, Unix

Leave a Comment

By default in OS X, when you change an extension for a file, you get a warning. This is somewhat annoying to me, as I do this pretty frequently and have never almost accidentally done so. So to disable, send a FXEnable ExtensionChangeWarning key into com.apple.finder as false:

defaults write com.apple.finder FXEnableExtensionChangeWarning -bool false

To then undo, simply run with a true key:

defaults write com.apple.finder FXEnableExtensionChangeWarning -bool true

July 22nd, 2015

Posted In: Mac OS X, Mac OS X Server, Mac Security

Tags: , , , ,

One Comment

One of the great things about cat is that you can view the contents of a file with line numbers. You do so using the -n option, as follows:

cat -n ~/Desktop/myFile

Sometimes a file is too big to view though, so you can pipe the output to less, to combine some of the best features of each:

cat -n ~/Desktop/myFile | less

Obviously, the same thing would work with more:

cat -n ~/Desktop/myFile | less

You can also do something similar with the grep command and the -n option:

grep -n ^ ~/Desktop/myFile | less

Enjoy.

July 21st, 2015

Posted In: Mac OS X, Ubuntu, Unix

Tags: , , ,

One Comment

Next Page »