Linux Exercise: Working with files and directories

Working with directories

  1. Make sure you are in your home directory.
    • $ cd
  2. Create a directory testdir. Run the ls command to verify the directory exists.
    • $ mkdir testdir
    • $ ls -l
  3. Switch to the testdir directory. Make sure it is empty. Switch back to your home directory and try to delete the testdir directory.
    • $ cd testdir
    • $ ls -l
    • $ cd ..
    • $ rmdir testdir
  4. Again, create a directory testdir. Switch to this directory and create a file "testfile". Go back to your home directory and try to delete the testdir with the rmdir command. What happens?
    • $ mkdir testdir
    • $ cd testdir
    • $ touch testfile
    • $ ls -l
    • $ cd ..
    • $ rmdir testdir
      You cannot remove the directory as it is not empty.
  5. Remove the testdir, this time with the rm command, and the option to delete things recursively.
    • $ rm -r testdir

Working with files

  1. Create a file "testfile" in your home directory.
    • $ touch testfile
  2. Look at the details of the testfile. At what date/time was it created?
    • $ ls -l testfile
  3. Make a copy of your testfile called testfile2. Take a look at the details. At what date/time was it created?
    • $ cp testfile testfile2
    • $ ls -l testfile2
      testfile2 is a new file, so it will have its own modification date/time.
  4. Move the file testfile to a new name, myfile. Take a look at the details. At what date/time was it created?
    • $ mv testfile myfile
    • $ ls -l myfile
      myfile is not a new file, but rather an existing file with a modified name. So it retains the original date/time of testfile.

Working with files and directories

  1. Create a directory mydir. Look at the properties of the directory mydir.
    • $ mkdir mydir
    • $ ls -ld mydir
      Note that if you were to use the command ls -l mydir, then you will see the contents of the directory mydir. By specifying the -d option, you looked at the properties of the directory itself.
  2. Copy both testfile2 and myfile to the directory mydir. Take a look at the date/time of these files.
    • $ cp testfile2 myfile mydir
    • $ ls -l mydir
      The files in mydir are new copies, so the copies get a new date/time.
  3. Create a directory mydir2. Move both testfile2 and myfile to the directory mydir2. What happens to the date/time now?
    • $ mkdir mydir2
    • $ mv testfile2 myfile mydir2
    • $ ls -l mydir2
      The files have been moved, but not modified. So the original date/time remains intact.
  4. Recursively remove both mydir and mydir2 in anticipation of the next exercise.
    • $ rm -fr mydir mydir2
End of exercise