<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">\documentclass[onecolumn,letterpaper,11pt]{article}

%===================================================================
\input{common_latex/common_latex_packages}
\input{common_latex/common_latex_definitions}
\input{common_latex/common_latex_help_format}
%===================================================================

\begin{document}

\begin{labcover}
\labtitle{Help Topic: Command Line Survival Basics}
\vspace{0.1in}
\labdate{Spring 2022}
\vspace{0.1in}
\labauth{Michael Benjamin, mikerb@mit.edu}
\labauth{Department of Mechanical Engineering}
\labauth{MIT, Cambridge MA 02139}
\labline
\end{labcover}

\wikidoc{file=Help.CMDBasics}
\wikidoc{auth=mikerb@mit.edu}

%====================================================================
\section*{Command Line Quick Survival Basics}
%====================================================================

The Terminal, i.e., the command line environment, offers an incredibly
powerful set of tools out of the box. These tools are readily
extendible through the addition of countless open source additions, as
well as through additions made by you, in this course and hopefully
well afterwards. To get started, the below list represents the bare
bones basics.  This list represents my own perspective, but you can
find similar material by just doing a web search on "command-line
basics".

\vspace{0.1in}
\noindent
Here we use the terms {\em Terminal} and {\em command line}
interchangeably. This reflects a Mac user's perspective since the {\em
  Terminal} application is the most common way to interact with the
command line. If you're a GNU/Linux user, you're more
likely to refer to the {\em Console} instead of the terminal.

\vspace{0.1in}
\noindent
Let's get started.

%---------------------------------------------------------------------
\subsection*{Viewing the Contents of Your Directory with \cmd{ls} Command}

The command line offers just another way of navigating through the
same file system you might otherwise navigate with GUIs such as
Finder. Perhaps the most commonly used command is \cmd{ls}, which just
displays the contents of present directory:

\vspace{0.15in}
\begin{consoleverb}
 $ ls
 mail  project_fango   public_html   report_a.pdf   report_b.pdf   test.txt
\end{consoleverb}
\vspace{0.15in}

\noindent
The \cmd{ls} command comes with a bunch of {\em command line
options}. These are extra things you can type on the command line as
arguments to the command being invoked. For example, in the above, six
items are reported. It turns out
that \var{mail}, \var{project\_fango}, and \var{public\_html} are
directories, i.e., folders, containing other files or
directories. This is hard to discern from the above output. But if you
use a simple command line option, "-F", you'll be able to see this
right away:

\vspace{0.15in}
\begin{consoleverb}
 $ ls -F
 mail/  project_fango/   public_html/   report_a.pdf   report_b.pdf   test.txt
\end{consoleverb}
\vspace{0.15in}

\noindent
Note all directories have the trailing "/" character. I also use
\cmd{-G} to make the directories show up in color. This can be done by
typing \cmd{ls -F -G}, or more simply, \cmd{ls -FG}. There are several
useful options worth exploring when you get a chance. For example, in
directories with many files, you can sort by file type or date. Or you
can filter to only show files of certain types. To explore, just type
\cmd{man ls} to see some options.

\pskip
\noindent
Note also that arguments provided on the command line are separated by spaces. 
This is why most command line users {\em loathe} file names with spaces. Note
the names \var{report\_a.pdf} above uses the underscore character instead.

%---------------------------------------------------------------------
\subsection*{Knowing what directory (folder) you are in with \cmd{pwd}}

Using the command line, you are always "in" a particular
directory. Remember, command line users tend to refer to a directory
where other users might refer to a folder. To know what the "present
working directory" is, simply use the "pwd" command with no arguments.

\vspace{0.15in}
\begin{consoleverb}
 $ pwd
 /Users/mary/lab_one
\end{consoleverb}
\vspace{0.15in}

\noindent
This is such a useful piece of information to have that many users
customize their command line prompt (the dollar-sign at the left) to
show the present working directory. As an exercise, search the web for
"customize command line prompt" and see if you can set this up. 

%For example (\var{/mary/lab\_one \$}). When you're ready
%for this, see the following help page:

%\vspace{0.1in}
%\noindent
%\urlxx{http://oceanai.mit.edu/ivpman/help/custom_shell_prompt}

%---------------------------------------------------------------------
\subsection*{Changing Directories with the \cmd{cd} Command}

Now that you know how to tell what directory you're, you might be wondering
how to change directories, moving around the overall file structure. To do
this we use the \cmd{cd} command. The most common argument is the full path name
of the directory you want to go to, as in:

\vspace{0.15in}
\begin{consoleverb}
 $ cd /Users/mary/lab_one
 $ pwd
 /Users/mary/lab_one
 $ cd /Users/bob/projects/lab_three
 $ pwd
 /Users/bob/projects/lab_three
\end{consoleverb}
\vspace{0.15in}

\noindent
There are a few handy shortcuts that should be in your mental toolbox
right away. The first is for changing to the directory right above
your current directory. For example, suppose you are presently in the
directory \var{/Users/bob/projects/lab\_three}, and you wish to move
up to the directory \var{/Users/bob/projects}. You could do it the
hard way:

\vspace{0.15in}
\begin{consoleverb}
 $ pwd
 /Users/bob/projects/lab_three
 $ cd /Users/bob/projects
 $ pwd
 /Users/bob/projects
\end{consoleverb}
\vspace{0.15in}

\noindent
Or you could do it the easy way, replacing the third line above with 
the simpler version below:

\vspace{0.15in}
\begin{consoleverb}
 $ pwd
 /Users/bob/projects/lab_three
 $ cd ..
 $ pwd
 /Users/bob/projects
\end{consoleverb}
\vspace{0.15in}

\noindent
The \cmd{../} notation is an actual "directory" in every directory in
your file system. This special shorthand name has been around
forever. Another shorthand name is \cmd{./} which stands for the
current directory. Because both directories begin with a dot, they are
also known as {\em hidden} directories, and don't show up when you use
the \cmd{ls} command. They can be seen if you use the \cmd{ls -a} option
for showing "all" files and directories, including the hidden ones. Go 
ahead and try it now, you will see both \cmd{./} and \cmd{../} regardless
of which directory you are in. Even the "root" directory, the top-most
directory in the tree, contains the \cmd{../} directory. Trying to go
up from the root directory will just land you back in the root directory
however.

\pskip
\noindent
A few other handy shortcuts are below. To move up two directories at once:

\vspace{0.15in}
\begin{consoleverb}
 $ cd ../../                      (go up two directories)
\end{consoleverb}
\vspace{0.15in}

\noindent
To move to straight to your home directory:

\vspace{0.15in}
\begin{consoleverb}
 $ cd                            (go directly to your home directory)
 $ pwd
 $ /Users/bob
\end{consoleverb}
\vspace{0.15in}

\noindent
To move to a directory within your home directory:

\vspace{0.15in}
\begin{consoleverb}
 $ cd ~/project                  (go to a subdirectory of your home directory)
 $ pwd
 $ /Users/bob/project
\end{consoleverb}
\vspace{0.15in}

\noindent
To move to the last directory you were in, prior to the last
invocation of \cmd{cd}:

\vspace{0.15in}
\begin{consoleverb}
 $ pwd
 $ /Users/bob
 $ cd /Users/mary/projects/one
 $ pwd
 $ /Users/mary/projects/one
 $ cd -                          (go to the directory prior to the last cd)
 $ pwd
 $ /Users/bob
\end{consoleverb}
\vspace{0.15in}

\end{document}

</pre></body></html>