Reference 4.3 Manipulate Files in the CLI
When you mange and edit files in the CLI, you have more options—and more chances to make mistakes.
File Examination Commands
Use the cat, less, file, and find commands to locate and examine files. Read the man pages for these commands to find out more about them.
Short for concatenate, the cat command displays a file sequentially to Terminal. The syntax is cat, followed by the path to the item you want to view. Use the cat command to append to text files using the >> redirect operator. In the following example, Joan uses the cat command to view the content of two text files in her Desktop folder, TextDocOne.txt and TextDocTwo.txt. Then she uses the cat command with the >> redirect operator to append the second text file to the end of the first text file.
MyMac:~ joan$ cat Desktop/TextDocOne.txt This is the content of the first plain text document. MyMac:~ joan$ cat Desktop/TextDocTwo.txt This is the content of the second plain text document. MyMac:~ joan$ cat Desktop/TextDocTwo.txt >> Desktop/TextDocOne.txt MyMac:~ joan$ cat Desktop/TextDocOne.txt This is the content of the first plain text document. This is the content of the second plain text document.
Use the less command to view long text files. It enables you to browse and search the text. Enter less, followed by the path to the item you want to view. The less interface is the same interface you use to view man pages, so the navigation shortcuts are the same.
The file command determines a file type based on its content. This is useful for identifying files that don’t have a filename extension. The syntax is file followed by the path to the file you’re trying to identify. In the following example, Joan uses the file command to locate the file type of two documents in her Desktop folder: PictureDocument and TextDocument:
MyMac:~ joan$ ls Desktop/ PictureDocument.tiff TextDocument.txt MyMac:~ joan$ file Desktop/PictureDocument.tiff Desktop/PictureDocument.tiff: TIFF image data, big-endian MyMac:~ joan$ file Desktop/TextDocument.txt Desktop/TextDocument.txt: ASCII English text
Use the find command to locate items based on search criteria. The find command doesn’t use Spotlight, but it does enable you to set search criteria and use filename wildcards. (Filename wildcards are covered in the next section.) The syntax is find, followed by the beginning path of the search, then an option defining your search criteria, and then the search criteria within quotation marks. In the following example, Joan uses the find command to locate picture files in her home folder by searching only for files with names that end in .tiff:
MyMac:~ joan$ find /Users/joan -name "*.tiff" /Users/joan/Desktop/PictureDocument.tiff /Users/joan/Pictures/FamilyPict.tiff /Users/joan/Pictures/MyPhoto.tiff
When you use the find command to start a search at the root of the system drive, you should also use the -x option to avoid searching the /Volumes folder.
To use Spotlight from the command line, enter the mdfind command. The syntax is mdfind followed by your search criteria.
Use Wildcard Characters
You can use wildcard characters to define pathname and search criteria. Here are three of the most common wildcards:
Use the asterisk (*) wildcard to match any string of characters. For instance, entering * matches all files, and entering *.tiff matches all files that end in .tiff.
Use the question mark (?) wildcard to match a single character. For example, entering b?ok matches book but not brook.
Use square brackets ([ ]) to define a range of characters. For example, [Dd]ocument locates items named Document or document, and doc[1-9] matches files named doc#, where # is a number between 1 and 9.
You can combine filename wildcards. Consider a collection of five files with the names ReadMe.rtf, ReadMe.txt, read.rtf, read.txt, and It’s All About Me.rtf. Using wildcards to specify these files:
*.rtf matches ReadMe.rtf, read.rtf, and It’s All About Me.rtf
????.* matches read.rtf and read.txt
[Rr]*.rtf matches ReadMe.rtf and read.rtf
[A-Z].* matches ReadMe.rtf, ReadMe.txt, and It’s All About Me.rtf
Use Recursive Commands
When you direct a command to execute a task on an item, it touches only the item you specify. If the item you specify is a folder, the command won’t navigate inside the folder to execute the command on the enclosed items. If you want a command to execute on a folder and its contents, you must tell the command to run recursively. Recursive means “Execute the task on every item inside every folder starting from the path I specify.” Many commands accept -r or -R as the option to indicate that you want the command to run recursively.
In the following example, Joan lists the contents of her Public folder normally, and then recursively using the -R option. When she lists the contents of the Public folder recursively, macOS lists the contents of the Drop Box and Drop Folder:
MyMac:~ joan$ ls Public Drop Box PublicFile1 PublicFile2 PublicFile3 MyMac:~ joan$ ls -R Public Drop Box PublicFile1 PublicFile2 PublicFile3 Public/Drop Box: Drop Folder DroppedFile1 DroppedFile2 Public/Drop Box/Drop Folder: DropFolderFile1 DropFolderFile2
Modify Files and Folders
The mkdir, touch, cp, mv, rm, rmdir, vi, and nano commands enable you to modify files and folders.
Short for “make directory,” mkdir is used to create folders. The syntax is mkdir, followed by the paths of the folders you want to create. The –p option tells mkdir to create intermediate folders if they don’t already exist in the paths you specify. In the following example, Joan uses the mkdir command with the -p option to create a folder called Private with two folders inside it called Stocks and Bonds:
MyMac:~ joan$ mkdir -p Private/Stocks Private/Bonds
Use the touch command to update the modification date of a specified item. The touch command creates an empty file if it doesn’t exist.
Use the cp (copy) command to copy items from one place to another. The syntax is cp, followed by the path to the original item, ending with the destination path for the copy. If you specify a destination folder but no filename, cp makes a copy of the file with the same name as the original. If you specify a destination filename but not a destination folder, cp makes a copy in your current working folder. Unlike the Finder, the cp command won’t warn you if your copy replaces an existing file. It deletes the existing file and replaces it with the copy you told it to create.
Use the mv (move) command to move items from one place to another. The syntax is mv, followed by the path to the original item, ending with the new destination path for the item. The mv command uses the same destination rules as the cp command.
Use the rm (remove) command to permanently delete items. There is no Trash in the CLI. The rm command removes items forever. The syntax is rm, followed by the paths of the items you wish to delete.
Use rmdir (remove directory) to permanently delete folders. The rmdir command removes folders forever. The syntax is rmdir, followed by the paths of the folders you want to delete. The rmdir command can remove a folder only if it’s empty. You can use the rm command with the recursive option, -R, to remove a folder and all its contents.
Use the vi (visual) command to edit files in the CLI. vi is one of several built-in text editors in the CLI. Enter vi and the pathname to a file to edit it. macOS High Sierra redirects vi to a newer version, vim. For basic functions you probably won’t notice the difference. Like the less command, vi takes over the Terminal window with the contents of the text file. When you first open vi, it’s in command mode and you must type predefined characters to tell vi which operation you want to complete. Use the arrow keys to browse a file in command mode. Enter the letter a to begin editing the text. In command mode, vi inserts new text wherever the cursor is. Use the arrow keys to move the cursor keys. Press the Esc (Escape) key to reenter vi command mode . After you’re in command mode, enter ZZ to save changes and quit vi.
The text editor nano features a list of commonly used keyboard shortcut commands at the bottom of the screen.