This is an old revision of the document!
cp (CoPy) command is quite flexible. There are a few ways it can be used to copy a file.
This can be used to copy the contents of a file (the source file) into a new file (the target file) with a new name:
cp <source_file.txt> <target_file.txt>
Exercise: Make a backup copy of a file. Navigate into your folder
160825_options_ex. Make a copy of
$ cp hg38_chrom_sizes.txt hg38_chrom_sizes_backup.txt $ ls $ more hg38_chrom_sizes.txt #peek in the file $ more hg38_chrom_sizes_backup.txt #peek in the backup
You can think of this as basically shorthand for…
$ cp ./file1.txt ./file2.txt
You can expand the source and target names to be absolute paths, too!
Once you make the connection that names, absolute paths, or relative paths can substitute in for <source_file.txt> or <target_file.txt>, you can see how you can place the copied file in some other directory, or pull a copy of a file from a source directory into your working directory.
Duplicating a file into a directory and renaming it:
cp <source_file.txt> <target/path/targetname.txt>
Duplicating a file from another directory into the current directory and renaming it:
cp <target/path/sourcename.txt> <./targetname.txt>
Exercise: Navigate into your folder
160825_options_ex. Make a directory called
backups. Place a copy of
backups and name it
$ mkdir backups $ cp mm10_chrom_sizes.txt backups/mm10_chrom_sizes_backup.txt $ ls $ ls backups
Quick Tip: Absolute paths as well as relative paths can be used as the source and target in
If you want to duplicate a file into a sub-directory, you don't need to change the name. To keep the name the same…
cp <source_file.txt> <target_directory>
Exercise: Try it out
$ cp dm6_chrom_sizes.txt backups $ ls $ ls backups
A list of files can also be copied in this way:
cp <source_file.txt> … <target_directory>
– where “…” means you can keep adding additional
source_files.txt, as many as you have.
Directories that contain files can also be duplicated using
cp. Just add the option
$ cp -R backups copy_of_backups $ ls $ ls backups $ ls copy_of_backups
Independent Exercise: I like to stay organized by adding notes to myself within project directories. I call these README or ABOUT files.
160825_options_excreate a directory called
Independent Exercise: Add a female genome.
hg38_chrom_sizes_female.txtto a new file called
hg38_chrom_sizes_female.txtand delete the entry for the Y chromosome. hint: you can't add a cursor anywhere you want. You'll need to navigate with arrow keys.
Once you know cp, mv is pretty much the same thing with one exception. The source file will disappear once the operation is complete. This ends up renaming your file if you are working within the same directory. It acts like cut-and-paste instead of a copy-and-paste if you're moving between directories.
|mv <source_file.txt> <target_file.txt>||Rename source_file.txt to be called target_file.txt|
|mv <source_file.txt> <dir/target_file.txt>||Move source_file.txt into dir and rename it target_file.txt|
|mv <source_file.txt> … <dir>||Move source_file.txt(s) into dir and keep the names the same|
Independent Exercise: Remember your directory called 160825_options_ex? MOVE that whole directory and its contents to the new directory
~/courseDirectory/02_Exercises you recently created within your course directory (where ~ stands for your appropriate path).