Linux Exercise: Working with files and directories
Working with directories
Make sure you are in your home directory.
Create a directory testdir. Run the ls command to verify the directory exists.
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
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.
Remove the testdir, this time with the rm command, and the option to delete things recursively.
Working with files
Create a file "testfile" in your home directory.
Look at the details of the testfile. At what date/time was it created?
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.
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
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.
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.
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.
Recursively remove both mydir and mydir2 in anticipation of the next exercise.
End of exercise