linux | Aslan Neferler
top of page

Why Learn Linux?

10 Things Linux Beginners Need to Know
The utilities and commands in this article are a great starting point to get more comfortable with Linux and accomplish most of the things you need to do on a regular basis.

  • Facebook
  • Twitter
  • LinkedIn
  • Instagram

content detail

The utilities and commands in this article are a great starting point to get more comfortable with Linux and accomplish most of the things you need to do on a regular basis.

1. File system navigation

As a developer, it's important to be comfortable navigating the Linux file system. You can use the pwd command  (Print Working Directory) to know where you are. From your current location, you can navigate to another directory with the cd (Change Directory) command. For example:

$ cd ~/.ssh/

 

In the example above, the tilde ( ~) represents your user's home directory. If you're inside a subdirectory and want to go up one level, you can do this with ..for example:

$ cd ../Documents

 

Most Linux beginners don't know that you can also use the tab key to autocomplete file and directory names.

You can use the touch command to quickly create a file.

$ touch hello.tx

 

Use the cp command to copy a file or directory. In the example below, the leftmost file is the original file and the rightmost file is the copy to be created.

$ cp hello.txt codescaptain.txt

 

You made a mistake naming your copied file and you want to rename it. You can do this with the mv(Move) command. You can use mv  da to move a file from one directory to another.

$ mv hello.txt bonjour.txt

We can delete it with the rm(Remove) command.

$ rm ciao.txt

 

rm. -f will remove the files without confirmation -r will remove the files inside the directory. Note: Beware here! Make sure you don't delete anything important.

$ rm -rf ~/Downloads

 

The Linux command line doesn't show you what's inside a particular directory unless you ask for it. It's hard to be productive if you can't see what you're working with. This is where the ls command comes in handy  .

ls ~/Downloads

ls(List Files) lets you see the filenames of any file in the given directory. If the command is run with no arguments, it defaults to the current directory, but you can also specify a path on the right side, as in the example above. ls is the simplest form of this command, which only lists filenames. Mostly, we would like to see more information about the files you are interested in. Sometimes you may also want to see hidden files that have important configurations.

The ls -l command will allow you to see a more detailed view of each file. It is so widely used that ll is an alias that does the same thing. If you want to see hidden files too, you should use ll -a or ls -la commands.

The 'long list' ( -l) version of the l command will show you the following information about each file you're looking at:

  • file owner

  • filegroup

  • File size

  • replacement time

  • File name

With these few commands, you should be able to move freely around the Linux file system and be able to create, move, and delete files.

2. cat, grep Wonders

cat is often the easiest way to quickly examine the contents of a file.

cat hello.txt

It becomes particularly powerful when connected to grep:

$ cat guest_list.txt | grep Lucy

'Piping', aka the | character, allows you to connect commands using the output of the left-hand command as input to the right-hand command. It is a useful technique that allows you to do complex output manipulation by combining simple commands.

One of the most common uses of the 'piping' command, grep, is the result of the left-hand command. grep, a catchy abbreviation for Global Regular Expressions Print, which is not very catchy, is a simple utility that searches for a line that matches the given pattern, in this case a line containing the word 'Lucy'.

$ cat /var/log/messages | grep '500 Internal Server Error'

grep can be used to search for any output, not just file content. Many Linux commands produce dozens of lines full of information. For example, if your Linux machine is running more than a dozen Docker containers, you can use grep to zero-login to just the container you're interested in:

$ dockerps | grep my-awesome-container

You'll learn more about the ps command for Linux in a nutshell.

You can also save the output of any command to a file using redirection ( >):

$ echo "Linux was created by Linus Torvalds" > bio.txt

The above command will create a new file or overwrite the contents of an existing file. To append to an existing file, use >> instead of >.

3. find

You've fallen into a directory with dozens of subdirectories. You know there is a file you need inside this directory, but you are not sure where. find can help!

$ find . -name CS101

The find command lets you search through a file hierarchy ( first argument to find command )  in several different sizes. You can type man find into your terminal to see them all, but the example above uses the -name flag to search for filenames including strings. 

4. File permissions and ownership

Every file and directory in the Linux file system has permissions and an owner. Permissions are what people are allowed to do with the file. To see the permissions on a file, use the command . You'll see something like this in the leftmost column: ls -l <filename>

-rw-r--r--

This is a little hard to read, so let's break it down in the example below:

..own grp oth -|---|---|---

If the file is a directory, the leftmost line will be replaced with a . The next three sets of lines represent the file's owner, the file's group, and others' permissions. The 'owner' of a file is the user who originally created it, but ownership can be changed (more on that shortly). The 'group' owning a file will be the group the owner belongs to, but this can also be changed. Permissions for “Others” apply to all users who do not own the file and are not in the group that owns the file. An exception is the 'root' user, which has full access to every file on the system.

Here is an example of a file where the owner has full permissions but no one else can read, write or execute the file:

-rwx------

When trying to do something with a file or directory you may sometimes get a 'Permission denied' or 'Username not available in sudoers file' error. This usually means that the user does not have the correct permissions for what they are trying to do. In this case you will need to switch to a user with privileges, for example:

$ su sudo

You can use sudo !! to rerun your previous command as root, where the two exclamation marks will be replaced with your previous command.

Sometimes you will need to change the permissions on a file:

$ chmod u=rwx,g=rx,o=r hello.txt

In the example above, we set read, write and execute permissions for the user, read and execute permissions for the group, and read permissions for other users.

If you're ready to learn, there is an even simpler acronym for setting permissions:

$ chmod 766 hello.txt

The owner represents 7permissions, 6group permissions, and the last 6permissions for the group. So where do these numbers come from?

Each trace is represented by a number. Permissions for each user type are aggregated to generate the final number.

  • 4 “read”,

  • 2 “write”,

  • 1 “execute”

  • 0 "no permission"

So 7 represents 4 (read) + 2 (write) + 1 (run). 6 represents 4 (read) + 2 (write) and so on.

You will need read permissions to examine the contents of a file, write permissions to make changes to the file, and execute permissions to run scripts or executables.

You can change the owner and group of a file with the chmod command. For example, suppose you have a file with the following permissions:

drwxr-xr-x 32 root root 4096 16 Jul 17:48 cowsay.sh

You decide that you want your user account to be the owner of the file and his group to be your group. As root, you can run the following command to change the owner and group of the file:

$ chown <your_user>:<your_group> hello.txt

When you run ls -l on the file, you will see that the owner and group have changed:

drwxr-xr-x 32 your_user your_group 4096 16 Jul 17:48 cowsay.sh

5. reverse-i-search

reverse-i-search is a useful utility for searching your command history and rerunning a previous command. You may know that pressing the up arrow lets you scroll through your command history, but what if you wanted to rerun a command you ran 5 minutes ago and have run dozens of commands since then? reverse-i-search is an excellent tool for these situations.

Type  reverse-i-search ctrl + r. Then you can start typing the characters found in your target command. The search is ambiguous, so these characters can be anywhere in the script. In cases where there are multiple matches, you can switch between them by pressing ctrl + r again.

6. Monitoring and Tracking

You may often want to rerun a command to periodically check for changes in the output. For example, you wanted to see the rate at which the memory usage on your machine changes over time:

$ watch -n 5 free -m

The above command will run the command to show the free memory usage in megabytes every 5 seconds.

What if you want to see the latest changes to a file, for example a log file?

$ tail /var/log/messages

By default, tail prints the last 10 lines of a given file to standard output. You can change the number of lines printed with -nFlag i.e. -n 1000. One limitation of this method is that it prints the file when the command is run. The output will not be updated as new lines are written to the file. We can fix this by adding the -f flag as follows:

$ tail -f -n 100 /var/log/messages

This will show you a live updated output of the last 100 lines.

7. manpages and getting help

Many Linux commands have optional parameters that improve their behavior. These optional parameters or signs are usually a hyphen, ie -l. It can be difficult to remember all the possible options you can pass to a command. This is one of the many reasons manpages are useful . 

8. Control and monitor system resource usage

You may encounter many system problems due to lack of system resources. These are: memory, CPU or disk space. Fortunately, Linux provides us with several tools that we can use to quickly diagnose these problems.

topGives information about all the different processes running on the machine, including memory consumption and CPU usage. This command is useful for identifying processes that are consuming a lot of resources or processes that should not be running but are running.

freeCommand lets you see the current memory usage on a machine. This is used to check if a machine has enough memory to do what you want from it, such as running a batch process that consumes several GB of memory.

9. Managing processes

You can see all the processes running on your machine with the following command:

$ ps aux

$ ps aux | grep ruby

Usually, you will want to end the offending process right away. An easy way to do this is with the following command, which allows you to terminate processes by name instead of PID (Process ID):

$ pkill -9 Slack

10. Vi

Vi is an old, powerful text editor that is installed by default on all Linux machines. First released in 1978, a more feature-rich variant called Vim has been produced ever since. Despite being an old tool, many programmers continue to use Vim. Its older and lighter variant, Vi, is the text editor most likely to be installed on any machine where you might need SSH. Therefore, having a basic knowledge of how to use Vi can help you quickly edit and change the contents of files on almost any machine without leaving your terminal.

Finally

I hope you have a little basic understanding of Linux. Unlike many of the tools, operating systems, and frameworks you'll encounter in your programming career, Linux is relatively stable. As changes and new versions arrive, the basics remain the same. So most of the Linux knowledge you learn will stay the same for a long time and you will continue to use it.

Contact

YOU CAN CONTACT US BY WRITING ON FACEBOOK AND INSTAGRAM PAGES

bottom of page