The terminal is pretty daunting. All that basically-formatted text, those sometimes extensive commands, and so much typing make for a barrier to entry that many artists find hard to get over. But a basic understanding of how to get around and find and move files is invaluable. And since Unix/Linux has a long history in the industry, you’ll probably have to become familiar with it eventually.
I have one example from my experience which will hopefully inspire you to power through this basic guide and expand your repertoire:
My file browser showed two folders, “wall” and “Wall.” Fine. But when I tried to click on “Wall,” it would disappear and only “wall” would remain. I was thoroughly confused. Thankfully, the terminal showed both folders and let me look at their contents. Since there was only ever supposed to be “wall,” anyway, I moved all the files over from “Wall” and deleted it.
Apparently, there exist file browsers (I won’t say which for now; that’s for another discussion) which ignore the case of a file or folder’s name and act strangely when they encounter files with differing case. There are ways to get around this issue without the terminal, but it was so much quicker to verify the problem and fix it with the power of the shell.
I’ll be talking about the bash shell, but these simple commands should apply to most standard *nix shells. Windows’ shell is a different beast, and we’re not going to cover that here, but some of the basic commands here exist in PowerShell.
Getting to the shell
This depends on your system, of course. I get to mine by going to the Application menu and searching for “Terminal.” It is sometimes filed under something like “Administration.”
Terminal window
What to expect
Your shell probably looks different from mine, but here are some things to look out for as we go through this.
Is it “Terminal” or “Shell?”
People often use “terminal” and “shell” interchangeably, and I guess I do too. Technically, the terminal is a program which lets us interact with the shell, which does the work.
Prompt ($)
In my shell, it’s a $
after which I can type in commands. When I press
Enter
, the command is run. For example:
$ hi
hi: command not found
Here the command “hi” isn’t recognized by the shell, so it tells us. The shell is all business. (Ususally.)
For the purposes of this post, you can type things in your shell that follow the $
,
and other lines are example output of those commands.
NOTE: Often there is more before the $
, like your username, the machine
name, or what folder you’re in, but for simplicity we’ll just show the $
in
examples here.
Cases & spaces
In the shell, Wall
is not the same as wall
, and it won’t assume to know what
you meant, it’ll spit out an error or do something you weren’t expecting (which
is sometimes scary).
Also, spaces separate options in a command, so if you have a folder with spaces, you can surround it with quotes.
$ ls Spaces folder
ls: cannot access 'Spaces': No such file or directory
ls: cannot access 'folder': No such file or directory
$ ls "Spaces folder"
its_just_easier_to_never_put_spaces_into_folder_names.txt file_names_either.txt
but_sometimes_you_have_to_deal_with_others_mistakes.txt
Tilde (~)
In a path in the shell, the tilde character (~) is an alias for your home
folder. So, for me, ~
is equivalent to /home/ebenard
.
Where you are
pwd — print working directory
Short for “print working directory,” this is like looking at the top of the page in a book to remind yourself where you are.
$ pwd
/home/ebenard
My shell starts me out in my home folder, so this is what I see when I use the
pwd
command. While you’re moving around the filesystem, it can be helpful to
remind yourself where you are.
What’s here
ls — list files
To list the files and folders in our current location, we use this command.
$ ls
Desktop Documents Downloads Music Pictures Public stuff.py Videos
This shows a space-separated list of the contents of the folder we’re currently in. There aren’t many details to this default view, so now might be the time to talk about command options.
Command options
When reading documentation about shell commands, you’ll often see lots of dashes and letters. These are options, which are essential to understand if you’re going to be using the shell.
$ ls -l --sort size Desktop/
Here’s a breakdown of this command:
-l
is an example of a flag-type option. It needs no more information to activate the option. In this case,-l
tells thels
command to output in a long format. NOTE: Both flag options and keyword options can have both short and long names. For example,-a
and--all
represent the same flag-type option (which lists both hidden and normally-visible files),-a
is just quicker to type.--sort
is an example of a keyword option which takes a value. The next thing separated by a space will be that value. In this case,size
is the value for the--sort
option, sols
will sort the output by size, instead of by the default,name
.Desktop/
is an example of a positional option. Its position as the first option without a keyword option before it makes it the “first positional option” of the command, which in this case, is the folder of which we’re trying to list the contents. Although other options can be in almost any order, the positional options must be in the order the command expects.
I don’t have anything on my Desktop, but here’s the output from my home directory:
$ ls -l --sort size ~/
total 8
drwxr-xr-x 6 ebenard ebenard 12288 May 10 12:01 Downloads
drwxr-xr-x 2 ebenard ebenard 4096 Apr 17 03:16 Desktop
drwxr-xr-x 2 ebenard ebenard 4096 Apr 17 03:16 Documents
drwxr-xr-x 2 ebenard ebenard 4096 Apr 17 03:16 Music
drwxr-xr-x 2 ebenard ebenard 4096 May 10 13:39 Pictures
drwxr-xr-x 2 ebenard ebenard 4096 Apr 17 03:16 Public
drwxr-xr-x 2 ebenard ebenard 4096 Apr 17 03:16 Videos
You can see here that it formed the output into a columned list, and
Downloads
, which is the largest (at 12288 bytes), is sorted to the top. The
columns preceding ebenard
are showing read/write/execute
permissions, which are important, but outside the scope for us here today.
less — view contents of a file
This command can be used to see the contents of a plain-text file. stuff.py
is
in this directory, so we’ll look at it by using this command.
$ less stuff.py
#! /usr/bin/env python
print("Hello world!")
stuff.py (END)
It shows the first “page,” and then you can use the spacebar to page through the
contents. It’ll tell you what page you’re on at the bottom (or “END” if you’ve
reached the end). At any time, you can press q
to exit back to the prompt.
Launching programs
To launch external programs from the terminal, usually we just type the program name at the prompt and hit enter.
$ firefox
Which will launch Firefox as if you had clicked the entry in the Application menu. When we go back to our terminal, though, we don’t have a prompt to keep working at. To get the prompt back, we have to close Firefox.
TIP: To prevent a launched program like Firefox from holding our prompt
hostage, when we launch it we can add a space and the ampersand &
after the command.
$ firefox &
If we want to edit stuff.py
in an external program, we’ll have to know the
program’s name. My basic text editor is “xed,” another common one on Linux is
“gedit.” To open stuff.py
in xed, I’d type the following:
$ xed stuff.py &
Which opens a xed window with the stuff.py
file opened and ready for editing.
Since we used the ampersand (&
), we can continue using our terminal window
without closing the text editor.
Getting around
cd — change directory
To move around folders, we use cd
to change directories. If we want to get
to the Downloads folder, we’d type the following:
$ cd Downloads
$ pwd
/home/ebenard/Downloads
Which moves us to the Downloads folder. We can check by using pwd
. We can also
specify a whole path to change to.
$ cd /home/ebenard
You can specify any path to change to if you have permission to access it. Typing all this can be very tedious, though. So now is probably a good time to talk about auto-complete with the tab key.
Auto-complete
If you don’t know what something is called, or even if you do, you can use the tab key to auto-complete a command or path. If there are more than one option, it won’t auto-complete, but if you press tab again, the options will be listed.
$ cd D [tab] [tab]
Desktop/ Documents/ Downloads/
We can use this to find what we want and continue typing until pressing tab auto-completes.
$ cd De [tab]
$ cd Desktop/
Relative paths
If we want to move up a folder without specifying the whole path from the top,
we can use ../
to go up a level.
$ pwd
/home/ebenard/Desktop
$ cd ../
$ pwd
/home/ebenard
The ../
represents the parent folder of the one we’re currently in. If we
wanted to move up several levels, we can use ../
in series. For example:
$ cd ../../
$ pwd
/
NOTE: Where ../
represents the parent folder, ./
represents the folder
we’re currently in. So trying to cd ./
won’t move us anywhere, since we’re
specifying the folder we’re in.
Making your mark
mkdir — make directory
If we want to create a folder to keep our .py
file in, we can use mkdir
to
make a directory called “Code” (after cd
ing back to our home folder).
$ mkdir Code
$ ls
Code Desktop Documents Downloads Music Pictures Public stuff.py Videos
If we want to make another folder inside Code
, maybe “Python,” we can do that,
even without moving into the Code
folder.
$ mkdir Code/Python
$ ls Code/
Python
mv — move
Now if we want to move our stuff.py
file into the Code/Python/
folder, we
would use the mv
command.
$ mv stuff.py Code/Python/
$ cd Code/Python/
$ ls
stuff.py
The mv
command takes two positional options: source and destination.
One way we can use this command is to rename a file. For example, if we decided
that stuff.py
was a woefully non-descriptive name.
$ mv stuff.py hello.py
$ ls
hello.py
cp — copy
To copy files or folders around, we can use the cp
command. It takes a source
and destination, just like the mv
command.
$ cp hello.py new_hello.py
$ ls
hello.py new_hello.py
At some point we may want to copy a whole folder of stuff. To do this we have to
use the “recursive” option (after we cd
out of the Code
folder).
$ cd ../../
$ ls
Code Desktop Documents Downloads Music Pictures Public Videos
$ cp -r Code/ New_Code/
$ ls
Code Desktop Documents Downloads Music New_Code Pictures Public Videos
Cleaning up
rm — remove
To remove individual files, use the rm
command with the path to the file after a space.
$ rm Code/Python/new_hello.py
$ ls Code/Python/
hello.py
We can remove all the files in a folder using a “wildcard,” which will match an arbitrary number of characters. For example:
$ rm New_Code/Python/*.py
$ ls New_Code/Python/
BEWARE: The asterisk is a dangerous thing if used unwisely. If you use rm *
it’ll remove every file in the current folder. Useful for clearing things out,
but bad if you didn’t mean to delete everything. Combined with the recursive
option explained below, this is extra dangerous. Be careful.
If we want to use rm
to delete folders and their entire contents, you can use
the “recursive” option, -r
. Be careful with this as well, since it will
remove everything in and including the folder that you specify.
$ rm -r New_Code
$ ls
Code Desktop Documents Downloads Music Pictures Public Videos
$ rm -r Code
$ ls
Desktop Documents Downloads Music Pictures Public Videos
While you’re getting used to how the terminal and the rm
command work, and
especially when you’re using the recursive option explained above, the -i
(interactive) option for rm
can be useful. It will ask you every step of the
way if it should continue. So, if you have a folder called test
with a file
called thing.txt
in it, you can combine -r
and -i
to get the following
step-by-step process:
$ rm -r -i test
rm: descend into directory 'test'? y
rm: remove regular file 'test/thing.txt'? y
rm: remove directory 'test'? n
$ ls
Desktop Documents Downloads Music Pictures Public test Videos
$ rm -r -i test
rm: remove directory 'test'? y
$ ls
Desktop Documents Downloads Music Pictures Public Videos
This will almost definitely get annoying after you’ve become comfortable with
the shell, but it is very useful to understand how rm
moves through the
folders and files.
Conclusion
I hope this quick intro was useful to get you started in the shell. Even if you don’t use it day-to-day, being familiar with the structure and concepts can open up methods of getting things done that weren’t available before.