Linux Basics Part3 1686944123
Linux Basics Part3 1686944123
Linux Basics Part3 1686944123
LINUX
COMMANDS
1
LIST OF COMMANDS:
• cd • rm • sort
• ls • tar • alias
• cp • history • sed
• mv • diff • top/htop
+ awk
!
2
W H AT I S A N D W H Y L I N U X ?
• Open Source Operating System (OS)
• 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!
3
sort [OPTION]… FILE/S…
• Powerful tool for text manipulation in the command line, which allows you to sort the lines of a file or
the output of a command in a specified order. By default, it sorts lines lexicographicaly or based on
numerical values if they are present. It can be customized using various flags.
• Useful flags:
– -r: sort in reverse order – from default it sorts in ascending order
– -f: case-insensitive sort
– -n: sort the input/lines numerically
– -k: sort the input based on a specific field or column
– -u: remove duplicate lines from the output
– -o: specify the output file
– -t: specifies a custom field separator for sorting
4
sort [OPTION]… FILE/S…
6
sort [OPTION]… FILE/S…
7
sort [OPTION]… FILE/S…
9
sort [OPTION]… FILE/S…
• The sort command is a versatile tool used for sorting lines of text in various ways, which
allows you to sort lines based on lexicographic order, numerical values, or a specific field
within each line.
• The sort command can be combined with other command-line tools, such as grep or cut, to
perform more advanced text processing tasks.
• By piping the output of one command to sort, you can sort the resulting data in a specific
order or based on certain criteria.
• For example, you can use sort in combination with grep to filter and sort lines that match a
particular pattern in a text file.
• The flexibility and versatility of the sort command make it a valuable tool for programmers,
system administrators, and data analysts in managing and sorting text data.
10
chmod [OPTIONS]… MODE… FILE/S…
• It enable modifying a file or directory’s read, write, and execute permissions. Often used in
shell scripting or command-line operations to set permissions on files or directories.
• Examples:
– chmod +x script.sh : Add executable permission to the script.sh file
– chmod 644 file.txt : Set read and write permissions for the owner, and read-only permissions for
others on the file.txt
– chmod -R 777 directory : Recursively set full permissions (read, write, execute) for all users on the
directory and its contents
• The use of numeric or symbolic notation with the chmod command allows precise control
over permissions, granting or restricting access for users, groups, and others.
11
chmod [xxx] FILE/DIR
• chmod assigns permissions using three numbers: the first number represents permissions
for the owner, the second number for the group, and the third number for others
Num Permission Type Symbol chmod Permission Type Description
500 dir To protect yourself against accidentally removing, renaming, moving files from this dir
1 Execute –x
600 file A private file only changeable by the user who entered the command
2 Write -w-
644 file A publicly readable file that can only be changed by the issuing user
3 Execute + Write -wx
660 file User belonging to your group can change this file, others do not have any access to it at all
4 Read r– 700 file Protects a file against any access from other users, while the issuing user still has full access
5 Read + Execute r-x 755 dir Files that should be readable and executable by others, but only changeable by the
issuing user
6 Read +Write rw-
775 file Standard file sharing, mode for a group
7 Read + Write +Execute rwx 777 file Everybody can do everything to this file
from ”Introduction to Linux, A Begginer’s Guide” by Machtelt Garrels
12
wget [OPTIONS]… URL…
• The non-interactive network downloader, it retrieves files using HTTP, HTTPS,
and FTP protocols. Often used to download files, web pages, or entire websites from
remote servers.
• Examples:
– wget https://example.com/file.txt : Download the file.txt from the specified URL
– wget -r -np https://example.com : Download the entire website from the specified URL, recursively
but without following links to external sites
– wget -O output.txt https://example.com/file.txt : Download the file.txt from the specified URL and
save it as output.txt
13
sudo [COMMAND]
• Used as a prefix for some commands that only superusers are allowed to run. Often used in
command-line operations to run commands that require elevated permissions.
• Examples:
– sudo apt update : Run the 'apt update' command with administrative privileges to update package
information.
– sudo rm -rf /path/to/directory : Delete a directory and its contents with administrative permissions.
– sudo useradd newuser : Create a new user account with administrative privileges.
• The use of the `sudo` command allows authorized users to perform tasks that require
elevated permissions, providing an additional layer of security and control - use it carefully!
14
alias [OPTIONS] [NAME]=‘[VALUE]'
• This command allows you to create custom shortcuts or abbreviations (aliases) for frequently used
commands or command sequences. It helps to simplify command-line usage by reducing the need
for typing long or complex commands repeatedly.
• To ensure that aliases are available for use in your terminal indefinitely, you can add them to your
shell's configuration file. The specific file to modify depends on the shell you're using:
– For Bash shell: Add aliases to ~/.bashrc or ~/.bash_aliases.
– For Zsh shell: Add aliases to ~/.zshrc or ~/.zsh_aliases.
15
Useful aliases
alias explanation
The ll alias simplifies the ls -l command to display a detailed list view
alias ll='ls -l':
of files and directories in a single command.
Creates an alias '..' for the 'cd ..' command, allowing quick navigation
alias ..='cd ..'
up one directory level.
Creates an alias 'grep' that enables colored output for the grep
alias grep='grep --color=auto'
command, making it easier to visually identify matches.
alias runtests='python -m unittest Creates an alias 'runtests' that runs unit tests using the unittest
discover -v' module in Python with verbose output.
alias l.='ls -d .* --color=auto' List all files, including hidden ones.
Creating a shortcut to search for files in the current directory and its
alias fh='find . -name '
subdirectories by specifying a name pattern.
alias updatepkgs='sudo apt-get This alias simplifies the process of updating and upgrading
update && sudo apt-get upgrade' packages on a Debian-based system with a single command.
*I am also creating aliases for long paths to make navigation through them easier, for example:
alias commonpath=‘common/used/long/path/to/make/my/life/easier’, --> cd commonpath, instead long path J
16
Aliases for GIT
17
passwd [OPTIONS]… [username]
• The passwd command is used to change or update user passwords in Linux systems. It
allows users to manage their own passwords or enables system administrators to modify
user passwords.
• Examples:
– passwd: Change the password for the current user.
– passwd username: Change the password for the specified user (requires administrative privileges).
– passwd -l username: Lock the specified user account by disabling the password.
– passwd -u username: Unlock the specified user account by enabling the password.
– passwd -e username: Force the specified user to change their password during the next login.
– passwd -d username: Remove the password for the specified user, making it passwordless.
– passwd -S username: Display password-related information for the specified user.
18
ssh user_name@host(IP/domain_name)
• The ssh command (Secure Shell) is used to establish secure, encrypted remote connections to Linux
servers or devices. It provides a secure method for logging into and executing commands on remote
machines.
• Examples:
– ssh username@hostname: Connect to the remote host specified by hostname using the username username
– ssh -p port username@hostname: Connect to the remote host using a non-default SSH port
– ssh -i private_key_file username@hostname: Connect using a specific private key file instead of the default key
• In a programmer's life, the ssh command is commonly used to securely connect to remote servers,
deploy applications, access version control systems (e.g., Git), and execute commands on remote
machines for development, debugging, and system administration purposes.
19
uniq [OPTION]... [INPUT [OUTPUT]]
• The uniq command filters adjacent duplicate lines in a file or input stream, displaying or
removing them as desired. Often used to identify and eliminate duplicate entries or to extract
unique lines from sorted data.
• Useful flags:
– -c: Displays the count of occurrences for each line, indicating the number of times each line appears in
the input.
– -d: Displays only the duplicate lines, filtering out unique lines from the input.
– -u: Displays only the unique lines, excluding duplicates from the input.
– -s N: Skips the first N characters before comparing lines for uniqueness. Useful when lines have a
common prefix that should be ignored.
– -i: Performs case-insensitive comparison, treating uppercase and lowercase letters as equal when
determining uniqueness.
20
uniq [OPTION]... [INPUT [OUTPUT]]
• Examples:
– uniq filename: Display unique lines from the specified file, removing consecutive duplicates.
– uniq -c filename: Display unique lines with a count of occurrences for each line.
– uniq -d filename: Display only the duplicate lines from the file.
– uniq -u filename: Display only the unique lines from the file, excluding duplicates.
– command | uniq: Pipe the output of a command to uniq to filter duplicate lines.
– command | sort | uniq: Pipe the output of a command through sort before using uniq to
process sorted data.
21
sed OPTIONS... [SCRIPT] [INPUTFILE...]
• A powerful tool for text manipulation in the command line.
• sed (stream editor) allows you to perform various operations on text files or input streams. It provides
capabilities for search, find-and-replace, text substitution, and other text transformations.
• Useful flags:
– -e: Allows multiple commands to be combined and executed in sequence.
– -i: Edits the file in-place, making changes directly to the original file.
– -r or -E: Enables extended regular expressions, providing more advanced pattern matching capabilities.
– -f script-file: Specifies a file containing sed commands to be executed.
– -n: sed will only print the lines explicitly requested.
– and many more…
22
sed OPTIONS... [SCRIPT] [INPUTFILE...]
• File: • Show files with removed lines
containing "@example.com"
23
sed OPTIONS... [SCRIPT] [INPUTFILE...]
• Replace "Avenue" with "Street” inplace: • Print only lines containing phone numbers:
24
top/htop [options]
• The top and htop commands are interactive system monitoring tools that provide real-time information
about system resources, processes, and system performance. They display a dynamic overview of CPU
usage, memory usage, running processes, and other vital system statistics.
• top is a standard command-line utility, while htop is an enhanced version with additional
features and a more user-friendly interface.
• Commonly used keys in top and htop:
– q: Quit the program.
– k: Send a signal to a selected process (kill).
– u: Display processes for a specific user.
– s: Change the update frequency or delay between updates.
– H: Toggle threads display (show or hide individual threads).
– M: Sort processes by memory usage.
– P: Sort processes by CPU usage.
– F: Choose the fields to display and their order.
25
E V E R Y DAY
• To navigate through system I need to change [cd] directories all the time
• I need to know what can I find so I need to list [ls] files and directories
• To pack and unpack tarball files I am using [tar –xzvf / -czvf] frequently
• Sometimes I am looking for files containing specific phrase so I need to [find] and [grep] for it
• I want to check what command I was using recently so I am looking into [history]
26
+ AWK
• The awk command is a versatile text processing tool that operates on files, extracting and
manipulating data based on patterns and actions. It is particularly useful for processing
structured data, such as columns or fields in tabular format.
– awk '{print $1}' file.txt: Print the first field of each line in the specified file.
– awk '/pattern/ {print $2}' file.txt: Print the second field of lines that match
the pattern in the specified file.
– awk -F ',' '{print NF}' file.csv: Print the number of fields in each line of
a comma-separated values (CSV) file.
– awk 'BEGIN {print "Start"} {print $0} END {print "End"}' file.txt: Execute actions before
processing, for each line, and after processing the file.
28
+AWK • awk '/example/ {print}' compare.txt
• Example file:
• https://www.geeksforgeeks.org/
• https://hackernoon.com/top-35-linux-console-tips-and-tricks-from-practical-experience
31
T H E E N D O F PA RT 3
T H A N K YO U
32