Linux Commands: The Basics I

Linux Commands: The Basics I

·

11 min read

Featured on Hashnode

When you start your coding journey it is essential to learn how to interact with the Operating System (OS) of your computer. If you are an Ubuntu user or Mac OS it means you're in luck, your OS is Unix/Linux which is quite similar because Linux is an operating system's kernel cloned from Unix.

If you are a Windows user, you would have to download and install Windows Subsystem for Linux (WSL). This is a compatibility layer for running Linux binary executables natively on Windows. Alternatively, you could host a Linux OS on a Virtual software application like Virtual box on your windows device.

Here's a link to install WSL

Terminologies

Let's go through some important terminologies:

Shell

So, basically, a shell is a program that receives commands from the user and gives it to the OS to process, and it shows the output. Linux's shell is its main part. Its distros come in GUI (graphical user interface), but basically, Linux has a CLI (command-line interface).

Directory

A directory is a location for storing files, this is commonly called a folder. Therefore, a directory housing other file(s) and directories would be called a parent directory.

Paths

This is the complete location name of where a computer file, directory or webpage is located.

Basic Linux Commands

Let's get into it, These are basic commands that would help you navigate through directories and work in the shell/terminal.

pwd - Print working directory

 DESKTOP-XXXXO:/mnt/c/WINDOWS/system32~$ pwd
 /mnt/c/WINDOWS/system32

This displays the pathname for the directory we are working in currently.

cd - Change directory

DESKTOP-XXXXO/mnt/c/Users/Name$ cd  Documents/
DESKTOP-XXXXO:/mnt/c/Users/Name/Documents$

Notice the Pathname now includes "Documents" because we have changed from the Name directory to the Documents directory.

ls - List

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents$  ls
'Budget.xlsx'
'Coursera certificates'
'My Music'
'My Pictures'
'My Shapes'
'My Videos'

This displays the directories and files in my Documents directory. I will refer to Documents as the parent directory for the sake of this guide.

mkdir - Make Directory

We would make a new directory named "Dev_folder" which would be the basis for subsequent commands.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents$ mkdir Dev_folder

This doesn't return any output, rather it creates a directory with the name "Dev_folder". Usually, its best practice to list the files and directories with "ls" to ensure the directory was created with the right name.

Next, use the "cd" command to navigate into the "Dev_folder" directory.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents$ cd Dev_folder
DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$

touch - Creates a new file

We would create a text file "months.txt"

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ touch months.txt

This also doesn't return an output but creates the text file. Similarly, the "ls" command is used to view the file created.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ ls
months.txt

echo - Input text into file

To input some text into the "months.txt" the echo command is used.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ echo "January" >> months.txt

The ">>" is used to add new text while ">" is used to overwrite the text stored in months.txt.

cat - Concatenate

Now we have added some text to our file "months.txt" we can use the "cat" command to read the data from the file and give an output.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ cat months.txt
January

If we "echo" a new month into month.txt with ">" we would overwrite the text previously stored.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ echo "February" > months.txt
DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ cat months.txt
February

mv - Move

This command is used to move files around folders. We would create a folder and a text file in order to properly demonstrate the move command.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ mkdir Weeks 
DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ touch monday.txt
DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ ls
monday.txt
months.txt
Weeks

Using the "move" command, we would move the monday.txt into the Weeks directory.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ mv monday.txt Weeks
DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ ls
months.txt
Weeks

When you navigate to the "Weeks" directory and use the "ls" command, you will find the monday.txt file.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ cd Weeks
DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder/Weeks$ ls
monday.txt

cp - Copy

The "copy" function works quite like the "mv" command. We would navigate back to the Dev_folder in order to copy a file.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder/Weeks$ cd ..
DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ touch tuesday.txt
DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ls 
months.txt
tuesday.txt
Weeks

Now we would copy the "tuesday.txt" into the Weeks directory.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ cp tuesday.txt Weeks
DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ ls 
months.txt
tuesday.txt
Weeks

Notice that the file "tuesday.txt" is still contained in the "Dev_folder" . Navigate to the Weeks directory to view the copied file in the Weeks directory.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder$ cd Weeks
DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder/Weeks$ ls
monday.txt
tuesday.txt

rm - Remove

To remove files the "rm" command is used.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder/Weeks$ rm monday.txt
DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder/Weeks$ ls
tuesday.txt

Notice that the monday.txt file has been deleted.

man - Manual

The manual command is used to display the user manual of any command that can be run on the terminal.

DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder/Weeks$ man pwd

PWD(1)                                User Commands                               PWD(1)

NAME
       pwd - print name of current/working directory

SYNOPSIS
       pwd [OPTION]...

DESCRIPTION
       Print the full filename of the current working directory.

       -L, --logical
              use PWD from environment, even if it contains symlinks

       -P, --physical
              avoid all symlinks

       --help display this help and exit

       --version
              output version information and exit

       If no option is specified, -P is assumed.

       NOTE:  your  shell  may have its own version of pwd, which usually supersedes the
       version described here.  Please refer to your shell's documentation  for  details
       about the options it supports.

AUTHOR
 Manual page pwd(1) line 1 (press h for help or q to quit)

Try using the man command on any of the above commands you've used.

help- Help

The help command is used to provide more information on the usage of another command.


DESKTOP-XXXXO:/mnt/c/Users/Name/Documents/Dev_folder/Weeks$ help cd

cd: cd [-L|[-P [-e]] [-@]] [dir]
    Change the shell working directory.

    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.

    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.

    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.

    Options:
      -L        force symbolic links to be followed: resolve symbolic
                links in DIR after processing instances of `..'
      -P        use the physical directory structure without following
                symbolic links: resolve symbolic links in DIR before
                processing instances of `..'
      -e        if the -P option is supplied, and the current working
                directory cannot be determined successfully, exit with
                a non-zero status
      -@        on systems that support it, present a file with extended
                attributes as a directory containing the file attributes

    The default is to follow symbolic links, as if `-L' were specified.
    `..' is processed by removing the immediately previous pathname component
    back to a slash or the beginning of DIR.

    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.

Tips and Tricks for Using Linux Command Line

  • You can use the clear command to clear the terminal if it gets filled up with too many commands.
  • TAB can be used to autofill in the terminal. For example, You just need to type “cd Doc” and then TAB and the terminal fills the rest up and makes it “cd Documents”.
  • The up arrow key can be used to get the last command.
  • Ctrl+C can be used to stop any command in terminal safely. If it doesn't stop with that, then Ctrl+Z can be used to force stop it.
  • You can exit from the terminal by using the exit command.