- Reference 4.1 Command-Line 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 you aren’t logged in as Local Administrator, log in now.
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.
In the TextEdit menu bar, choose Format > Make Plain Text, or press Command-Shift-T.
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
From the Text Edit 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
Switch to or open Terminal.
Enter cd to change to the Documents folder.
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.
Mac-17:Documents ladmin$ cp Comps.txt MacModels.txt
Many commands that take a source and a destination list the source first.
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
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.
Enter cp to copy MacModels.txt into AppleInfo (don’t forget to try Tab key completion):
Mac-17:Documents ladmin$ cp MacModels.txt AppleInfo
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.
Remove the Comps.txt file from the Documents folder and the MacModels.txt file from the AppleInfo folder:
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.
Move the MacModels.txt file into the AppleInfo folder using the mv command:
Mac-17:Documents ladmin$ mv MacModels.txt AppleInfo
Enter cd to change your working directory to AppleInfo.
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
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. 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).
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.
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.
Enter nano to create a new file named fruit.txt:
Mac-17:Documents ladmin$ 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’ll see “Save modified buffer (ANSWERING ‘No’ WILL DESTROY CHANGES)?”
Enter Y.
You’ll see “File Name to Write: fruit.txt.”
Press Return.
nano saves your file and exits, returning you to the prompt.
Quit Terminal.