Quick, create six folders to hold songs for these six artists, using your “Finder” or “Explorer”.
How long did that take?
Here’s how you do it on the Command Line:
mkdir 'Charli XCX' Halsey 'The Weeknd' Khalid 'Lady Gaga' Beyoncé
Note that sometimes quotes are needed. See why?
People that often use the command line are sometimes called power users. Why is the command line powerful?
You can do almost anything from the command line. You don’t need to launch many separate window-based applications. With the command line, you can:
These notes do not cover native Windows commandsWe are only covering the command language of Bash, which is native to the Mac and the most popular shell on Unix systems. If you are using Windows, you can install Git Bash or other Bash-enabled terminal, no problem! The basic ideas of command line usage, though not the actual commands and shortcuts themselves, will transfer over just fine to native Windows command shells such as cmd and PowerShell.
Your command line application carries out a dialog between you and the user. At a prompt, you type in a command and hit Enter (usually). The command executes, perhaps with a response from the system, and then you get a new prompt. Here is a sample session. In this example, the prompt is $
, but on your system it might be different:
$ date Mon Jan 20 17:40:16 PST 2020 $ echo Hello how are you Hello how are you $ grep qi /usr/share/dict/words Iraqi Iraqian qintar Saqib $ host www.google.com www.google.com has address 172.217.11.68 www.google.com has IPv6 address 2607:f8b0:4007:802::2004 $
Nice! We’ve just seen four commands:
date
, which prints what the computer thinks to be the current datetime.echo
, which echos back what you type (though not always exactly).grep
, which finds things in files. Here we looked for the character sequence “qi” in a file of words (sorry, this file is not on Windows), and four words were found. (More about files coming up soon.)host
, which looks up IP addresses for domain names (okay sorry for the network lingo, but hey that was faster than firing up graphical tool, right? Oh also, Git Bash doesn’t have the host
command.)The set of available commands differs between systems, but there’s a couple hundred that are common.
ls -1F /bin | grep '\*$' | grep -v '\.dll\*$' | sed 's/\*$\|\.exe\*$//g'
Commands are super flexible. They have arguments and options. Try:
ls ls /bin ls -l /bin ls -lFG /bin ls -lG
The arguments and options for each command are listed and explained in great detail in something called the command’s manpage.
We’re going to learn how to navigate and manage files using some cool commands and a bunch of powerful keyboard shortcuts, through an in-class exercise of making a travel photo library. But first, there are things about the computer’s file system we just need to know.
A few brief notes before we get started:
/
. Here’s an example:
/ ├── Library │ ├── Fonts │ │ ├── Farisi.ttf │ │ ├── GillSans.ttf │ │ └── Sana.ttc │ └── Keychains │ └── System.keychain ├── Users │ ├── jessica │ │ ├── homework │ │ │ ├── hw1.py │ │ │ └── hw2.txt │ │ └── resume.pdf │ └── kimiko │ ├── IMG0001.jpg │ └── IMG0002.jpg └── bin ├── date ├── hostname ├── kill └── sleep
/bin /bin/kill /Users/jessica/resume.pdf /Users/jessica/homework/hw1.py /Library/Fonts/Farisi.ttf /Library/Keychains
resume.pdf homework homework/hw1.py .. (parent folder) ../kimiko ../kimiko/IMG0001.jpg ../.. ../../bin ../../bin/hostname ../../Library ../../Library/Fonts/Sana.ttc . (current folder) ./resume.pdf (pretty cool, right?)
~
. You can use this to make pathnames, too. If your home folder was /Users/kimiko, then you can always refer to your picture 1 as ~/IMG0001.jpg
, regardless of the current folder.Tokyo\ Tower.jpg "Tokyo Tower.jpg"
“Folder” or “Directory”?Doesn’t matter. Same thing. You can say either.
/Library/Fonts
?
We will make a travel photo library, together in class. (For homework, you will clean up and build out a movie library, starting with a very messed up an incomplete set of starter files.)
In this exercise we will learn:
mkdir
, touch
)ls
, find
, tree
)pwd
, cd
)mv
, cp
, rm
, rmdir
)Can’t find the exercise content?We’ll be doing it in class, that’s why! Take good notes.
Not every character is legal in a filename; some have special meaning within Bash. If you really, really must put them in a filename, you probably can, but in order to use the file, you’ll need quotes around the file name, and even then there’s no guarantee things will work (at least not without some serious worry.) Here are some good things to know:
/
, as that separates path components.{
and }
) because these get expanded.*
and ?
and [
and ]
) because these get expanded.&
, |
, <
, >
because these are used to combine commands and do redirections (whatever those are...).What does “get expanded” mean?
Braces and the asterisk are used to generate text! It might appear complicated, but really it’s pretty amazing. Braces make arbitrary text. This is called brace expansionHere are examples:
$ echo {b,r,spl,ch}at bat rat splat chat $ echo {2..16} 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $ echo b{a,e,i,o,u}t bat bet bit bot but
touch card{0..99}.txt
do? Glad you learned the command line now, right?
The asterisk, question mark, and square brackets participate in filename expansion: they generate filenames only (which must exist). Though the actual rules are complicated, *
matches any string, ?
matches a single character, and [ ]
matches a single character from the enclosed pattern, and its pattern language can be quite powerful. We won’t get into the details, but will just look at examples:
$ mkdir scratch $ cd scratch/ $ touch dog.txt rat.jpg bat.txt animals personal.html $ ls animals bat.txt dog.txt personal.html rat.jpg $ ls ?at* bat.txt rat.jpg $ ls *.txt bat.txt dog.txt $ ls *o* dog.txt personal.html $ ls *[sgv] animals rat.jpg
We learned these in our in-class exercise.
Command | Description |
---|---|
pwd | Print Working Directory |
cd dir | Change Directory: Make dir the new current working directory |
cd .. | Change to the parent directory |
cd / | Change to the root directory |
cd ~ | Change to your home directory |
cd | Change to your home directory (the cool way) |
cd - | Change to the previous directory you were in |
mkdir dir(s) | Make a directory or directories |
touch file(s) | Create a file or files. If any of the files |
ls dir | List contents of a directory |
ls | List contents of current directory |
find | Find and lists files |
mv source target | Move source to target. If target is a directory and different than the one the source is in, moves it to that directory. Otherwise renames. |
cp source target | Copies source to target. Only copies files. To copy whole tree, use cp -R . |
rmdir dir(s) | Remove folders. The folders must be empty, or else this won’t work. You can use rm -r to blow away whole trees of your file system if you like. Obviously, you need to be very careful about doing this |
rm file(s) | Remove files |
open file(s) | Start the program associated with the given files. |
You don’t use the command line only for managing your files. There are tons of other things to do, from starting and stopping programs, to networking, to controlling devices. Please browse this awesome online reference of commands. It covers Linux, but there is a Mac Version too. Most of these commands work in Git Bash too. You can also find a list at Wikipedia.
Some of the cooler commands to learn are:
If you are into details, details, details, see The Bash Reference Manual.
To use the command line like a pro, and save tons of time, you’ll be happy knowing at least of few keyboard shortcuts. Some of the more useful ones are:
Keyboard Shortcut | Description |
---|---|
Tab | Filename completion (sometimes called “autocomplete”) |
Ctrl+A Ctrl+E | Move cursor to beginning or end of line |
Option+LeftArrow Option+RightArrow | Move cursor back or forward one word |
Up Arrow Down Arrow | Populate line with previous or next command |
Ctrl+K | Clear the terminal window |
Ctrl+R | Reverse history search |
But there are dozens more! Here is a great list of keyboard shortcuts. You can also, if you have tons of time and really, really want to know the details, read the section on command line editing in the Bash Reference Manual.
These operations on commands should be a part of any command line users’ knowledge. They can be learned over time.
|
command2&&
command2||
command2;
command2<
file>
file>>
file&
Examples (meant to be executed in order):
$ echo 'print("Hello")' > hello.py
$ echo 'print("World")' >> hello.py
$ tr '[a-z]' '[A-Z]' < hello.py
$ javac Greeter.java && java Greeter
tree
program, but if it fails (including by not existing), run find
$ tree || find .
$ history | grep 'git '
$ curl http://www.gutenberg.org/files/158/158-0.txt > emma
$ cat emma | tr '[A-Z]' '[a-z]' | sed -E $'s/[^a-z]+/\\\n/g' | grep [a-z] | sort | uniq -c | sort -rnk 1 | head -n 25
Don’t be afraid to get too “advanced.” Instead embrace what can be done. Check out what you have time for, and ignore, for now, what seems a little arcane. There will be time. When you’re ready, here are some good places to go for details, tips and tricks, and fun times:
Try this command:
$ nc towel.blinkenlights.nl 23
You’ll notice a lot of existing folders and files, even on a brand-new machine. A number of these folders are “standard.” The standard folders are operating system dependent. For fun, here are the ones you’ll see in most Linux-based systems (based on this thing called the FHS):
Folder | Description |
---|---|
/bin | Essential binaries (executables) |
/sbin | System binaries - executed by the root user |
/lib | Libraries used by binaries |
/usr/bin | Binaries for end user apps, not the OS itself (non-essential) installed by a package manager |
/user/local/bin | Binaries the end user made themselves |
/etc | (Editable Text Configuration) config files |
/home | Parent folder for all the users’ home folders |
/boot | Contains the kernel and other files need to boot up the OS |
/dev | Device files (interesting) |
/opt | “Optional” software |
/var | “Variable” files (heavily modified) like log files and caches |
/tmp | Temporary files, not persisted, can go away at any time |
/proc | Basically a view into the OS's control of running processes |
Make sure you can discuss or explain each of the following:
mkdir, cd, pwd
touch
ls
, find
mv
, cp
rm
, rmdir
cat
open
.
..
~
|
(The pipe)&&
We’ve covered: