Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Linux Commands

Uploaded by

G SATHVIK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Linux Commands

Uploaded by

G SATHVIK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Basic Linux Commands for Lazy Engineers

Nishant Soni
Contents
1 Linux Terminal Commands 3
1.1 Say hello to the Linux terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.1 GNOME Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 date command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 cal command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 whoami command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 id command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6 pwd command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 cd command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.8 . directory and .. directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.9 ls command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.10 mkdir command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.11 rm command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.12 cp command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.13 Renaming or moving a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.14 tree command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.15 cat command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.16 wc command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.17 echo command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.18 Redirecting the command output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.19 Using > to redirect output to a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.20 man command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.21 Counting files in a directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.22 Moving around in the command line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1 Linux Terminal Commands
In Linux the terminal is the lifeline for the software developers, programmers and for any aspiring Linux user.
Anything which can be done on GUI (by clicking on different widgets, icons and buttons) can also be done and
infact much more efficiently on the terminal by using commands. The documentation Linux OS (commands
and usage) can be quite overwhelming for new users and it might not be possible even for the proficient devel-
opers to remember all the commands but with regular usage one can easily master few of the most commonly
used commands.

This document is aimed at introducing the reader to minimal set of basic commands which are required to
Linux operating systems efficiently. Having been said that please keep in mind that it’s just the tip of the
iceberg.

1.1 Say hello to the Linux terminal


It will be appropriate to begin by first asking the question, What is the Linux terminal? The Linux
terminal is a text-based interface used to control a Linux computer. It’s just one of the many tools provided
to Linux users for accomplishing any given task, but it’s widely considered the most efficient method available.
Outside of writing code, it’s certainly the most direct method possible. It’s so popular, in fact, that Apple
changed its foundation to Unix and has gained the Bash and Z shell, and Microsoft developed PowerShell, its
very own open source command line.

The Linux command line is often referred to as the shell, terminal, console, prompt or various other names,

To follow along this tutorial you will need a computer with Ubuntu or some other version of Linux. Every
Linux system includes a command line of one sort or another. This tutorial includes some specfic steps for
Ubuntu 20.04 but most of the content should work regardless of your Linux distribution.

1.1.1 GNOME Terminal


GNOME Terminal is a terminal emulator for the GNOME desktop environment written by Havoc Pennington
and others. Terminal emulators allow users to access a UNIX shell while remaining on their graphical desktop.

When you launch the command prompt, a window will pop up which contains the following information,

[username@hostname directoryname]

A screenshot of the Gnome terminal application is shown in Figure 1. In this case, the username is nishant,
hostname is Julia, and directoryname mentioned is ∼. The ∼ is a special character. It means the home
directory of the user. In this case, the home directory path is /home/nishant.

Figure 1

The Gnome terminal is one of many implementations of terminal emulators. Different Linux environments
may come pre-installed with different terminals. Please refer to the Wikipedia articles to learn about computer
terminals, terminal emulators, and shell.

1.2 date command


As the name suggests, date command prints the current date time.
In case, you would like to know the current data/time in Coordinated Universal Time (UTC), please use the
following command.

The date command can also save you time if you want to see yesterday’s date, or 21 days ago, or 2 years 2
months 3 days ago etc., you can even do that as well.

1.3 cal command


A natural extension to the date command is the cal command which displays a calendar in the terminal. The
current month calendar is displayed by default.

If you want to see the calendar of a particular month and year, you can even do that using the following
command,
cal month year
1.4 whoami command
whoami command tells the user which user account they are using on the system.

1.5 id command
id outputs real user id and various other details related to the account.

1.6 pwd command


pwd command stands for print working directory. It provides the information about the absolute path of the
current working directory.

1.7 cd command
cd command is the short name for change directory. This command helps the user to change his/her current
directory. For example, if you would want to move to the /tmp directory it can be done with cd /tmp.

In the above example, first we moved to /tmp directory, and then we moved back to the home directory by
using ∼ character. If we just write cd without any directory name, it will take us to the home directory, ∼,
by default.
1.8 . directory and .. directory
. and .. has special meaning in the Linux.

1. . means the current directory and

2. .. means the parent directory.

We can use these in various situations for daily activities.

1.9 ls command
We use ls command to list the files and directories inside any given directory. If you use ls command without
any argument, then it will work on the current directory by default. Few examples on the usage of ls command
is shown below.

In the last three commands we provided a path as the argument to the ls command. / is a special directory,
which represents root directory in Linux filesystem.

1.10 mkdir command


mkdir command is used to create a new directory. For our example we will create a WorkSpace directory inside
our home directory.

We can also create nested directories in a single command using the -p option.
1.11 rm command
rm command is used to remove a file, or directory. The −r option is being used to remove a folder with
its contents in a recursive way. With −f you force the removal, ignoring errors and never prompt. You can
chain the flags, so instead of rm −r −f you can as well type rm −rf. But, always double check before you
use rm −rf command, if you by mistake give this command in your home directory, or any other important
directory, it will not ask to confirm, but it will delete everything there. So, please be careful and read twice
before pressing enter key.

1.12 cp command
We can use the cp command to copy a file in the Linux shell. To copy a folder with its contents recursively
we use the cp command with the -r flag. To copy an existing file we use the following format :

cp file to copy new location

In the following example, we are copying hello.txt to hello copy.txt in the same directory.

In the following example, I am copying the file FlowThermoLab.jpg from the Pictures directory in my home
directory to the current directory, ∼/C++/Slides/Images. Recall that the current directory absolute path can
be directly specified with a single . and we need not provide the entire path.
In another example, I will be copying the images directory (and everything inside it) from the Downloads
directory under home to the /tmp/ directory.

1.13 Renaming or moving a file


The mv command is used to rename or move a file or directory. In the following example, the file hello copy.txt
is renamed to hello2.txt.

1.14 tree command


You can visualize the directory structure using tree command. In case it is not installed on your system
you can install it with the command, sudo apt-get install tree. The screenshot below shows the directory
structure for my home directory in a nice tree design visual.
1.15 cat command
cat (concatenate) command is very frequently used in Linux. It reads data from the file and gives their content
as output. It also helps us to create, view, concatenate files. In the following, we explore some frequently used
cat commands.

• To create a new file


Command : cat > newfile
Output It will create a new file named newfile.

• To view a single file


Command : cat filename
Output It will show content of given filename.

• To view multiple files


Command : cat file1 file2
Output : This will show the content of file1 and file2.

• To view contents of a file preceding with line numbers


Command : cat -n filename
Output : It will show content with line numbers.
Apart from the above examples, cat command can help us to perform the following operations,

• Copy the contents of one file to another file.


Command : cat [filename-whose-contents-is-to-be-copied] > [destination-filename]
Output : The content will be copied in destination file.

• Cat command can suppress repeated empty lines in output.


Command : cat -s filename
Output : Will suppress repeated empty lines in output.

• Cat command can append the contents of one file to the end of another file.
Command : cat file1 >> file2 Output : Will append the contents of one file to the end of another file

• Cat command can display content in reverse order using tac command.
Command : tac filename
Output : Will display content in reverse order.

• Cat command can highlight the end of line.


Command : cat -E filename
Output : Will highlight the end of line.

• Cat command to merge the contents of multiple files.


Command : cat file1 file2 file3 > merged file
Output : Will merge the contents of file in respective order and will insert that content in merged file.

1.16 wc command
wc command is the short for word count, is an useful command which can help us to count newlines, words
and bytes of a file.

• -l flag finds the number of lines in a file,

• -w counts the number of words in the file.


1.17 echo command
echo command echoes any given string to the console.

1.18 Redirecting the command output


In Linux terminal, we can redirect the command output to a file, or as input to another command. The pipe
operator | is the most common way to do so. Using this we can count the number of directories in the root (/)
directory very easily.

The | is known as pipe. Please watch this video to know more about pipe operator.

1.19 Using > to redirect output to a file


We can use > to redirect the output of one command to a file, if the file exists this will remove the old content
and only keep the input. We can use >> to append to a file, means it will keep all the old content, and it will
add the new input to the end of the file.
1.20 man command
The man command shows the system’s manual pages. This is the command we use to view the help document
(manual page) for any command. The man pages are organized based on sections, and if the same command
is found in many different sections, only the first one is shown.

The general syntax is man section command. For example, the command man 1 ls will produce the
output shown below. You will need to press to press q to quit the program.

You can learn about different sections from the man Wikipedia page. A screenshot for the manual sections
is provided below.
1.21 Counting files in a directory
Normally ls commands shows all the files and directories in multiple column. But if you pipe the output to
any another command, then it prints one name in a line. We can combine that with wc -l to count the number
of files in a directory.

1.22 Moving around in the command line


There are key shortcuts available in Bash which will help you to move around faster.

Shortcut Keys Action


Ctrl + A Move to the beginning of the line
Ctrl + E Move to the end of the line
Alt + B Move to the previous word
Alt + F Move to the next word
Ctrl + U Cuts to the beginning of the line
Ctrl + K Cuts to the end of the line
Ctrl + W Cuts the previous word
Ctrl + P Browse previously entered commands
Ctrl + R Reverse search for previously entered commands
Ctrl + Y Pastes the text in buffer

You might also like