Get To Know Linux Bash In Under 30 Minutes

File Management Creation

mktemp

Creates a temporary file with a random name. Guarantees that the new file doesn’t exist

touch new_file.txt new_file_2.txt

Creates both new_file.txt and new_file_2.txt

touch file_name_{a..c}

Creates file_name_a, file_name_b, and file_name_c

Moving

cp file_name.txt file_name_copy.txt 

Creates a copy of file_name.txt named file_name_copy.txt in the same working directory

Reading

head file_name.txt 

Prints first 10 lines of a file

tail file_name.txt 

Prints last 10 lines of a file

touch new_file.txt 

Creates a new file

touch {new_file, new_file_2}.txt

Creates both new_file.txt and new_file_2.txt

touch file_name_{1..3}

Creates file_name_1, file_name_2, and file_name_3

`mv file_name.txt new_file_copy.txt`

Moves file_name.txt to new_file_copy.txt in the same directory, renaming the file

cat file_name.txt 

Prints full contents of a file

less file_name.txt

Prints a part of file contents

Deletion

rm file_name.txt 

Removes a file

wc file_name.txt

Prints number of lines words and characters in the file

rm -f file_name.txt

Removes a file ignoring non-existent files

Compression and Decompression

zip archive_name.zip file_name_1.php file_name_2.php
tar -cvf archive_name.tar file_name_1.php file_name_2.php 
tar -zcf archive_name.tar.gz file_name_1.php file_name_2.php

Archives separate files (depending on the format)

zip -r archive_name.zip directory
tar -cvf archive_name.tar directory 
tar -zcf archive_name.tar.gz directory

Archives a whole directory (depending on the format)

Replacing in Files

unzip archive_name.zip
tar -xvf archive_name.tar
tar -zxvf archive_name.tar.gz

Extracts an archive (depending on the format)

sed -i 's/search_query/replace_query/' file_name.txt

Modifies the content of the original file if the replacement query exists in the file

sed 's/search_query/replace_query/g' `file_name.txt` > `new_file_name.txt`

Replaces search_query with replace_query in file_name.txt and saves everything in new_file_name.txt

sed 's/search_query/replace_query/g' file_name.txt Replaces search_query with replace_query in file_name.txt

Search in Files

grep 'contents' /file_name.txt 

Searches for contents inside file_name.txt

grep 'contents_1|contents_2' /directory -R 

Searches for contents or contents_2 inside directory recursively

grep 'contents' /directory -i

Performs case-insensitive search

grep 'contents' /directory -x 

Matches the entire line and prints it out

grep 'contents' /directory -l

Only displays files that match contents query

Directories Navigation

cd directory

Goes to the listed sub-directory

cd -

Goes to the previous directory

ls -l

Shows where symbolic links are pointing

grep 'contents' /directory -r

Searches for contents inside directory recursively

grep 'contents' /directory -v 

Displays only the lines that don’t match contents

grep 'contents' /directory -i

Performs case-insensitive search

grep 'contents' /directory -n

Displays line numbers along with the results

grep 'contents' /directory -L

Only shows files that don’t match contents query

 cd

Goes to the home directory

ls

Lists all directories

ls -l -h

Lists all directories in long format, -h flag uses the human-readable format

cd ..

Goes the directory one level up from the current directory

ls -a

Lists all directories, including hidden ones

ls -t

List directories by modification time, showing newest first

cd ~
tree

Shows directory and file trees

stat `file.txt`

Lists file’s size alongside created and modified timestamps

Creation

mkdir new_directory 

Creates a new directory

mkdir -p parent/child/nested_child 

Creates nested directories

mktemp -d

Creates a temporary directory with a random name. Guarantees that the new directory doesn’t exist

Moving

mv old_directory new_directory Moves old_directory to new_directory
cp -r directory directory_copy

Copies directory to directory_copy recursively

tree -d

Shows directory tree tree -a Shows directory and file trees, including hidden ones

stat directory

Lists directory’s size alongside created and modified timestamps

pwd

Shows current directory path

  mkdir new_directory_1 directory_2 

Creates multiple new directories

mkdir -p {dir_one, dir_two}/nested 

Creates multiple nested directories

  cp directory directory_copy Copies directory to directory_copy

Deletion

rmdir directory 

Removes directory

rm -r directory

Removes dicrectory recursively

Symbolic Links

ln -s path link

Creates a symbolic link called link to the path directory

ln -s -f path link

Overwrites existing symbolic link called link

Popular Directory Permission

777 All possible permissions. Not recommended for security reasons.

755 Directory owner has full permissions. Other users can list the directory but won’t be able to manage the files.

700 Directory owner has full permissions. Other users have no permissions.

Permission Management

ls -l

Displays directory contents in a long format, which displays both permissions and ownership

chown :new_group file_name 

Changes group for file_name

chmod u=rwx, g=r, o-rwx file_name

Sets file_name read, write, execute permissions for the owner and other users, leaving read-only permissions for the group

chmod u+x file_name.txt

Sets the user permissions to execute

chmod u+x, g+x, o+x file_name.txt
chmod a+x file_name.txt
chmod +x file_name.txt

Sets everyone's permissions to execute

chown username:new_group file_name Changes both the owner and group

for file_name

 chown username file_name

Changes the owner for file_name

chmod 777 -R directory

Sets read, write and execute permissions for everyone in directory recursively

chmod g+x file_name.txt

Sets the group permissions to execute

Arrays Creation

indexed_array = (element_1, element_2, element_3) 

Creates an index array

declare -A associative_array = ([key_1] = element_1, [key_2] = element_2, [key_3] = element_3)

Creates an associative array

Adding Elements
indexed_array += (new_element) 

Adds a new element to an indexed array

Printing Out
echo ${indexed_array[0]} 

Prints out the first array element

associative_array += ([key_1] = new_element

Adds a new element to an associative array (note that the key needs to be provided as well)

echo ${indexed_array[@]} 

Prints out the whole array

  echo ${!associative_array[@]}

Prints out all the keys for an associative array

Deletion
unset indexed_array[2]

Removes the third element from an indexed array

unset associative_array[key]

Removes the key element from an associative array

Resource Usage and Processes

top htop

Lists out all the processes interactively

nice -n 10 process_name 

Changes the priority to 10 for process_name

kill 2468

Kills a process that has process ID 2468

 jobs -p

Shows all processes jobs alongside their IDs

 du

Shows current directory, sub-directories and file sizes

Shutdown and Reboot

ps all

Lists out all currently running processes

renice 10 2468

Changes the priority to 10 for a process that has process ID 2468

killall process_name

Kills all processes that have process_name in their name

lsof

Shows all open files and processes that use them

du /directory/sub-directory

Lists specified directory, sub-directories and file sizes

pidof process_name 

Prints out the process ID of process_name

ps -o ni 2468

Displays the priority for a process ID 2468

jobs

Shows all background processes

free

Displays memory usage

df

Shows disks alongside their used and available space

shutdown -r
reboot

Immediately reboots the system

shutdown

Shuts the system down after one minute

shutdown -r +10

Reboots the system after 10 minutes

shutdown now

Immediately shuts down the system

shutdown -c

Cancels a shutdown or reboot

shutdown +10

Shuts the system down after 10 minutes

reboot -f 

Forces a reboot

Scheduled Tasks Crontab Syntax

Access via crontab -e

[minute] [hour] [day] [month] [week] [command]

Each line represents one command to be run as directed

crontab -i

Will show a prompt before removing a user’s crontab

@daily cat /home/hello_world.sh

Schedules a background job to run every day

@reboot cat /home/hello_world.sh

Schedules a job to run after each system reboot

crontab -l

Used to view crontab entries (cron jobs), and display system crontab file contents

crontab -r

Will remove the current crontab file

* * * * * cat /home/hello_world.sh

Schedules a job to run every minute

00 08-17 * * * cat /home/hello_world.sh

Schedules a job to run every weekday, including weekends, from 8am to 5pm

@monthly cat /home/hello_world.sh

Schedules a job to run at the beginning of each month

0 12,15,17,19,21 * * * cat /home/hello_world.sh

Schedules a job to be run five times a day at 12pm, 3pm, 5pm, 7pm and, 9pm

HTTP Requests

curl https://domain.tld 

Returns response body for domain.tld

curl -o file.txt https://domain.tld Outputs to a text file

Network and DNS

ip addr

Shows all IP addresses on a system

ping -c 15 -i 3 domain.tld

Pings the domain 10 times, 3 seconds apart

traceroute domain.tld

Displays all servers the network traffic goes through

curl -i https://domain.tld

Returns response body for domain.tld and includes status code and HTTP headers

curl -H|--header "User-Agent: Agent" https://domain.tld 

Adds an HTTP header

ip route show

Shows all IP addresses to router

netstat -l

Shows all open ports

nmap 0.0.0.0

Scans for the 1,000 most commonly open ports on localhost

host example.net

Display IPv4 and IPv6 addresses for domain.tld

ping domain.tld

Sends multiple ICMP protocol ping requests netstat -i Shows all open ports with in/out usage

nmap 255.255.255.255

Scans for the 1,000 most commonly open ports on remote IP address

dig example.net

Display complete DNS information

dig example.net +short

Display complete DNS in short format

dig example.net ns 

Query NS records

dig example.net txt 

Query TXT records dig example.net A Query A records

dig example.net cname 

Query CNAME records

dig example.net MX Query MX records

Secure Shell Protocol (SSH)

ssh hostname

Connects to hostname using current username via default SSH port 22

ssh root@255.255.255.255 -p 1023

Connects via given username and IP via given SSH port

ssh root@255.255.255.255

Connects via given username and IP via default SSH port 22