The command line—often called the terminal or shell—is your powerful tool for interacting with computers at a deeper level than point-and-click. It lets you perform tasks instantly with a few keystrokes, manage files effortlessly, and even build scripts for repetitive work. Whether you’re setting up a new system, troubleshooting, or learning programming basics, mastering these core prompt techniques will save you time. No prior experience is needed; this guide covers the absolute basics you’ll use every day.
What You'll Need
- The terminal built into your operating system (macOS/Linux have it by default; Windows requires third-party software like Windows Terminal).
- A few command-line tools pre-installed (like `cd`, `ls`, `ls`—yes, it’s the same across platforms after setup).
- A short mental note of common commands (like `cd` for changing directories or `ls` for listing files).
Step-by-Step
-
Open your terminal: Start by launching it. On macOS/Linux, press
Ctrl+Alt+T(orCmd+Spaceand type "Terminal"). On Windows, use the "Windows Terminal" app. Your prompt will appear asuser@host:~$or similar, showing your username and location. If your prompt isn’t showing: Check if the terminal is running. Typewhich terminal(macOS/Linux) orwhere terminal(Windows) in a text editor to verify it’s installed. -
Navigate to your home directory: Type
cd ~. This moves you to your home folder, where you can explore without worrying about lost credentials. If you get an error, ensure the terminal is open in your home directory (e.g.,pwdshould show/home/username). -
List files and folders: Use
ls(Linux/macOS) ordir(Windows) to see what’s in your current directory. For hidden files (like shell scripts or system files), addls -a(Linux/macOS). On Windows, usedir /a. Key difference: In Linux/macOS,lsis lowercase; in Windows,DIRis uppercase. Type it exactly as shown and check for typos. -
Create and move files/folders:
- Create a new folder:
mkdir myfolder - Move to it:
cd myfolder - Create a text file:
touch myfile.txt(creates an empty file)
sudo touch myfile.txt, but this grants temporary superuser access—avoid it for beginners unless you’re sure. Instead, check if the file exists by typingls -l. - Create a new folder:
-
Basic file operations:
- View a file’s contents:
cat myfile.txt(Linux/macOS) ortype myfile.txt(Windows). - Rename a file/folder:
mv oldname.txt newname.txt. - Delete a file:
rm myfile.txt(Linux/macOS). On Windows, usedel myfile.txt.
rmpermanently deletes files—double-check the filename before running it. - View a file’s contents:
-
Create a simple file with text: Use a text editor like
nano myfile.txt(Linux/macOS) ornotepad myfile.txt(Windows). Type your text, save, and exit. In Linux/macOS, pressCtrl+Oto save andCtrl+Xto exit.
Common Mistakes
- Typo-induced errors: Commands like
ls -arequire spaces between options (e.g.,ls -a, notls-a). Always check the exact syntax for your system. - Forgetting to navigate: If you try
ls /home, the command might fail because you’re not in the correct directory. Usecdfirst or specify full paths (e.g.,ls /home/username). - Permission issues: If you can’t create files/folders, you may lack write permissions. Run
chmod +x filename(Linux/macOS) to grant execute permissions, or adjust folder settings in Windows Explorer. - Overwriting files: Use
mv oldname.txt newname.txtinstead ofnewname.txt oldname.txtto avoid accidental renames. On Windows,copy oldname.txt newname.txtdoes the same.
Tips
- Use tabs for autocompletion: Press the tab key after a command or filename to let the terminal guess and fill in the rest (e.g.,
cd myfile.txt→cd myfile.txt/if the file is a folder). - Save commands for later: Type
historyto see previously run commands, or save them to a file (e.g.,exec > commands.txtin Linux/macOS). - Shortcut keys:
- Move cursor:
Ctrl+A(beginning of line),Ctrl+E(end of line). - Delete word:
Ctrl+D(deletes word),Ctrl+K(deletes from cursor to end). - Undo:
Ctrl+UorCtrl+Zthenfg(Linux/macOS).
- Move cursor:
- Learn keyboard shortcuts: Familiarize yourself with
Ctrl+Shift+T(new tab in terminal) orAlt+F2(switch to terminal in macOS). - Check your prompt: The prompt tells you your location and user. If it’s
user@host:/home/username, you’re in your home folder. Usepwdto confirm.
Troubleshooting
-
Prompt doesn’t appear:
- Restart your computer or terminal window.
- Check if the terminal app is running (look for a mini-window or icon in the taskbar).
- If it’s missing entirely, reinstall the terminal software (e.g.,
Terminal.appon macOS, or installWindows Terminalfrom Microsoft Store).
-
Commands aren’t recognized:
- Ensure the command exists in your system. Use
man ls(Linux/macOS) to read the manual forls. - If you’re on Windows, some Linux commands will fail. Stick to Windows-specific commands like
dirorcls. - Update your system (macOS:
Software Update; Linux: `sudo apt update && sudo apt upgrade`; Windows: Windows Update).
- Ensure the command exists in your system. Use
-
Permission denied:
- Check if you have write access to the folder. Use
ls -ld directoryto see folder permissions. - On Linux/macOS, run
chmod +w directoryto grant write permissions (temporarily). - Avoid
sudounless necessary—it can expose your system to security risks.
- Check if you have write access to the folder. Use
-
Files deleted unexpectedly:
- Check your recycle bin (Windows) or trash (Linux/macOS) first.
- If the file was
rm-ed, restore from backups or usefind(Linux/macOS) to locate deleted files. - On Windows, use command-line tools like
attrib -R -H -S filename(remove read-only attributes) before recreating the file.
Frequently Asked Questions
How do I switch between terminals and text editors?
On macOS/Linux, Ctrl+T opens a new terminal tab while staying in the current shell. For text editing, nano or vim open directly in the terminal. On Windows, use notepad in the terminal for basic editing or open the terminal in a separate window. Always save files with the correct extension (e.g., .txt).
Can I use the command line on Windows without installing anything?
No, Windows doesn’t include a terminal by default. Install Windows Terminal from the Microsoft Store for a modern experience. For basic commands, Command Prompt (cmd) or PowerShell work, but they lack some Linux/macOS features like ls and mv.
Why does my terminal say "Permission denied" when I try to create files?
This happens when your user account lacks write permissions in the current folder. Move to a folder with permissions (e.g., your home directory) or ask your system administrator to adjust settings. Avoid sudo until you understand its risks.
How do I save my terminal history for future use?
Type history to see past commands. To save them to a file, use exec > commands.txt (Linux/macOS) or redirect output in Windows Terminal. For more control, edit .bash_history (Linux/macOS) or similar files in your home directory.
What’s the difference between ls and dir?
ls is a Linux/macOS command that lists files, while dir is Windows’ equivalent. Both serve the same purpose, but their syntax differs slightly. Always use the command native to your OS.
How do I check if I’m in the correct directory?
Type pwd (print working directory) in Linux/macOS to see your current path, or echo %cd% in Windows Command Prompt to confirm your location.