Basic Linux Commands that You Should Know
Photo by Gabriel Heinzer on Unsplash

Basic Linux Commands that You Should Know

A terminal or the command line can be intimidating to anyone at the beginning.

But in fact it's the best friend of a developer or anyone who use a Linux system.

By following this guide, you can learn all the basic commands that can help you to navigate through your file system, manipulate or create files etc. all from the comfort of your command line.

Even if you are an experienced developer you may pick something new.

For a beginner, all of the commands can seem like a unnecessary burden. But the truth is, if you get good at using them, you will love it and can make your life a lot easier.

Like every skill, to get good at using these commands, you should be practicing them every time you are on your machine.

When I say practice, I mean deliberate practice.

Now let's jump onto the terminal.

Launching the terminal

The quickest way to launch your terminal on your Linux machine is by

pressing CTRL + ALT + T

No alt text provided for this image

There you go!

Well, what is this thing?

What the heck is a terminal?

A terminal is just an interface to another program called system shell.

A system shell is a program that exposes the operating system to the user. It's called the shell, because it's the outermost layer of the operating system.

The shell uses an interface called terminal or the command line to receive inputs (otherwise called the commands) from the end user.

Basically, a terminal is a direct access to your operating system!

Now we have the terminal launched, let's try some commands.

If you look at the terminal again, you would notice a $ sign.

What does it mean?

It can mean a few things.

It can mean that you are in the system shell and it is ready to receive commands.

It can also indicate where you can type your commands.

Truth is, you can just ignore that dollar sign. You will encounter that dollar sign in front of every Linux command over the internet. Whenever you copy those commands, you can avoid that dollar sign since it's already there in your terminal.

A $ just means - "please type here what follows after the $"

Now let's try our first command.

$ whoami

whoami


It returns your username.

Cool!

Present Working Directory (pwd)

To see where you are now in the file system use the command $ pwd

By default you will be placed in your home directory in Linux.

pwd


The home directory is yours. You will be placing your documents, downloads or anything under the home directory. No one can access your home directory other than you and the root user.

You can use $ pwd whenever you are lost in the file system and wants to know where you are.

List Files and Directories (ls)

To see what are all the things present under a directory use $ ls

list

You can see all the directories present in my home directory.

List Another Directory

You can list contents of another directory without leaving your present directory.

Simply type $ ls <directory path>

Replace <directory path> here with the path of the directory you want to list.

list another directory


List all files (-a)

There are some files that are hidden to a normal $ ls command.

Linux systems have such hidden files that are prefixed with a "." (dot).

You can list them all using -a flag.

list all files

You can see that a lot more files and directories with prefix "." got listed.

Listing in Long Form (-l)

Our files and directories have a lot more information attached to it.

A simple $ ls command won't list those details.

Use -l flag to list files and directories with detailed information.

list in long form

The details listed there are as the following - file mode, number of links, owner name, group name, number of bytes in the file, month the file was last modified, day the file was last modified, hour and minute the file was last modified, the pathname.

Human Readable Size (-h)

$ ls -l can give details of the file such as the size. It gives the information in bytes.

In the above example you can see Documents directory have size 4096 bytes.

But it's more useful if we can get the information as KB, MB etc.

4096 bytes is 4KB

To get that information use -h flag

list in human readable form

You can now see, instead of 4096 it's listed as 4.0K which is 4KB.

Sort by Size (-S)

When you just list using $ ls , the files are sorted alphabetically.

But what if you want to list them by size.

Then use -S flag.

list after sorting by size

You can see the video.mp4 coming up first after sorting by size.

Sort by Last Modified Time (-t)

To list after sorting by last modified time, use -t flag

sort by last modified time

Sort in Reverse (-r)

To reverse the sorting you can use -r flag.

If you are using $ ls -r command, it will list things in reverse alphabetic order.

If you use $ ls -Sr command, it will reverse the sorting by size.

reverse sort

See how it is reversed the list of $ ls -lt command here.

Links (ln)

Link is an association between a file/directory to another one.

As the name suggests it's a link. It just points from a source to a target.

There few use cases for links.

One is, if you have a game and the game use a particular library.

One day the library got updated and the game crashed because it requires the older version of the library.

The only change in the library was the version number.

In that case you can create a link with the old version number and still point the new version of the library. The game will work.

So next time the library version changes again, your game still works!

There are two types of links.

  1. Hard link
  2. Symbolic link

Hard Link

$ ln command is used to create a link. By default it creates hard link.

To create a link, call the $ ln command with source file(file we want to link) followed by the target file(file we want to create).

create hard link

You can see b.txt have the same properties as the source file a.txt

Hard links can only be created within the current file system. Otherwise you have to create symbolic links.

Try adding some content to a.txt if it's empty. Now check the contents of b.txt

You can use cat tool to view the contents within the terminal.

view contents of file

You can try changing the content of your source file and see how the target file gets updated automatically.

So with hard link the target file gets updated automatically whenever the source file is updated.

Try deleting the source file using $ rm command.

delete source file

Even after deleting the source file, the target file will become an independent file.

Forcing Link (-f)

If the target file already exists, you may get an error.

file exist error

In this case, you can force an existing file to be the target file using -f flag.

force link

Symbolic Link

Hard links cannot be created for directories. It only works for files.

We can't create a hard link outside our file system as well.

In all those cases you can use a symbolic link.

Use -s flag to create a symbolic link.

symbolic link

See that unlike a hard link, the properties of source and target files are not identical.

Why is that?

That's because, with symbolic link the operating system creates a new small file that points to the target file/directory.

You may have already noticed an arrow mark (-->) after b.txt

That represents a symbolic link.

Did you see one for a hard link before?

Change Directory (cd)

You often have to move around the file system.

For that use $ cd command.

change directory

You can also move directly to a particular directory by providing the exact path.

change directory

You can move up or back to the parent directory by using $ cd ..

move to parent directory

If you want go back to the home directory from anywhere in the filesystem, just type $ cd and hit enter key.

go to home directory

Create Directory (mkdir)

To create directory use $ mkdir followed by the name of the directory you wish to create.

create directory

You can see I have created a directory called test

We can also create nested directories.

To create nested directories use the flag -p.

create nested directory

Verbose output (-v)

Verbose is a flag that print the results of a command. $ mkdir supports verbose flag

To try, use -v flag with $ mkdir to create a directory.

verbose flag

Copying Files (cp)

Use $ cp command to copy a file.

To copy a single file use $ cp followed by source file and then the location to which the file has to copied.

The location can be a name of a file you wish to copy to or a directory to which the files needs to copied.

copy single file

To copy multiple files, the last argument to the $ cp command must be a directory to which the files need to be copied.

copy multiple files

Here, I have created a new directory called my_files and gave that directory as the last argument to $ cp command.

You can also use some other tricks to achieve the same.

Here *.txt is a pattern that helps you to copy all the .txt files.

copy multiple files using a pattern

$ cp command also supports -v (verbose flag)

copy file with verbose flag

Copy Directories (-R)

If we try to copy one directory to another we will get an error.

copy directory error

We can use the -R flag to recursively copy contents from test1 to a test2 directory. (Notice that, test2 is not already created. If you have already created the test2 directory, then test1 directory itself will be moved to test2.)

copy directory

You can see, the a.txt from test1 has been copied to test2 directory.

Force Overwriting of a File

force overwriting a file

Here I have two files with different owners.

If I try to copy a.txt to b.txt, I would get the permission denied error.

To avoid that, I can force the file to be copied by using a -f flag.

Confirm Overwriting of a File (-i)

When you are copying several files at once, some of the files may already exist. So to avoid any overwrite, it's a good idea to ask for a confirmation before that.

-i flag will ask you for a confirmation if you're about to overwrite any file. You can type "yes" if you want to overwrite. Typing "no" will skip the file.

overwriting file

Deleting File (rm)

Use $ rm command to delete a file.

It supports the same flags as cp command.

You can use $ rm * if you want to delete all the files in a directory.

delete a file

Deleting Directory (rmdir)

Use $ rmdir to delete an empty directory.

delete a directory

If you want to delete a directory that is not empty then you can use the $ rm command with -r flag to delete the directory recursively.

recursively delete directory

Moving Files (mv)

Use $ mv to move a file.

It's kind of same as the $ cp command.

What is actually happening behind the scenes of a $ mv command is $cp followed by $ rm command.

Use $ mv command followed by a source file and then a destination folder.

move file or directory

If you use -v flag, you could see the result printed as "renamed"

In fact, you could use $ mv command to rename a file or directory if the destination file or directory doesn't exist already. The second argument will be the new name you want to give.

rename files or directories

Redirecting Operator (|)

Using the | (found above the enter key on most keyboards) operator, we can transfer the output from one side, as the input to the other side of the operator.

redirecting operator with grep

Here I have given the output of $ cat a.txt as the input to the other side of the | operator.

The other side is grep hello

grep is simply a utility that is used to search text that matches a given expression. Check this link to learn more about grep.

Here grep tries to match "hello world!" to the expression given as "hello".

So in the output you could see "hello" printed in red color as it is matched with the text given as input(hello world!).

Redirecting operator help us to do such chaining of multiple commands.

Here is another interesting thing to do with the help of another Linux utility called sed.

redirecting operator with sed

Here we changed the hello world to hi world using the sed utility.

Check this link to learn more about sed command.

Writing to a File (>)

We can write to a file using the > operator.

In the examples of redirecting operator, we were passing the output from one process to another. Instead of that, let's pass the output to a file.

write to a file

Isn't it interesting that we achieved the same effect of a $ cp command!

And that's it! ??

We have covered the most basic Linux commands.

There are so many more Linux commands and tools out there.

Here is a cheat sheet for you to refer and learn the most popular commands.

I have created this guide based on the chapter 1 of conquering the command line. Please check out that as well.

Other great resources to check

The Art of Command Line

Learn Enough Command Line to Be Dangerous

Video Series from LearnLinuxTv

Hope you have learnt something new today.

As I said it's all about practice and deliberately trying to improve your skills. You will get there ??

Bonus Section

Since you are here, here is a bonus trick to try. Next time you try to type something on the terminal, try hitting the tab button.

By hitting the tab button, your terminal will try to autocomplete the command or directory/file names for you.

Here is an example.

Try typing $cd Doc

Now hit tab.

It's obvious to the your command line that you are looking for the Documents directory. So it autocompletes it for you.

If it has any confusion it will show the different options that matches what you have typed so far.

Try $ cd D

Now hit tab twice.

You would have seen the below options given by your terminal.

tab completion

Tab completion is a great trick to know and it will make your life a lot easier.

Thanks for reading! ??

要查看或添加评论,请登录

Harikrishnan Rajagopalan的更多文章

  • The Strange Case of Emojis in Java

    The Strange Case of Emojis in Java

    I stumbled upon some string operations in Java, but this time the string may contain an emoji. What’s the issue with an…

  • What is Git and GitHub? Are they same?

    What is Git and GitHub? Are they same?

    Hi, if you're here, you probably may have heard developers using the words - 'Git' and 'GitHub'. Or you already have…

  • How Does the Internet Work?

    How Does the Internet Work?

    There are around 4 billion active internet users in this planet. But most web users including developers aren't aware…

    2 条评论
  • My 10,000 Hours of Programming

    My 10,000 Hours of Programming

    For the past few weeks I have been coding in python. I had this previous idea about programming that, there was a magic…

社区洞察

其他会员也浏览了