Super basic bash guide →
This guide meant to help absolute beginners use the command line on Mac and Linux computers. For windows, you'll need to install the git command line tool or cygwin in order to use these commands. For Mac, you can open the command line with the Terminal application, inside Applications > Utilities.
This guide is supposed to be brief enough to be learned in a few minutes. For more in-depth guidance on using the command line, try this tutorial or this tutorial. Please feel free to leave comments suggesting changes, additions, or clarifications.
Note: In the examples below, the #
symbol indicates a comment. Anything following a #
will be ignored by the
command line. I'm just using them to explain what each command does, so focus
on the part before the #
.
pwd
-- Print Working Directory: tells
you the folder that you are currently working in. In other words, it asks
"Where am I right now?". The "working directory" is also called the "current directory".
cd
-- Change Directory: This command allows you to move to different
folders on the computer. This and ls
are probably the most used bash
commands. You will often enter cd somedirectory
, then ls
, then cd someotherdirectory
,
then ls
, ... and so on. In the animation below, when you see a folder name
suddenly completed, that's because I pressed the tab
key to use autocomplete.
cd ~ # change to my user home directory
cd .. # go up one folder
cd myfolder # change to `myfolder` (`myfolder` must be in the current directory)
cd / # go to the root folder for this computer
cd /Users/bgolder/Desktop # move to the desktop
cd Desktop # move to the Desktop (`Desktop` must be in the current directory)
cd ~/Desktop/myfolder # go to my folder, which is in `Desktop`
cd ../myfolder # go up one folder and then go into `myfolder`
cd ../../.. # go up three folders
cd - # go to the previous folder
ls
-- List Stuff: Allows you to list the contents of a folder. Adding
options allows you to list more details and see hidden files.
ls # list the contents of the current directory
ls .. # list the contents of the directory above this one
ls ~ # list the contents of my user directory
ls myfolder # list the contents of `myfolder`
ls -l # list contents in a vertical format with details
ls -a # list contents, including hidden files
ls -l -a # list contents in detail format, including hidden files
ls -l -a ~ # list the contents of my home user directory, in detail, with hidden files
cat
-- Like a CAT coughing up a hairball, spit out the contents of a file:
This will print out the contents of any file onto the command line, so you can
read it without opening it. If a file is binary instead of plain-text, the
result will look like gobbledygook. Secretly, cat
is actually short for concatenate, and this command has many applications covered by more in-depth tutorials.
cat myfile.txt # print out the contents of `myfile.txt`
cat Desktop/notes.txt # print out the contents of `notes.txt`, in the Desktop folder
mv
-- Move: This command moves files and folders. It can also be used to
rename things.
mv myfile.txt ~/Desktop/ # move `myfile.txt` onto the desktop
mv myfile.txt ../ # move `myfile.txt` to the folder above
mv myfile.txt file.txt # rename `myfile.txt` to `file.txt`
mv myfile.txt ../myfolder/file.txt # move `myfile.txt` up and then to `myfolder` AND rename it to `file.txt`
mv myfolder ../ # move `myfolder` up one directory
mv myfolder folder # rename `myfolder` to `folder`
cp
-- Copy: This command copies files and folders, obviously. For folders,
you need to use the -r
option, which copies all the contents of a folder as
well as the folder.
cp myfile.txt file.txt # make a copy of `myfile.txt` and call the copy `file.txt`
cp myfile.txt myfolder/ # copy `myfile.txt` to `myfolder`, don't rename it.
cp myfile.txt myfolder/file.txt # copy `myfile.txt` to `myfolder`, and call the copy `file.txt`
cp -r myfolder ~/Desktop/ # copy `myfolder` and its contents to `Desktop`
cp -r myfolder folder # make a copy of `myfolder` called `folder`
mkdir
-- Make Directory: creates a new folder with the given name.
mkdir myfolder # creates a new folder in the current directory called `myfolder`
mkdir ~/Desktop/myfolder # makes `myfolder` on the desktop.
rm
-- Remove: deletes files or folders. For folders, you need to use the
-rf
option which deletes all the contents of the folder before deleting the
folder.
rm myfile.txt # delete `myfile.txt`
rm -rf myfolder # delete `myfolder` along with everything it contains
rm myfolder/myfile.txt # delete `myfile.txt` which is inside `myfolder`
rm *.csv # delete all files that end with `.csv` in the current directory.
rm -rf ~/Downloads/* # delete everything in the `Downloads` folder
ssh
-- Secure Shell: allows you to connect to another computer. It's like
cd
, but gets you to completely different computer instead of just a
different folder. Often you'll need to enter your password after entering this
command. You should enter the password for the user account you are logging
into the server with. In the examples below, I would use the password for
bgolder
on athena. By default, this will put you on the ~
user home directory for that
user account on the server, an important detail to know for scp
. When you enter your password, you wont see any visible changes.
ssh [email protected] # log into the athena servers as the user `bgolder`
ssh [email protected] # log into the webfaction servers as user `bengolder`
scp
-- Secure Copy: just like cp
but it can be used to copy stuff to
or from other computers. It's like cp
but for sending things through ssh
.
Just like cp
, you need to use the -r
option for folders. This will often
be followed by a request for your password.
scp myfile.txt [email protected] # put `myfile.txt` into `bgolder`'s home folder on athena
scp myfile.txt [email protected]:www/ # put `myfile.txt` into the `www` folder inside `bgolder`'s home folder on athena
scp -r myfolder [email protected]:www/ # put `myfolder` in `~/www` on athena
scp [email protected]:myfile.txt ./ # copy `myfile.txt` from `~/` on athena into this current directory from `~/` on athena into this current directory
scp -r [email protected]:www ./ # copy `~/www` folder on athena to this current directory
scp -r [email protected]:www ~/Desktop/ # copy `~/www` folder on athena to the Desktop
- Press the
↑
and↓
arrow keys to scroll through previous commands. This is useful when using the same command repeatedly. - When you are entering a file or directory path, you can use the tab key to autocomplete the names of folders and files. If it doesn't autocomplete, that probably means there is more than one file or folder that begins with the same letters that you've typed.
- When you enter your password for
ssh
orscp
, you won't see anything change in the terminal window. That's okay, just type your password and press enter. - on a Mac, you can enter
open .
to open the current directory in Finder. You can useopen
with anything actually, and it will open the input file or folder with the default program. For example,open niceviz.psd
would openniceviz.psd
in Photoshop. - making a file in
~/
called.bash_profile
(the.
is important and will make it a hidden file), will allow you to add any custom shortcut, also called "alias". I often usealias ll='ls -l -a'
which means if I typell
, it will be a shortcut forls -l -a
. In fact, feel free to look at my most used bash shortcuts.
directory -- the same thing as a folder. It's just a place on the computer that can store files.
option -- an additional piece of text that you give to a command in order
to change how the command works. These typically follow the command name, and
begin with dashes, for example ls -l -a
.