0 ratings0% found this document useful (0 votes) 38 views14 pagesBasic Linux Commands
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
BASIC LINUX
COMMANDS
Sakib Haque Zisan
(Contributor)
Basic LINUX Commands_Bengal Black Diamond
Gray Hat Hacker's Community
Join with us on Telegram
We Clickee1, Introduction
Purpose of This Learning
To help students understand the basics of Linux shell commands.
1.1 What is Linux?
Linux is the best-known and most-used Unix-like open souse operating system.
Usually the term “Linux” refers to the Linux kernel, but it also includes the set of
programs, tools, and services that are typically bundled together to make Linux a fully
functional operating system.
Linux is different from other operating systems in many ways. First, Linux is open
source software. The source code of Linux is free and available to the public to view,
edit, and even appropriate skilled users can contribute to it!
There are many distributions of Linux, which include different software options. This
means that Linux is incredibly customizable. Moreover, companies and individuals
choose Linux for their servers because it is secure, and there are excellent support
from a large community of users and commercial companies.
1.2 What is Command Line Interface?
A command-line interface is a means of interacting with a computer program
(like kernel) where the user issues commands to the program in the form of
successive lines of text (command lines).
A program which handles the interface is called a command language
interpreter or shell, For example, the MS-DOS Prompt application in a Windows
operating system
1.3 What is a shell?
Shell is an environment in which a user can execute commands, programs, and shell
scripts.. A shell is not an operating system rather than a way to interface with the
operating system and run commands.1.4 Shell Prompt
The shell prompt (or command line) is where one types commands. The prompt, $,
which is called the command prompt, is issued by the shell. While the prompt is
displayed, users can type a command. Shell reads users’ input after user presses Enter
in keyboard.
1.5 Shell Types
In Unix, there are two major types of shells —
* Bourne shell — In this shell, the $ character is the default prompt.
© C shell — In this shell, the % character is the default prompt.
Subcategories of the Bourne Shell
© Bourne shell (sh)
© Kor shell (ksh)
© Bourne Again shell (bash): one of the most popular and rich in functionality
shells, an expansion of sh (AT&T Bell Labs)
1.6 How is BASH different from the DOS command prompt?
* Case Sensitivity: In Linux/UNIX, commands and filenames are case
sensitive.
“\" vs. “/": In DOS, the forward-slash “ / ” is the command argument
delimiter, while the backslash “\” is a directory separator. In Linux/UNIX,
the “/”” is the directory separator, and the “\” is an escape character.
+ Filenames: The DOS world uses the “eight dot three” filename convention,
meaning that all files followed a format that allowed up to 8 characters in the
filename, followed by a period (“dot”), followed by an option extension, up
to 3 characters long (e.g. FILENAME.TXT). In UNIX/Linux, there is no such
thing as a file extension. Periods can be placed at any part of the filename, and
“extensions” may be interpreted differently by all
1.7 Special Characters
Before we continue to learn about Linux shell commands, it is important to know
that there are many symbols and characters that the shell interprets in special ways.
This means that certain typed characters:
a) cannot be used in certain situations, b) may be used to perform special
operations, or, c) must be“escaped” if you want to use them in a normal way.
Character
Descriptions
Escape character. IT you want to reference a special character, you
must “escape” it
with a backslash first.
Example: touch /tmp/filename\*
Directory separator, used to separate a string of directory names.
Example: /ust/sre/linux
Current directory. Can also “hide” files when it is the first
character in a filename
Parent Directory
Represenis 0 or more characters in a filename, or by itself, all files
ina directory.
Example: pic*2002 can represent the files pic2002,
picJanuary2002, picFeb292002,
ete.
Represents a single character in a filename.
Example: hello? txt can represent hello1 xt, helloz.txt, but not
hello22.txt
[TT
Can be used to represent a range of values, e.g. [0-9], [A-Z], etc.
Example: hello[0-2].txt represents the names hello0.txt,
txt
hellol txt, and hello2.txt
Pipe”. Redirect the output of one command into another
command.
Example: Is | more
Redirect output of a command into a new Tile. Ifthe file already
exists, over-write
it
Example: Is > myfiles.txt
=
Redirect the output of a command onto the end of an existing Tile.
Example: echo .Mary 555-1234. >> phonenumbers.txt
Redirect a file as input to a program,
Example: more < phonenumbers.txt
‘Command separator, Allows you fo execute mulliple commands on}
a single line.
Example: cd /var/log ; less messagesCommand separator as above, but only runs the second command
&& if the first one
finished without errors.
Example: cd /var/logs && less messages
Execute a command in the background, and immediately get your
& shell back
Example: find / -name core > /tmp/corefiles.txt &
Used to start a comment or return the Tength of an array.
Example: # this is comment
$#path, this return the length of $PATH environment variable.2. Linux terminal command
2.1 Executing Commands
« The Command PATH:
> “Most common commands are located in your shell's “PATH”, meaning that
you can just the
name of the program to execute it.
> Your shell's “PATH” variable includes the most common program
locations, such as /bin, /ust/bin, /ust/X11R6/bin, and others.
» To execute commands that are not in your current PATH, you have to give
the complete location of the command.
Examples: /home/bob/myprogram
./program (Execute a program in the current directory)
~/bin/program (Execute program from a personal bin directory)
© Command Syntax
> Commands can be run by themselves, or you can pass in additional
arguments to make them do different things. Typical command syntax can
look something like this:
command [-argument] [-argument] [—argument] [file]
Examples: Is List files in current directoryIs -l
Js -1 --color
cat
filename
cat -n
filename
Lists files in —longl format
As above, with colorized output
Show contents of a file
Show contents of a file, with line numbers
2.2 Navigating the Linux File system
The Linux file system is a tree-like hierarchy of directories and files. At the base of
the file system is the “ /” directory, otherwise known as the “ root ” (not to be
confused with the root user).The following table describes many of the most
common Linux directories.
Directory Descriptions
Zssential command binaries (programs) are stored here (bash,
/bin Is, mount,tar,
ete.)
Device Tiles. In Linux, hardware devices are acceessd just like
Idev other files, and
they are kept under this directory.
/ete Host-specific system configuration files.
Location of users’ personal home directories (.g.
/home /home/ susan).
The Base directory for most shareable, read-only data
Jusr (programs, libraries,
documentation, and much more).
Jusr/bin Most user programs are kept here (cc, find, du, ete.)
Jusr/include | Header files for compiling C programs.
Tusr/lib Libraries for most binary programs.2.3 Linux
commands
2.3.1 Part 1
Linux Command
Description
hort for print working directory, this command can be used
Pwd to display the
directory in which you are currently working.
hort for Change Directory, when typed all by itself, it retums
Ca you to your
home directory.
ed directory
Change into the specified directory name.
Example: ed /usr/sre/linux
ed. Move up one directory. For example, if you are in
/home/vic and you type “ ed ..”, you will end up in /home.
ed- Return to previous directory. An easy way to get
back to your previous location!
Ls List all files in the current directory, in column format,
Is directory List the files in the specified directory, Example: Is /var/loj
Iss List files in “long” format, one file per line. This
also shows you additional info about the file, such as
ownership, permissions,
date, and size.
Is—a List all files, including “hidden” files, Hidden files
4are those files that begin with a“. ”, e.g. The bash_history file
in your home
directo
Js /usr/bin/d*
List all Higs whose names bogin wih the Teter alin he
fust/bin directory.
Linux Command Description
Mkdir Make Directory. Example: mkdir /imp/myfiles
[Cat Display the contents of a text file on the screen. For
example: cat mp3files.txt would display the file we
created in the previous section,
Head Display the first few lines of a text file.
Example: head /ete/services
Display the last few lines of @ text file Example: tail
/etc/services
Display the Tast few lines of a text Tile, and then output
appended data as the file grows (very useful for following
log files!).
Example: tail -f /var/log/messages
[Cp Copies a file from one location to another.
Example: cp mp3files.txt /tmp (copies the mp3files.txt file to
the /tmp directory)
DMy Moves a file to a new location, or renames it.
For example: mv mp3files.txt /tmp
(copy the file to /tmp, and delete it from the original location)
Rm Delete a file. Example: rm /tmp/mp3files.txt
[Rmdir Remove Directory. Example: rmdir /mp/myfiles
This is used to display information about a command.
Man Example: man command
name, like man ed2.3.3 Part 3
Linux Command
Description
SHOWS he Tu path Of shell commands Tound ih your path
Which For example, i
you want to know exactly where the “grep” command is
located on the
filesystem, you can type “which grep”. The output should
be something
like: /bin/grep
‘A very powerful command, but sometimes icky to use. Tt
Find can be used to
search for files matching certain patterns, as well as many
other types of
searches. A simple example is
find . name \*mp3
This example starts searching in the current directory “
” and all
subdirectories, looking for files with “mp3” at the end of
their names.
‘A quick way to search Tor files anywhere on the filesystem.
Locate For example,
you can find all files and directories that contain the name
Smozillal by
typing: locate mozillaPs
Lists currently running process (programs).
Ww Show who is logged on and what they are doing.
Id Print your user-id and group id's
[Free Displays amoumt of Wee and used memory in Me system.
cat /proc/epuinto
Displays information about your CPU.
cat /proc/meminfo
Display Tots of information about current memory usage.
uname -a
Prints system information to the screen (kernel version,
machiné type, etc.)
2.3.4 Part 3
Linux Command
Description
‘Clear
Clear the screen
Display text on the screen. Mostly useful when writing shell]
Echo scripts. For
example: echo “Hello World”
Display a file, or program output one page at a time.
More Examples:
more mp3files.txt
Is-la |
more
‘An improved replacement for the —morel command,
Less Allows you to scroll
backwards as well as forwards.
earch for a pattern in a file or program output. For
Grep example, to find |
outwhich TCP network port is used by the —nfsl service,
you can do thi
grep .nfs. /etc/services
This looks for any line that contains the string —nfsl in the
file
—ete/servicesl and displays only those lines.
Lpr Print a file or program output. Examples:
Ipr mp3files.txt - Print the mp3files.txt file
Is -la | Ipr - Print the output of the —Is —la” command.
“Switch User”. Allows you to switch to another user's
Su account temporarily.
The default account to switch to is the root/superuser
account. Examples:
su- Switch the root account
Switch to root, and log in with root's
su-- environment
su larry -_ Switch to Larry's account3. Advanced topics
3.1 Piping Commands Together
The pipe character, —||, is used to chain two or more commands together. The
output of the first command is piped! into the next program, and if there is a
second pipe, the output is sent to the third program, ete. For example.
Is -la /usr/bin | less
In this example, we run the command —Is -la /usr/binl, which gives us a long
listing of all of the files in /ust/bin, Because the output of this command is typically
very long, we pipe the output to a program called —lessl, which displays the output
for us one screen at a time.
4.2 Redirecting Program Output to Files
‘There are times when it is useful to save the output of a command to a file, instead
of displaying it to the screen. For example, if we want to create a file that lists all
of the MP3 files in a directory, we can do something like this, using the —>1
redirection character:
Is -I /home/vie/MP3/*.mp3 > mp3files.txt
A similar command can be written so that instead of creating a new file called
mp3files.txt, we can append to the end of the original file:
Is -I /home/vie/extraMP3s/*.mp3 >> mp3files.txt
Task 4: Go to working directory and print the files under working directory
using piping. Then redirect the output of the previous command to a text
file.
4.3 Shortcuts
When you start using the Bash shell more often, you will appreciate these
shortcuts that can save you very much typing time.
Shortcut
Up/Down
Atrow Scroll through your most recent commands. You can
Keys scroll back to an old command, hit ENTER, and execute the
command without having to re-type it.
history
1 Show your complete command history.
comman
dTAB
Completi
on
If you type a partial command or filename that the shell
recognizes, you can have it automatically completed for
you if you press the TAB key. Try typing the first few
characters of your favorite Linux command, then hit TAB a
couple of times to
see what happens,
Search your
comman
d
history
with
CTRL-R
Press CTRL-R and then type any portion of a recent
command. It will search the commands for you, and
onceyou find the
command you want, just press ENTER.
Scrolling the
screen with]
Shift-PageUp
and Page
Down,
Scroll back and forward through your terminal.
Reference:
Ben oo
. vic.gedris.org/Manual-ShellIntro/l.2/Shellintro. pdf
. Wikipedia
. Online tutorials
|. thdp.org/LDP/intro-tinux/intro-linux.pdf
Further reading and videos
http://www.ee.surrey.ac.uk/Teaching/Unix/
s
L
2. https://www.tutorialspoint.com/unix/
3. https://www.youtube.com/watch?v=XOvisrCv3Bk
. https://www.voutube.com/watch?v=GuOS-wEH_A0About Contributor -
Sakib Haque Zisan
Mentor, Cyber Security Researcher.
Instructor,
The Tech-Cher Bd
Team owner,
Bengal Black Diamond
Gray Hat Hacker's Community.
CEO & Founder,
Byte Capsule Ltd.
Community
Fecebook Croup Ethical Hacking STEM fie cs FE
YouTube Chanel The Tech Cher Bd
Oficial Telegram Chanel - Bengal Black Diamond OFFicia