To create a copy of a file using your terminal you need to use the command cp
(copy), but if you need to copy a whole directory along with all its content you need to use an argument/flag, cp -r
(copy recursively). In both cases you should also provide the path of where to save the copy. For example, cp file_example.txt ~/Documents/destination_folder/
(this will create a file with the same name in ~/Documents/destination_folder/
). If you want to create a copy in the same directory you have to provide a new name, something like cp file_example.txt copy_file_example.txt
.
Copy a file to a different folder
1 – Click to do exercise in a controlled environment before practicing quiz:
LinuxSurvival (copying files)
2 – Practicing a bit more in your own computer:
Open replit, check your working directory, now copy your favourite recipe to the backup folder you made in the last execise.
List the contents of the backup folder and you should have the file copied.
.
In other cases if you want to just move, you need to mv
(move) an item like this: mv file_example.txt ~/Documents/new_Directory/
This will move the file inside of the folder new_Directory.
However, the command mv
(move) is also used to rename items, so if you type mv file_example.txt new_file_name.txt
, what you are actually doing is renaming your file or directory (moving from one name to the other).
In the next session we will learn how to delete files and folders in the terminal.
.
Before we move on, we need to add one extra layer of complexity!
To all the commands we are learning, but specially for creating a copy or moving files, we can combine the knowledge of how to write a path and the command itself. This is the best way to assure you are accessing and connecting the desired directories.
So you can use:
mv file.txt ../../Documents/my_new_folder/subfolder/
or
cp Documents/my_folder/file.txt Documents/my_new_folder/subfolder/
or
mkdir Documents/my_folder/NEW_FOLDER
or
ls -la Documents/my_folder/
and so own ….
Example:
1 – Click to do exercise in a controlled environment before practicing quiz:
LinuxSurvival (moving and renaming files)
2 – Practicing a bit more in your own computer:
Open replit, check your working directory, create a folder named with today’s date inside your backup folder, then move your favourite recipe into this new folder, then finally rename that recipe to note that it is a backup.