Helpful Terminal Commands

Using the Terminal or Command Line will prove to be a super handy and important aspect of your software development process. Here are a few commands that will most definitely be useful for you when using the Terminal. I’m using a Mac.

Make a new folder

mkdir folder-name

Go into folder (only up 1 level)

cd folder-name

To cd into a folder with a space in the name, use the backslash character between the words

cd Software\ Development

Go backwards by one level in the folder system

cd ..

Go to your computer’s root directory

cd ~

Clear the Terminal window (just for declutter)

clear

See the contents of a folder

ls

See the hidden files in a folder

ls -a

Copy file to a different directory. This line copies the file file-name.txt to the directory/file one above the current directory/file.

cp file-name.txt ../

Rename a file

mv file-name.txt NEW-FILE-NAME.txt

Remove file from current directory

rm file-name.txt

Shows where an application lies in your file system. In this example, we are seeing the location of the Git application. This command will either return the file system location if the application exists on your machine or no output if there is no application with that name on your machine

which application-name
which git

Shows where you are currently in your file system

pwd

Basically a print statement but in the Terminal

echo hello

Make a new file and put the defined text inside of it. In this case, we put the text hello in a file called hello.txt

echo hello > hello.txt

How to Build a Node.js Project for Beginners

First, make sure you have Node installed. I’m not going to show how to do that in those tutorial. For developers that already know how to use Node, this tutorial can serve as a guide to help jog your memory whenever you forget all the steps to building a new Node.js project.

Once you have Node installed, you can write these commands in Terminal to get started:

mkdir folder-name    (create a new folder)
npm init             (initialize Node Package Manager)
touch index.js       (create server file)

For the npm init step, it will make you click through a bunch of prompts/set-up questions to set up NPM in your folder.

For your server file, it doesn’t matter what it’s name is as long as you make sure it has the same name as the one you defined in the NPM initialization questions.

In Terminal within your project directory, add these NPM packages to your project using the command npm i or npm install

npm i express         (install express - a Node web framework)
npm i -D nodemon      (install nodemon as a dev dependency)

Open up your server file (index.js) in a code editor (I prefer Visual Studio Code) and type this Node code:

const express = require('express');

const app = express();

const PORT = process.env.PORT || 4000;

app.listen(PORT, console.log(`Server started on port ${PORT}`));

The Express framework is the most popular Node backend web framework by far and it will make working with Node for the web 100x easier.

To run this and make sure it works, type this command in your Terminal (make sure your in your project directory):

nodemon index.js

The Terminal should output the string “Server started on port 4000” (or port 5000, 6000, etc. based on what you put in your code).

This means that the project is running on the specified port on your localhost.