Reference 4.3 Manipulate Files in the CLI
When you manage and edit files in the CLI, you have more options—and more chances to make mistakes.
File-Examination Commands
Use the cat, less, and file commands to examine files. Read the man pages for these commands to find out more about them.
Short for concatenate, the cat command displays the contents of 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.
joan@MyMac ~ % cat Desktop/TextDocOne.txt This is the content of the first plain text document. joan@MyMac ~ % cat Desktop/TextDocTwo.txt This is the content of the second plain text document. joan@MyMac ~ % cat Desktop/TextDocTwo.txt >> Desktop/TextDocOne.txt joan@MyMac ~ % 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:
joan@MyMac ~ % ls Desktop/ PictureDocument.tiff TextDocument.txt joan@MyMac ~ % file Desktop/PictureDocument.tiff Desktop/PictureDocument.tiff: TIFF image data, big-endian joan@MyMac ~ % file Desktop/TextDocument.txt Desktop/TextDocument.txt: ASCII English text
To use Spotlight from the command line, enter the mdfind command. The syntax is mdfind, followed by your search criteria. Like Spotlight, mdfind returns only items that the current user has permissions to access. Read Reference 16.2, “Spotlight and Siri,” for more information.
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.
Modify Files and Folders
The mkdir, touch, cp, mv, rm, rmdir, 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:
joan@MyMac ~ % 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.
The text editor nano features a list of commonly used keyboard shortcut commands at the bottom of the screen.