Written by Ian Melnick on 09:11 reading time

In Part 3 of this series, we had a look at the find command and the set builtin, two tools that have a lot to offer and come with some lengthy manuals.

Commands dealing with file permissions have short manuals. They shouldn’t!

File modes

Unix file permissions seem to be another thing about the command-line that annoy people. You have these commands, chmod, chown, chgrp, etc, and while the latter two seem reasonable enough, the first can accept numbers or letters as arguments; neither technique is all that intuitive. When Terry asked us during a lightning talk, “do you use numbers or letters?”, most responded, to his astonishment, with “numbers.”

I like both methods for different reasons. But let’s back up first. The most basic file permissions dictate if a file is readable, writeable, or executable (or any combination of them). A directory is a type of a file, but it behaves a little differently than what you might expect depending on its readable, writeable, and executable status...

Read more

Written by Ian Melnick on 08:03 reading time

In Part 2 of this series, we had a look at looping strategies to help us determine which strategy might be best for us under particular circumstances.

One of the other options that we didn’t touch on was using the find command to help us operate on a group of files. We’ll look into find here, and also the set builtin: both tools that have many options, are a source of confusion, and can also be very useful.

Stop fearing find

The find command always seems to bother people. When using find, think, “I’m looking somewhere for something.”

Right now, I have three files in the directory called “stuff” within my home directory: fobar, foobar, and fooxbar. Let’s say I want to get a list of all files matching “foo” in there:

~$ find ~/stuff -name 'foo*' -exec basename '{}' \;
foobar
fooxbar

(I threw that -exec in for you, Terry.)

In the bourne shell, the tilde character can be used as a shorthand for the full path to your home directory. You may also use the $HOME variable, but unless you...

Read more

Written by Ian Melnick on 09:56 reading time

In Part 1 of this series, we explored how control characters can get in your way and what to do about them.

Now let’s have a look at loops and some interesting system limits. Loops are one of those features that give you awesome power.

Getting Loopy

Consider the situation where you have a large number of files in a directory, and you’d like to perform an operation on each one.

You have many options; here are a few:

  1. Use a for loop with a backtick operation
  2. Use a while loop that reads standard input for each file name
  3. Use the find command with -exec

The third variant is discussed later. For now, let’s just review the first two.

The first variant might look like:

for filename in `ls *`
do
  echo "A file called $filename"
done

This executes the list command first, loading its entire output into memory, and then evaluates each item (delimited by $IFS) as an argument to the for loop.

The second variant might resemble:

ls * | while read filename
do
  echo "A file called $filename
Read more

Written by Ian Melnick on 06:02 reading time

As a computer-something student, one of the things I felt that was under-emphasized in my college education was the importance of the command-line in “real life.” Fire up an IDE with auto-completion, code away, and deploy the source to production—erm, professor—for evaluation by paper. Pray for low concentrations of red ink in the result set. That blasphemous blob of junk you delivered ran at least once (your classmate swears she saw it run, too); whether it runs again or not doesn’t matter so much. What matters more, in this setting, is the student’s interpretation of a narrowly-defined problem and the demonstrated application of theory to solve it.

This method of learning might be considered harmful. We jump to the meat of the “problem” before we get our feet wet in the “meta-basics.”

Why is it that, when I paired with a fellow classmate, we would often have ideas for how to approach a problem, but lacked a sense for where to begin?

Why is it that, more often than not, there is...

Read more