The command line isn't just for wise Linux beards. It's actually an awesome tool with almost limitless functionality. Here's a primer on how it works, and how you can do almost anything with it.
A major attraction of Mac OS is its underlying UNIX kernel. Developers love UNIX and its array of tools for development and testing. In Mac OS X to access these tools through terminal, you need to install Command Line Tools. Mac OSX Server Command-Line Administration. Notes About Specific Commands and Tools 19 serversetup 19 serveradmin Chapter 2 21 Installing Server Software and Finishing Basic Setup 21 Installing Server Software 21 Automating Server Setup 21 Creating a Configuration File Template 22. Install the AWS Command Line Interface on macOS. The recommended way of installing the AWS CLI on macOS is to use the bundle installer. The bundled installer includes all dependencies and can be used offline.
Note: This article is meant for people who are either new to the command line or only have a couple of command-line tricks up their sleeve. If you're already conversant with most basic commands, you can send this article to others that still aren't up to your skill level and spread the good word about how great the command line really is.
Advertisement
What Is the Command Line?
The command-line interface, sometimes referred to as the CLI, is a tool into which you can type text commands to perform specific tasks—in contrast to the mouse's pointing and clicking on menus and buttons. Since you can directly control the computer by typing, many tasks can be performed more quickly, and some tasks can be automated with special commands that loop through and perform the same action on many files—saving you, potentially, loads of time in the process.
Advertisement
The application or user interface that accepts your typed responses and displays the data on the screen is called a shell, and there are many different varieties that you can choose from, but the most common these days is the Bash shell, which is the default on Linux and Mac systems in the Terminal application.
By default, Windows systems only include the anemic Command Prompt application, which has nowhere near the power of Bash, so for the purposes of this article we're going to suggest you use the open source Cygwin tool as your Windows command line, since it's quite a bit more powerful. You'll also at some point want to read parts one, two, and three of our series on using the Bash shell under Cygwin—the tips apply to Linux and OS X as well.
Advertisement
Geek to Live: Introduction to Cygwin, Part I
Introduction to who? If that's what you thought when you saw the title of this article, then…
Read more ReadBasic Command Line Usage
To get started with the command line, you'll need to open up a terminal window and get ready to start typing commands. Here's a list of basic commands you can use, organized by the type of activity that you might want to perform.
Advertisement
When you run your terminal application (Cygwin on Windows, Terminal on Mac and Linux), your command prompt will start up pointing to a specific folder on your hard drive. You can navigate between folders, act on files inside those folders, or perform other actions.
List Files
First, let's display a list of files inside the active folder. For this task, you'll need to use the ls command. You can pass a number of parameters to the command to display extra details or change the sorting. For instance, if I add -l
to the end of my ls
command, I'll see a detailed listing; -t
will sort the results by file time; -S
will sort by file size; and -r
will reverse the sorting. You could use a combination of these together, like this command, which will show all files sorted by file size with the largest files at the bottom:
ls -lSr
If you use the –a option, you can see hidden files, and you'll also notice something else in the listing: there are two entries for '.' and '..' at the beginning of the list. These represent the current folder—the '.' folder—and the parent folder—the '..' folder.
Advertisement
Change Directories
You can change between directories using the cd
command, and using what we just learned about the '..' folder, you can use the following command to switch to the directory directly above the current one:
cd ..
You can navigate to either full or relative paths. For example, the command above navigates to a relative path—one above the current folder. If you're in /path/to/
and you want to navigate to the folder stuff
inside that folder, you can simply type:
cd stuff
You can also navigate to absolute paths. Again, if I were in /path/to/
and I want to navigate to /another/path/
, I would simply type:
cd /another/path
To swap directories to the previous working directory, the '-' (hyphen) shortcut is handy to have on hand. For example, if you were in the /first/folder/path/
directory and then switched over to /etc/
to make a configuration change, you might not want to type in the full path to switch back. You can quickly switch back to the previous working directory with this command:
cd -
Creating or Removing Folders
To create a new folder, you can simply use the mkdir <foldername>
command. You can then remove any folder with the rmdir <foldername>
command—as long as the folder is empty. If there are files in the folder, you'll have to delete those files before you can remove the folder.
Advertisement
Creating and Removing Files
You can use the touch <filename>
command to create a new, blank file, and then use the rm <filename>
command to delete files:
rm filename
You can quickly remove all files in a directory by using the '*' (asterisk) wildcard—another simple tool that will come in very handy during your time in the command line. For example, if you're in a folder and want to delete every file inside that folder, just type:
rm *
If you want to delete a list of files and folders, including all files from subdirectories, without prompting you for every single entry, you can use the -r option for recursive, and the -f option for force. This command will wipe out every instance of a matching filename pattern (note the slightly different use of the wildcard) from the current directory and below:
rm –rf filename.*
Edit Plain Text Files
The command that you use to edit text files will be different based on the platform you're using and the application you prefer to use. If you're using Ubuntu Linux, you can use the nano editor to quickly edit files, which might be more suitable for beginners.
nano /path/to/file
Otherwise, the vim editor is available on just about any system and can be invoked with the vi <filename> syntax.
Advertisement
Displaying Files
You can display the file contents directly on the screen with the cat
command, but the results will probably go flying past you on most large files, so it's usually better to use the more
or less
commands. For instance:
more <filename>
This will display the contents of a file on the screen, and prompt you to scroll through the file a screen at a time.
Advertisement
Command Redirection
Each command line application can accept standard input and writes to standard output, and you can use the >
or |
operators to redirect output from one command into another, which lets you chain commands together into much more powerful commands.
For instance, if you want to use ls –l to display a list of files but it keeps scrolling off the screen, you can pipe the output from the ls –l command into the input of the more
command by using the | character:
ls –l | more
If you wanted to save the output of that list directly into a file instead of displaying on the console, you could use the >
operator to redirect the output into a file instead:
ls -l > filename.list
You could then use the cat
command to display the contents of that file, pipe that into the grep
command (detailed further below), and then redirect that output into a separate file:
cat filename.list | grep keyword > filefound.list
Running a Script in the Current Folder
If you have an application or shell script in the current folder, you can't simply type the name of the command and expect it to start. You'll need to add a ./ to the beginning of the command in order to start it. Why? Because in the Bash shell, the current directory, or '.' folder, is not included in the system path. So to launch scriptname.sh in the current folder, you'll need to use:
./scriptname.sh
Using History
You can use the history command to show a list of all the recently used commands, or the up/down arrows to loop through them. The Ctrl+R shortcut key will start a search mode where you can type the first few characters of a command to search through your recent history.
Advertisement
Looping Over a Set of Files
If you want to loop through a set of filenames and perform an action on each one, you can use the for command to loop through a set of files. For instance, to loop through all the .txt files in the current directory and display them on the console, you could use:
for f in *.txt;do echo $f;done
Find Files
You can use the very powerful find
command to search for files on your system. For instance, if you wanted to find all files with .txt in the name that were modified in the last 5 days, you would use this command:
Find Anything From the Terminal
Oldie but goodie terminal command, find, can be used to locate literally any file on your system.…
Read more Readfind . –name '*.txt' –mtime 5
Find a Text String in Files
The grep
command can be used to quickly find text within files, even searching through subdirectories. For instance, if you wanted to search through all files in the current directory and below it for 'text string', you could use this command:
grep –ir 'text string' *
Batch Rename Files
You can use the rename command to quickly rename files using a regular expression pattern. For instance, if you wanted to rename all files containing foo to contain bar instead, you could use a command like this one:
rename –v 's/foo/bar/g' *
Using Bash Shortcut Keys
There are a number of very useful shortcut keys you can use in the bash shell, and it pays to master them all. Here's a couple to get you started:
- Ctrl + U: Clears the line from the cursor point back to the beginning.
- Ctrl + A: Moves the cursor to the beginning of the line.
- Ctrl + E: Moves the cursor to the end of the line.
- Ctrl + R: Allows you to search through the previous commands.
Command For Console
Customizing Your Command Shell
Advertisement
There's no need to do your work in a boring terminal when you can do all sorts of tricks to customize it, like changing the colors, fonts, and adding aliases to complicated commands to save yourself time.
You'll want to start off by reading our guide to customizing the command prompt, which will show you how to change the colors and add them to your profile so they show up when you launch a new shell.
Advertisement
Ask Lifehacker: How do I customize my command line prompt?
Dear Lifehacker,
Read more ReadUsing Aliases
Aliases save you loads of time by shortening long, complicated commands down into really simple ones, or by setting the default parameters to a command so you don't have to type them every time. For instance, if you wanted to set up an alias for installing packages on your Ubuntu setup that's quicker and simpler than sudo apt-get install packagename, you could use something like this:
alias agi='sudo apt-get install'
This alias would make it so you could simply type agi packagename
at the shell to install any package in fewer keystrokes. You can also use aliases to set the default arguments for a command, so if you always wanted ls to actually do ls –l, you could use this alias:
alias ls='ls –l'
There are any number of useful aliases that you can use to personalize your setup, but if you're having trouble coming up with good ideas, check out the list of the ten most useful aliases.
Advertisement
Ten Handy Bash Aliases for Linux Users
TechRepublic's 10 Things blog posts 10 shortcut ideas for Linux users (and Terminal-friendly…
Read more ReadControl Your System from the Shell
Advertisement
The terminal has a rich set of tools for manipulating processes and checking on system stats. You can use the ps
command to see a list of system processes like this:
ps aux
You can then use the kill <pid>
command to get rid of any processes that you want to kill. You can also use top
to easily kill processes from a more graphical list of running processes by simply using the K key.
Advertisement
Control Your System with the Top Command
On Linux and Mac systems, the top terminal command gives you a great bird's eye view of what…
Command For Console Tool Machine
Read more ReadThese examples not enough for you? Check out our top 10 command-line tools, our list of useful commands for Mac users, our guide to turbocharging your terminal, or the list of ten handy bash aliases.
Advertisement
What are some of your favorite command-line tricks? Share your favorite tips, tricks, and advice in the comments.
The How-To Geek spends 47% of his time at a bash prompt. His geeky articles can be found daily here on Lifehacker, How-To Geek, and Twitter.
Advertisement
Mastering a few crucial Mac keyboard shortcuts will make using your Apple computer easier and much more efficient. Cutting your reliance on your mouse will help you work more quickly, and you’ll undoubtedly impress your family, friends and co-workers to no end. You might even end up becoming the go-to Mac person in your office, and we all know how wonderful that will be.
Here are the top 10 Mac keyboard shortcut tricks you really need to memorize right now, whether you’re a Mac newbie or a veteran user who still uses the mouse for everything out of habit.
Top 10 Mac keyboard shortcuts
Ingredients:
- Any Mac running OS X
- Mac-compatible keyboard (has a Command key, not Windows)
Directions:
First up, take a look at the Mac keyboard in front of you to familiarize yourself with a few Mac-specific keys. The Command key has a special symbol (⌘) to help you recognize it, while the Option key can also say “alt” on it, a term borrowed from a Windows environment. Your keyboard may also have a Function key (fn) next to the Control key (which just bears its own name — “control”).
Many of these shortcuts have an equivalent menu item you’ll find at the top of your Mac’s screen. One way of finding new shortcuts is to look to the right of any menu item and see if a keyboard shortcut is listed.
Now that you’ve got a good idea of where to find these keys, let’s take a look at some great ways to use them. (In addition to writing out the instructions, we’ve also created a pair of videos to walk you through them in case you prefer to learn that way. You’ll find them at the bottom of this post.)
Find the best, fastest web hosting
Looking for the best web hosting? Read the reviews first to choose a top 10 host. Whats The Host helps you choose a high-quality, reliable host so you won’t have to.
Quit any Mac program
Command-Q: If you’re coming from a Windows computer, you might have gotten used to “X-ing out” of your applications by clicking on the X button at the top of any application window. In OS X, you close windows with the red X button (in the upper left of your window), but it will not quit the app. To fully exit out of any Mac program in OS X, you’ll need to use the Quit command with this shortcut, or click on the app menu, then choose Quit.
Close Mac windows quickly
Command-W, Option-Command-W: The first of these will close whatever active window you are using, while the second one will close all the windows in the currently active app (or Finder, which is also an app, really). These shortcuts will do the same thing as the Close Window option in the Finder and most other apps. Chrome, for example, delineates between Close Window (Command-W) and Close Tab (Command-Shift-W).
Open a new web browser tab on Mac
Command-T: Whether you’re in a web browser like Safari or Chrome or in the Finder itself, this keyboard shortcut will open a new tab for you. In Chrome, Command-Shift-T will open the most recently closed tab for you. Keep hitting this shortcut to open multiple tabs (or continue opening tabs in reverse chronological order in Chrome).
Quickly switch between Mac applications
Command-Tab, Command-~ : The first of these shortcuts will activate Mac OS X’s built-in application switcher, which will let you switch between active apps running on your Mac. Keep holding down the Command key and press Tab repeatedly to go to the next app from left to right. Use Command-~ (tilde, usually above the Tab key) to switch to running apps from right to left. Holding down the Command key and hitting Q will quit whatever program you are currently highlighting.
Cut, copy and paste on Mac
Command-X, Command-C, Command-V: These are three of the things I do most often in my writing life, so mousing up to the Edit menu in an app to choose these functions from a menu makes me cringe. Learn these three essential shortcuts (Command-X for cut, Command-C for copy and Command-V for paste — go figure), and you’ll save a ton of time every day.
Find something fast on your Mac
Command-F: Search is a massive part of any computer user’s workflow, from finding the right document to looking for a key word or phrase in Safari. To find something in the Finder, Safari or Chrome, or in a Pages or Word document, simply hit the Command-F key combination and a little window will show up where you can type in your search terms. Boom — you’ll find what you need.
Command For Console Tool Mac Address
Take Mac screenshots
Command-Shift-3, Command-Shift-4: Screenshots are a way of life in my daily work, and I’m willing to bet you’ve needed to take a quick capture of your screen at some point. Command-Shift-3 will take a picture of your entire Mac’s screen, from the upper left to the bottom right. Command-Shift-4 will turn your mouse cursor into a set of crosshairs (not unlike a sniper rifle sight) that you can then click and drag around any portion of your screen to capture only the relevant area. Pro tip: Tap the spacebar once to take a screenshot of a specific window, or hold the spacebar to move the selected area around without changing its dimensions.
Open Mac Finder folders
Command-Shift-A, Command-Shift-U, Command-Shift-D, Command-Shift-H: In the Finder, you’ll need to navigate to any number of common folders: Applications, Utilities, Desktop, and Home. Simply hit the Command key and then the first letter of each of these to go directly to them: Do not pass Go, do not collect $200.
Force quit a Mac app
Command-Option-Esc: If an app stops responding, you might need to force it to quit. You can do that with a right-click on the app icon in the Dock, but it’s even easier if you hit this keyboard shortcut. This will bring up the Force Quit dialog, which you can then use to kill that unresponsive app. You might need to Command-Tab your way out of an active frozen app first, or use Command + Shift + Option + Esc to quit the currently active app.
Hide Mac apps
Command-H, Command-Option-H: Doing something at work you shouldn’t be when your boss walks by? Whoops! It’s an easy fix to hit Command-H on your keyboard to hide the current active app. If you just need to declutter your view, Command-Option-H will hide all the other apps in the background, letting you focus on the one in front.
See top Mac keyboard shortcuts in action
Command For Console Tool Macbook
The Cult of Mac how-to videos below will walk you through these shortcuts if you prefer to watch rather than read. We’ve broken up the top 10 into two easy parts. Here are the first five Mac keyboard shortcuts …
Mac Console Application
And here are five more: