- Reference 4.1 CLI Basics
- Reference 4.2 CLI Navigation
- Reference 4.3 Manipulate Files in the CLI
- Reference 4.4 Manage macOS from the CLI
- Reference 4.5 Command-Line Tips and Tricks
- Exercise 4.1 Command-Line Navigation
- Exercise 4.2 Manage Files and Folders with Commands
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
If necessary, authenticate as Local Administrator at the prompt.
Open TextEdit.
TextEdit should be in your Dock from when you performed Exercise 3.3, “Download Participant Materials.” If it isn’t in your Dock, you can find it in /Applications.
In the TextEdit menu bar, choose Format > Make Plain Text, or press Shift-Command-T.
Add the following names to the yet Untitled (default) TextEdit document:
MacBook Air MacBook Pro iMac iMac Pro Mac Pro Mac mini iPhone iPad
From the TextEdit menu bar, choose File > Save and name the document Comps.
Leave the document in the Documents folder.
Close the Comps.txt document window.
Open a new document in TextEdit and change the format to Plain Text.
Save and name the new document Empty. Leave the document in the Documents folder.
Quit TextEdit.
Copy and Move Files and Create a Folder
In the Finder, open your Documents folder.
Open Terminal, then arrange the Finder window showing the Documents folder and the Terminal window so that you can see most of both on the screen.
You will observe how commands that you run in Terminal affect the Finder.
Enter cd ~/Documents to change to the Documents folder.
A dialog appears requesting that you allow Terminal to access files in your Documents folder. Click OK.
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.
Enter cp to make a copy of Comps.txt and rename it MacModels.txt.
ladmin@Mac-17 Documents % cp Comps.txt MacModels.txt
Many commands that take a source and a destination list the source first.
Enter less with the complete filename to view both files. Type q to exit each file.
MacModels.txt is an exact copy of Comps.txt.
ladmin@Mac-17 Documents % less MacModels.txt
Type q to exit.
Enter the same command for the file Comps.txt:
ladmin@Mac-17 Documents % Mac-17:Documents ladmin$ less Comps.txt
When you are done, type q to exit.
Create a Folder and Copy a File to It
Create a new folder in the Documents folder:
ladmin@Mac-17 Documents % mkdir AppleInfo
Because AppleInfo is a relative path, the folder is created in the Documents folder.
Enter cp to copy MacModels.txt into AppleInfo (don’t forget to try Tab key completion):
ladmin@Mac-17 Documents % cp MacModels.txt AppleInfo
Enter ls to view the contents of AppleInfo:
ladmin@Mac-17 Documents % 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.
Remove the Comps.txt file from the Documents folder and the MacModels.txt file from the AppleInfo folder:
ladmin@Mac-17 Documents % 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.
Move the MacModels.txt file into the AppleInfo folder using the mv command:
ladmin@Mac-17 Documents % mv MacModels.txt AppleInfo
Enter cd AppleInfo to change your working directory to AppleInfo.
Enter mv to rename the MacModels.txt file to AppleHardware.txt.
ladmin@Mac-17 AppleInfo % mv MacModels.txt AppleHardware.txt
You can also move and rename a file in one command:
% mv MacModels.txt AppleInfo/AppleHardware.txt
Remove a Folder
Change your working directory back to the Documents folder. You can do so in one of three ways:
Use the absolute path /Users/ladmin/Documents.
Use the home folder shortcut ~/Documents.
Use the relative path (..).
The .. notation refers to the parent directory of the current directory. 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 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).
Move the AppleHardware.txt file to Documents and rename it AppleHardwareInfo.txt.
Don’t press the Return key until you enter AppleHardwareInfo.txt.
ladmin@Mac-17 Documents % 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.
Enter rmdir to remove the AppleInfo directory:
ladmin@Mac-17 Documents % 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:
% 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.
Enter nano to create a new file named fruit.txt:
ladmin@Mac-17 Documents % nano fruit.txt
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
Press and hold the Control key and press and release X (Control-X) to quit nano.
You see “Save modified buffer (ANSWERING ‘No’ WILL DESTROY CHANGES)?”
Enter Y.
You see “File Name to Write: fruit.txt.”
Press Return.
nano saves your file in the Documents folder inside your home folder, or ~/Documents, and exits, returning you to the prompt.
Quit Terminal.