You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 11 Next »

test

Now that we know how to move about our file system, it's time to start interacting with some of our files! For this portion, we'll start in our c2s2 directory from before, and assume that we have another empty directory named test inside of the c2s2 directory:

cd ~/c2s2
ls

We'll also start by creating a file called foo.txt with the text "foo bar" inside of it. We'll use the following command to do this - don't worry too much about it right now, we'll cover it later, but we can think of it as sending "foo bar" to the new file:

echo "foo bar" > foo.txt

Feel free to also do this with a code editor, if you've used one already!

For the sake of having another text file that's somewhat larger - we'll use the King James Bible, which you can store in a text file named james.txt with the following command (don't worry, we'll cover this later as well):

wget https://www.gutenberg.org/cache/epub/10/pg10.txt -O james.txt

Viewing Files

To view the text of a file in the terminal, the first command we can use is the cat command (short for concatenate), which takes in as an argument the file we want to view the text of, and simply prints it all to the terminal:

cat foo.txt

Note how this works well for short files, but not as well for large files - if you try running cat james.txt , you'll quickly get a very long output that's hard to understand all in the terminal! Instead, we can use a different command to better parse the output.

One of the oldest commands is the more command, which can help incrementally show you more of a file at a time:

more james.txt

Here, you can scroll with the arrow keys, and exit by pressing "q". However, the output still remains on the terminal, and scrolling leaves something to be desired. To add on to this, many systems have the less command, which we recommend:

less james.txt

Many of the controls are the same, but you can now scroll with your mouse/trackpad, and the output isn't left on the terminal at the end.

If your terminal every gets too cluttered for your liking, you can use the clear command to get clear away the history from the terminal and start a new prompt at the top

Copying and Moving Files

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:

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!

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:

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

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:

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)

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 
  • Move a file from within test up to the parent directory, c2s2 
  • Copy the test directory to have your NetID directory as its parent, and rename it test_renamed 

If you are given a choice between copying a directory (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 directory in the directory 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

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, there is no way to get files deleted by rm back, so use the command sparingly and only when you're confident! 1

rm bar.txt

Like cp, if we want to delete an entire directory, we must use the r flag:

rm -r test

Using rm (carefully), clean up all the files and directories created by the tutorial inside c2s2 so far, as well as copy of the test directory that you moved under your NetID directory previously

Addendum: Downloading Files from the Web

Footnotes

  1. Strictly speaking, what rm does under the hood is eliminates the pointer to the files from the directory structure (basically, lose the reference our operating system has to its contents) and frees up the memory for the operating system to store other content there. If you were able to externally access the hard drive before the operating system re-wrote that location (hint: turn off the machine immediately, hopefully before the operating system uses that memory), and discern the location in memory where the content was previously stored, one could theoretically get the contents back. There are specialists who have the tools to do this, but for 99.9% of users, it is impossible. If you're curious, to really get rid of data, you can use the shred command to overwrite the memory such that it can't be recovered even by experts - such an operation may be useful if you're disposing of sensitive data.

  • No labels