The prompt is the place in the terminal where you type commands. By default, it usually shows your computer’s name and your username:
Alexs-MacBook-Pro:~ alexrandall$ pwd
When commands are entered successfully, you don’t always receive feedback that something happened. The usual indication that everything is OK is the appearance of the prompt.
The prompt often starts with a $
. You installed the starship prompt as part of the computer setup instructions. That changed the way your prompt looks and the information it displays. With starship installed, your prompt will start with this character: ❯
instead of $
.
The structure of commands follows a consistent pattern.
Options and arguments are not always required, but different commands have different usages.
$ command [-options] [arguments]
Note: The dollar sign symbol is there to indicate the start of the command line prompt. Don't type that in!
Arguments are information included with a command to modify its behavior.
One or more arguments may usually be included, separated by whitespace.
$ cal Nov 2018
$ ls ~/Documents/
⚠️ Remember: don't type in the $
! That's just there to indicate the start of your command line prompt.
# doesn't work because the command requires two arguments
$ cal may
# passing the option -m lets you use a single argument for the month
$ cal -m may
## More arguments and options
$ ls
$ ls ~/Documents/
$ ls -l ~/Documents/
$ ls -lt ~/Documents/
How do you know what a command does, or what arguments and options a command can accept? The man
(short for "manual") command accepts the name of other commands as arguments.
$ man cal
man pages use another utility called less to display information. Check out the man pages on it!
To get back to the prompt from less, press "q".
If the command line hangs or you want to stop a process that is ongoing, use the keystroke Ctrl-C. Sometimes you’ll see it written as ^C.
man
pages use another utility called less
to display information. Check out the man pages on it!
To get back to the prompt from less
, press "q".
If the command line hangs or you want to stop a process that is ongoing, use the keystroke Ctrl-C. Sometimes you’ll see it written as ^C
.
Try this out by using the following command:
$ cat
Hint: It’s waiting for your input…
Up-arrow cycles through commands you’ve typed previously in the same session.
Left-arrow and Right-arrow let you move around the line and edit what you’ve typed.
The command isn’t submitted until you hit Enter/Return.
This command, short for "super user do", appended before you enter any other command, gives you root privileges.
That is, you can execute any command, including modifying sensitive system files.
$ sudo nano /etc/hosts
A directory is what you know as a folder.
You can see your current working directory (the one you are "in") with pwd
.
You can change your working directory with cd
.
$ pwd
$ cd directory_name
Several special characters are used for shortcuts in navigating the file structure. You’ll see them used often in paths.
-
The single dot
.
represents the current directory. -
The double dot
..
represents the parent directory. -
The forward slash
/
by itself represents the root directory. -
The tilde
~
represents the home directory.
Try these out for yourself!
$ cd ..
$ cd ../..
$ cd /
$ cd ~
$ cd
$ ls
$ ls -l
$ ls -a
$ ls -al
$ mkdir new_directory
$ rmdir empty_directory
$ rm -rf directory_full_of_stuff # watch out!
Let’s practice using the command line! Don’t forget about using the arrow keys to find previously typed commands.
-
Navigate to your home directory
-
Create a
momentum/class
directory path -
Navigate to that directory
-
Create a directory named
command-line
here -
Navigate up two directories
-
Explore around your directories, viewing their contents with
ls
-
Return to your home directory
-
Verify what your current working directory is (hint: use
pwd
) -
Remove the
momentum/class/command-line
path
Spaces and capitalization DO matter!
Don’t put spaces in your file or directory names. They have to be escaped when typed on the command line.
$ cd My\ Project\ Folder
$ cd my_project_folder
Paths indicate the "address" of a file or directory.
$ cd projects/ruby/blackjack
$ cd /Users/myusername/code/projects/ruby/go-fish
$ cd ~/Documents/bash_commands.txt
$ cd ../study_notes
Relative paths are relative to the current working directory.
Absolute paths are relative to the root directory and begin with "/".
One or more pathnames, delimited by colons, in which to search for commands to execute.
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
$ touch new-file.txt
$ open index.html
$ cp index.html main.html
$ mv index.html ~/projects/website/index.html
$ rm new-file.txt
$ cp -R projects portfolio
$ mv projects ~/momentum/portfolio
$ mv -i oldname newname
The mv
command is useful for renaming files and directories. The -i
option is a safeguard against an accidental overwrite.
$ cat ~/.bash_profile
$ tail log/development.log
$ head addresses.csv
You can open the default text editor, or any desktop app on a Mac, from the command line.
$ open -t newfile.txt
$ open -a "Visual Studio Code" index.html
Sometimes the screen can feel cluttered. To clear your terminal screen you can:
-
Type
clear
at the prompt -
Cmd-K
on OS X
This doesn’t clear your command history; it just makes your screen look neater!
Let’s practice working with files from the command line.
-
Create a new directory called
cli-practice
and make that your working directory. -
Create two text files and name them
file1.txt
andfile2.txt
. Don’t forget the file extension! -
Open
file1.txt
in an editor and add a sentence or two to it, saving your changes. -
Copy
file1.txt
to a third file. -
Create a directory called
files
and movefile1.txt
into it. -
List the contents of the
files
directory without changing your current working directory. -
Rename
file1.txt
tofile_one.txt
-
Remove the
files
directory without deletingfile_one.txt
first. -
Clear your terminal!