The CLI is a direct line of communication between us developers and the underlying operating system you're working on. It allows us to run commands to accomplish tasks - without needing to use any application or user interface that someone else designed.
Why Use CLI?
As a software developer, you must become comfortable using the command line interface (CLI, aka "the terminal"). The CLI (or terminal) can be a bit intimidating at first but don't worry - in no time; you'll be whipping around in there like you're the master of the universe! :)
What are CLI Commands?
Even though there is an enormous list of possible commands in the CLI, many are constantly used for basic functionality. Before entering a command, there are three important things:
- Open a terminal window.
- Hit the Enter button after writing each command below.
- Update all fields wrapped in carrots
< >.
The commands below demonstrate basic CLI usage for a Linux/Mac OS, or Git Bash in Windows.
List Contents of the Current Directory
Show every file and folder (directory) inside the current working directory.
ls
To list the contents of the current directory, including extra information, add the a' and l` flags. Here, the "a" and "l" are arguments that you're passing to the "ls" command. Arguments always follow the "-".
ls -al
Change Directories
Change your current working directory to another place on your hard drive.
cd /Users/<your\_machine\_user_name>/Desktop
For example, I am moving to my Desktop:
cd /Users/Robert/Desktop
Move Up One Directory
Moving up one directory is equivalent to moving outside the directory that you're currently inside.
cd ../
Move To Your Home Directory
Move to the home directory of the currently logged-in user.
cd ~
Move to the Root Directory of Your Machine
Move to the machine's root directory, which is the highest directory possible.
cd /
Move to the Last Directory You Were In
Move to the last directory that you were in. This is like the back button!
cd -
Print the Working Directory
Print the current working directory (to make sure you are where you think you are!).
pwd
Copy a File from One Place to Another
Copy a file from one directory to another.
cp </path/to/existing/file.txt> </path/to/new/location>
Recursively Copy a Directory from One Location to Another
Copy a directory and everything inside of it to another location. Without the r flag, the cp command will not work on a directory.
cp -r /path/to/existing/folder /path/to/new/location
Move a File from One Place to Another
Move a file's location from one place to another.
mv /path/to/existing/folder /path/to/new/location
Recursively Move a Folder from One Location to Another
Move a folder and everything inside from one place to another.
mv -r /path/to/existing/folder /path/to/new/location
See the Instruction Manual for Any Command
Have the documentation printed in the terminal for any command.
man <command>
Create a New Directory
Create a new directory with the name you provide.
mkdir <directory_name>
Delete a File
Delete a file from your machine that is in the current working directory.
rm <file_name>
Delete a File in a Different Directory
Delete a file from your machine that is in a different directory.
rm </path/to/file_name>
Delete a Directory and All Its Contents Recursively
Delete a folder and everything inside from your machine.
rm -r </path/to/directory_name>
Summary: What is CLI?
- The CLI stands for command-line interface
- The CLI is the direct form of communication with your machine
List of Basic Commands
- List the contents of the current directory
- Change directories
- Move up one directory
- Move to your home directory
- Move to the root directory of your machine
- Move to the last directory you were in
- Print the Working directory
- Copy a file from one place to another
- Recursively copy a directory from one location to another
- Move a file from one place to another
- Recursively Move a Folder from one location to another
- See the instruction manual for any command
- Create a new directory
- Delete a file
- Delete a file in a different directory
- Delete a directory and all its contents recursively
Now you know how to move around and interact with your machine without the help of an application like Finder!