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

Compare with Current View Page History

« Previous Version 6 Next »

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 folder from before, and assume that we have another empty folder named test inside of the c2s2 folder:

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 
  • 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 

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

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 1

  • No labels