Terminal Basics

Terminal Basics

Introduction to Terminal

This article is intended to give you the basic foundation and the most useful terminal commands to start using a terminal on your Mac OS or Linux computer.

What is Terminal?

Terminal provides a command-line interface to the operating system which you’re using, it’s a text-based interface to interact with the computer.

What is Shell?

You might also hear about shell terminology. Shell is the program (software) running on the terminal.

The default shell on Mac and on most Linux systems is Bash, on Windows it is PowerShell, and another popular shell is ZShell (ZSH) which is an alternative to Bash but is much more customizable.

No alt text provided for this image

The commands I’m about to show you work on both Mac OS and Linux.

But why should you use terminal?

The graphical user interfaces (GUI) that we’re used to like finder on a Mac, Explorer on Windows, or Dolphin on Linux make the experience easier.

No alt text provided for this image

Why do we go to the terminal instead of using the graphical interface?

There are a lot of things that you only can do from the terminal like using git commands, interacting with databases, starting up a server, executing a script, and much more.

Also once you learn the basic commands it is much faster to navigate through folders, find or remove things and run different commands.

Most useful commands

So there are some basic things that we do regularly on graphical user interfaces, which are navigating through folders, or checking in which folder we are currently, let’s look at the equivalent of that in Terminal.

Navigating

  • ls

We usually open the GUI and see the files and folders in the opened directory, we can do the same in the terminal with the ls command, this will list contents of the current directory

  • ls –l

If you add the -l flag, it will list contents of the current directory with extra details, like last modified date, user who created the file/folder, etc

  • cd

Next thing we do, is go to one of the folders in the finder window, the corresponding to this in terminal is the cd command followed by the name of the directory (cd Desktop/Sites => takes us to the Sites folder)

  • pwd

We also see the folder path where we are currently in, in terminal we can use the pwd command to print the current directory and see where we are currently (pwd => Users/admin/Desktop/Sites)

  • cd ..

To go up one level in folder hierarchy we use the cd .. (cd .. => takes us to the Desktop)

  • cd -

There is a back arrow in finder which takes us back to the last directory we were in, in terminal we use cd - to go back (cd- => takes us back to the Sites folder). This is similar to the previous command but it takes you back instead of one level up in the hierarchy instead of going back

  • cd ~

When you open the terminal you usually start from the home directory. To go back to the home directory, where we started we can cd ~ (cd ~ => takes us back to the home ~ directory)

Creating folders and files

  • mkdir

We right-click and then select “New folder” to create a new directory, that same is possible in the terminal with mkdir command followed by the folder name we are creating mkdir dir_name

  • touch

To create a file in the current directory use the touch command followed by the file name we are creatingtouch file_name

  • cp

To copy and paste a file, like we do right-click copy and then go somewhere and paste it, you can use the cp command, cp current_file_name new_file_name copies the current file into a new file

  • cp -r

To copy and paste a directory with its content, use the same cp command with -r (recursively) flag, cp –r dir_name newdir_name copies the whole directory with its content

Removing folders and files

  • rmdir

To remove a folder in GUI we right-click on it and select “Move to trash”, to do the same in terminal, use the rmdir command followed by the directory name you want to remove, rmdir mydir removes the directory called mydir. mydir must be empty

  • rmdir -rf

If the directory is not empty, you should remove it with the -rf (recursively and forced) flag, rm –rf mydir this will delete the directory mydir along with all its content without asking you for confirmation

  • rm

To remove a file we use the rm command rm myfile removes the file called myfile

  • rm -f

To remove a file without asking you for confirmation add the -f flag in front of it rm –f myfile

Moving and renaming things

  • mv move file

We also move files and folders across directories in GUI, to replicate this in terminal we use mv command mv myfile newlocdir moves myfile into the destination directory newlocdir

  • mv rename file

To rename a file use the same mv command, here the second parameter is the new file name you want to rename with mv myfile newname renames myfile to newname. If a file called newname exists, this will overwrite it.

  • mv move folder

Move a folder mv dir subdir moves the directory called dir to the directory called subdir

  • mv rename a folder

Rename a folder mv dir newdirname renames directory dir to newdirname

Terminal Shortcuts

Some useful shortcuts in terminal

  • Use the up arrow to bring the last command you typed
  • Tab to autocomplete the directory/file name
  • Use the history command to see the history logs which you typed in your terminal
  • Use the clear command to clear the terminal

Customize your terminal

Customize the terminal window

  • Consider iTerm2 instead of the default mac terminal

More customizations for Zshell users

Resources

Terminal Cheat Sheet


#terminal #shell #zshell #bash #terminalcommands

To view or add a comment, sign in

More articles by Hayk Simonyan

  • Beginner’s Guide to Prompt Engineering with ChatGPT

    Intro Prompt Engineering is one of the highest-leverage skills that you can learn in 2023. Whether you’re developing…

  • Functional Programming Simplified

    Introduction Functional Programming revolves around the principle of separating concerns, specifically the separation…

  • REST vs GraphQL

    Introduction RESTful and GraphQL APIs are two popular choices for building web APIs, each with its own strengths and…

  • React Lifecycle Methods and Their Equivalents in Functional Components

    React is the most popular JavaScript library for building user interfaces, and it provides a set of lifecycle methods…

    1 Comment
  • Deploying a NestJS app for Free on Cyclic

    Introduction In this article, we’re going to deploy a Nestjs app for free on Cyclic Cyclic is a cloud platform that…

    1 Comment
  • Master TypeScript Interviews

    Intro Are you preparing for a TypeScript interview and want to know what to expect? In this article, we'll go over the…

  • 7 Design Patterns You Should Know

    What are Design Patterns? Design patterns are repeatable solutions to commonly occurring problems in software design…

  • What is Dependency Injection?

    Dependency Injection (DI) is a programming design pattern that makes a class independent of its dependencies. It…

  • OOP Concepts Simplified

    Intro In this article, we’ll look at the core OOP concepts with real code examples, which will make it easier for you…

  • Deploying Your Website to Firebase

    Introduction In this article, we will deploy your website frontend to Google Firebase for FREE in less than 5 minutes…

Insights from the community

Others also viewed

Explore topics