Create powerful scripts, tools, and even apps. Open Script Editor for me. System Information. Get details about your Mac, check its warranty, and see how to free up space. Open System Information for me. Access the complete UNIX operating system in macOS. Open Terminal for me. VoiceOver Utility. Customize VoiceOver, the screen reader. Terminal may be one of the least used but most powerful apps included with a Mac. At first glance, Terminal seems to be the antithesis of the Mac's friendly GUI (Graphical User Interface), presenting instead a simple command line interface. The power of Mac. Dedicated apps for music, TV, and podcasts. Smart new features like Sidecar, powerful technologies for developers, and your favorite iPad apps, now on Mac. MacOS is the operating system that powers every Mac. It lets you do things you simply can't with other computers.
Check out Mac Terminal Commands and Apps To Work With Text Files at YouTube for closed captioning and more options.
Video Transcript: Hi, this is Gary with MacMost.com. Today let's look at working with Text Files using Command Line Functions and Apps in the Terminal.MacMost is brought to you thanks to a great group of more than 700 supporters. Go to macmost.com/patreon. There you can read more about the Patreon Campaign. Join us and get exclusive content and course discounts.So let's say you're in the Terminal like this and you want to work with some basic text files. There's a lot of different commands that you should know. First, let's create a new text file. To do so we're going to echo some text. We're going to use the Echo command and then we'll put some text in single quotes. This will simply just repeat the text. But if we use the same command, I'm going to use the up arrow to repeat the command, and I'm going to use greater than> to send the result of this Echo function to a file. So let's call this test.txt. Now I'm actually at the desktop so you should see the file appear over here. So I'll hit Return and sure enough there's the text file.Let's double click it to open it up in TextEdit and see what we've got. So there you can see it just contains that text. Now what if we wanted to view that in the Terminal. We can use the cat command. So I'll use cat space and I'll type the name. Now I don't have to type it all out because there's only one file on the desktop. If I type t and then hit tab it'll fill the rest in since there are no other files that begin with the letter t. I'll hit Return and it will show me the contents of that file. Now cat is short for concatenate. So what it actually can do is show us the results of several files, one after the other.So let's rename this test file using the Move command, mv. I'm going to make test.txt to test1.txt. Now you can see it changed names right here on the desktop. Let's create a new text file. I'm going to up arrow to go back to this and I'm going to change the text here to this and I'm going to have it save this out to test2.txt. So now you can see I've got two files, test1 and test2. If I do cat test1.txt I would get the contents of that file. If I also add to the line test2.txt it'll show me both files concatenated. I can send this to a new file. We'll call this test.txt and it creates this new file here. If I open it up in TextEdit I can see it has both there. So this is how I can merge files. I've merged two files here but I could merge many more.Now a lot of times when you want to work with text files you already have a text file. So in this case I've created a new test file that is just a list of 500 random names. So let's work with this one in the Terminal. Let's say I didn't want to see all of the lines of this file. I just want to see the first ten lines. I could use the command head and type the file name. It gives me the first ten lines. tail and the file name will give me the last ten lines. I could change the number of lines by using something like tail or head dash n and a number like 5 and the name of the file. It just gives me those five lines.Now what if we want the entire thing. I can type cat and the name but it's going to scroll up 500 lines. Instead I can use less. Less is a little app that's a viewer for you to be able to see text files. So, less test.txt and now I see all the lines but it stops there at the bottom. I can use the arrow keys to go down and up and even spacebar to jump by page. So I can navigate through this file to see what's in it.I'll use Q to quit.Now with a lot of terminal apps like this there's a ton of functionality. So you want to use the manual, man, and then the name of the command and then you get this full manual here that tells you everything you could do with the file. I could hit Return to go line by line or spacebar to jump through pages and read more about Less or any of these other commands I'm using to see what they are capable of.Let's really work with this file. We can use the sort command to sort a file so every line is sorted alphabetically. So, sort and then test.txt. It's going to just give the output right to the screen. So we get 500 lines scrolling up and you could see they are in alphabetical order. Now if we wanted to output this to a new file we could do sort, then the name of the file, then dash o, and the name of the new file. So, sorted.txt. Now we could see we get this file here which is a sorted list. You could look at the manual page for sort to find all sorts of cool things you could do with it including to be able to randomly shuffle the line. So sort and then dash and a capital R, and then test.txt. Now we get those 500 lines but in a random order. We can do it again and it's a new random order.There's also a command called wc which will give you a word count. It actually gives you more information than that. So here it tells us that there are 500 lines, 1000 words and 7056 characters. Now what if we wanted to search through this. I can use grep to do a search using Regular Expression. So let's look for the name Gary in this random list of names. You can see it comes up with one line that has the name Gary in it. Let's do the same thing but maybe for Ben. We'll see we actually have three lines, a Bentley, a Benjamin, and a Bennett. In one case it's a last name.We can use sed if we want to actually change the file. So we include a command in this. s for substitute. Let's say we want to substitute every capital T with an exclamation point. ! like that. Then we do test.txt. It will give us the results here and you could see the T's were replaced with !. You could do it for a group of characters. There's a whole bunch of other commands if you look at the manual for sed.Now you can send this to a new file but if you actually use sed with dash i then it will actually do it in place. So it would change the file right there. So that's a little dangerous but it may be what you want to do in a particular case. A more powerful command for doing even more with text files is awk. With things like grep, sed, and awk manual pages are huge. There are tons of websites that go into detail about all the different things you can do with them. But let's look at an example here. If I use awk with dash F and then quotes around a space it's going to look for spaces and it's going to breakup the file by spaces. So maybe another type of file you might want to break it up by tabs, or by commas, or semicolons. Then it's going to output just the first element. So we know every name is a first name and last name. In this case we're just going to get the first name. So here if I hit Return you can see it outputs all the first names. If I do the same thing but say give me the second element on each line it outputs all the last names. Here's a command that will do the same thing, break each line apart by spaces but it's going to output the last name and then a comma and a space and then the first name.What if you want to work with these files directly. Of course you can use TextEdit or a third party text editing app but there are two text editing apps inside of Terminal. One of them is vi. You can use this to go into editing mode here. I can use the arrow keys to move around and I can edit text. Now vi's are really complex text editor that dates back decades. The best reason to use vi is if you've already used vi before and you kind of have become an expert with it. So a lot of people that were using computers in the 60's, 70's, and even 80's are pretty good at vi. But otherwise you want to skip vi and go with a much easier little text editor, nano. Nano actually has little commands at the bottom so it helps you figure out what you're doing and it works a little bit more like a regular word processor, like TextEdit, would work. Then you can make changes to this file, save them, and exit using little control commands and work on text files without ever having to leave Terminal.So there's a starting point for using the Terminal to be able to work with text files. Certainly commands like sed, awk, and greb are good reasons to work with text files in the Terminal. There's no reason you can't combine these techniques with desktop apps. So in other words work with the file in TextEdit to edit it but maybe every once in a while use a greb or an awk command to perform special functions.Terminal User Guide
Terminal Application For Mac
Use these shortcuts to save time when using Terminal.
Work with Terminal windows and tabs
Action | Shortcut |
---|---|
New window | Command-N |
New window with same command | Control-Command-N |
New tab | Command-T |
New tab with same command | Control-Command-T |
Show or hide tab bar | Shift-Command-T |
Show all tabs or exit tab overview | Shift-Command-Backslash () |
New command | Shift-Command-N |
New remote connection | Shift-Command-K |
Show or hide Inspector | Command-I |
Edit title | Shift-Command-I |
Edit background color | Option-Command-I |
Make fonts bigger | Command-Plus (+) |
Make fonts smaller | Command-Minus (–) |
Next window | Command-Grave Accent (`) |
Previous window | Command-Shift-Tilde (~) |
Next Tab | Control-Tab |
Previous Tab | Control-Shift-Tab |
Split window into two panes | Command-D |
Close split pane | Shift-Command-D |
Close tab | Command-W |
Close window | Shift-Command-W |
Close other tabs | Option-Command-W |
Close all | Option-Shift-Command-W |
Scroll to top | Command-Home |
Scroll to bottom | Command-End |
Page up | Command-Page Up |
Page down | Command-Page Down |
Line up | Option-Command-Page Up |
Line down | Option-Command-Page Down |
Edit a command line
Action | Shortcut |
---|---|
Reposition the insertion point | Press and hold the Option key while moving the pointer to a new insertion point. |
Move the insertion point to the beginning of the line | Control-A |
Move the insertion point to the end of the line | Control-E |
Move the insertion point forward one character | Right Arrow |
Move the insertion point backward one character | Left Arrow |
Move the insertion point forward one word | Option-Right Arrow |
Move the insertion point backward one word | Option-Left Arrow |
Delete to the beginning of the line | Control-U |
Delete to the end of the line | Control-K |
Delete forward to the end of the word | Option-D (available when Use Option as Meta key is selected) |
Delete backward to the beginning of the word | Control-W |
Delete one character | Delete |
Forward-delete one character | Forward Delete (or use Fn-Delete) |
Transpose two characters | Control-T |
Terminal App For Mac
Select and find text in a Terminal window
Action | Shortcut |
---|---|
Select a complete file path | Press and hold the Shift and Command keys and double-click the path |
Select a complete line of text | Triple-click the line |
Select a word | Double-click the word |
Select a URL | Press and hold the Shift and Command keys and double-click the URL |
Select a rectangular block | Press and hold the Option key and drag to select text |
Cut | Command-X |
Copy | Command-C |
Copy without background color | Control-Shift-Command-C |
Copy plain text | Option-Shift-Command-C |
Paste | Command-V |
Paste the selection | Shift-Command-V |
Paste escaped text | Control-Command-V |
Paste escaped selection | Control-Shift-Command-V |
Find | Command-F |
Find next | Command-G |
Find previous | Command-Shift-G |
Find using the selected text | Command-E |
Jump to the selected text | Command-J |
Select all | Command-A |
Open the character viewer | Control-Command-Space |
Check out Mac Terminal Commands and Apps To Work With Text Files at YouTube for closed captioning and more options.
Video Transcript: Hi, this is Gary with MacMost.com. Today let's look at working with Text Files using Command Line Functions and Apps in the Terminal.MacMost is brought to you thanks to a great group of more than 700 supporters. Go to macmost.com/patreon. There you can read more about the Patreon Campaign. Join us and get exclusive content and course discounts.So let's say you're in the Terminal like this and you want to work with some basic text files. There's a lot of different commands that you should know. First, let's create a new text file. To do so we're going to echo some text. We're going to use the Echo command and then we'll put some text in single quotes. This will simply just repeat the text. But if we use the same command, I'm going to use the up arrow to repeat the command, and I'm going to use greater than> to send the result of this Echo function to a file. So let's call this test.txt. Now I'm actually at the desktop so you should see the file appear over here. So I'll hit Return and sure enough there's the text file.Let's double click it to open it up in TextEdit and see what we've got. So there you can see it just contains that text. Now what if we wanted to view that in the Terminal. We can use the cat command. So I'll use cat space and I'll type the name. Now I don't have to type it all out because there's only one file on the desktop. If I type t and then hit tab it'll fill the rest in since there are no other files that begin with the letter t. I'll hit Return and it will show me the contents of that file. Now cat is short for concatenate. So what it actually can do is show us the results of several files, one after the other.So let's rename this test file using the Move command, mv. I'm going to make test.txt to test1.txt. Now you can see it changed names right here on the desktop. Let's create a new text file. I'm going to up arrow to go back to this and I'm going to change the text here to this and I'm going to have it save this out to test2.txt. So now you can see I've got two files, test1 and test2. If I do cat test1.txt I would get the contents of that file. If I also add to the line test2.txt it'll show me both files concatenated. I can send this to a new file. We'll call this test.txt and it creates this new file here. If I open it up in TextEdit I can see it has both there. So this is how I can merge files. I've merged two files here but I could merge many more.Now a lot of times when you want to work with text files you already have a text file. So in this case I've created a new test file that is just a list of 500 random names. So let's work with this one in the Terminal. Let's say I didn't want to see all of the lines of this file. I just want to see the first ten lines. I could use the command head and type the file name. It gives me the first ten lines. tail and the file name will give me the last ten lines. I could change the number of lines by using something like tail or head dash n and a number like 5 and the name of the file. It just gives me those five lines.Now what if we want the entire thing. I can type cat and the name but it's going to scroll up 500 lines. Instead I can use less. Less is a little app that's a viewer for you to be able to see text files. So, less test.txt and now I see all the lines but it stops there at the bottom. I can use the arrow keys to go down and up and even spacebar to jump by page. So I can navigate through this file to see what's in it.I'll use Q to quit.Now with a lot of terminal apps like this there's a ton of functionality. So you want to use the manual, man, and then the name of the command and then you get this full manual here that tells you everything you could do with the file. I could hit Return to go line by line or spacebar to jump through pages and read more about Less or any of these other commands I'm using to see what they are capable of.Let's really work with this file. We can use the sort command to sort a file so every line is sorted alphabetically. So, sort and then test.txt. It's going to just give the output right to the screen. So we get 500 lines scrolling up and you could see they are in alphabetical order. Now if we wanted to output this to a new file we could do sort, then the name of the file, then dash o, and the name of the new file. So, sorted.txt. Now we could see we get this file here which is a sorted list. You could look at the manual page for sort to find all sorts of cool things you could do with it including to be able to randomly shuffle the line. So sort and then dash and a capital R, and then test.txt. Now we get those 500 lines but in a random order. We can do it again and it's a new random order.There's also a command called wc which will give you a word count. It actually gives you more information than that. So here it tells us that there are 500 lines, 1000 words and 7056 characters. Now what if we wanted to search through this. I can use grep to do a search using Regular Expression. So let's look for the name Gary in this random list of names. You can see it comes up with one line that has the name Gary in it. Let's do the same thing but maybe for Ben. We'll see we actually have three lines, a Bentley, a Benjamin, and a Bennett. In one case it's a last name.We can use sed if we want to actually change the file. So we include a command in this. s for substitute. Let's say we want to substitute every capital T with an exclamation point. ! like that. Then we do test.txt. It will give us the results here and you could see the T's were replaced with !. You could do it for a group of characters. There's a whole bunch of other commands if you look at the manual for sed.Now you can send this to a new file but if you actually use sed with dash i then it will actually do it in place. So it would change the file right there. So that's a little dangerous but it may be what you want to do in a particular case. A more powerful command for doing even more with text files is awk. With things like grep, sed, and awk manual pages are huge. There are tons of websites that go into detail about all the different things you can do with them. But let's look at an example here. If I use awk with dash F and then quotes around a space it's going to look for spaces and it's going to breakup the file by spaces. So maybe another type of file you might want to break it up by tabs, or by commas, or semicolons. Then it's going to output just the first element. So we know every name is a first name and last name. In this case we're just going to get the first name. So here if I hit Return you can see it outputs all the first names. If I do the same thing but say give me the second element on each line it outputs all the last names. Here's a command that will do the same thing, break each line apart by spaces but it's going to output the last name and then a comma and a space and then the first name.What if you want to work with these files directly. Of course you can use TextEdit or a third party text editing app but there are two text editing apps inside of Terminal. One of them is vi. You can use this to go into editing mode here. I can use the arrow keys to move around and I can edit text. Now vi's are really complex text editor that dates back decades. The best reason to use vi is if you've already used vi before and you kind of have become an expert with it. So a lot of people that were using computers in the 60's, 70's, and even 80's are pretty good at vi. But otherwise you want to skip vi and go with a much easier little text editor, nano. Nano actually has little commands at the bottom so it helps you figure out what you're doing and it works a little bit more like a regular word processor, like TextEdit, would work. Then you can make changes to this file, save them, and exit using little control commands and work on text files without ever having to leave Terminal.So there's a starting point for using the Terminal to be able to work with text files. Certainly commands like sed, awk, and greb are good reasons to work with text files in the Terminal. There's no reason you can't combine these techniques with desktop apps. So in other words work with the file in TextEdit to edit it but maybe every once in a while use a greb or an awk command to perform special functions.Terminal User Guide
Terminal Application For Mac
Use these shortcuts to save time when using Terminal.
Work with Terminal windows and tabs
Action | Shortcut |
---|---|
New window | Command-N |
New window with same command | Control-Command-N |
New tab | Command-T |
New tab with same command | Control-Command-T |
Show or hide tab bar | Shift-Command-T |
Show all tabs or exit tab overview | Shift-Command-Backslash () |
New command | Shift-Command-N |
New remote connection | Shift-Command-K |
Show or hide Inspector | Command-I |
Edit title | Shift-Command-I |
Edit background color | Option-Command-I |
Make fonts bigger | Command-Plus (+) |
Make fonts smaller | Command-Minus (–) |
Next window | Command-Grave Accent (`) |
Previous window | Command-Shift-Tilde (~) |
Next Tab | Control-Tab |
Previous Tab | Control-Shift-Tab |
Split window into two panes | Command-D |
Close split pane | Shift-Command-D |
Close tab | Command-W |
Close window | Shift-Command-W |
Close other tabs | Option-Command-W |
Close all | Option-Shift-Command-W |
Scroll to top | Command-Home |
Scroll to bottom | Command-End |
Page up | Command-Page Up |
Page down | Command-Page Down |
Line up | Option-Command-Page Up |
Line down | Option-Command-Page Down |
Edit a command line
Action | Shortcut |
---|---|
Reposition the insertion point | Press and hold the Option key while moving the pointer to a new insertion point. |
Move the insertion point to the beginning of the line | Control-A |
Move the insertion point to the end of the line | Control-E |
Move the insertion point forward one character | Right Arrow |
Move the insertion point backward one character | Left Arrow |
Move the insertion point forward one word | Option-Right Arrow |
Move the insertion point backward one word | Option-Left Arrow |
Delete to the beginning of the line | Control-U |
Delete to the end of the line | Control-K |
Delete forward to the end of the word | Option-D (available when Use Option as Meta key is selected) |
Delete backward to the beginning of the word | Control-W |
Delete one character | Delete |
Forward-delete one character | Forward Delete (or use Fn-Delete) |
Transpose two characters | Control-T |
Terminal App For Mac
Select and find text in a Terminal window
Action | Shortcut |
---|---|
Select a complete file path | Press and hold the Shift and Command keys and double-click the path |
Select a complete line of text | Triple-click the line |
Select a word | Double-click the word |
Select a URL | Press and hold the Shift and Command keys and double-click the URL |
Select a rectangular block | Press and hold the Option key and drag to select text |
Cut | Command-X |
Copy | Command-C |
Copy without background color | Control-Shift-Command-C |
Copy plain text | Option-Shift-Command-C |
Paste | Command-V |
Paste the selection | Shift-Command-V |
Paste escaped text | Control-Command-V |
Paste escaped selection | Control-Shift-Command-V |
Find | Command-F |
Find next | Command-G |
Find previous | Command-Shift-G |
Find using the selected text | Command-E |
Jump to the selected text | Command-J |
Select all | Command-A |
Open the character viewer | Control-Command-Space |
Work with marks and bookmarks
Action | Shortcut |
---|---|
Mark | Command-U |
Mark as bookmark | Option-Command-U |
Unmark | Shift-Command-U |
Mark line and send return | Command-Return |
Send return without marking | Shift-Command-Return |
Insert bookmark | Shift-Command-M |
Insert bookmark with name | Option-Shift-Command-M |
Jump to previous mark | Command-Up Arrow |
Jump to next mark | Command-Down Arrow |
Jump to previous bookmark | Option-Command-Up Arrow |
Jump to next bookmark | Option-Command-Down Arrow |
Clear to previous mark | Command-L |
Clear to previous bookmark | Option-Command-L |
Clear to start | Command-K |
Select between marks | Shift-Command-A |
Other shortcuts
Action | Shortcut |
---|---|
Enter or exit full screen | Control-Command-F |
Show or hide colors | Shift-Command-C |
Open Terminal preferences | Command-Comma (,) |
Break | Typing Command-Period (.) is equivalent to entering Control-C on the command line |
Command-P | |
Soft reset terminal emulator state | Option-Command-R |
Hard reset terminal emulator state | Control-Option-Command-R |
Open a URL | Hold down the Command key and double-click the URL |
Add the complete path to a file | Drag the file from the Finder into the Terminal window |
Export text as | Command-S |
Export selected text as | Shift-Command-S |
Reverse search command history | Control-R |
Toggle 'Allow Mouse Reporting' option | Command-R |
Toggle 'Use Option as Meta Key' option | Command-Option-O |
Show alternate screen | Option-Command-Page Down |
Hide alternate screen | Option-Command-Page Up |
Open man page for selection | Control-Shift-Command-Question Mark (?) |
Search man page index for selection | Control-Option-Command-Slash (/) |
Complete directory or file name | On a command line, type one or more characters, then press Tab |
Display a list of possible directory or file name completions | On a command line, type one or more characters, then press Tab twice |