User Tools

Site Tools


wiki:2018cpmv

This is an old revision of the document!


Copying & Moving

Copying files and directories with cp

The cp (CoPy) command is quite flexible. There are a few ways it can be used to copy a file.


Duplicating a file and giving it a new name

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 hg38_chrom_sizes.txt called hg38_chrom_sizes_backup.txt.

$ 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!


Duplicating a file into a directory

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>

OR

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 mm10_chrom_sizes.txt into backups and name it mm10_chrom_sizes_backups.txt .

$ 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 cp.

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.


Duplicating a directory and its contents

Directories that contain files can also be duplicated using cp. Just add the option -R.

$ 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.

  1. Within your directory called 160825_options_ex create a directory called NOTES.
  2. Copy the file README_download.txt into the NOTES directory.

:!: Independent Exercise: Add a female genome.

  1. Copy hg38_chrom_sizes_female.txt to a new file called hg38_chrom_sizes_female.txt.
  2. Using nano, go into hg38_chrom_sizes_female.txt and 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.

Moving files and directories with mv

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).


wildcards

wiki/2018cpmv.1535023871.txt.gz · Last modified: 2018/08/23 05:31 by erin