Become a Terminal Ninja! Common Linux Commands( Linux 101 )steemCreated with Sketch.

in #linux5 years ago (edited)

1.cleaned.png

In this tutorial we're going to learn CLI commands everyone should know. If this is your first time messing with the CLI(aka terminal) and you find it daunting don't worry! Today you'll learn commands that will take you from newb to ninja quickly. You'll be amazed how with just a few commands you can quickly accomplish whatever you want in Linux. Before we get to that let's cover a few basics.

  • The terminal is much like the command prompt in Windows. It's used because once it's learned you can do things far faster and more efficently than using the traditional point and click method.

  • Many OS terminals use the same or similar commands meaning once you learn one set of commands you'll find it much easier to use it on various OS's(all of which have some type of terminal).

  • The terminal has some very helpful features to save you time such as TAB-complete, the ability to search through program manuals very quickly, and of course the most important one if you're a hacker... turning the terminal to have green font :)

What We'll Cover:

We will cover how to navigate the terminal, create, delete, edit, copy, & search files, as well as update your system.

Accessing The Terminal:

To access the terminal in Linux is very simple. Simply hit the Windows key on keyboard and type "terminal". You can also access it by clicking on an icon that looks something like this:1.cleaned.png

Now that you've pulled it up you can type commands. It's important to remember that commands ARE case sensitive. In the below example I accidentally capitalized the D in the command "cd".

If you type it in wrong or are trying to use a command that doesn't exist you can expect an error like this...

2.cleaned.png

Important Tips:

Command Parameters

All commands have parameters you'll use to do what you need to do. These are called options and often have a - before them. Some common examples(though they can be different for each command) are -r for recursive & -f for force. To quickly see available options you can type --help OR -h after the command. See "Getting Help" for more info.

Getting Help

Most commands allow you to pull up information in two ways. To find a summary of how to use a command use the -h OR --help command after you type the command. For a more in-depth guide type man before the command. For example to find out how to use the grep command you could :

  • grep --help (most important information)
    OR
  • man grep (in-depth manual)

Tab Complete

Many commands/file names can be long and cumbersome to type. Luckily we have "TAB Complete" which will automatically fill in the rest of the command. For example to remove a directory that has other directories you need to type rmdir --ignore-fail-on-non-empty which is quite long. With tab complete you simply can type rmdir --igno and hit TAB and it will fill the rest in for you. This also works with file/directory names.

With that out of the way here are some of the most important commands:

Navigating the Terminal

Changing Directories

  • "cd" lets you change directories. You simply type where you want to go. For example:

    • cd ~/Downloads takes you to your download folder.

    • cd ../ takes you back one directory from where you currently are

    • cd ~ takes you to your home directory, typically where files like pictures, downloads, documents, etc... are stored

    • cd / takes you to your root directory.

Notice the blue text showing you what directory you're currently in.

1.cleaned.png

Listing Directories/Files

To list directories or see files available you simply use the ls command. It can be for your present directory or for a directory located somewhere else. For example:

  • ls provides a short listing of all files in your current directory

  • ls -la provides a more in depth listing of every file(including hidden which begin with".") and their permissions, user,
    size, date, and name.

  • ls -la ~/DirectoryToList to "long" list all files in a particular directory. You can see the differences below.
    1.cleaned.png

Manipulating Files

In this section we'll cover how to create, delete, copy, rename, and move files.

Remove Files/Directories

To remove a file you simply use the rm command. The -f stands for force so you don't get prompts. Here are some examples

  • rm -f filename to remove a single file

  • rm -f filename filename2 filename3 to remove multiple files

  • rm -f *.png to remove all files in a directory ending with ".png"

To remove a directory(and all directories in it) type:

  • rm -rf directoryname the -rf stand for recurssive[meaning all directories in that directory] and force(so you're not prompted)

You can check to make sure the file is deleted by using the ls command.

Copying Files/Directories

  • cp filetocopy newcopyname to copy a file to current directory:

  • cp filetocopy ~/directorytocopyfileto to copy a file to another directory

  • cp file1tocopy file2tocpy ~/directorytocopyfileto to copy multiple files

Moving or Renaming Files/Directories

For this you use the mv command. When using the mv command you have two options you can either rename the file or you can move the file to where you need it. For example:

  • mv currentfilename newfilename rename's a file in current directory

  • mv currentfilename ~/directory/tomove/to moves a file to directory you choose(in this case the "to" directory)

Creating Files(including text):

To create a normal file you'd simply type

  • touch nameoffile

To create(or edit) a text file simply use nano:

  • nano NameofNewTextFile.txt
    3.cleaned.png
    It will open up a typing application in the terminal. Once you've typed what you needed to simply hit Ctl+X, hit Y to save file, and Enter to confirm file name.

Searching Files For Information:

We are now getting to some of the most powerful and important tools you'll learn in the terminal. These tools allow you to search files(including manuals) for any information you desire. This is useful in helping you quickly find out information about how to use a command, examine logs, or comb through huge amounts of data quickly). You do this by using the cat OR grep command and combining them with something called a pipe(the | character usually above Enter key)

Display All Text of File

cat filename

Search Text of file

First we need to cover two important options for the grep command. They are:

  • -i case INsensitive

  • -w whole words only(meaning if you search for Tim it will not show you Timmy or any combination other than Tim)

  • | to filter data through

For example say you want to find out how to use a command(in this case grep command and to disable case-sensitivity) and you know one of the keywords has the word case in it. You would type:

  • grep --help | grep -i -w "case"

What this command does is first pull up the help menu(grep --help), then pipes that data( | ) to our next command which searches for the word case(grep -i -w "case") and returns our result.
2.cleaned.png

Updating Your System

Updating the system is very simple. Simply type the following(hitting "Y" at any prompts):

  • sudo apt-get update && sudo apt-get dist-upgrade

So there you go you now know the basics of using the terminal. It's especially important you master the use of grep and | as it will enable you to easily find the info you need while using Linux and especially new commands. I will cover this in more depth in a future post. With practice these things will become second nature so make sure to use the terminal whenever you can. In my next post we'll discuss networking and file permission commands which are important for any user to know.

If you have any questions or comments please leave them below and I'll get back to you shortly and always if you found this useful please upvote and follow. Until next time, my friends :)