Unays Learn ยท Tools

Command line basics for beginners

Many programming tools are used from the command line. A dozen commands are enough to move around, manage files and run your code with confidence.

Opening a terminal

On macOS open Terminal; on Linux open your terminal app; on Windows open PowerShell or the Terminal app. You type a command, press Enter, and the result appears as text. The commands below are for macOS and Linux; PowerShell accepts most of them too (ls, cd, pwd, mkdir, cat), and differences are noted.

Where am I? Moving around

pwd            # print working directory: the folder you are in
ls             # list what is in this folder
ls -l          # list with sizes and dates
cd Documents   # go into the Documents folder
cd ..          # go up one level
cd ~           # go to your home folder

Paths can be relative (from where you are, like projects/site) or absolute (from the top, like /home/arta/projects/site). Press Tab while typing a name and the terminal completes it for you.

Creating and managing files

mkdir -p school/notes
cd school
echo "Remember the homework" > notes/today.txt
cat notes/today.txt
cp notes/today.txt notes/backup.txt
mv notes/backup.txt notes/old.txt
ls notes
rm notes/old.txt
ls notes
  • mkdir -p makes a folder, including any missing parent folders.
  • > sends a command's output into a file (replacing it); >> adds to the end.
  • cat prints a file, cp copies, mv moves or renames, rm deletes.

Be careful with rm: deleted files do not go to the recycle bin. Check the folder with pwd and ls before deleting, and never run a delete command you copied from the internet without understanding it.

Searching and reading

mkdir -p demo
printf "apple\nbanana\ncherry\nbanana split\n" > demo/fruit.txt
grep banana demo/fruit.txt
wc -l demo/fruit.txt
head -n 2 demo/fruit.txt

grep shows lines that contain a word, wc -l counts lines and head shows the start of a file. In PowerShell, Select-String banana demo/fruit.txt does what grep does.

Running your programs

python3 hello.py      # run a Python file (on Windows: python hello.py)
node app.js           # run a JavaScript file with Node.js
git status            # check a Git project

Press the up arrow to bring back earlier commands, and Ctrl+C to stop a program that is still running. Next, learn to save your work with Git or build a server with Node.js.

Try it yourself

Make a folder called portfolio with subfolders css and images, create an index.html file inside it, list everything with ls -R, then rename the file to home.html. Signed-in users can also practise Bash in Unays Code's server sandbox.

This tutorial was written with AI assistance and reviewed by Unays. The runnable code examples were tested before publishing. Spotted a mistake? Tell us at [email protected].