The command prompt in mac os x is a bash shell, so you can execute linux commands.
You might be like me - I used computers for twenty years without ever touching a command prompt. I didn't know anything about it, and it seemed scary and overwhelming. I thought it was something only really advanced users knew anything about. When I inevitably encountered a situation where I had to gain some basic command line knowledge, I discovered how useful and easy it is. Show
Learning to use the command line will open up endless possibilities for you - it is undoubtedly essential in web development and programming, but even regular users doing everyday tasks will benefit. Follow along in this tutorial and you'll see how simple it is to use, and how powerful it can be. If you've never used the command line, this article will be extremely helpful to you. If you have a basic or intermediate knowledge of the command line, you may learn some new tips and tricks.
Prerequisites
Goals
What we'll learn
What is the command line?I promise this is the most important thing to understand, and it completely blew my mind when I finally understood it. Windows, macOS - whatever operating system you're using - is simply a visual representation of your computer. This is known as a Graphical User Interface (GUI). Take away the file explorer, the desktop, the icons, and all the other graphics, and you're left with the command line. Instead of dragging and dropping, pointing and clicking, you're typing. That's the only difference. I really want to reiterate this. Your entire computer can be accessed through the command line. You can do everything through the command line. It's the same computer you already know and love. On a Mac, if I open Finder, this is my home folder. It's called
I can access the same exact
files through a web browser. If I open Google Chrome and I type in
The same is true of the command line interface. I'm going to open Terminal.app by opening Spotlight search (command + spacebar) and type in Terminal.
Just as I was "in" the Here's what I type. And here's what is written (printed) to the screen.
It wrote
And now I see exactly what I see in Finder and Chrome/a web browser! Now you should understand that you're accessing the same files and folders from the command line as you would from any program on the computer. If it doesn't quite make sense yet, just follow along and I promise it will very soon. If you think that's incredibly simple and I spent way too much time explaining it, then you're probably a little brighter than I am.
Understanding the syntaxWhen I open terminal, I see this. You'll probably see something else, but the
CommandsWe're going to learn how to do a lot of the regular things you do on a computer with a mouse or keyboard shortcuts. We're going to move between directories, create files and folders, delete them, move them, copy and paste them, and edit files. You can also press So far, we've learned three things.
Always remember to type
Moving between directoriesRight
now, I'm in my home folder. If I want to move somewhere else, I will use the Here is the output.
First, I moved to the Music folder. The terminal will understand a directory regardless of case, so I can write music or
Music. As you can see, it says That's great, but I don't really want to do anything in the Music folder. How do I go back? In the terminal, one dot ( I want to go back one directory, back into my home folder. By typing Spaces in directory and file namesIf you try to move into a directory that has a space, you may encounter an issue. For example, in my Music folder, there was a directory called Audio Music Apps. However, if I try to simply type that..
The shell thinks I'm trying to move into Audio instead of Audio Music Apps because it does not recognize the space. There are two ways to remedy this. Using QuotationsWrap any file in double quotes to preserve the spaces. EscapingType a backslash
Creating files and directoriesYou can create files and folders from the command line. Create directoryLet's create a folder for practicing named Shell with the Congratulations, you created a directory! If I type
Now you can move into the Shell directory by inputting
Create fileNow let's make a file. You can do this with the I'm not very unique with my example names, so I just called it test.html. When you input this code, it won't output anything to signify that the command was
successful. You can You can create any sort of file, but it likely only makes sense to create text based files through terminal. You can also create multiple files at the same time.
Write text to a fileWe used
Now I have a plain text file called hello.txt that contains the contents View contents of a fileI can check this by opening it from Finder, but I can also see the contents through Terminal with the
At this point, I would recommend creating some more files and directories and moving between them to get more familiarized with the commands.
Deleting files and directoriesNow hopefully you've make a big mess of files and directories in your testing folder, so we can start cleaning it up. Delete a fileUse the Note that this will permanently delete the file - it won't send to the Trash/Recycling bin.
Delete a directoryNow, let's say you create a new directory called goodbye with
No problem, we'll just delete it with And now it's gone. If you want to remove a folder that has files in it, you'll have to run a slightly different command. Just like with
Copying files and directoriesWe can also copy and paste files through the command line with the I'm in my Shell folder. Let's say I make a new directory called websites with This is the same as copying and pasting (command + C). To copy an entire directory, use the Duplicating a fileYou can also duplicate a file in the same folder. Moving files and directoriesYou can move files just as you copy them with the This is similar to cutting and pasting (command + X).
Running multiple commandsWe can run multiple commands with the double ampersand (
I just created a new file and moved it to a different directory with one command. Changing permissionsFile permissions aren't often taken into consideration when you're a casual Windows or Mac user, but if you've ever worked on a web server you likely have experience with permissions. You can change
permissions with the I've given Run as administrator
The term You can also switch to the administrator user. This is not something you should do often, but it's useful to know. At times you will need to be logged in as admin to move between restricted directories. Now my terminal looks different - it has a Since I don't really want to be logged in as admin, I'm going to Connecting to another computer or serverIf you're a web developer or designer, you'll be familiar with connecting to a web server via FTP or SFTP. You can achieve a secure connection via the command line with You connect to the server with the same information you'd use to connect via a GUI like FileZilla or Transmit. Once you've entered your password, you are now "inside" the other server. Your console will most likely look something like this: All the commands we've already learned will work on your web host as well. You can exit the server and return to your own computer with the A few useful commandsSometimes, when I'm having an existential crisis, I turn to my computer to bring me back to reality.
I often need to check the IP address of a given domain when I'm migrating a website. I can do this with the
If I forget something, I can also check the help guide for the command line with
To exit the manual pages, simply press q. Using a basic text editorYou may have heard of programs such as Vim or Emacs. These are Terminal-based text editors. Both of these programs have a bit of a learning curve. Most (if not all) Macs and Linux-based computers come with a program installed called Nano, which is a very simple text editor. I can use the First, I'll create a new file. Then I'll edit it with Now my Terminal screen will look something like this.
At the top, I can see what file I'm
editing (index.html) and at the bottom are the various commands I can do. So I'll just type something unique.
Now to save my file, I'll press control + O (the letter), and enter to confirm. I can exit Nano at this point by typing control + X, and I'll be back where I started. Now I can check the contents of index.html with
RecapHere's a recap of all the commands I went over today.
ConclusionNow that you know how to use the command line, you can do a few things like...
and plenty more. There are no more limits! Have fun. Can I execute Linux commands in Mac?After you obtain a BASH shell, you can run many of the same commands you've become accustomed to on Linux. It's also important to note that the same syntax rules that you used on Linux apply to BASH on macOS: All commands are case sensitive. Regular options (-h) and POSIX options (--help) are listed for most commands.
Is Mac terminal a bash shell?Mac OS X Shells
Mac OS X comes with the Bourne Again SHell (bash) as the default user shell and also includes the TENEX C shell (tcsh), the Korn shell (ksh), and the Z shell (zsh). bash, ksh, and zsh are compatible with sh, the original Bourne shell.
What is bash shell in Mac?As mentioned earlier, bash is Mac OS X's default Unix shell. Apple made the switch from tcsh to bash because of its support for Unicode text, something that's very important in the international market.
Does bash work in Mac?Historically, the default shell for macOS was bash. But with the introduction of macOS Catalina Apple changed the default to zsh (zshell).
|