How to Use Command Line (10 Commands) Windows & Mac

1. Alias

With the command line, you can create an alias for a command.

As an example, let’s create an alias surf that opens up medium.com. To open up medium.com via the command line you need to use open [URL] syntax:

open https://www.medium.com

To create an alias surf for this command:

  1. Open up your .bash_profile:
open ~/.bash_profile

2. Create a new alias surf by adding the following into the .bash_profile:

alias surf='open https://www.medium.com'

3. Save the file and restart the command line window.

4. Now type in surf and medium.com opens up in the default browser.

2. History

View your command line’s command history by typing history in the command line:

history

3. Find Commands

This is the one I use a lot. It is a time saver when there is a complex command you’ve run some time ago and you cannot remember/find it.

To find earlier commands from your command line’s history:

  1. Open up a command line window.
  2. Type Ctrl-R.
  3. Start typing (e.g. letters you remember from the command you’re searching for).
  4. As you type, the command line keeps suggesting a command that matches what you’ve typed. To execute the suggested command, hit enter.

4. Show Hidden Content

To show hidden files and folders, type these two commands:

defaults write com.apple.finder AppleShowAllFiles -bool TRUE
killall Finder

Be careful with the hidden files. There is likely a reason why they are hidden by default. Do not delete anything you are not sure you want to delete.

To hide these files back, run the above commands by replacing TRUE with FALSE.

5. Move Files

Use ditto to copy and paste files from a folder to another.

The syntax is:

ditto [original_folder][new_folder]

For example, if you have a folder called folder1 and you want to create a new folder, folder2, and copy-paste the contents of folder1 there, just type:

ditto folder1 folder2

6. Download Files

If you don’t want to use the browser to download a file, the command line can help.

Before downloading with command line, notice that the downloaded file will end up in the directory where you currently are. So if you want it to end up in the Downloads, change directory there before downloading:

cd ~/Downloads/

To download the file, use cURL -O [URL_OF_THE_FILE].

For example:

curl -O https://www.medium.com/example_download.txt

7. Keep Mac Awake

To disable your Mac from falling asleep, just type:

caffeinate

To stop caffeinating, just type Ctrl-C.

To caffeinate for a period of time use caffeinate -u -t [num_seconds]. For example, let’s caffeinate for 10 minutes (600 seconds):

caffeinate -u -t 600

(Naturally, this can be escaped with Ctrl-C too.)

8. View Contents without Opening a File

Use cat [path_to_file] to view the contents of a file without opening it.

For example, say you have a python file example.py in your current directory and you want to check its contents with the command line.

To achieve this, just type:

cat example.py

9. Make Your Mac Speak

Use say [what_to_say] command to make your Mac speak.

For example, to make your Mac say Hello, World!, run the following command:

say Hello, world!

10. Repeat Characters by Holding Down a Key

Holding a key to repeat characters is something you might be familiar with. By default, this is disabled in a Mac. Luckily, there is a simple way to enable it:

defaults write -g ApplePressAndHoldEnabled -bool FALSE

To revert this change, just run the command above and replace FALSE with TRUE.

11. Hide Content

To hide a particular secret file or a folder, use this command:

chflags hidden [path_of_the_folder]

And to make it visible again, use:

chflags nohidden [path_of_the_folder]

For example, given you have a folder called important on your desktop, you can hide it by:

chflags hidden ~/Desktop/important

Keep in mind this doesn’t hide the files completely. For example, you can still run ls to see the hidden content in the command line.

12. Command Line ASCII Art

To display ASCII art text, use this syntax:

banner -w [banner_width][banner_text]

As an example, let’s print "Hello" as below:

banner -w 50 Hello

13. Check for macOS Updates

To change the frequency with which your Mac checks for macOS updates from weekly to daily, type:

defaults write com.apple.SoftwareUpdate ScheduleFrequency -int 1

15. List Contents of a Folder and Its Subfolders

The ls command lists the contents of a directory. But did you know you can add a -R to make it list the contents of the subfolders too?

ls -R [path_to_folder]

For instance, if there is a folder called example in my current directory, I can list all the contents of it with:

ls -R example
A command line list representation of contents of a folder

Thanks for reading! I hope you enjoyed it!