Basic Command-line Knowledge for Beginners

Hicran Şevik
6 min readJul 4, 2021

--

Hello everyone,

In this post we will talk about some beginner-level Linux commands and make some practical exercises. You do not need to have any prior knowledge. Let’s get started.

The Terminal Basics

In the previous post, we mentioned UNIX is have a multi user system. Let’s talk some about what is this multi user. In the early of Unix was designed to run as a mainframe and terminals that only have keyboard and screen (no mouse, no graphic, no even color), connected to this mainframe remotely. There was no feature that this terminals have such that running any program independently from mainframe. Terminal were sending texts and receiving text.

In contrast of graphics, text is very lightweight on resources. Even in the 70s, compared to today’s standards, it provides pretty quick and efficient interaction with programs. Commands were also kept very short to less keystroke and speed up the user. Therefore, these commands still sustain it’s popularity.

which

We told that commands are executable program that we call binary. We apply which command to locate the executable file associated with the given command by searching it in the PATH (environment variable).

$ which ls
/bin/ls

Recognizing and running terminal commands is also possible thanks to $PATH. It holds directories to search for executable files. When we type the command to the terminal, the terminal scans all directories in the $PATH, then it runs the program.

Let’s go over some basic commands:

  • pwd means print working directory. It shows our current directory we working on.
$ pwd
/home/userName

Before referring to the cd command, I would like to talk about UNIX File System.

In the Window systems, each drive has a letter, such as C:, D:. But Unix and Unix-like OS don’t split up the drives like that. Instead they have a single unified file system, and individual drives can be attached (“mounted”) to whatever location in the file system makes most sense. The “/” directory, often referred to as the root directory, is the base of that unified file system. All other directories in Linux can be accessed from the root directory.

Root has also special meaning in the Unix world. Root was used for superuser. Super user had more privileges than normal users. Let’s talk about it later.

cd

  • cd is an abbreviation of change directory.
cd ./../myNotes
  • We can use . for symbolizing our current branch.
  • .. is used for pass upper directory. Here, the command pass to the upper folder from the working directory, then it goes to inside of myNotes folder.

We need to know one more environment variable that we call $HOME. $HOME is an environment variable that contains the location of your home directory, usually /home/$USER. The name of a user’s home directory is by default identical to that of the user. The tilde(~) is also used to represent users' home directories, including users' home directories that are used to store web pages on Unix-like web servers.

cd ~/Desktop

ls

  • ls is for listing files.
$ ls 
my-file.txt picture.jpg my-folder
$ ls my-folder
a-file
$ ls -l
drwxr-xr-x 4 user user 4096 Jun 12 11:19 my-folder
-rw-r--r-- 1 user user 10380 Jul 1 01:12 my-file.txt
-rw-r--r-- 1 user user 276659 Dec 21 2018 picture.jpg
$ ls -a
. .. .git my-folder my-file.txt picture.jpg

As you can see commands can take additional parameters which changes the behavior of the given program. One important skill to have is being able to discover what these options are. You can always try the --help parameter (or one it’s variations, -h, -help).

$ ls --helpUsage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
...

man

This will print out all the information and use cases about the given program. You can also refer to the “man pages” (man is an abbreviation for manual) where you can find even more detailed information.

$ man ls

There are some applications that allow us to experience the Unix terminal in different operating systems. “Git Bash” is one of them. It is a package that installs Bash, some common bash utilities, and Git on a Windows operating system.

Git Bash does not support “man pages”, but you can always try --help parameter for getting help for the program you are using.

Relative and Absolute Path

Absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.

You may would like to ask which one is better to use? It depends, because it is all related to your current requirements. To give an example, if you want to switch to a directory somewhere nearby, you can benefit from relative path. But if you put a path to the PATH environment variable, it is the better option to choose absolute path.

Manipulating Folders and Files

Now that we know how to navigate through the file-system, let’s see how we can create new files and folders.

You can of course delete or move files and folders.

Working with the Output

As you know, calling a command generally produces some output that is printed on our terminal screen. What if we want to save this output into a file so that we can just refer to it later instead of running the program? Easy:

$ ls -l > file-list.txt

The output of the ls is saved into the file-list.txt file.

$ cat file-list.txt
drwxr-xr-x 4 user user 4096 Jun 12 11:19 a-directory
-rw-r--r-- 1 user user 10380 Jul 1 01:12 my-file.txt
-rw-r--r-- 1 user user 276659 Dec 21 2018 picture.jpg

wc

Let’s say I want to find out how many files and folders in current directory. We can use the wc (word count) command with the -l parameter to find out how many lines a file have. We can combine wc and the file we just created:

$ wc -l file-list.txt
3

First we saved the output of the ls -l into a file and then used wc -l to find out how many lines the file has. But this may be a bit too cumbersome to just find out how many files/folders in the current directory. We can simplify this process and use | to redirect the output of ls -l to wc:

$ ls -l | wc -l
3

| (pipe), redirects the output of the command on the left to the input of the command on the left.

Conclusion

We learned some basic command line tools, how we can combine them and how we can find our way around in the terminal. This is good foundation to build your knowledge around the terminal. See you in my next post. ❤

References

--

--

Hicran Şevik

Software Engineer who loves to talk about zombies 🧟‍♀️