Versions Compared

Key

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

...

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

Code Block
languagebash
themeDJango
cd ~/c2s2
ls

...

Info

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

For the sake of having demonstration, we'll also want to have 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):

Code Block
languagebash
themeDJango
wget https://www.gutenberg.org/cache/epub/10/pg10.txt -O james.txt
Info

As an aside; a nice command to create an empty file named new_file.txt is the touch command, which is run as touch new_file.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:

...

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

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

Infowarning

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 these files deleted by rm back, so use the command sparingly and only when you're confident!

Footnote

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, some systems have 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.

Code Block
languagebash
themeDJango
rm bar.txt

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

Code Block
languagebash
themeDJango
rm -r test

In addition, if a directory is empty, the command rmdir can remove it - this is sometimes safer, as it will only remove empty directories

Code Block
languagebash
themeDJango
mkdir test_dir
ls
rmdir test_dir
ls
Info

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

Alright; while we're at it, let's revisit our wget command from before, which is installed on many systems, including the C2S2 server. wget takes in a file that is located on the internet, and downloads the file to your current directory. For example, if we wanted to download the Bill of Rights, we might do so with the following command:

Code Block
languagebash
themeDJango
wget http://www.textfiles.com/anarchy/billofrights.txt

Note that this will store it as the original file name, billofrights.txt . Previously, we used the O flag, which specified that we wanted the new file name to be the next argument.

If you want to try this on your own, you can browse some cool text files here

Info

If you try to download a website that isn't explicitly a file, you'll get a file named index.html - the HTML source code that makes up the website!

Footnotes

Footnotes Display
Test test