Common Terminal Commands
The terminal commands you'll use most (pwd, cd, ls, mkdir, touch, cp, mv and rm) for moving around and managing files and folders.
Novice · Operating System · Software Development · Updated
After this lesson you can
- Find where you are with pwd and move between folders with cd, using ~, .. and folder names
- Create, copy, move, rename and delete files and folders with mkdir, touch, cp, mv and rm
Key takeaways
- pwd prints the folder you are in. cd moves you to another one.
- cd ~ goes to your home folder, cd .. goes up one folder, and cd recipes goes into the recipes folder inside this one.
- mkdir makes a folder, touch makes an empty file, cp copies, and mv moves or renames.
- rm deletes for good. There is no Recycle Bin or Trash to get it back from.
These commands work the same in Git Bash on Windows and in the Terminal on a Mac. On Windows, run them in Git Bash, not PowerShell.
Each example shows the command to type in one box, and what it prints in the next. Copy only the command.
Where am I? pwd
pwd (print working directory) prints the full path of the folder you are in:
pwd
/c/Users/alex
That is your home folder in Git Bash. On a Mac it prints /Users/alex.
Make a practice folder: mkdir and cd
mkdir (make directory) makes a new folder. cd (change directory) moves you into it:
mkdir practice
cd practice
pwd
/c/Users/alex/practice
You are now inside practice. Every command you type runs here until you cd somewhere else.
Make files and folders: touch and ls
touch makes a new, empty file. ls lists what is here, so you can check:
mkdir recipes
touch shopping-list.txt
ls
recipes/ shopping-list.txt
It is common to run ls after a command that changes files, to see what happened. On a Mac, ls leaves out the / after recipes.
Move around: cd
| Command | Where it takes you |
|---|---|
cd recipes |
Into the recipes folder, which must be inside the folder you are in |
cd .. |
Up one folder, to the one that holds this one |
cd ../.. |
Up two folders |
cd ../recipes |
Up one folder, then into recipes |
cd ~ |
Your home folder, from anywhere |
cd ~/practice/recipes |
Straight to that folder, starting from your home folder |
.. always means "the folder above this one", and ~ always means your home folder.
Copy, move, rename and delete
| Command | What it does |
|---|---|
cp shopping-list.txt backup.txt |
Copies shopping-list.txt to a new file named backup.txt |
cp shopping-list.txt recipes/ |
Copies shopping-list.txt into the recipes folder |
mv backup.txt old-list.txt |
Renames backup.txt to old-list.txt |
mv old-list.txt recipes/ |
Moves old-list.txt into the recipes folder |
rm old-list.txt |
Deletes the file old-list.txt |
rmdir recipes |
Deletes the folder recipes, only if it is empty |
rm -r recipes |
Deletes the folder recipes and everything inside it |
mv does two jobs: give it a new name and it renames; give it a folder and it moves the file there.
rm deletes for good. It doesn't use the Recycle Bin on Windows or the Trash on a Mac, so there is no undo. Run ls first and check the name.
More ways to list: ls
| Command | What it lists |
|---|---|
ls |
The files and folders here, sorted by name |
ls -a |
All of them, including hidden ones whose names start with a period, such as .git |
ls -l |
One per line, with each one's size, date and permissions |
ls -al |
All of them, hidden ones too, one per line with the details |
Try it. In your practice folder, make a folder named music, then a file named playlist.txt inside it, without leaving practice. Hint: touch music/playlist.txt. Check with ls music, then delete the whole practice folder from your home folder with cd ~ and rm -r practice.
Check yourself
2 questions to try on the lesson page.
So what
Git, the build tools and the setup steps in this course all run in the terminal, so these few commands are how you get to your project before you run anything else.
Key terms
- pwd
- cd
- mkdir
- touch
- rm
- mv