close icon
daily.dev platform

Discover more from daily.dev

Personalized news feed, dev communities and search, much better than what’s out there. Maybe ;)

Start reading - Free forever
Start reading - Free forever
Continue reading >

CLI Basics for Developers

CLI Basics for Developers
Author
Nimrod Kramer
Related tags on daily.dev
toc
Table of contents
arrow-down

🎯

Learn the basics of using the Command Line Interface (CLI) for developers. Understand its history, differences with GUI, navigating directories, handling files, text processing, scripting, customization, and best practices.

Mastering the Command Line Interface (CLI) is essential for developers to enhance productivity, gain deeper control and access, and understand computing systems better. This guide covers the basics of CLI, including navigating directories, handling files, text processing, and more, to help developers become proficient in using this powerful tool.

  • CLI Basics: Learn what a CLI is, its history, and the difference between CLI and GUI.
  • Understanding CLIs: Discover the role of the shell, why mastering CLI is crucial for developers, and how to access the terminal on different operating systems.
  • Basic CLI Commands: Get started with navigating directories, working with files/folders, and text processing.
  • Intermediate Concepts: Dive into scripting & automation, customization & tools, and managing dotfiles & environments.
  • Tips & Best Practices: Enhance your efficiency with keyboard shortcuts, command history, and tab completion.

Whether you're new to the command line or looking to refine your skills, this guide provides the foundational knowledge and practical tips to make the most of CLI's capabilities.

Brief History of CLIs

Back in the 1960s, before we had fancy graphics, operating systems like UNIX and DOS used CLIs. They were a direct way to control computers. Even now, CLIs are everywhere across different operating systems because they let you do a lot more with less hassle.

CLI vs GUI

CLIs are all about typing commands and reading text, while GUIs use pictures, windows, and a mouse or touch. With CLIs, you can automate tasks and have more control over your computer. GUIs, on the other hand, are easier for most people to use.

Understanding CLIs

This part explains what makes up a CLI and why it's good for developers to know how to use them.

What is a Shell?

Think of the shell as the part of your computer that listens to what you type and tells your computer what to do. It's like a translator between you and your computer's operating system. Some well-known shells are Bash (the default on Linux and macOS), Zsh, and PowerShell. Each shell has its own special features.

Here's what the shell does:

  • Waits for you to type commands
  • Figures out what you mean and tells the computer
  • Deals with complex commands, like moving data from one place to another
  • Lets you set up shortcuts and rules for your tasks
  • Can handle multiple tasks at the same time, some in the background
  • Lets you write scripts, which are like small programs for doing tasks automatically

So, the shell is your way of talking to your computer in a text-only environment. It's powerful because it lets you do things quickly, automate tasks, and customize how you work.

Why Developers Should Master CLI

Here are some big reasons developers should get good at using the command line:

Productivity

  • Make repetitive tasks automatic with scripts
  • Do complex things quickly with simple commands
  • Move and change data between programs easily
  • Find, change, and organize files and programs without a lot of clicking

Control and Access

  • Change system settings that you can't with just a mouse
  • Get direct access to the computer's files and resources
  • Build programs from their basic code
  • Find and fix problems in applications

Portable Skills

  • Use what you know on Linux, macOS, and Windows
  • Work easily on both cloud servers and your own computer
  • Useful for many programming languages and tools

Coding and Deployment

  • Fits right into how developers work
  • Tools for working with code, like Git, are used through the CLI
  • Set up and manage how software gets from development to use
  • Control how computers and servers run your software

Understanding Systems

  • Learn what's happening inside the computer
  • Understand how different parts of computing work together
  • A base for learning more advanced tech stuff

Getting good at CLI means developers can save time, be more flexible, and use advanced features no matter what computer or system they're on. It's a key skill for developers to have.

Getting Started with CLI

Accessing the Terminal

The terminal is your gateway to using the CLI. On Windows, you can use Command Prompt or PowerShell. If you're on Linux or Unix, just open your terminal app. Mac users, look for the Terminal app.

Accessing Command Prompt and Powershell on Windows

  • To open Command Prompt, hit the Windows key + R, type cmd, and hit Enter.
  • For Powershell, do the same but type powershell instead.
  • Windows Terminal is a cool tool that lets you use Command Prompt, Powershell, and WSL with extra features like tabs. You can download it from the Microsoft Store.

Using WSL on Windows

Windows Subsystem for Linux (WSL) is a neat feature that lets you run Linux right on your Windows PC.

  • Turn on WSL from the Turn Windows features on or off menu.
  • Pick a Linux version from the Microsoft Store, like Ubuntu, and install it.
  • Open it up to get a Linux terminal window right on your Windows.

Opening Terminal on macOS

  • You can find Terminal in Applications -> Utilities.
  • Or, use Spotlight search by typing Terminal.

Accessing Terminal on Linux

Most Linux setups already have a terminal app ready to go, like:

  • GNOME Terminal
  • Konsole
  • xterm

Look in your Linux's app menu. You can also get new terminal apps through your package manager.

Customizing Your Terminal

Some cool terminal programs to try are iTerm2 for macOS, Terminator for Linux, and Windows Terminal for Windows. They offer features like:

  • Splitting your screen into panes and tabs
  • Choosing your own color themes and styles
  • Setting up keyboard shortcuts
  • Creating different profiles for various projects

For instance, iTerm2 can run commands for you automatically, send you notifications, use your GPU for better performance, and let you pick from lots of color themes.

Making your terminal your own can really help make your work smoother and faster.

Basic CLI Commands

Let's talk about some simple commands you can use in the command line interface (CLI) to make working with files and folders a breeze. These commands are like shortcuts that help you move around your computer's folders, create or change files, and handle text easily.

Moving around your computer's folders is easy with these commands:

  • pwd - Shows you where you are in your computer's folder structure.
  • ls - Lists everything in your current folder. Add -l to see more details or -a to see hidden files.
  • cd - Lets you go to a different folder. You can go up one level with cd ../ or jump to a specific place like cd /home/user/.
  • pushd/popd - These commands let you quickly switch between folders.

Working with Files/Folders

Here's how to handle files and folders directly from your command line:

  • mkdir - Creates a new folder.
  • touch - Makes a new, empty file.
  • cp - Copies files or folders.
  • mv - Moves or renames files.
  • rm - Deletes files (be careful, they don't go to the recycle bin!). Use -r to delete folders.
  • cat - Shows you what's inside a file right in the terminal.
  • less - Lets you scroll through a file's contents in the terminal.

Text Processing

These commands help you work with text in files:

  • grep - Finds text that matches what you're looking for.
  • sed - Changes text based on rules you set.
  • awk - Great for dealing with text that's organized in columns.
  • sort - Puts lines of text in order, alphabetically or numerically.
  • uniq - Gets rid of repeated lines that are next to each other.
  • wc - Counts how many lines, words, and characters are in a file.

You can link commands together with a pipe (|) to do even more. For example, to count how many times a word appears in a file:

cat file.txt | grep 'keyword' | wc -l

With these basic commands, you'll be able to zip through tasks involving files, sort out text, and even write your own little programs in no time.

sbb-itb-bfaad5b

Intermediate Concepts

This part dives into some more advanced things you can do with the command line, like making your work automatic and tweaking your setup to suit you better.

Scripting & Automation

Bash and Zsh are types of command lines that let you write little programs, or scripts, to do tasks without having to manually repeat them. Here's a quick rundown:

  • You can use variables to store information, and if/else statements and loops to decide what your script does next.
  • Link commands together to tackle more complicated tasks.
  • Use cron to have your scripts run automatically at set times.
  • Example scripts could include setting up your work environment automatically, backing up files, or sending notifications for certain events.

Learning to automate can save you a lot of time.

Customization & Tools

You can make your command line work better for you in many ways:

  • Add zsh plugins for extra features like better typing suggestions.
  • Use tools like fzf to find files or commands fast.
  • Get extensions for tools you use a lot, like Git or Docker.
  • Set up command lines for specific tasks, like heroku for web apps.
  • Pick your favorite colors, fonts, and shortcuts with dotfiles.

Some key tools to know include:

  • Git: For saving and sharing your code with others.
  • cURL: For sending data over the internet.
  • npm: For managing JavaScript packages.
  • pip: For managing Python packages.
  • Docker: For running your apps in containers.

Getting to know these tools will make you a more versatile developer.

Dotfiles & Environments

Dotfiles are special files that let you set up your command line the way you like.

  • Keep your dotfiles in a place where you can easily update them, like a GitHub repository.
  • Use scripts to set up your environment quickly, especially when using a new computer.
  • You can have different settings for different computers.
  • Share your setup with your team or find others' setups online for ideas.

With dotfiles, you can make any computer feel like yours in just a few steps.

Tips & Best Practices

This part of the guide is all about making you faster and more efficient when using command lines. We'll cover shortcuts, how to use your command history, and more.

Keyboard Shortcuts

Keyboard shortcuts help you move around faster without having to type everything out. Here are some you should know:

  • CTRL+C: Stops what's currently running
  • CTRL+A: Moves your cursor to the beginning of the line
  • CTRL+E: Moves your cursor to the end of the line
  • CTRL+L: Cleans up your screen
  • CTRL+R: Lets you look for commands you've used before
  • CTRL+D: Closes the terminal window
  • CTRL+W: Deletes the word before your cursor
  • CTRL+U: Deletes everything before your cursor

Command History

You can easily find and use commands you've typed before. Here's how:

  • Use the up and down arrow keys to go through your past commands.
  • !!: Repeats your last command quickly.
  • !apt: Re-runs the last command that started with 'apt'.

Keep your go-to commands in a file named ~/.bash_history for quick access.

Tab Completion

Tab completion is a handy feature that finishes typing for you, saving you time. Just start typing and hit the tab key.

If you're not sure what to type next, hitting tab twice will show you options.

For example:

  • Typing cd /ho and pressing tab will complete it to cd /home.
  • Typing apt ins and pressing tab will change it to apt install.

You can also set up your own shortcuts with complete for an even faster workflow.

Conclusion

Using the command line is super useful for anyone who writes code. It helps you do your work better and faster. Here's why learning about command lines is a smart move:

Efficiency and Productivity

  • You can make boring, repetitive tasks run on their own with scripts.
  • It's easy to work with files and manage your computer with a few quick commands.
  • You'll spend less time clicking around and more time doing important stuff.
  • It makes the process of writing code, setting things up, and managing them smoother.

Control and Customization

  • You can tweak your computer to work just how you like it.
  • Get to the core of your computer to do things you can't do with just clicks.
  • Create your own tools and scripts for what you need.
  • Change settings that you can't change in the usual settings menu.

Portability Across Platforms

  • Use the same skills whether you're on Linux, macOS, Windows, or using cloud servers.
  • It's easier to switch between different computers without getting confused.
  • Common tools like bash, zsh, PowerShell work pretty much the same everywhere.

Understanding of Computing Fundamentals

  • Get a peek into how computers and programs work together.
  • Learn basic stuff that's useful for many kinds of coding.
  • Build a strong base for learning more complex things.

The command line isn't just for tech wizards - it's for anyone who wants to get more done with less fuss. This guide is just the start. To get even better, try making your own scripts to save time. Make your command line feel like home with custom settings. Get good at command line tools like Git, Docker, and Kubernetes for the tech we use today. Learning all this can really help you out, no matter what kind of programming you do.

What is the basic knowledge of CLI?

A command line interface (CLI) lets you use text commands to talk to your computer. Some basic things you should know include:

  • How to move around your computer's files and make new folders with commands like cd, ls, mkdir.
  • How to look at, make, copy, move, and delete files with commands like cat, nano, cp, mv, rm.
  • Using pipes (|) to connect commands together.
  • Knowing common CLI tools like bash, PowerShell, git, python, node/npm.
  • Using quick tricks like tab completion, looking back at commands you've used, and special keys to do things faster.

With these basics, you can do a lot of tasks quickly by typing commands.

Is it hard to learn CLI?

No, learning the command line isn't too hard. You mainly need to remember some commands and get the hang of the way things are done. With practice, most people can get pretty good in a few weeks. The tricky parts are:

  • Remembering different commands and what they do. But tab completion can help.
  • Understanding how files and folders are organized.
  • Knowing which tool is right for your task.
  • Finding command line versions of GUI apps you're used to.

So, starting with the basics is easy. Getting really good takes more time. Just begin with the simple stuff.

Why do developers use CLI?

Developers like using the command line for lots of reasons:

  • Automation - Putting commands together to save time.
  • Control - Getting straight to system resources.
  • Speed - Doing things quickly without a GUI.
  • Power - Doing more than what GUIs allow.
  • Portability - Using the same commands on different systems.
  • Integration - Connecting programs together.
  • Scalability - Handling servers and big projects.
  • Coding - Working with tools like git, npm, docker.

For developers, the CLI makes things faster, easier, and more flexible than using graphical apps.

What are the basics of terminal?

The terminal is a text-based way to talk to your operating system. Here are some basics:

  • Anatomy - The place where you see your command prompt, where you type your commands.
  • Navigation - Use cd to change where you are, ls to see what's in a folder.
  • Files/Folders - Use mkdir/touch to make new ones, rm to delete, mv/cp to move or copy.
  • Text Manipulation - Use cat, grep, sed, awk, sort, wc for dealing with text.
  • Customization - Change colors, shortcuts, and how your shell works.
  • Scripting - Create scripts to do tasks automatically.
  • Tools - Use CLI tools like git, curl, docker, kubectl for different tasks.
  • Help - Use man pages and --help to get instructions.

Learning these basics helps you do more with the terminal.

Related posts

Why not level up your reading with

Stay up-to-date with the latest developer news every time you open a new tab.

Read more