2.1 Navigation and File Management
Make sure you are logged into your machine! You will practice how to organize files and use directories.
Start from your home directory on your machine
Create 6 of the each of the following files:
song[X].mp3
snap[X].jpg
film[X].avi
You could do this one by one:
touch song1.mp3
touch song2.mp3
touch song3.mp3
touch song4.mp3
touch song5.mp3
touch song6.mp3
Or make multiple at the same time with the same command:
touch snap1.jpg snap2.jpg snap3.jpg snap4.jpg snap5.jpg snap6.jpg
Or make them all at the same time using some very basic scripting:
touch film{1..6}.avi
In the end, when listing all files your output should look like this:
[greater@rhcsa ~]$ ls -l
total 0
-rw-rw-r-- 1 greater greater 0 Nov 23 16:04 film1.avi
-rw-rw-r-- 1 greater greater 0 Nov 23 16:04 film2.avi
-rw-rw-r-- 1 greater greater 0 Nov 23 16:04 film3.avi
-rw-rw-r-- 1 greater greater 0 Nov 23 16:04 film4.avi
-rw-rw-r-- 1 greater greater 0 Nov 23 16:04 film5.avi
-rw-rw-r-- 1 greater greater 0 Nov 23 16:04 film6.avi
-rw-rw-r-- 1 greater greater 0 Nov 23 16:05 snap1.jpg
-rw-rw-r-- 1 greater greater 0 Nov 23 16:05 snap2.jpg
-rw-rw-r-- 1 greater greater 0 Nov 23 16:05 snap3.jpg
-rw-rw-r-- 1 greater greater 0 Nov 23 16:05 snap4.jpg
-rw-rw-r-- 1 greater greater 0 Nov 23 16:05 snap5.jpg
-rw-rw-r-- 1 greater greater 0 Nov 23 16:05 snap6.jpg
-rw-rw-r-- 1 greater greater 0 Nov 23 16:04 song1.mp3
-rw-rw-r-- 1 greater greater 0 Nov 23 16:04 song2.mp3
-rw-rw-r-- 1 greater greater 0 Nov 23 16:04 song3.mp3
-rw-rw-r-- 1 greater greater 0 Nov 23 16:04 song4.mp3
-rw-rw-r-- 1 greater greater 0 Nov 23 16:04 song5.mp3
-rw-rw-r-- 1 greater greater 0 Nov 23 16:04 song6.mp3
Now lets make sure that we are in our home directory, we do this by using the pwd (print working directory) command.
The result should be something like below:
[greater@rhcsa ~]$ pwd
/home/greater
We can move to a different directory with using cd (change directory). And the ~ sign correlates to our home directory. So to make sure we change to our home directory we can use below command:
[greater@rhcsa ~]$ cd ~
Next we’re going to create some directories for the files in your home directory. This is done with the mkdir (make directory) command. Using this command, create the following directories:
music
pictures
movies
For example:
mkdir music pictures movies
Lets move the files to their corresponding directories with the mv (move) command:
Move the song files to music
Move the snap files to pictures
Move the film files to movies
We can do it all in the same command:
mv song1.mp3 song2.mp3 song3.mp3 song4.mp3 song5.mp3 song6.mp3 music
Alternatively we can use a wildcard *, which will select every file that starts with the text before the wildcard symbol:
mv snap* pictures
Finally we can again use basic scripting to select all files:
mv film{1..6}.avi movies
Now lets organize the files according to project create the directories:
friends
family
work
mkdir friends family work
You will collect some of the new files into the project directories for family and friends. Use as many commands as needed. You do not have to use only one command as in the example. For each project, first change to the project directory, then copy the source files into this directory. You are making copies, since you will keep the originals after giving these projects to family and friends.
Copy files (all types) containing numbers 1 and 2 to the friends folder. Copy files (all types) containing numbers 3 and 4 to the family folder. When collecting files from multiple locations into one location, change to the directory that will contain the destination files. Use the simplest path syntax, absolute or relative, to reach the source for each file management task.
note the . at the end of the copy command: this instructs the computer to copy the files to the location you are in and keep the name.
cd friends
cp ../music/song1.mp3 ../music/song2.mp3 ../pictures/snap1.jpg ../pictures/snap2.jpg ../movies/film1.avi ../movies/film2.avi .
It should look like this:
[greater@rhcsa friends]$ ll
total 0
-rw-rw-r-- 1 greater greater 0 Nov 23 20:13 film1.avi
-rw-rw-r-- 1 greater greater 0 Nov 23 20:13 film2.avi
-rw-rw-r-- 1 greater greater 0 Nov 23 20:13 snap1.jpg
-rw-rw-r-- 1 greater greater 0 Nov 23 20:13 snap2.jpg
-rw-rw-r-- 1 greater greater 0 Nov 23 20:13 song1.mp3
-rw-rw-r-- 1 greater greater 0 Nov 23 20:13 song2.mp3
cd ../family
cp ../music/song3.mp3 ../music/song4.mp3 ../pictures/snap3.jpg ../pictures/snap4.jpg ../movies/film3.avi ../movies/film4.avi .
It should look like this:
[greater@rhcsa family]$ ll
total 0
-rw-rw-r-- 1 greater greater 0 Nov 23 20:14 film3.avi
-rw-rw-r-- 1 greater greater 0 Nov 23 20:14 film4.avi
-rw-rw-r-- 1 greater greater 0 Nov 23 20:14 snap3.jpg
-rw-rw-r-- 1 greater greater 0 Nov 23 20:14 snap4.jpg
-rw-rw-r-- 1 greater greater 0 Nov 23 20:14 song3.mp3
-rw-rw-r-- 1 greater greater 0 Nov 23 20:14 song4.mp3
cd ../work
cp ../music/song5.mp3 ../music/song6.mp3 ../pictures/snap5.jpg ../pictures/snap6.jpg ../movies/film5.avi ../movies/film6.avi .
It should look like this:
[greater@rhcsa work]$ ll
total 0
-rw-rw-r-- 1 greater greater 0 Nov 23 20:14 film5.avi
-rw-rw-r-- 1 greater greater 0 Nov 23 20:14 film6.avi
-rw-rw-r-- 1 greater greater 0 Nov 23 20:14 snap5.jpg
-rw-rw-r-- 1 greater greater 0 Nov 23 20:14 snap6.jpg
-rw-rw-r-- 1 greater greater 0 Nov 23 20:14 song5.mp3
-rw-rw-r-- 1 greater greater 0 Nov 23 20:14 song6.mp3
Now that you are done playing around with files, lets clean up the files we no longer need.
Change to your home directory. Attempt to delete both the family and friends project with a singe rmdir command.
cd ~
Now let’s try to remove family and friends, who needs those anyways!
rmdir family friends
You should get an error like this:
greater@rhcsa ~]$ rmdir family friends
rmdir: failed to remove 'family': Directory not empty
rmdir: failed to remove 'friends': Directory not empty
The above command failed because there are still files in the directories. The below command will use the addition -r for recursive. This will succeed in removing your friends and family directories and the files in inside. This will require confirmation for each file you are removing, if you do not wish to provide confirmation for each file then you can choose to use the -f (force) option as well.
rm -r family friends
Check with ls -l if the directories were removed and finish up by removing each file within the work folder.
cd work
rm song5.mp3 song6.mp3 snap5.jpg snap6.jpg film5.avi film6.avi
or
rm *
change the directory back with the cd command and remove the directory.
cd
rmdir work
This should delete the work directory. Clean up the remaining directories yourself.