• Mac OS X,  Ubuntu,  Unix

    Removing Those Thousands Of Empty Directories In Bash

    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 {} \;

  • Mac OS X,  Mac OS X Server,  Mac Security,  Ubuntu,  Unix

    Using Inputs in Bash Scripts

    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)?…

  • Mac OS X,  Mac OS X Server,  Mac Security,  Ubuntu,  Unix

    Grab Your WAN IP In Scripts

    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…

  • Mac OS X,  Ubuntu,  Unix

    Using My Mac As An Alarm Clock

    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…

  • Mac OS X,  Ubuntu,  Unix

    Get Line Numbers With The Less Command

    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.

  • Mac OS X,  Ubuntu,  Unix

    cd To The Previous Directory

    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.

  • Mac OS X Server,  Ubuntu

    Use ffmpeg to Extract mp3 files from Videos

    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!