0% found this document useful (0 votes)
8 views27 pages

Linux Basics Commands

This document provides an overview of 30 useful Linux commands for beginners, highlighting their functions and examples. It emphasizes the importance of learning these commands to enhance productivity and automate tasks. The commands covered include file management, searching, and viewing file contents, among others.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views27 pages

Linux Basics Commands

This document provides an overview of 30 useful Linux commands for beginners, highlighting their functions and examples. It emphasizes the importance of learning these commands to enhance productivity and automate tasks. The commands covered include file management, searching, and viewing file contents, among others.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

30 USEFUL

LINUX
COMMANDS

WITH EXAMPLES FOR


BEGINNERS, PART 2

1
W H AT I S A N D W H Y L I N U X ?
• Open Source Operating System (OS)

• Eagerly chosen by programmers. Why?


– It’s free
– More secure, it will protect your systems from trojans, viruses, adware etc.
– Easy to customize
– Variety of distribution: Ubuntu, Fedora, Deepin and many more…

• Linux is heavily documented

• Over 1000 commands, but I will present you 30 the most common in daily basis

• I highly recommend to learn at least basics of it, your life will be easier and you’ll be
able to automate a lot of tasks!
2
LIST OF COMMANDS:
• cd • rm • sort

• cat • touch • chmod

• echo • find • wget

• man • less • sudo

• ls • tar • alias

• pwd • grep • passwd

• mkdir • head • ssh

• tee • tail • uniq

• cp • history • sed

• mv • diff • top/htop

3
O N E S E N T E N C E E X P L A N AT I O N TA B L E

Command Meaning
rm removes files and directories from the system
touch used to create empty files or update the timestamps of existing files
find search for files and directories based on various criteria, such as name, size, or modification time
less allows you to view the contents of a file in a paginated manner, allowing you to scroll through it
tar create, extract, or manipulate tar archives, commonly used for file compression and archival purposes
grep search for patterns or specific text within files,
head displays the beginning or top part of a file or output, typically showing the first few lines
tail displays the end or bottom part of a file or output, typically showing the last few lines
history displays a list of previously executed commands in a terminal session
diff compare the differences between two files or directories
4
rm [OPTION]… FILE/S…
• Remove file, files or directories with their content depending on syntax being used

• Useful flags:
– -i : interactive mode, asking for confirmation before deleting a file
– -r : enable you to remove directories with their content
– -v : be verbose when deleting files, showing them as they are removed.

• Remember to use the rm command with caution as it permanently deletes files and
directories.

• Double-check your commands before executing them to avoid accidental data loss.

5
touch [OPTION]… FILE/S…
• Create an empty file or generate and modify a timestamp

• For example:
– touch file.txt create a new fille called file.txt in the current directory, if the file exists command
updates the file’s timestamp to the current time without modyfing its content

• Using the touch command can be helpful when you need to ensure that a file exists or
when you want to update the timestamps of files for various purposes, such as sorting
files based on their modification times or triggering certain actions based on file
access times.

6
find [option] [path] [expression]
• Search for files and directories in a directory hierarchy based on specified criteria and
perform subsequent operations if given
• For example:
– find . –type f : it finds all files in the current directory ( `.` - current directory)
– find /path/to/search -type d -name "directory_name” – finds all directories with name
“directory_name” in the ”path/to/search” directory
– find /path/to/search -name "*.txt" -exec cat {} \; search for files in the /path/to/search directory that
have a .txt extension and displays the content of those files in the terminal

• The find command is very versatile and allows for complex searches based on various
criteria. By combining different options and operators, you can create sophisticated file
searches.

7
less [OPTIONS] FILE/S
• View the contents of a file in a pager-like interface.

• When you open a file with less, it displays the first screenful of the file. You can then use
commands like arrow keys, Page Up, Page Down, or specific commands mentioned
within the less interface to navigate through the file and access additional features.
• For example:
– less file.txt : opens file.txt and fisplays its content, use arrows and other commands to navigate
– less -p "pattern" file.txt : -p option allows you to search for a specific pattern within the file. In
this example, less will search for the word "pattern" in file.txt and highlight matching
occurrences.

• Quit for less with `q` key

8
tar [OPTIONS]… FILE
• Create, manipulate and extract files from tape archives (tar files) with optional
compression.

• Useful flags:

– -c : create an archive file


– -x : extract the archive file
– -f : specify the name of the archive file
– -v : displays verbose information
– -z : compress the resulting archive with gzip

9
tar [OPTIONS]… FILE
• Create a tar archive from few files:

– tar -cvf archive.tar file1.txt file2.txt

• Extract files from a tar archive:


– tar -xvf archive.tar

• Extract files from a tar archive to a specific directory:

– tar -xvf archive.tar -C /path/to/destination

• Compress files while creating a tar archive:

– tar -czvf archive.tar.gz directory

10
g re p [ O P T I O N S ] … PAT T E R N F I L E / S …
• One of the most useful commands, especially while dealing with a lot of text data, for example log files.
• Print lines matching a pattern, it lets you find a pattern by searching through all the texts in a specific file.
• Useful flags:
– -i: matches patterns case-insensitively
– -v: prints lines which do not match the given pattern
– -r: recursively search in sub/directories
– -l: prints only the names of the files containing the pattern
– -c: prints only the count of matching lines
– -E: extended regular expressions
– -o: outputs only the matched portion of the line
– -n: display line numbers

• The tool is highly versatile and can be combined with regular expressions to perform more advanced text
searching and manipulation tasks!
11
g re p [ O P T I O N S ] … PAT T E R N F I L E / S …
• Examples:

– From the basic usage, let’s consider file filled with 1000 words of lorem ipsum
(https://www.lipsum.com/feed/html). We would like to check how many times word „lorem” occurs.
We can write a short script in python to do it, but it is easier to use grep with –c flag, let’s check it:

12
g re p [ O P T I O N S ] … PAT T E R N F I L E / S …
– If we look for case-sensitive „Lorem” we got the next 7 occurances, but if we want to look for case-
insensitive „lorem” – it is enough to add –i flag:

13
g re p [ O P T I O N S ] … PAT T E R N F I L E / S …
– For the next example, let’s look at file with the following content:

• We would like to:


1. Display lines that are not emails
2. Find all phone numbers
3. Display only the names of files containing
”Main Street” inside
4. Conduct a search throughout a directory and
its subdirectories to find instances of the
pattern ”Lorem" recursively displaying line
numbers along with the matching lines

14
g re p [ O P T I O N S ] … PAT T E R N F I L E / S …
1. Display lines that are not emails
Ø Using occurances „@example.com” specific to Ø Using extended regex pattern (-E) with –v flag,
the file we are using with –v flag: which is looking for all possible emails :

15
g re p [ O P T I O N S ] … PAT T E R N F I L E / S …
2. Find all phone numbers (-E for extended regexes and –o for output only the matched portion of
lines)

3. Display only the names of files containing ”Main Street”:


The -l flag in grep is used to display only the names of files that contain a specific pattern, such as "Main Street” in
all files with a .txt extension in the current directory. Instead of showing the actual lines containing the pattern, it
outputs only the names of the files that have a match.

16
g re p [ O P T I O N S ] … PAT T E R N F I L E / S …
4. Conduct a case-sensitive search throughout a directory and its subdirectories to find instances
of the pattern ”Lorem" recursively displaying line numbers along with the matching occurances
- We need to use the following flags: -r, -n, -o;
- We can see results in the following format: path/to/file:line_numer:pattern

17
g re p [ O P T I O N S ] … PAT T E R N F I L E / S …
• grep is a powerful command-line tool used for searching and filtering text.

• It is commonly used for log searching, allowing developers to quickly pinpoint errors
or extract relevant information from log files.
• It's also useful for searching through files in a directory, enabling programmers to
locate specific code snippets or configurations.
• grep can be applied in natural language processing (NLP) tasks, such as extracting
specific patterns or analyzing textual data.
• With its flexibility and power, grep proves to be an essential tool for programmers in
daily tasks involving text analysis and retrieval.

18
head [OPTION]… FILE/S
• Display the beginning lines of a file or input, it is commonly used to preview the content of files

• It is often used to quickly examine the first few lines of a file without opening the entire file

• Examples:

– head file.txt : display the first 10 lines of the file.txt in the terminal
– head -n 5 file.txt : display the first 5 lines of the file.txt in the terminal
– head -c 20 file.txt : display the first 20 bytes of the file.txt in the terminal

• -The use of options such as -n and -c with the head command allows customization of the
number of lines or bytes to be displayed, providing flexibility in previewing file content as
needed.

19
head [OPTION]… FILE/S
• Example:

20
tail [OPTION]… FILE/S
• Display the ending lines of a file or input, commonly used to view the last few lines of files

• It is often used to monitor log files or display the most recent entries in a file

• Examples:

– tail file.txt : display the last 10 lines of the file.txt in the terminal
– tail -n 5 file.txt : display the last 5 lines of the file.txt in the terminal
– tail -f file.txt : continuously display the last lines of the file.txt as it updates (useful for monitoring log
files)

• The use of options such as -n and -f with the tail command allows customization of the
number of lines to be displayed or to follow the updates of a file in real-time.

21
tail [OPTION]… FILE/S
• Example:

22
history
• List up to 500 of previously executed commands

• It is often used to recall and rerun previous commands, saving time and improving
productivity

• Examples:

– history : display a numbered list of previously executed commands in the terminal


– history -c : clear the command history

• The history command provides a convenient way to review past commands,


empowering users to recall and rerun previous actions effortlessly, fostering a
streamlined and efficient command-line workflow.

23
diff [OPTION]… FILE/S…
• Compare files or directories and display the differences between them, commonly used for
file comparison and version control purposes
• It is often used to identify changes made to files, track modifications, and merge changes
from different sources
• Examples:
– diff file1.txt file2.txt : display the differences between file1.txt and file2.txt in the terminal
– diff -r dir1 dir2 : recursively compare the files in dir1 and dir2 and show the differences
– diff -u file1.txt file2.txt : display the differences in a unified format, making it easier to read and
understand

• The use of options such as -r and -u with the diff command allows comparing directories
and generating more readable and informative diff output.

24
diff [OPTION]… FILE/S…
• Let’s consider the following two files: example.txt and compare.txt :

25
diff [OPTION]… FILE/S…
• When using the diff command to compare two files, it provides information about the lines
being compared (for example 8,10c8, 10 inform us that lines from range 8-10 are being
compared now) and prints those lines with a divider (----) between them. This allows us to easily
identify the differences between the files:

26
T H E E N D O F PA RT 2

T H A N K YO U

27

You might also like