Maker.io main logo

A Beginner's Guide to Linux and Shell Commands: The Popular Ones

2023-11-16 | By Tasnia Jamal

In a world driven by various types of interfaces for consumer satisfaction, the command line interface (CLI) stands as the backbone of computing. Linux, one of the most versatile operating systems, offers an abundant variety of commands that empower users to efficiently interact with their machines. Many of these commands are universally shared among other Operating System shells so even if you are using MacOS or Windows, you will be able to navigate your computer using them. Without much further ado, this guide will walk you through the fundamental Linux CLI tools you cannot miss having in your skillset.

The Terminal:

To access the inner workings of your computer, you will typically use an interface called a Shell. On MacOS, this is the Terminal application, and on Windows, this is known as PowerShell. When you first open your command interface, you will be greeted with a black screen with a small prompt for input. This is where you input your commands - they can have different parameters and will be interpreted by the shell accordingly. For beginners, you can surprisingly do a lot from here. From coding to even video editing, the only limit from here is on the command packages you have installed. However, we will only be focusing on the ones you can use without further installments.

pwd (print working directory):

Usually, your shell opens to a home directory. This is the location that contains most of your files and data. While navigating through different directories on your computer, you can check how you got there using the `pwd` command. This map may also be referred to as a path. For instance, if I am working in a folder called "MakerIO_Linux" the path to said folder will look something like this:

Copy Code
[tasniajamal@Tasnias-Air MakerIO_Linux]$ pwd
/Users/tasniajamal/Desktop/MakerIO_Linux

This indicates that the folder is in my Desktop directory, which is immediately accessible from my home directory.

ls (list):

It can be difficult to blindly navigate through folders if you don't know what's inside. Thankfully, the ls command allows you to check the contents inside your present directory.

Copy Code
[tasniajamal@Tasnias-Air MakerIO_Linux]$ ls
hello    hi    ls_example

As we can see here, I have 3 folders or files inside the MakerIO_Linux folder.

Moreover, ls can be used to peek into other folders without having to open that directory. While in MakerIO_Linux, I can see what's in the folder "hello".

Copy Code
[tasniajamal@Tasnias-Air MakerIO_Linux]$ ls hello
kitty

cd (change directory):

This command is used to move from one directory to another. To use it, the directory we want to go to must be specified. If the folder is directly adjacent to the working directory, you can just use its name. Otherwise, you may use the path to the directory.

Copy Code
[tasniajamal@Tasnias-Air MakerIO_Linux]$ cd hi
[tasniajamal@Tasnias-Air hi]$

In the example above, I went from the MakerIO_Linux folder to the folder named "hi".

Some other useful notes:

  1. The '.' character stands for the current directory, so `cd .` won't move you anywhere.
  2. '~' is equivalent to your home directory. `cd ~/` will follow a path that starts at your home directory, so a command such as `cd ~/Desktop` will work anytime, assuming you have the Desktop directory on your computer.
  3. `-` is the last directory you were in. Say you are in /home/<USER>/Documents/MyProject and move into /home/<USER>/Downloads. Well, `cd -` will move you right back!
  4. `cd` is equivalent to `cd ~`. Use it to warp back home instantly.

mkdir (make directory):

This command is used to create a new folder. To use it, we must specify the name of the folder. For example, if I were to create a new folder named 'pineapple', the command would look something like the one below. 

Copy Code
[tasniajamal@Tasnias-Air hi]$ mkdir pineapple
[tasniajamal@Tasnias-Air hi]$ cd pineapple

Note that after creating the new folder, I am still in the 'hi' directory. I have to cd into my newly created file if I were to add anything to it.

touch:

This command is similar to mkdir - however, it is used to create an empty file. I want to create a text file named pineapple so I would input:

Copy Code
[tasniajamal@Tasnias-Air pineapple]$ touch pineapple.txt
[tasniajamal@Tasnias-Air pineapple]$ ls
pineapple.txt

On using ls, we can verify that pineapple.txt has indeed been created!

echo, cat (concatenate), and wc (word count):

You might be able to guess what echo does... That's right! It echoes the text you want it to, onto the terminal interface.

Copy Code
[tasniajamal@Tasnias-Air pineapple]$ echo "Linux is great!"
Linux is great!
[tasniajamal@Tasnias-Air pineapple]$

Similarly, we can echo text into a file as well. Let's say I want to write a sentence into pineapple.txt:

Copy Code
[tasniajamal@Tasnias-Air pineapple]$ echo "Pineapples have a lot of Vitamin C." > pineapple.txt
[tasniajamal@Tasnias-Air pineapple]$

To verify that the pineapple.txt file has been modified, we can use cat. This was made to read out the contents of multiple files to you. Although, it is mostly used to peer into smaller files. At the same time, I can use wc to obtain some general info about the text inside. This will print out the number of lines, the number of words, and the number of characters respectively.

Copy Code
[tasniajamal@Tasnias-Air pineapple]$ cat pineapple.txt
Pineapples have a lot of Vitamin C.
[tasniajamal@Tasnias-Air pineapple]$ wc pineapple.txt
 1  7 37 pineapple.txt

It worked!

cp (copy) and cp -r:

Linux also allows us to make copies of files and directories. I created a new empty text file called fruit and I want to populate it with the contents of pineapple.txt. Here's what I will input:

Copy Code
[tasniajamal@Tasnias-Air pineapple]$ touch fruit.txt
[tasniajamal@Tasnias-Air pineapple]$ cp pineapple.txt fruit.txt
[tasniajamal@Tasnias-Air pineapple]$ cat fruit.txt
Pineapples have a lot of Vitamin C.

As you can see, the text in the new file is now identical to that of pineapple.txt! We can also copy directories using cp -r. If I want to move a step back and copy the whole pineapple folder to a new folder called fruit_basket, I can input the following commands.

Copy Code
[tasniajamal@Tasnias-Air pineapple]$ cd ..
[tasniajamal@Tasnias-Air hi]$ cp -r pineapple fruit_basket
[tasniajamal@Tasnias-Air hi]$ ls fruit_basket
pineapple.txt

Note that I did not use touch to create the fruit_basket directory - cp -r creates the directory itself. You may use either an existing directory to copy your directory contents to or create one on the spot.

rm (remove), rm -r and rmdir (remove directory):

Often while working with files and directories on your computer, you might want to remove (or delete) particular items. If you want to remove a file, you would use rm. Meanwhile, to remove a directory, you would use rm -r. Keep in mind that it can be risky if you accidentally delete a directory with valuable items. You might prefer to use rmdir in this case as it only deletes non-empty directories - this might serve as a warning.

Copy Code
[tasniajamal@Tasnias-Air hi]$ rmdir basket
rmdir: failed to remove 'basket': Directory not empty
[tasniajamal@Tasnias-Air hi]$ rm -r basket
[tasniajamal@Tasnias-Air hi]$ ls
pineapple

unzip:

This command is particularly helpful if you'd like to unzip a file in your current directory.

If I have a file named 'Archive.zip' in the 'hello' directory that I want to unzip, I would prompt in the following.

Copy Code
[tasniajamal@Tasnias-Air hi]$ cd ../hello
[tasniajamal@Tasnias-Air hi]$ unzip Archive.zip
[tasniajamal@Tasnias-Air hi]$ ls
princess.ipynb    peach.pdf

And finally, man (manual) and clear:

Linux has a myriad of commands that you might not always know by heart. That's why the man command can be extremely helpful at times, as it provides you with documentation for its commands. Through using 'man <command name>', the terminal will display everything you need to know about that command and its parameters. To exit, clicking the q key on your keyboard will take you back to the previous interface.

Copy Code
[tasniajamal@Tasnias-Air hi]$ man mkdir

I would observe something like this:

Copy Code
MKDIR(1)                      User Commands                     MKDIR(1)

NAME
       mkdir - make directories

SYNOPSIS
       mkdir [OPTION]... DIRECTORY...

DESCRIPTION
       Create the DIRECTORY(ies), if they do not already exist.

OPTIONS
       -m, --mode=MODE
              set file mode (as in chmod), not a=rwx - umask

       -p, --parents
              no error if existing, make parent directories as needed

       -v, --verbose
              print a message for each created directory

       -Z     set SELinux security context of each created directory
              to the default type

       --context[=CTX]
              like -Z, or if CTX is specified then set the SELinux
              or SMACK security context to CTX

       --help display this help and exit

       --version
              output version information and exit

AUTHOR
       Written by David MacKenzie and Jim Meyering.

REPORTING BUGS
       GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
       Report mkdir translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright © 2010 Free Software Foundation, Inc.  License GPLv3+:
       GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       rmdir(1)

       The  full  documentation  for  mkdir  is  maintained as a Texinfo
       manual. If the info and mkdir programs are properly installed at
       your site, the command

              info coreutils 'mkdir invocation'

       should give you access to the complete manual.

Finally, it's natural to want to clear your view a bit. Linux allows you to clear all the text output in your terminal using the clear command. This won't undo or take you back to the home directory. It is solely for tidying up.

Some other tips and shortcuts:

- tab allows the terminal to autocomplete while prompting. If there are multiple file or directory names with the same prefix, it will appear as various options. Be aware that it's case-sensitive!

- the up-arrow key lets you view your previous commands so you don't have to retype anything.

- Instead of hitting backspace or delete multiple times, you can use CTRL + U to delete everything. If you'd like to delete just a word, CTRL + W will do the trick.

- Similarly, if you want to take your cursor to the beginning of your input, you can use CTRL + A. To take it to the end of your command, click the CTRL + E keys.

Conclusion:

In this blog, we've delved into the world of Linux commands, exploring over 15 of these powerful tools. From navigating directories to manipulating files, these commands form the foundation of any Linux user's toolkit. Remember that these commands are just the tip of the iceberg. Linux is a versatile and robust operating system, offering a vast array of tools for different tasks. The ones we've covered are fundamental and will serve as a solid starting point in your Linux journey. However, you should not hesitate to look up documentation for any problem you want to achieve. Have fun Linuxing!

TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum