#terminal#commands#cli

Command the Command Line: Essential Terminal Commands Every Developer Should Know

Ashwani Kumar Jha's avatar

Ashwani Kumar Jha

Hey there! 👋 Are you like me, someone who used to treat the terminal like a haunted forest, always avoiding it at all costs? Well, it's time to come clean and admit that we've been missing out on something special. In this blog, together we will learn terminal basics, one step at a time.

What is a Terminal?

The terminal, also known as the command line interface (CLI), is a software program where we can type text commands to perform specific tasks, instead of using the mouse and the icons.

Why is it necessary?

The terminal might seem intimidating at first, but it's actually a powerful tool. It allows us to perform complex tasks more efficiently than using a graphical user interface (GUI). It's like having a superpower: once we get the hang of it, we can do things faster and more precisely.

What we might be missing without it?

Without the terminal, we do tasks like copying, renaming, or moving files manually by clicking and dragging with the mouse in the GUI. It's like taking the stairs instead of the elevator; both will get us there, but one is clearly more efficient!

So in nutshell we might be missing out on a faster and more efficient way of doing things.

Where to find the Terminal on our device?

  • On MacOS, we can find it by searching 'Terminal' in Spotlight.
  • On Windows, we can access the terminal by searching for 'Command Prompt' in the start menu.

What happens when we type something?

When we type a command in the terminal and press enter, the terminal interprets the command and performs the desired task. It's like having a conversation with our computer, where we give the instructions and it follows them.

OKAY! Now let's look into some of the most basic and frequently used terminal commands.

A Humble Note: I have put together this guide based on my experiences with MacOS. While many of these commands will also work in a similar way on Windows, some might not or might require a different syntax. If you encounter a command that doesn't work on your Windows system, I recommend searching for the equivalent Windows command using the description provided for the command in the blog.

Opening a Tab and Cleaning the Terminal

Opening new tabs and cleaning the terminal help us manage our terminal workspace efficiently. Opening new tabs can help us in running multiple commands concurrently, and clearing the terminal helps us to keep our workspace clean and clutter-free.

  • cmd + t: Opens a new tab in the terminal.

  • clear or cmd + k: Clears the terminal.

Listing Files and Directories

These commands we can use to explore and navigate the file system. In a GUI, we use a file explorer or finder to do this.

  • ls: Lists all files and directories in the current directory.

  • ls -ltr: Lists files and directories with additional details. Lists files with details, sorted by modification time in reverse order.

Navigation Commands

These commands are like map and compass in the world of the terminal. They allow us to move around the file system and know where we currently are.

  • cd <directory>: Changes the current directory to the specified directory.
  • cd ~: Navigates to the home directory.

  • cd ..: Navigates up one level to the parent directory.

  • cd ../../: Navigates up two levels.

  • cd -: Returns to the previous directory.

  • pwd: Prints the full path of the current directory.

File Operations

These commands are the tools for managing files and directories. They allow us to create, copy, move, and delete files and directories. They're like the right-click menu in a GUI, but much more powerful.

  • touch <file>: Creates a new file in the current directory.

    • Example: touch newfile.txt
  • touch <file1> <file2> <file3>: Creates multiple new files in the current directory.

    • Example: touch file1.txt file2.html file3.css
  • mkdir <directory>: Creates a directory

  • mkdir -p <directory>/<subdirectory>: Creates a new directory and its subdirectories.

    • Example: mkdir -p MyDirectory/SubDirectory
    • -p: is required to create the parent directory
  • cp <source> <destination>: Copies files or directories from the source to the destination.

    • Example: cp sourcefile.txt destinationfile.txt
  • mv <source> <destination>: Moves files or directories from the source to the destination (Rename)

    • Example: mv oldname.txt newname.txt
  • rm <file>: Deletes a file. It permanently removes files.

    • Example: rm myfile.txt
  • rm -r <directory>: Delete directory with all of its content

File Viewing

These commands allow us to view the contents of a file right in the terminal, without needing to open the file in a separate application.

  • cat <file>: Displays the content of a file in the terminal.

    • Example: cat myfile.txt
  • less <file>: Allows you to peruse a file in the terminal.

    • Example: less myfile.txt
  • head <file>: Shows the first 10 lines of a file.

    • Example: head myfile.txt
  • tail <file>: Shows the last 10 lines of a file.

    • Example: tail myfile.txt

Searching

These commands are our search engine within the terminal. They allow us to find files or directories and search for specific content within a file.

  • find <directory> -name <file>: Searches for a file in a specific directory and its subdirectories.

    • Example: find . -name myfile.txt
  • grep <pattern> <file>: Searches for a pattern within a file and prints the lines where the pattern is found.

    • Example: grep 'Hello' myfile.txt

Running Applications and Commands

These commands are used to run applications, scripts, or other commands. They're like clicking on an application's icon in a GUI.

  • echo <message>: Displays a message in the terminal.

  • which <command>: Shows the location where a command is running from.

  • open <directory>: Opens the specified directory. (Provide full path)

  • say <message>: Converts text to speech.

    • Example: say Hello, World!
  • echo $PATH: Displays all the paths where the terminal looks for commands, separated by colons.

  • ./<command>: Runs a command in the current directory.

    • Example: ./myscript.sh

Network Commands

These commands are used to test and troubleshoot network connectivity.

  • ping <host>: Sends a request to the host to check if the host is reachable.

    • Example: ping www.google.com
  • whois <domain>: Provides information about a domain, such as the registrar, creation date, and more.

    • Example: whois example.com

Miscellaneous

These commands provides us a variety of information and functionality, from displaying system information to managing disk usage.

  • uname -a: Provides a wide range of system information.

  • uptime: Shows how long the system has been running.

  • man <command_name>: Displays the manual/help for a specific command.

  • du: Shows disk usage by files and directories.

  • df: Shows disk space usage.

  • ping domain_or_ip: Checks network connectivity to a domain or IP address.

  • lsof -i :<port>: Identify the process running on specified port

    • Example: lsof -i :3000
  • kill $(lsof -t -i :<port>): Terminates the running process on the specified port.

    • Example: kill $(lsof -t -i :3000)

That will be the end of this blog. I hope our little adventure into the magical world of the terminal was as exciting for you as it was for me. In last just remember that the terminal isn't as scary as a dragon... unless you type rm -rf /. So don't do that!