Linux Commands for Absolute Beginners

ubaid darwaish

Most of the newbies are always scared of the terminal, but once you have mastered Linux commands for absolutely beginners, all that fear should be a thing of the past.

We have shortlisted 10 commands that are absolutely essential for someone who has just started with the Linux commands

Lets begin:-

  1. PWD:

The first command that we need to learn is the ‘pwd’, it stands for ‘print working directory’.

It helps us to know what is the directory that we are working in.

2. LS:

This is the basic command that is used in Linux and Unix to list the files and folder(directories). It supports multiple arguments and we will cover them as well.

ls –l : Option to linux and unix commands are supplied using ‘-‘ (Hyphen) as shown below. The option –l to ls command enables it to do ‘long listing’. As you can see, it shows the listing of files and directories along with additional details.

ls –lt : This is a further enhancement to command above, where we add another option ‘t’ to make the long listing sorted by ‘time’. Here t stands for time/timestamp.

ls –ltr: This command also builds on the previous one, where we add another option ‘r’ which stand for reverse; to sort the long listing with time in reverse order.

ls –la: This is a command that we used to view the long listing, but in this case we also want to see the hidden files, so we add the option ‘a’ which stands for ‘all’

ls –lu : This linux or unix command is one of my favorites because of its application.

Here we do a long listing with option ‘u’ which stands for used. So basically the date-timestamp that you see in this listing is that of the last used, instead of last modified. Last used includes both when a file/directory was last modified and also when it was last read.

This finds a important application when you want to delete the old files that haven’t been updated/modified since long, but you are not sure if anybody else is using them i.e. reading them.

You can also use listing with wildcards, here in this example where trying to list all text files within a directory.

3. CD:

cd also known as change directory command is one of the basic commands that is built in linux and unix and is used to navigate and change directories. 

In the example above the method that we used the cd command is the ‘absolute path method’. In this method we specify the full path to the command where we need to navigate to.

As shown below we are currently in the path ‘/Users/ubaid/folder1’ and need to change the working directory to ‘/Users/ubaid/folder2’

However there is another method that is quite convenient and often preferred, its called the ‘relative path method. As the name suggests we use relative path instead of the absolute or full path.

For example by using ‘..’ in my cd command, I will be able to move one directory up/back from my current working directory. As is shown in example below.

Similarly using ‘../..’ will move me two directories up/back from my current directory.

Another argument that we can use in cd command is the hyphen ‘-‘. It takes us back to our previous working directory. As shown below.

Tilde ‘~” is another popular argument that we can use in cd command and it take us to our home directory. As shown below.

“/” is another argument that we can use in cd command and it take us to the root. As shown below.

Before we conclude on ‘cd’ command lets take a look at how we can use “./” where dot denotes the current directory and is followed by the rest of the path relative to current directory. Its clear from example below:-

4. CP:

cp command is the copy command that is used to copy the files and folders.

In the example below we will try to make the copy of the file ‘sample.txt’ and name it ‘copyofsample.txt’

Lets first check where we are and list the files in there

Now lets copy the file

You must have noticed that we have used “./” the dot denotes the current directory.

5. MV:

mv is the command that is used to move files instead of copying them, in language of windows we can say move works like ‘cut and paste’.

In the example below we move the file ‘copyofsample.txt’ to another directory ‘tempdir’ that is within our existing directory

One more interesting using of move is to rename files, this can be accomplished as follows:

mv oldname newname

6. MKDIR:

This popular unix and linux command is used to create/make directories.

Usage:

mkdir directory_name

Lets also explore the option ‘p’ for this command which stands for parent.

In example below we are trying to create a folder ‘newfolder’ within a folder ‘freshdir’ 

However the directory ‘freshdir’ doesn’t exist in first place. So when we try to create it, it throws an error.

But if we use the option ‘-p’. It will create all parent directories as well, the ones that don’t exist.

Also the option R in command ‘ls –ltR’ does a long listing recursively.

7. CAT :

This command is used to open the contents of the file. In example below we are reading the file original.txt

Another method of reading the contents of the file is to use the command ‘more’. The command is different from cat because cat opens the full contents of the file on your screen. While as more only opens a portion of the file.

Usage: more original.txt

You can use “Enter” to display additional contents of the file.

Also “more” command enables you to search for text while you are reading the contents of the file.

Once you have opened the file with command more, you can use the forward slash followed by the word you are searching for. Like in example below we are trying to search for word ‘Error’ in the file.

/Error

You can press ‘n’ to go to the next instance of the word that you are looking for. And also you can press ‘N’ to go to the previous instance of the word that you are looking for.

8 .Head and tail

Head and Tail commands enable to read only fewer line of the files.

Head enables you to read the file from the top while as tail enable you to read the file from bottom.

-n is the number of lines you want to read. 3 in  example below.

One interesting use of the tail command is to read the contents of the log as and when contents are written to it. In this case you have to use an option –f.

To exit this view you can use ctrl+c or cmd+c

9. History :

The history command gives us the history of the command that we have previously used.

As is shown below

This is the right time to introduce the use of ‘|’ pipe. The pipe enables to pass the contents/output of one command to another. In the following example we are passing the output of history command into the tail command and read only last 5 lines.

10. GREP :

grep is one of the most widely used commands used in Linux and Unix operating systems. It is used to look for the string patterns within a file. Its usage is as follows:

grep text_to_find file_name

Lets take a look at the file that we will try to find text in, so we will use the cat command to view the contents of the file:

Lets try to look for string ‘apple’ within file ‘orginal.txt’

As we know that linux and unix is case sensitive ,if we look for “Apple” instead of “apple”. We will not get any results.

To overcome that we will use the option ‘-i’ which stands for case insensitive search.

grep can also be used with option –c, to count the number of times it can find the string that we are looking for.

You might have noticed that the command returns both ‘apple’ and ‘pineapple’. If we want to search for exact word instead of the part of the string we use option –w.

Generally when looking for “errors” in log there are additional important messages that help with debugging, these messages are generally located around the error message. To check logs for these messages we can use some additional options of the ‘grep’ command. It will be more clear with the following options that we can use in grep command:

A1: Returns the string that we are looking for and also one line after it.

B1: Returns the string that we are looking for and also one line before it.

C1: Returns the string that we are looking for and also one line above and below it.

The grep command can also be used to search a string recursively, as in example below we use the option –r to recursive look for string ‘apple’ within current folder and all sub directories therein.

Another variation of grep command is the egrep command, its used to look for multiple words in the file, as shown in example below.

Contributed by: Ubaid Darwaish