Part 2. Manipulating files and directories

In Part 1, you created a file in your home directory using a text editor and you were able to see that file in the file system using the ls command. Now, we’re going to do the opposite: We’re going to manipulate files on the command line and examine the results in the GUI—the graphical user interface.

We’re going to be using some new commands.

  • mkdir Creates a new directory
  • cp Copies files or (with a suitable option) directories
  • rm Removes files or (with a suitable option) directories
  • mv Moves (or renames) files and directories
  • rmdir Removes an empty directory (this is less commonly used than rm for this operation)
  • man This shows a manual page which provides a bunch of information about many of the shell commands and programs
  • wget Downloads files from the Internet

Your task

Create the directory cs241/lab1 in your home directory using the mkdir command. (If your current directory is not your home directory, you can run $ cd or equivalently $ cd ~ to change to your home directory.)

If you try the “obvious” command, you’ll likely encounter a confusing error.

$ mkdir cs241/lab1
mkdir: cannot create directory ‘cs241/lab1’: No such file or directory

The problem here isn’t that cs241/lab1 doesn’t exist (which is good because we’re trying to create this directory), the problem is that the cs241 directory doesn’t exist. It’d be great if the error message reflected which component of the path didn’t exist, but alas, it does not.

Caution

In general, pay close attention to error messages but be aware that when the error message is about a file path, it could be about any component of that path.

To create the cs241/lab1 directory, you can use two invocations of mkdir, first to create cs241 and second to create lab1 within it.

Move the file task1.txt you created in Part 1 inside the cs241/lab1 directory using mv. The basic syntax for the mv command is

$ mv src dest

Keep in mind that when naming a file with a path, you either need to give it an absolute path starting with / or you need to give it a path from the current directory. Using ~ for your home directory can be a big time saver when the files you want to use are in your home directory (or in a directory in your home directory).

Open your home directory using the Files application from the left sidebar in the desktop. The task1.txt file should not be there. Open the cs241/lab1 directory. This should contain task1.txt and nothing else.

Create a directory named books in the lab1 directory using mkdir (you should see the new directory appear in Files).

Run $ man wget to pull up the manual page for the wget program which you can use to download files from the Internet. Press q to exit man when you’re done. Run $ wget --help to see similar information.

cd into the books directory you created and use wget to download a copy of Bram Stoker’s Dracula from https://www.gutenberg.org/cache/epub/345/pg345.txt.

Rename the file from pg345.txt to Stoker, Bram - Dracula.txt. Note that this name has spaces in it. You’ll need to specify it as 'Stoker, Bram - Dracula.txt' or Stoker,\ Bram\ -\ Dracula.txt.

Use wget again to download James Joyce’s Dubliners from https://www.gutenberg.org/files/2814/2814-0.txt. Rename it Joyce, James - Dubliners.txt.

Info

Different versions of ls will print file names with spaces in them differently. Some versions simply print the file names with the spaces. Others print the file names with quotes around them. When you run ls inside the books directory, you may see something like this.

$ ls
'Joyce, James - Dubliners.txt'  'Stoker, Bram - Dracula.txt'

At this point, the files and directories in your cs241 directory should look like this.

cs241/
└── lab1
    ├── books
    │   ├── Joyce, James - Dubliners.txt
    │   └── Stoker, Bram - Dracula.txt
    └── task1.txt

If you’d like to generate a similar picture of your files, try the tree command. tree directoryname will print a drawing like this of the directory contents to the terminal.