Maintain, optimize and troubleshoot your NLE
Professional cloud workflow platform
Simplified media management
< Blog Home

Getting to Know the Terminal Part 2: More File Operations

In the previous article, I showed you how to launch the Terminal and perform basic tasks like navigating through folders and dealing with files. Today I will build on this with more advanced file operations, working with directories, and wildcards.

Working with Directories


To create a new directory, navigate to the relevant parent directory using the cd command and type the following:

mkdir MyNewFolder

Remember: as discussed last time, if you want a file name with spaces, you must do either of the following:

mkdir "My New Folder"

or:

mkdir My\ New\ Folder

What if you need to create several new directories? Simple:

mkdir Dir1 Dir2 Dir3

A little-known feature of the mkdir command is that you can create multiple directories with just one command. Just separate them with spaces.

If you want to create a long chain of directories (e.g. Renders/Video/TIFF) you can use the -p parameter like so:

mkdir -p Renders/Video/TIFF

Press Enter and it's all done, instantly. Imagine how long it would have taken to do that within the Finder GUI! This is why the command-line is still used in the 21st Century even though we have pretty GUIs to look at.

To remove a directory, use the rmdir command:

rmdir Renders/Video/TIFF

The above command assumes that all directories are empty. If they are not, an error will be returned. To remove a directory and all its subfiles and folders, use the following command:

rm -dR Renders

(the d parameter tells it to include directories, R tells it to be recursive)

Wildcards


Last time I mentioned file operations such as cp for copying and mv for moving files. But what if you need to perform an operation on a large number of files at once?

Wildcards can be used to substitute characters. So if you want to copy, say, 100 files that are named Image.001.jpg to Image.100.jpg, you would use wildcards to substitute for the numbers. This will allow you to copy all of the files with one command instead of a hundred like so:

cp Image.*.jpg Renders

Question mark (?) substitutes a single character. So for instance, if you had files called render_v1.iff, render_v2.iff and render_v10.iff and you typed ls render_v?.iff, it would return only renders 1 and 2 because they are a single digit whereas render 10 is two digits.

Asterisk (*) substitutes a potentially infinite length of characters (including no characters at all). Use this if you don't know the precise length of the substitution. In the example above, if you type ls render_v*.iff, it will return all three files because it matches both the 1- and 2-digit numbers. You can also type, for example, *. jpg to return all JPEG images or *.* to return all files in the directory.

So, going back to the 100-frame image sequence previously mentioned, you would type ls Image.*.jpg to return a list of all of the images. That's all well and good, but what if you don't want to return all of the images - what if you only want a specific range?

Braces ([ ]) can be used to be more specific. In the above example, type ls Image.[20-30].jpg to restrict the results to frames 20-30. You can also restrict characters in the same way, such as [b-f] (remember that everything is case-sensitive).

If your numbers or letters don't fall into a sequential range, you can pick a set of non-sequential numbers or letters such as [brz] or [179] to match any of these characters. You can also combine them with ranges like so: ls Image.[1-10,12,14,20-40].jpg. This will return frames 1-10, skip frame 11, return 12, skip 13, return 14 and display frames 20-40.

And if you don't want specific characters to be returned, use the following syntax: [^bgv].

Combining the examples above: if you want to match a file starting with the letters a-c or x-z, with three miscellaneous characters in the middle, ending with a three-digit number at the end, you would type ls [a-cx-z]???[0-9][0-9][0-9].*. As you can see, it gets complicated pretty quickly.

But imagine if you had to search through and move or copy those files manually. It would be time-consuming, tedious and prone to human error. The command line comes into its own when you want to perform operations on a large number of files at once.

Editors can get away with not knowing this (although it can be very useful for them to know) but it is required knowledge for anyone who wants to get into visual effects.

I had originally planned to cover permissions here but I'm going to move it to the next article because I went into a lot of detail and it's too large to add on the end of this one. I'd much rather delay it than miss out information.

So next time I will be covering file permissions, symbolic links and opening, viewing and saving text files.
Posted by Jon Chappell on Jan 19 2009 to Visual Effects, Software, Apple