Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In addition to viewing files, we can also copy and move their contents around! The first command we'll us to do this is the cp command, which takes in two arguments; the first is the file you want to copy, and the second is the name of the new file you want to create:

Code Block
languagebash
themeDJango
cp foo.txt foo_copy1.txt
cat foo_copy1.txt

Under the hood, these are using relative paths to know which files you're referring to; knowing this, we can copy files into different directories as well!

Code Block
languagebash
themeDJango
cp foo.txt test/foo_copy2.txt
cat test/foo_copy2.txt

In addition to copying files, we can use the mv command to move files. This is just like copying, except it deletes the source file at the end:

Code Block
languagebash
themeDJango
mv foo.txt test/foo_moved.txt
ls                              # No foo.txt in our c2s2 directory
cat test/foo_moved.txt

While mv is similar to cp , it also has a bit more functionality. For instance, we can use mv to rename files by simply moving them to something with a different name (this is how we rename files in Linux):

Code Block
languagebash
themeDJango
mv foo_copy1.txt bar.txt
ls
cat bar.txt

In addition to moving files, we can use mv to rename and move directories as well! This will move the directory and its contents:

Code Block
languagebash
themeDJango
mv test test_moved
ls

While cp can copy directories, simply typing something like cp test_moved won't work; cp doesn't work just by itself on directories. Instead, we must explicitly specify that we'd like to copy the directory and its contents by using the r flag (short for recursive, indicating that we'd like to recurse into the directory to copy all of its contents)

Code Block
languagebash
themeDJango
cp -r test_moved test_copied
ls
ls test_copied                 # ls works with paths to directories as well!

Take some time to play around with the files that you have at your disposal now, seeing all the different ways that you can copy and move them. In particular:

  • Make another copy of bar.txt named bar2.txt 
  • Copy a file from within test up to the parent folder, c2s2 
  • Move the test folder to have your NetID folder as its parent, and rename it test_renamed 
Info

If you are given a choice between copying a folder (using cp) and moving it (using mv), the latter will always be faster. cp has to move all the contents, but when using mv, the operating system simply has to move around the pointer to the folder in the folder hierarchy, and doesn't have to move all of the data individually (if you're curious about what a pointer is, check out the C/C++ Training!)

Removing Files

Info

The commands discussed in this section will have permanent, irreversible effects. Use them with caution and at your own risk. Unless you are certain that you will never need the files again, it is recommended that you instead move them to a temporary location (C2S2 will likely come up with a command to automatically do this, poke Aidan or the team lead if you want it sooner (smile))

To remove a file, we will use the rm command. Unless you happen to be a forensic computer scientist

Footnote