Bookmark this page

Guided Exercise: Manage Files with Command-line Tools

In this exercise, you create, organize, copy, and remove files and directories.

Outcomes

  • Create, organize, copy, and remove files and directories.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command prepares your environment and ensures that all required resources are available.

[student@workstation ~]$ lab start files-manage

Instructions

  1. Log in to the servera machine as the student user. In the student user's home directory, create three subdirectories: Music, Pictures, and Videos.

    1. Use the ssh command to log in to the servera machine as the student user. The systems are configured to use SSH keys for authentication; therefore, a password is not required.

      [student@workstation ~]$ ssh student@servera
      ...output omitted...
      [student@servera ~]$
    2. In the student user's home directory, use the mkdir command to create three subdirectories: Music, Pictures, and Videos.

      [student@servera ~]$ mkdir Music Pictures Videos
  2. Use the touch command to create sets of empty practice files to use during this lab. In each set, replace X with the numbers 1 through 6.

    • Create six files with names of the form songX.mp3.

    • Create six files with names of the form snapX.jpg.

    • Create six files with names of the form filmX.avi.

    [student@servera ~]$ touch song1.mp3 song2.mp3 song3.mp3 \
    song4.mp3 song5.mp3 song6.mp3
    [student@servera ~]$ touch snap1.jpg snap2.jpg snap3.jpg \
    snap4.jpg snap5.jpg snap6.jpg
    [student@servera ~]$ touch film1.avi film2.avi film3.avi \
    film4.avi film5.avi film6.avi
    [student@servera ~]$ ls -l
    total 0
    -rw-r--r--. 1 student student 0 Mar  7 20:58 film1.avi
    -rw-r--r--. 1 student student 0 Mar  7 20:58 film2.avi
    -rw-r--r--. 1 student student 0 Mar  7 20:58 film3.avi
    -rw-r--r--. 1 student student 0 Mar  7 20:58 film4.avi
    -rw-r--r--. 1 student student 0 Mar  7 20:58 film5.avi
    -rw-r--r--. 1 student student 0 Mar  7 20:58 film6.avi
    drwxr-xr-x. 2 student student 6 Mar  7 20:58 Music
    drwxr-xr-x. 2 student student 6 Mar  7 20:58 Pictures
    -rw-r--r--. 1 student student 0 Mar  7 20:58 snap1.jpg
    -rw-r--r--. 1 student student 0 Mar  7 20:58 snap2.jpg
    -rw-r--r--. 1 student student 0 Mar  7 20:58 snap3.jpg
    -rw-r--r--. 1 student student 0 Mar  7 20:58 snap4.jpg
    -rw-r--r--. 1 student student 0 Mar  7 20:58 snap5.jpg
    -rw-r--r--. 1 student student 0 Mar  7 20:58 snap6.jpg
    -rw-r--r--. 1 student student 0 Mar  7 20:58 song1.mp3
    -rw-r--r--. 1 student student 0 Mar  7 20:58 song2.mp3
    -rw-r--r--. 1 student student 0 Mar  7 20:58 song3.mp3
    -rw-r--r--. 1 student student 0 Mar  7 20:58 song4.mp3
    -rw-r--r--. 1 student student 0 Mar  7 20:58 song5.mp3
    -rw-r--r--. 1 student student 0 Mar  7 20:58 song6.mp3
    drwxr-xr-x. 2 student student 6 Mar  7 20:58 Videos
  3. Move the song files (.mp3 extension) to the Music directory, the snapshot files (.jpg extension) to the Pictures directory, and the movie files (.avi extension) to the Videos directory.

    [student@servera ~]$ mv song1.mp3 song2.mp3 song3.mp3 \
    song4.mp3 song5.mp3 song6.mp3 Music
    [student@servera ~]$ mv snap1.jpg snap2.jpg snap3.jpg \
    snap4.jpg snap5.jpg snap6.jpg Pictures
    [student@servera ~]$ mv film1.avi film2.avi film3.avi \
    film4.avi film5.avi film6.avi Videos
    [student@servera ~]$ ls -l Music Pictures Videos
    Music:
    total 0
    -rw-r--r--. 1 student student 0 Mar  7 20:58 song1.mp3
    -rw-r--r--. 1 student student 0 Mar  7 20:58 song2.mp3
    -rw-r--r--. 1 student student 0 Mar  7 20:58 song3.mp3
    -rw-r--r--. 1 student student 0 Mar  7 20:58 song4.mp3
    -rw-r--r--. 1 student student 0 Mar  7 20:58 song5.mp3
    -rw-r--r--. 1 student student 0 Mar  7 20:58 song6.mp3
    
    Pictures:
    total 0
    -rw-r--r--. 1 student student 0 Mar  7 20:58 snap1.jpg
    -rw-r--r--. 1 student student 0 Mar  7 20:58 snap2.jpg
    -rw-r--r--. 1 student student 0 Mar  7 20:58 snap3.jpg
    -rw-r--r--. 1 student student 0 Mar  7 20:58 snap4.jpg
    -rw-r--r--. 1 student student 0 Mar  7 20:58 snap5.jpg
    -rw-r--r--. 1 student student 0 Mar  7 20:58 snap6.jpg
    
    Videos:
    total 0
    -rw-r--r--. 1 student student 0 Mar  7 20:58 film1.avi
    -rw-r--r--. 1 student student 0 Mar  7 20:58 film2.avi
    -rw-r--r--. 1 student student 0 Mar  7 20:58 film3.avi
    -rw-r--r--. 1 student student 0 Mar  7 20:58 film4.avi
    -rw-r--r--. 1 student student 0 Mar  7 20:58 film5.avi
    -rw-r--r--. 1 student student 0 Mar  7 20:58 film6.avi
  4. Create three subdirectories for organizing your files, and name the subdirectories friends, family, and work. Use a single command to create all three subdirectories at the same time.

    [student@servera ~]$ mkdir friends family work
    [student@servera ~]$ ls -l
    total 0
    drwxr-xr-x. 2 student student   6 Mar  7 21:01 family
    drwxr-xr-x. 2 student student   6 Mar  7 21:01 friends
    drwxr-xr-x. 2 student student 108 Mar  7 21:00 Music
    drwxr-xr-x. 2 student student 108 Mar  7 21:00 Pictures
    drwxr-xr-x. 2 student student 108 Mar  7 21:00 Videos
    drwxr-xr-x. 2 student student   6 Mar  7 21:01 work
  5. Copy files that contain numbers 1 and 2 to the friends directory, and files that contain numbers 3 and 4 to the family directory. Keep in mind that you are making copies; therefore, the original files must remain in their original locations after you complete the step.

    When you copy files from multiple locations into a single location, Red Hat recommends that you change to the destination directory before you copy the files. Use the simplest path syntax, whether absolute or relative, to reach the source for each file management task.

    1. Copy files that contain numbers 1 and 2 to the friends directory.

      [student@servera ~]$ cd friends
      [student@servera friends]$ cp ~/Music/song1.mp3 ~/Music/song2.mp3 \
      ~/Pictures/snap1.jpg ~/Pictures/snap2.jpg ~/Videos/film1.avi \
      ~/Videos/film2.avi .
      [student@servera friends]$ ls -l
      total 0
      -rw-r--r--. 1 student student 0 Mar  7 21:02 film1.avi
      -rw-r--r--. 1 student student 0 Mar  7 21:02 film2.avi
      -rw-r--r--. 1 student student 0 Mar  7 21:02 snap1.jpg
      -rw-r--r--. 1 student student 0 Mar  7 21:02 snap2.jpg
      -rw-r--r--. 1 student student 0 Mar  7 21:02 song1.mp3
      -rw-r--r--. 1 student student 0 Mar  7 21:02 song2.mp3
    2. Copy files that contain numbers 3 and 4 to the family directory.

      [student@servera friends]$ cd ../family
      [student@servera family]$ cp ~/Music/song3.mp3 ~/Music/song4.mp3 \
      ~/Pictures/snap3.jpg ~/Pictures/snap4.jpg ~/Videos/film3.avi \
      ~/Videos/film4.avi .
      [student@servera family]$ ls -l
      total 0
      total 0
      -rw-r--r--. 1 student student 0 Mar  7 21:04 film3.avi
      -rw-r--r--. 1 student student 0 Mar  7 21:04 film4.avi
      -rw-r--r--. 1 student student 0 Mar  7 21:04 snap3.jpg
      -rw-r--r--. 1 student student 0 Mar  7 21:04 snap4.jpg
      -rw-r--r--. 1 student student 0 Mar  7 21:04 song3.mp3
      -rw-r--r--. 1 student student 0 Mar  7 21:04 song4.mp3
  6. Copy the family and friends directories and their contents to the work directory.

    [student@servera family]$ cd ../work
    [student@servera work]$ cp -r ~/family ~/friends .
    [student@servera work]$ ls -l
    total 0
    drwxr-xr-x. 2 student student 108 Mar  7 21:05 family
    drwxr-xr-x. 2 student student 108 Mar  7 21:05 friends
  7. Your project tasks are now complete, and it is time to clean up the directories. Use the rm -r command to recursively delete the family, friends, and work directories and their contents.

    [student@servera work]$ cd ..
    [student@servera ~]$ rm -r family friends work
    [student@servera ~]$ ls -l
    total 0
    drwxr-xr-x. 2 student student 108 Mar  7 21:00 Music
    drwxr-xr-x. 2 student student 108 Mar  7 21:00 Pictures
    drwxr-xr-x. 2 student student 108 Mar  7 21:00 Videos
  8. Return to the workstation system as the student user.

    [student@servera ~]$ exit
    logout
    Connection to servera closed.
    [student@workstation ~]$

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish files-manage

This concludes the section.

Revision: rh124-9.0-398f302