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

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

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

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

The cd command has lots of fun little shortcuts. One I use frequently is the -. The ~ always takes you to your home directory, but using cd – will take you to the last directory you were in. For example, if you do the following on a Mac:

cd ~

Then you do .. (which is a shortcut for the directory above the one you’re in):

cd ..

Then pwd will show that you’re in /Users. But, if you cd to – again:

cd -

Now you’re back in your home folder. The – expands to OLDPWD. Quick tip. Nothing more to see here.

July 20th, 2015

Posted In: Mac OS X, Ubuntu, Unix

Tags: , , , , ,

One Comment

The ffmpeg command is pretty rad. I’ve written about it before, but now I’m using it to rip the audio out of video podcasts that don’t need video. Extracting an mp3 from video that doesn’t need any video means I can have way more on my iPhone. So here goes:

ffmpeg -i randopodcast.flv -vcodec mpeg2video randopodcast.m2v -acodec copy randopodcast.mp3

The video is pretty irrelevant for what we’re trying to do, but it’s great to have smaller files and more of ’em!

July 14th, 2015

Posted In: Mac OS X Server, Ubuntu

Tags: , , , ,

One Comment

The mkdir command is used to create directories. Sometimes, in testing, you need to have a lot of directories. So, you can use the following command to create 10,000 5 digit directories, named 00001 to 10000:

mkdir $(printf '%05d\n' {1..10000})

July 13th, 2015

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

Tags: , , , ,

One Comment

The following command will remove all empty lines from a file called badcommand.sh:

sed '/^$/d' badcommand.sh

July 12th, 2015

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

Tags: , , , ,

2 Comments

You see a lot of entries for various things in log files. Here, we’re going to print out the number of entries with backupd in them:

awk '/backupd/{print NR}' /var/log/system.log

July 11th, 2015

Posted In: Mac OS X, Ubuntu, Unix

Tags: , , , , , ,

Next Page »