The Command Line

The command line, is a text-based interface for interacting with your computer.

Build tools, version control systems and package managers (like Gradle git and npm) are primarily driven through the command line.

While graphical interfaces simplify certain tasks, a solid command-line foundation is essential for web developers to be efficient, effective, and adaptable in their work.

Basic Commands

To open Terminal, click on the Spotlight icon (magnifying glass) in your menu bar. Type "Terminal" and press Enter.

pwd: Where am I

Type pwd and press Enter will print current working directory.

ls: List files and directories

  • To list files under working directory: ls
  • To list hidden files: ls -a

cd: Change directory

  • To move to the Desktop: cd ~/Desktop
  • To move to your home directory: cd ~
  • To move to the parent directory: cd ..
  • To move back to the previous directory: cd -

mkdir: Create a new directory

  • To create a directory named "projects": mkdir projects

rm: Remove a file

Be careful with rm, as it permanently deletes files.

  • To remove a file: rm myfile.txt
  • To remove a empty directory: rmdir mydir
  • To remove a non-empty directory: rm -rf mydir

mv: Move or rename a file or directory

  • To move a file named "file.txt" to the "Documents" directory: mv file.txt ~/Documents/
  • To rename a file named "old.txt" to "new.txt": mv old.txt new.txt

cp: Copy a file or directory

To backup a file named "file.txt": cp file.txt backup.txt

Tips and Tricks

Don't type a long command by hand, use Ctrl + R to search in the history.

There are also shortcuts for editing:

  • Ctrl + A: Moves the cursor to the beginning of the line.
  • Ctrl + E: Moves the cursor to the end of the line.
  • Ctrl + L: Clears the screen.

Stop the current command

Use Ctrl + C to interrupt the current command.

Sometimes you might need Ctrl + D to exit.

Running Multiple Tasks

Some commands are long-running tasks, such as a web server. To run another command, you typically need to open a new terminal.

Alternatively, you can use Ctrl + Z to suspend the current command. After executing the new command, you can use fg to resume the suspended command.

If you want to run multiple long-running tasks, you can use bg to send the suspended command to the background. Once it's finished, use kill to terminate it.

$ bun server.ts
[1] + 55407 suspended  bun server.ts
$ bg %1
[1] + 55407 continued  bun server.ts
$ kill %1
[1] + 55407 terminated  bun server.ts

To start a command in the background, simply append an & to the command, like this:

bun server.ts &

Another more intuitive solution is to use a terminal multiplexer, such as tmux or screen.