Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Exercise 4.2 Manage Files and Folders with Commands

In this exercise, you learn to copy, move, rename, and delete files and folders with commands.

Create Files

  1. If you aren’t logged in as Local Administrator, log in now.

  2. Open TextEdit.

    TextEdit is in your Dock if you performed Exercise 3.4, “Download Participant Materials for Classroom Use.” If it isn’t in your Dock, you can find it in /Applications.

  3. In the TextEdit menu bar, choose Format > Make Plain Text, or press Command-Shift-T.

  4. Add the following names to the yet Untitled (default) TextEdit document:

    MacBook
    MacBook Air
    MacBook Pro
    iMac
    iMac Pro
    Mac Pro
    Mac mini
    iPhone
    iPad
  5. From the Text Edit menu bar, choose File > Save and name the document Comps.

    Leave the document in the Documents folder.

  6. Close the Comps.txt document window.

  7. Open a new document in TextEdit and change the format to Plain Text.

  8. Save and name the new document Empty. Leave the document in the Documents folder.

  9. Quit TextEdit.

Copy and Move Files and Create a Folder

  1. Switch to or open Terminal.

  2. Enter cd to change to the Documents folder.

  3. Enter ls to view the files in the Documents folder.

    When you save a plain text file from TextEdit, the program adds the filename extension “.txt” to it.

  4. Enter cp to make a copy of Comps.txt and rename it MacModels.txt.

    Mac-17:Documents ladmin$ cp Comps.txt MacModels.txt

    Many commands that take a source and a destination list the source first.

  5. Enter less to view both files.

    MacModels.txt is an exact copy of Comps.txt.

    Mac-17:Documents ladmin$ less MacModels.txt
    Mac-17:Documents ladmin$ less Comps.txt

Create a Folder and Copy a File to It

  1. Create a new folder in the Documents folder:

    Mac-17:Documents ladmin$ mkdir AppleInfo

    Because AppleInfo is a relative path, the folder is created in the Documents folder.

  2. Enter cp to copy MacModels.txt into AppleInfo (don’t forget to try Tab key completion):

    Mac-17:Documents ladmin$ cp MacModels.txt AppleInfo
  3. Enter ls to view the contents of AppleInfo:

    Mac-17:Documents ladmin$ ls AppleInfo

Fix a Naming Error

The text list in MacModels.txt includes a couple of items that are not technically Mac computers. Let’s rename the file and clean up the extra copies.

  1. Remove the Comps.txt file from the Documents folder and the MacModels.txt file from the AppleInfo folder:

    arr.jpg Mac-17:Documents ladmin$ rm Comps.txt AppleInfo/MacModels.txt

    You entered the command once to delete both files. The command line doesn’t have an undo function. Any change you make is permanent.

  2. Move the MacModels.txt file into the AppleInfo folder using the mv command:

    arr.jpg Mac-17:Documents ladmin$ mv MacModels.txt AppleInfo

  3. Enter cd to change your working directory to AppleInfo.

  4. Enter mv to rename the MacModels.txt file to AppleHardware.txt.

    Mac-17:AppleInfo ladmin$ mv MacModels.txt AppleHardware.txt

    You can move and rename a file with just mv:

    $ mv MacModels.txt AppleInfo/AppleHardware.txt

Remove a Folder

  1. Change your working directory back to the Documents folder. You can do so in one of three ways:

    arr.jpg Use the absolute path /Users/ladmin/Documents.

    arr.jpg Use the home folder shortcut ~/Documents.

    arr.jpg Use the relative path (..).

    The .. notation refers to the parent directory of the current directory. So, because your current working directory is /Users/ladmin/Documents/AppleInfo, .. refers to /Users/ladmin/Documents.

    Occasionally, you see the .. notation in the middle of a path instead of at the beginning—for example, /Users/ladmin/Documents/../Desktop. It still has the same meaning, so in this example, it refers to Local Administrator’s Desktop folder.

    Similarly, a single . refers to the current directory or location in the path.

    Each directory actually contains a reference to both itself and its parent. These are visible if you use ls -a (note the lowercase a instead of the uppercase A you used previously).

  2. Move the AppleHardware.txt file to Documents and rename it AppleHardwareInfo.txt.

    Don’t press the Return key until you enter AppleHardwareInfo.txt.

    Mac-17:Documents ladmin$ mv AppleInfo/AppleHardware.txt AppleHardwareInfo.txt

    The path AppleHardwareInfo.txt is relative to your current working directory, so this step moves AppleInfo/AppleHardware.txt to the current working directory (Documents) and renames it AppleHardwareInfo.txt.

  3. Enter rmdir to remove the AppleInfo directory:

    Mac-17:Documents ladmin$ rmdir AppleInfo

    rmdir succeeds because AppleInfo is empty. rmdir removes only folders that are empty. Enter rm –r to remove a folder that contains files:

    Mac-17:Documents ladmin$ rm -r AppleInfo

Create and Edit a Text File

macOS includes several command-line text editors. In this exercise, you use the nano editor to create and edit a file.

  1. Enter nano to create a new file named fruit.txt:

    Mac-17:Documents ladmin$ nano fruit.txt
  2. Enter the following words in the file on separate lines. Press Return at the end of each line.

    apple
    pineapple
    grapefruit
    pear
    banana
    blueberry
    strawberry
  3. Press and hold the Control key and press and release X (Control-X) to quit nano.

    You’ll see “Save modified buffer (ANSWERING ‘No’ WILL DESTROY CHANGES)?”

  4. Enter Y.

    You’ll see “File Name to Write: fruit.txt.”

  5. Press Return.

    nano saves your file and exits, returning you to the prompt.

  6. Quit Terminal.

  • + Share This
  • 🔖 Save To Your Account