Introduction To Unix Commands

Introduction

Commands were the only way of interacting with computers before GUI based operating systems were introduced. Now, everyone is pretty comfortable with trusty mouse to perform series of clicks and get things done. This is indeed intuitive and welcoming for beginners. However commands and terminals are preferred ways of doing things in software development. Why you may ask? It’s easier to automate certain things such as building projects, deploying code, and monitoring servers via command executions in terminals.

In reality commands via terminals could do a lot more than what we would be discussing in this post. I enjoy executing commands written in scripts, and when executed it automates my workflow with a single command.

ex: make prodBuild is a command that I use in my Hugo Project which runs following actions in series of commands to publish a post to https://bensonshaji.dev/posts.

  • Fetch all posts from my Headless CMS ready to be published.
  • Generate Markdown files as content for Hugo.
  • Build Hugo site with production configurations.
  • Deploy the site to DigitalOcean Droplet via Rsync.
  • Additionally cleanup any junk files remaining on remote and local.

How cool is that! A single command automates multiple steps in series for easy deployments. I am pretty sure it would have taken at-least 6 separate commands and few GUI clicks to upload website files. I prefer the first option over the GUI method any day and it makes me feel like a hacker. What more do you want?

Working in a Linux terminal is the goto way for any developer who wants to get things done quickly.

In a series of post, We will be discussing the most commonly used Linux commands. Throughout this series I would be executing these commands on MacOS, However it should work on any Linux Distribution. Windows 10/11 users can download Ubuntu on WSL app from Microsoft store to learn along the way.

Working with Directories

Every operating system has the ability to organise content via files and folders. I am sure you are aware of it and know how to use it with GUI. It’s easy to click your way through to get into any directory. However, In terminal you need to use a program called cd meaning change directory.

By default when you open terminal you will be in Home directory of the current user. In MacOS it is /Users/<username>/. For other *nix distributions you could check your current working directory by running command pwd meaning print working directory. Hurray! That’s your first command ran on a terminal. It prints the current working directory.

Now let us run a few more commands with various options passed to the program.

~$ cd Documents/

Change current working directory to Documents.

~$ cd Documents/Business/

Change current working directory to a sub directory called Business inside Documents.

~$ cd ..

Go back by one directory from the current directory.

~$ cd ../../

Go back by two directories from the current directory. As you can see they can be chained multiple times with a forward slash.

~$ pwd

Prints the current working directory

Listing files in a Directory

Now that we have learnt how to navigate between directories, it is time to see how to list files in a folder. It is done by running a program ls meaning list directory contents

~$ ls

Lists all files and folder in the current directory

~$ ls -l

Lists all files and folder in the current directory, along with other information such as permissions, size, date & owner for each individual file.

~$ ls -al

Same as the ‘-l’ command, but including ‘-a’ shows files or folder that are hidden. Hidden files and folders are prefixed to a dot . ex: ‘.zshrc’, This is the default configuration file for z-shell.

~$ ls -alh

Same as the ‘-al’ command but including ‘-h’ shows all files size in human-readable format. ex: Instead of showing 3497 in size column it shows 3.4K, indicating the file is 3497 Bytes or 3.4 Kilo Bytes

$ ls /Users/<username>/Documents/Business/ 

Here we are trying to list the contents of Business from our Home folder. Notice that you don’t need cd into the above path to see its contents.

~$ ls ~/Documents/Business/

It is same as running the prev command. Here ‘~/‘ will be expanded to /Users// during execution. Output is same as the previous command.

~$ ls a*

Lists all files and folders starting with the character ‘a’. This is our first example for pattern matching. It is supported by majority of the Linux commands that you would execute through out this series.

~$ ls a*e

Lists all files beginning with letter ‘a’ and ending with letter ‘e’

~$ ls *ama*

Lists all files containing the text ‘ama’ anywhere in the filename.

~$ ls ????e

Lists all files containing five characters in the file name that ends with the letter ‘e’

~$ ls [a-g]*

Lists all files whose names begin with characters ‘a’ through ‘g’

Create directories

We now have learnt how to list files and folder in a directory, It’s time to create one in terminal. And it’s as simple as running the program ‘mkdir’ and passing a folder name to it.

~$ mkdir Finance

Creates a new folder with name Finance in the current working directory

~$ mkdir -p Finance/Family/Personal

Creates 2 sub-directories in Finance. Notice the use of ‘-p’ in the command to create intervening parent directories if they do not exist

With these commands you should be able to navigate between directories, list contents of a directory and even create one from the terminal.

In the upcoming post we will take a step further and work with files from the terminal.

Tags