<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: Renaming and Copying Files}
\vspace{0.1in}
\labdate{Spring 2020}
\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.CMDMoveCopy}
\wikidoc{auth=mikerb@mit.edu}

%====================================================================
\section*{Moving/Renaming and Copying Files on the Command Line}
%====================================================================

Moving (renaming) and copying files and directories are done on the
command line using the \cmd{mv} (move) command and the \cmd{cp} (copy)
command.

%---------------------------------------------------------------------
\subsection*{Moving and Renaming a file with the \cmd{mv} command}

The jobs of moving and renaming files are both handled by the \cmd{mv}
command. In the simplest case for example, \var{file1} can be renamed
to file \var{file2} like this:

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

\noindent
The \cmd{mv} command may also be used to move one or many files into a 
directory:

\vspace{0.15in}
\begin{consoleverb}
 $ ls -F
 file1   file2   file3   file4   file5   project/ 
 $ mv file* project
 $ ls -F
 project/
 $ ls project
 file1   file2   file3   file4   file5 
\end{consoleverb}
\vspace{0.15in}

\noindent
If you try to move a file into a directory that doesn't exist you will get
an error. 

\vspace{0.15in}
\begin{consoleverb}
 $ ls -F
 file1   project/ 
 $ mv file1 project23/
 mv: rename file1 to project23/: No such file or directory
\end{consoleverb}
\vspace{0.15in}

\noindent
By default the \cmd{mv} command will let you rename one file to
another, even if the target file already exists. Most people find this
a bit unsettling to allow this without prompting for confirmation of
intention. If the \cmd{mv} command is used with the \cmd{-i} option,
the user will indeed be prompted for confirmation if the target file
already exists.  For this reason most people employ an alias for
the \cmd{mv} command that makes the interactive mode the default:

\vspace{0.15in}
\begin{consoleverb}
 $ alias mv 'mv -i'       (if using tcsh)
 $ alias mv='mv -i'       (if using bash)
\end{consoleverb}
\vspace{0.15in}

\noindent
See the help topics on aliases and shell configuration to do this. Once
this is done, you can always use the \cmd{-f} (force) command line option
to force the renaming of file to a target, even if the target already exists,
without prompting the user for confirmation.


%---------------------------------------------------------------------
\subsection*{Copying files and directories with the \cmd{cp} command}

Files and whole directories can be copied with the \cmd{cp} command. The
simplest case of copying one file to another:

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

\noindent
Copying a directory with \cmd{cp} requires a couple additional arguments 
on the command line:

\vspace{0.15in}
\begin{consoleverb}
 $ ls -F
 project/
 $ cp -rp project project_backup
 $ ls
 project/ project_backup/
\end{consoleverb}
\vspace{0.15in}

The first argument \cmd{-r} stands for {\em recursive} and indicates that
all subcomponents of the folder being copied should also be copied, following
the tree structure recursively in each subfolder's subfolder and so on. The
second argument \cmd{-p} is not required. It ensures that certain file 
attributes are preserved when copying. This includes the read-write 
permissions, and date of last modification. When copying a directory it
is a good habit to always use this option. 

\pskip

\noindent
A note of caution.  If you are copying a file to a file that already
exists, the \cmd{cp} does not, by default, prompt you for a
confirmation of your intentions.  I highly recommend you change this
default behavior. See the following additional help topic:

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

\end{document}

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