Basic Linux Commands for Beginners with some advance concepts

Sahil Sardana
7 min readDec 15, 2020

--

In Linux there are 2 Types of commands:

  1. Builtin commands, these are feature of Shell.
  2. External command aka Binaries found in bin directory like /usr/bin or /sbin or /bin

So Let’s get started with the basic command:

  1. pwd aka present working directory: First of all whenever you login to a Unix or Linux System aka Linux machine, you are going to check where are you currently ? For that you can run command pwd which stands for present working directory. This will give you the current location where you are inside the server.

This command tells you that I am in the / directory which is basically the root directory. Root directory is denominated by a “/” which is the home of a root user.

2. ls or list : This command is used to list the files in a directory. You can use several options along with this like

ls -l : printing a long list

ls -r : sorting the list of files

ls -t listing the files by their modified time

ls -a : Listing all the files in a directory including hidden files.

ls -d : listing the directories

Including all above options it becomes:

ls -lart

Running above command in the root directory gives you list of all the files and directories including the hidden file.

In above command we have not mentioned any directory aftere ls -lart, so it just listed files and directories inside current directory i.e. / or root directory.

  • If you want to list the contents of any other directory then you can run pass the directory name after the ls <options> /<directory_name>. Here I will run “ ls -lrt
  • Now if you want get the information about the directory itself like when it was created and who is the owner or it then you can run command:

You will see that this has only printed the directory name, ownere and time when it was created.

  • You can also use the wildcard sign to list the files which are matching the pattern you are searching for, for example in below command I am going to run with an initial along with a wild card operator *

ls -lrt file* . This command will give you the list of all the files which havee names starting with file and rest of the name doest not matter.

3. mkdir or make directory : This command can be used to create directories in current location or any other location.

Above command will create a directory in your current directory. However if you want to create a directory in Remote location then you can add a directive or called parameter after mkdir “-p” along with the location & directory name

4. touch : This command is used to create a blank file inside current location or a remote location, if the file does not exists. With touch command if you just specify the file name then it will create the blank/dummy file in current directory or if you mention an exact path aka absolute path, then the file will be created in that directory:

You will notice that the file has been created but the size of the file is 0 Bytes. Another usage of touch is to update the timestamp of a file to the current timestamp. If there is a need that you want to update the file’s timestamp but do not want to actually make changes inside the file contents then you can use touch command.

In above screenshot you will see that earlier the timestamp of file1.txt was 07:50 but after we ran touch command against file1.txt, the time was changed to the current time. Here you have learnt an additional command i.e. date , which tells you the time inside the server’s clock.

5. cp or copy command: This command is used for copying files or directories. You can copy the file from one directory to any other directory or if you want to copy the file in the same directory then you need to give the new name of the file that you want to keep.

Now if you want to copy a directory then you need to use a directive called -r along with cp command:

cp -r

In above command we had a directory named first_directory in /tmp/medium.com/ and we copied this directory to /tmp , which you can see in the list.

  • Another catch here is that if you want to retain the permissions and timestamp of the file or directory you then you can use a directive called -p , which retains the permission and timestamp. For example if you are taking the backup of a file which was created some time back or you want to keep the ownership and permissions same.

4. mv or move directory: This command is used for moving a file or a directory from one location to another location. This is also used for renaming a file or directory. For example if I want to rename my directory from first_directory to second_directory then I will run below command:

mv <old_filename> <new_filename>

or If you want to move to another location then mv <first_directory> /<target_directory>/

or if you want to give it another name then

mv <second_directory> /tmp/<third_directory>

6. echo : echo is one of the most commonly and widely used built-in command for Linux bash and C shells, that typically used in scripting language and batch files to display a line of text/string on standard output or a file.

echo [option(s)] [string(s)]

Above command prints any plain text written after the command.

When you are using echo command with $ sign ,then it evaluates the value of variable which you have defined inside your environment.

7. Variables in Linux: Variables are widely used in shell scripting and they are the basics of any programming language, you can define them with an equal operator like:

HELLO=”Hello Learners”, Here HELLO is the variable name and Hello Learners is the value. This value can be a string or a number as well.

8. less or more or cat command to view files: Now once you have the files created, if you want to view those files then you can use less command or more command. This opens your file in read only mode and then you can view the content of the file on your screen starting from first page and then pressing the enter button you can keep going down or if you want to shift full page then you can press the Space Bar button.

less text.log

To exit the view mode, Press the colon button and then enter q button.

To view the complete file in 1 go, you can use the cat command:

cat <filename>.

9. grep : This command is used for basically searching for a particular pattern in files or folders. This can be used in several ways like:

a. If you want to list a file with a matching pattern.

ls -lrt | grep -i file ( Here you are listing the files in the curr

b. if you want to search for occurrence of a pattern in a file.

c. If you are not sure of the file name and you have huge number of files, then you can use this command, for example if you want to check a specific config and not sure which file contains that, then you can use grep command like this:

you start with command grep, then the directives means:

-i : ignore the case, does not matter upper case or lower case.

-r : recursively search for all the files and folders. If you do not use this option then it will only search for files located in the current directory.

error: this is the pattern we are searching inside the files.

“*” or wildcard means all the files & folders inside current directory.

10. Pipe command “|” : This command is used to merge 2 commands into 1, with that output of the first command can be used as input to second command.

--

--

Sahil Sardana
Sahil Sardana

Written by Sahil Sardana

Site Reliability Engineer @ Management Consulting Company . Linkedin Profile: https://www.linkedin.com/in/sahilsardana2003/

No responses yet