How To Get Familiar With Linux

’ten Things Every Linux Beginner Should Know’ is an article is based on Natasha Postolovski’s experiences as a self-taught developer, at present working equally a software developer at ThoughtWorks in Commonwealth of australia. You tin follow her on Twitter at @npostolovski.


I work at a software consultancy and am currently staffed as part of a DevOps team. We’re focused on operations: making information technology easy for developers to build and deploy applications. This involves lots of traditional system assistants tasks, and then I spend most of my days spelunking around the Linux control line.

When I joined the team I knew just the very basics of Linux, plenty to navigate around the filesystem, manage files, and run scripts. In the by few months I’ve learned a neat deal (though I even so have a lot to learn), and I’ve noticed myself and my team coming back to the same subset of utilities and commands once again and again.

Linux is a beast, and at that place’due south a great bargain yous can do with it. Only for most developers, and for near purposes, you can become far simply by learning the ten things listed in this commodity. Only similar a builder may take hundreds of tools, but keep returning to her nail-gun again and again; the utilities and commands in this commodity are a fantastic starting point for growing more than comfortable with Linux, and being able to achieve most things you’ll need to exercise on a regular ground.

In writing this, I’ve been inspired by my colleague Cam Jackson’s article on 9 Things Every React.js Beginner Should Know.

1. Navigating the file system

Every bit a programmer, you’ll need to be comfy navigating around the Linux file system. Later opening up a final multiplexer like Terminator, you’ll be dropped into the file organisation. Y’all can use thepwd
(Print Working Directory) command to meet where you are. From your electric current location, you can motility anywhere else with the
cd(Change Directory) command. For example:

        $ cd ~/.ssh/
        
      

In the to a higher place example the tilde (~) represents your user’s home directory. If you’re inside a sub-directory and desire to become up a level, you can do so with
.., for example:

        $ cd ../Documents
        
      

Many Linux beginners don’t realize that y’all can besides use the tab fundamental to automobile-complete file and directory names.

To quickly create a file you tin use the
bear on
command.

        $ touch howdy.txt
        
      

To copy a file or directory, utilise the
cp
control. In example below, the left-most file is the original file, and the correct-almost file is the copy to be created.

        $ cp hi.txt ciao.txt
        
      

Perchance you fabricated a mistake when naming your copied file and want to rename it. You can do this with the
mv
(Move) command. Y’all can also employ the
mv
command to move a file from one directory to another.

The below invocation of
mv
volition rename the file
hello.txt
tobonjour.txt.

        $ mv howdy.txt bonjour.txt
        
      

Let’s say the creation of
ciao.txt
was a fault. Nosotros can delete information technology with the
rm
(Remove) control.

        $ rm ciao.txt
        
      

If we desire to delete a directory and everything in it, you tin can pass the-rf
flag to
rm.
-f
volition remove the files without confirmation, and
-rwill remove files inside the directory. Note: exist careful with this command. Make sure you are not deleting something important.

        $ rm -rf ~/Downloads
        
      

Let’south hold up a 2d. The Linux command line doesn’t show you what’s
inside
a item directory unless you ask it to. Information technology’s tough to exist productive if you can’t see what you’re working with. This is where thels
command comes in useful.

        $ ls ~/Downloads
        
      

ls
(List Files) allows you lot to see the filenames of any files in the given directory. If the command is run without an argument it volition default to the current directory, but y’all tin also specify a path on the right-paw side, as seen in the above case.
ls
is the simplest form of this command, listing only filenames. Often, you lot’ll want to run into more data about the files yous’re interested in. You may as well want to see subconscious files, which sometimes hold important configuration.

The
ls -l
command allows you to come across a more detailed view of each file. Information technology’s so usually used that
ll
is an allonym that does the aforementioned thing. If you lot want to see subconscious files as well, utilize
ll -a
or
ls -la.

The ‘long list’ (-l) version of the
ls
command will show you the following information about each file you are inspecting:

  • File owner
  • File group
  • File size
  • Modification time
  • Filename

With these few commands, you should exist able to comfortably motility around the Linux file arrangement, and create, move and delete files.

2.cat,
grep, and the wonders of piping

The
cat
or
concatenate
control writes files to the standard output (the text you see on your screen in your last).

        $ cat hello.txt
        
      

cat
is frequently the easiest way to quickly inspect the contents of a file. It becomes especially powerful when piped into
grep:

        $ cat guest_list.txt | grep Lucy
        
      

‘Piping’, i.east. the
|
character, allows you to concatenation commands together, using the output of the left-hand side command as input to the command on the right-paw side. It’due south a useful technique that allows you lot to practice complex output processing by combining unproblematic commands together.

I of the most common uses of the pipe command is to
grep
the result of the left-paw side control.
grep, a tricky acronym for the not-so-catchy proper name Global Regular Expressions Print, is a simple utility that searches input for a line that matches the given pattern, in this instance, a line containing the word ‘Lucy’.

Another very common use of
cat
and
grep
together is searching for a specific event in a big log file, i.east.
/var/log/messages.

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

grep
can be used for searching any kind of output, non just file contents. Many Linux commands output dozens of lines packed with information. For example, if your Linux automobile is running over a dozen Docker containers, you can use
grep
to nothing-in on only the container you lot are interested in:

        $ docker ps | grep my-awesome-container
        
      

Y’all’ll learn more virtually the Linux
ps
command shortly.

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

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

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

three.
notice

Yous’ve dropped within a directory with dozens of sub-directories. You know in that location’southward a file inside this directory that you lot demand, but yous aren’t sure where it is.
detect
tin can help!

        $ find . -name CS101
        
      

The ‘find’ command lets you walk a file hierarchy (the outset statement to the
discover
control), and search it on several different dimensions. You tin blazon
man observe
into your terminal to see them all, but the example above uses the
-proper noun
flag to search for filenames including the character sequence
CS101. Instead of bumbling through directory after directory similar someone searching for an orphaned sock under the bed, consider using
find.

4. File permissions and ownership

Every file and directory in the Linux file system has permissions and an possessor. Permissions are
who
is allowed to exercise
what
with the file. To encounter the permissions on a file, apply the command
ls -50 <filename>. You’ll come across something similar this in the left-most cavalcade:

        -rw-r--r--
        
      

This is a little difficult to read, so let’s interruption it downwards in the example below:

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

The nuance on the far left will be replaced with a
d
if the file is a directory. The next three groups of three dashes represent permissions for the owner of the file, the grouping of the file, and all others. The ‘possessor’ of a file is the user who initially created it, though ownership tin can be inverse (more on that shortly). The ‘grouping’ that owns a file volition exist the grouping that its owner belongs to, though this tin too exist changed. The permissions for ‘others’ utilize to any user who is not the owner of the file and not in the group that owns the file. Ane exception is the ‘root’ user, which has total access to every file on the arrangement.

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

        -rwx------
        
      

Yous may occasionally get a ‘Permission denied’ or ‘Username is non in the sudoers file’ mistake when trying to do something with a file or directory. This generally means your user does not have the right permissions for what you are trying to practice. You will need to change to a user who does, for example:

        $ su sudo
        
      

To re-run your previous command as root, you lot tin can use
sudo !!, where the two exclamation marks will be replaced with your previous command.

Y’all will occasionally need to modify the permissions on a file:

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

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

If yous’re game to learn it, there’southward an even simpler shorthand for setting permissions:

        $ chmod 766 hello.txt
        
      

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

Each permission is represented by a digit. The permissions for each user type are added together to form the final number.

  • 4 is “read”,
  • two is “write”,
  • 1 is “execute”
  • 0 is “no permissions”

And so, 7 represents four (read) + 2 (write) + ane (execute). six represents 4 (read) + 2 (write), and so on.

You’ll demand read permissions to inspect the contents of a file, write permissions to make changes to the file, and execute permissions to run scripts or executables.

You lot can change the owner and group of a file with the
chmodcommand. For case, let’s say you have a file with the following permissions:

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

You decide that you desire your user business relationship to be the owner of the file, and its grouping to be your group. Equally root, yous can run the following command to change the file’s owner and group:

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

When y’all run
ls -l
on the file, you’ll see that its 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 handy utility to search back through your command history and re-run a previous command. Yous may know that hitting the up pointer allows you lot to cycle through your command history, but what if you want to re-run a command you ran five minutes ago, and accept run dozens of commands since and so?reverse-i-search
is the perfect tool for these situations.

To initiate a
opposite-i-search, type
ctrl + r. You can and then starting time typing in characters that feature in your target control. The search is fuzzy, and so these characters tin can occur anywhere in the command string. In cases where there are multiple matches, you tin cycle through them by hitting
ctrl + r
again.

6. Watching, Tailing, and Following

You lot’ll oft want to re-run a command to check for changes in the output at regular intervals. For example, you may want to come across the rate that memory usage on your car is changing over time:

        $ watch -n 5 free -thousand
        
      

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

What about when you want to run across the latest changes to a file, for example, a log file?

        $ tail /var/log/messages
        
      

By default,
tail
volition print the concluding 10 lines of a given file to standard output. You can modify the number of lines printed with the
-north
flag, i.due east.
-due north g. One limitation of this method is that it prints the file at the time the command was run. The output won’t be updated as new lines are written to the file. We tin can set up this by adding the
-f
flag as follows:

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

This volition show y’all a live updated output of the final 100 lines of/var/log/messages.

7.
man
pages and getting assist

Many Linux commands accept optional parameters that refine their behavior. These optional parameters or flags are often prefixed with a dash, i.e.
-l. It can exist difficult to call back all the possible options you tin pass to a command. This is ane of the many reasons why
manpages are useful. By typing
human <control>
y’all’ll get access to a description of what the command does, also equally a listing of all possible options and what they do.
human being
in this case stands for
transmission, though I do chortle occasionally when running commands like
man true cat.

8. Checking and monitoring system resource usage

It’s surprising how many operating arrangement issues are caused by a shortage of system resources: retentiveness, CPU, or disk space. Luckily, Linux provides us with several tools we tin utilize to chop-chop diagnose these bug.

The
height
control presents you with lots of information virtually all the different processes (essentially, applications) running on your auto, including memory consumption and CPU utilization. This command is useful for identifying rogue processes that are consuming as well many resources, or processes that should not exist running, only are.

The
gratuitous
command allows you to see current retention usage on a machine. This is useful for checking that a machine has enough memory to do what y’all require of it, for example, running a batch procedure that consumes several GB of memory.

The
df -h
command shows disk usage on your machine in a man readable format (megabytes and gigabytes rather than bytes). You tin see at a glance which of your disks is reaching shut to 100% used, and free upwards space if necessary. A full deejay tin cause all kinds of problems on a automobile.

9. Managing processes

Y’all tin can run into all the processes running on your auto with the following command:

        $ ps aux
        
      

The
aux
options tell
ps
to bear witness processes owned by all users. This is a useful diagnostic tool when you call back a process might be running when it shouldn’t exist, or that information technology might be consuming as well many resources. You may also desire to start a new process and detect that y’all become an error considering the same procedure is already running.

This command has a lot of output, and is often paired with
grep
when yous desire to run across data for a single procedure.

$ ps aux | grep blood-red

Oftentimes, you lot’ll want to kill the offending process. An like shooting fish in a barrel way to practise this is with the following command, which allows you lot to kill processes past name rather than past their PID (Process ID):

        $ pkill -9 Slack
        
      

Y’all can as well impale every instance of a procedure with
killall. An case usage is
killall blood-red, to impale all red processes. Yet, exist careful that you’re killing the correct procedure, and use these commands with intendance.

10. Half dozen

Vi is an ancient, powerful text editor that is installed on all Linux machines by default. Kickoff released in 1978, it has since spawned a more characteristic rich variant chosen Vim. Though it’s an one-time tool, many programmers swear past Vim. Information technology’s older and lighter-weight variant Vi is the text editor most probable to be installed on whatsoever machine you may need to SSH on to. For this reason, having a basic understanding of how to utilise Six tin can assist you to apace edit and manipulate the contents of files on near any machine without leaving the comfort of your terminal.

Becoming a Vi/Vim expert is a multi-twelvemonth journey, but yous can go quite far with but a niggling chip of knowledge. Vim Adventures is a cute and artistic mode to become familiar with some basic commands. This tutorial will besides help you configure Linux for development with Vim.

Beyond the basics

If you’ve enjoyed learning these Linux tips and tricks, there are some neat books yous can read to take your cognition further:

  • How Linux Works
    by Brian Ward
  • The Linux Command Line
    past William E. Shotts
  • Linux For Beginners
    by Jason Cannon
  • Linux Command Line and Shell Scripting Bible
    by Richard Blum & Christine Bresnahan

I hope you’ve enjoyed learning more than near Linux. Unlike many tools, operating systems and frameworks yous’ll encounter in your programming career, Linux is relatively stable. Throughout changes and new versions, the fundamentals remain basically the same, and have done so for a while. The knowledge you’ve gained is probable to final you a long time.

Source: https://www.codementor.io/linux/tutorial/10-things-every-linux-beginner-should-know