Linux for CG
Why learn the terminal
Because the cool kids rock a terminal.
Most of what you might type into a linux terminal you can do with regular file browsers, but often you can jump around a filesystem faster from a terminal. Here's the basics to get you started.
What I'm about to talk about goes by lots of names, a terminal, a shell, a prompt, a command line, a bash etc... for the most part they all refer to the same thing.
Under linux and OSX, run a terminal. Under OSX its a single thing, linux will probably give you 5 different options, just go with the one you see everyone else using (probably a gnome terminal or kde terminal, it doesn't really matter).
For windows you need to install a unix style terminal, I give some pointers for this on the General Utilities page.
Type words, hit enter
With a mouse and windows you double click to make stuff happen. In a terminal you type words and hit enter.
Those words could be commands, like listing the contents of a directory, or it could be a combination of a command and a filename, like deleting a file. You can eventually form complex 'sentences' to do all sorts of things, but for now we'll concentrate on simple stuff.
ls - list the contents of a directory
Some of those entries might be directories though, how can you tell? By adding a switch to the command, most linux commands have a variety of switches to do different things. Running 'ls -F' will make it append a slash onto directories so they look different from regular files:
cd - change directory
cd followed by a directory will move you into that directory, where you could now ls again to see what's in there:
pwd - present working directory
In a GUI file browser the title bar will normally tell you what folder you're currently in. I have my terminal tell me that from the prompt (the little green '~/render' thing next to where I start typing), but you can also get a terminal to tell you where you are by typing pwd:
But wait, why does my prompt say ~/render, and pwd gave me /c/Users/matt/render/cache? The tilde ( ~, little squiggly line normally shift-`, next to 1 on a english keyboard ) is often used as an alias for your home folder, much faster to type than the complete path from the top of the directory structure of your hard drive.
mv - move and rename files
To rename a file is mv oldname newname:
To move it to a different folder, its mv file newdirectory:
cp - copy a file
cp file newfile
And finally clear will clear the terminal, and exit will close the terminal:
Tab completion and wildcards
All pretty straightforward and dull so far, and arguably slower than using windows and a mouse for similar operations.
There's 2 super handy tricks when working with a terminal, the tab key and wildcards, which is represented by an asterisk: *
When working with files and folders, you can type the first few characters of a file, hit the tab key, the shell will try and autocomplete the name for you. If there's more than one match press tab again, it will show you options. This can be a very fast way to zip around folders. For example if I need to get to renders/cache/sim, I'll type ren
<tab> etc to get to where I need to be:
Wildcards can utillise patterns in file names. For example if I want ls to only show me exr files, I can type ls *.exr:
This also works for moving files, so I could move all my files that start with 'foo' into a subfolder 'sim' with mv foo* sim:
Spaces are important
So far you can see terminal interactions are commands and filenames, like simple sentences. Like sentences, spaces are important, they're what separate the elements. So lets bring on the simple mistake folk new to linux do; create a file with a space in it. Easy enough to do from a graphical window, but look what happens when we try and rename it from a terminal:
The mv command got confused. It doesn't understand the space in the filename isn't a separator, so it tries to find a file called 'my', which we're renaming to 'file.txt', and it just says 'nope'.
There's ways around it of course:
- the ls command implies this, you can wrap the filename in quotes
- you can protect, also known as 'escape', the space by prefixing it with a backslash, so the file would be called my\ file.txt.
But you know whats even easier? Don't use spaces. More cg pipelines around the world get bugs and errors because people put spaces in folder names and filenames. Don't do it. Use an underscore if you must, or camelCase, or whatever convention a studio lilkes, but don't use spaces.
Don't put spaces in names. Please.
Slashes and dots and tildes
If you want to refer to the directory above you, use 2 dots, eg cd ..
to go up more folders at once keep using 2 dots, separated by a forward slash, eg cd ../..
Using directory paths in this way ('go 2 folders higher than where I am now') are called relative paths, handy if you have the same basic folder structure for every shot on your show, and you're currently in the scene folder, but want to refer to the render folder you know is 2 folders up and one down, you could type
A forward slash is used as a folder separator. To use a / by itself means 'the top folder of everything'. Whenever you use paths that start with a slash, meaning 'going from the top of everything', this is called an absolute path. It doesn't matter where you currently are, that path is an explicit path to a given location. Eg
Folk who have used Houdini will recognise all these dots and slahes and relative vs absolute patterns. Houdini's base architecture is modelled heavily on a unix file system, hence the similarity.
Rv from the command line
Most linux studios will have rv installed, which is very powerful when run from a terminal. You can get it to immediately load all the exrs in a folder:
or all the pngs in all the folders v001 to v009:
or a million other silly tricks.
ffmpeg from the command line
As I state on the GeneralUtilties page, ffmpeg is a workhorse for converting images and video. It has a ton of switches and options, but at its core you tell it what the input video is, and what the output video is.
ffmpeg -i myoldvideo.mov thenewvideo.mp4
'With great power comes great responsibility' and all that jazz. You can delete your entire home folder in an instant, with no undo. Trash a shot in miliseconds. Garble files beyond recognition. If you're working in a big facility they might have some safety nets like timed snapshots and stuff, but go slow at first, breathe before you run any drastic commands, you've been warned.
rm - remove, or delete files and folders
Delete a single file is easy
if you try and remove a folder, it won't, and warn you that you tried to delete a folder. You can force it to delete by adding the -f switch:
rm -f renders
if there's folders within the folders, it still won't let you delete. You add -r, meaning recursively delete:
rm -rf renders
A common thing when cleaing up caches and whatnot is delete all the files in a subfolder that end in .abc, for example:
rm -rf cache/*.abc
The rm command can deal with multiple files as long as their separated by spaces:
rm -rf file1.hip notes.txt renders/*.abc
so imagine the fun that happens if you accidentally put spaces around a slash:
rm -rf file1.hip notes.txt renders / *.abc
Yes, this will try and delete file1.hip, then notes.txt, then the renders folder, then EVERYTHING FROM THE TOP OF THE FILESYSTEM DOWN, then your .abc caches. Hopefully it won't be able to as it'll run into permission errors, but it can still do a helluva lot of damage. Don't do that.