Copy A File
Part of the Fundamentals section of Coddy's Terminal journey — lesson 12 of 82.
The cp command stands for copy. It lets you create a copy of a file in a new location or with a new name.
The basic format is:
cp source destinationFor example, to copy readme.txt and name the copy backup.txt:
cp readme.txt backup.txtThe original file stays in place and a new copy is created.
Copy a file into a folder:
You can copy a file directly into another directory:
cp readme.txt documents/readme.txtOr simply pass the folder as the destination and the filename will be kept:
cp readme.txt documents/Copy a folder and all its contents:
To copy an entire directory, use the -r flag (recursive):
cp -r documents backup_documentsNote: cp will overwrite the destination file if it already exists.
Use the -i flag to get a confirmation prompt before overwriting:
cp -i readme.txt backup.txtChallenge
BeginnerCopy readme.txt to a new file called backup.txt, then use ls to confirm both files exist.
Hint: The format is
cp source destination.
Cheat sheet
The cp command copies files or directories.
Basic syntax:
cp source destinationCopy a file with a new name:
cp readme.txt backup.txtCopy a file into a directory:
cp readme.txt documents/Copy a directory recursively:
cp -r documents backup_documentsPrompt before overwriting:
cp -i readme.txt backup.txtTry it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline