<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: Creating and Removing Directories}
\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.CMDMkdir}
\wikidoc{auth=mikerb@mit.edu}

%====================================================================
\section*{Creating/Removing Directories and Files on the Command Line}
%====================================================================

Now that we know how to see what directory we're in with \cmd{pwd}, and see
the contents of the directory with \cmd{ls} and change between directories with 
\cmd{cd}, we discuss how to make and remove contents of directory. A directory
has two types of elements: other directories, and files. We begin first with 
directories.

%---------------------------------------------------------------------
\subsection*{Making a New Directory with the \cmd{mkdir} Command}

Making a new directory is done with the \cmd{mkdir} command. It
typically has only a single argument, the name of the new
directory. For example, suppose your current directory has the
following contents:

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

\noindent
You can make a new directory, \var{project\_fango}, using \cmd{mkdir}:

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

\noindent
That's it. In our example, we were in the directory where we created the 
new directory, but that's not necessary. You can provide a full path name
of the directory you wish to create. For example:

\vspace{0.15in}
\begin{consoleverb}
 $ mkdir ~/project_fango
\end{consoleverb}
\vspace{0.15in}

\noindent
will create the new directory in your home directory regardless of where you
are in your file system. If the directory already exists, it will tell you so:

\vspace{0.15in}
\begin{consoleverb}
 $ mkdir ~/project_fango
 $ mkdir ~/project_fango
 mkdir: project_fango: File exits
\end{consoleverb}
\vspace{0.15in}

\noindent
Lastly, to make a directory, you need write permissions in the directory
where you're creating the new directory. By default you always have write
permission in your home directory and in the new directories you make. But
eventually you may encounter the issue of permissions. 

%---------------------------------------------------------------------
\subsection*{Removing a Directory with the \cmd{rmdir} Command}

A directory may be removed with the \cmd{rmdir} command:

\vspace{0.15in}
\begin{consoleverb}
 $ rmdir project_fango
\end{consoleverb}
\vspace{0.15in}

\noindent
To use this command the directory must be empty. If you really wish to
remove a directory containing a whole tree of other files and
directories without having to crawl through the tree, you can do this
with the \cmd{rm -rf} command discussed below. Be careful, this command
wields great power.

%---------------------------------------------------------------------
\subsection*{Making a File with the \cmd{touch} Command}

There are many ways to make a new file; with an editor, as output from a 
program and so on. Assuming no knowledge yet of these things, the easiest
way to create an empty file is with the \cmd{touch} command

\vspace{0.15in}
\begin{consoleverb}
 $ touch file1 file2 file3
 $ ls
 file1 file2 file3
\end{consoleverb}
\vspace{0.15in}

\noindent
The above action creates three empty files. To check that they are
empty it may be a good time to introduce the \cmd{-l} (for {\em long}
format) option of the
\cmd{ls} command:

\vspace{0.15in}
\begin{consoleverb}
 $ touch file1 file2 file3
 $ ls -l
 -rw-r--r--   1 john  staff     0B Nov  3 09:51 file1.txt
 -rw-r--r--   1 john  staff     0B Nov  3 09:51 file2.txt
 -rw-r--r--   1 john  staff     0B Nov  3 09:51 file3.txt
\end{consoleverb}
\vspace{0.15in}

\noindent
The fifth column confirms that each new file created with \cmd{touch}
has a size of zero bytes.  The important thing is now we know a simple
way to make a file to demonstrate the next essential command, \cmd{rm}
for removing a file.

%---------------------------------------------------------------------
\subsection*{Removing a File with the \cmd{rm} Command}

Existing files may be removed with the \cmd{rm} command, which accepts one
or more file names as arguments:

\vspace{0.15in}
\begin{consoleverb}
 $ ls
 file1 file2 file3
 $ rm file3
 $ ls
 file1 file2 
\end{consoleverb}
\vspace{0.15in}

\noindent
A note of caution. The \cmd{rm} command can be brutally powerful.  It
does not, by default, ask you if you're sure about what you've just
requested to be removed. I highly recommend you change this default
behavior.  The \cmd{rm} command has a command line option \cmd{-i}
that changes \cmd{rm} to ask for confirmation:

\vspace{0.15in}
\begin{consoleverb}
 $ ls
 file1 file2 file3
 $ rm -i file3
 remove file3? 
\end{consoleverb}
\vspace{0.15in}

\noindent
To see how to make this interactive mode the default mode for the 
\cmd{rm} command, see the following help topic:

\vspace{0.1in}
\noindent
\urlx{http://oceanai.mit.edu/ivpman/help/cmdline_interactive_rm_mv_cp}

\vspace{0.1in}
\noindent
For now, just be really careful using the \cmd{rm} command.

%---------------------------------------------------------------------
\helpsubsection{Removing Groups of Files with the \cmd{rm} Command}

The command line offers a generic ability for matching groups of files or 
directories by naming a pattern. To remove all three of the files in the 
example above, one could instead just type:

\vspace{0.15in}
\begin{consoleverb}
 $ ls
 file1 file2 file3
 $ rm file*
 $ ls 
 $
\end{consoleverb}
\vspace{0.15in}

\noindent
The asterisk pattern matches any combination of characters against all file
and directory names in the current directory. You could also remove, for example,
all PDF files with \cmd{rm *.pdf}. Or any PDF file in any subdirectory with 
\cmd{rm */*.pdf}. Obviously this should be used with caution.


%---------------------------------------------------------------------
\helpsubsection{Removing Directories with the \cmd{rm} Command}

It was discussed above that a directory may be removed with the \cmd{rmdir}
command. This only works when the directory being removed is empty. To remove
an entire directory, including all its subdirectories, you can use two
additional command line arguments:

\vspace{0.15in}
\begin{consoleverb}
 $ rm -rf project
\end{consoleverb}
\vspace{0.15in}

\noindent
The \cmd{-r} options stands for {\em recursive} and will recursively
remove the entire tree in the named directory. The \cmd{-f} option
stands for {\em force} and overrides any \cmd{-i} option, meaning no
prompt for confirmation will be presented after the command is
invoked. Extremely powerful, extremely dangerous.  Use with caution.

\pskip
\noindent 
As with \cmd{mkdir}, the \cmd{rm} command will not let you remove
files or directories for which you do have write permisions. But you
should certainly pause and think for a second whenever you begin a
shell command with \cmd{rm -rf}.

\end{document}


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