Large Linux resource.

Vim

Copying and Pasting

From Vim

Paste

First:

:set paste

Then enter insert mode. You will see the status bar insert (paste). Paste your code. ESC to exit out of that mode.

Then, lastly:

:set nopaste

Services

To check the status of a specific service:

service service_name status

To check the status of all services on the system:

service --status-all

To start, restart, stop, services:

service service_name stop
service service_name start
service service_name restart

Compressing

Zipping a file:

gzip -k filename

Run command replacing text with lines from file

The first step was to create a file which had a list of the names required. From this, a number of commands were used to edit this file in order to run it on the Linux system. The first step is to make sure that the file is saved as a LF line ending, as opposed to a CRLF ending. If this is not the case, Linux will see ^M characters at the end of each line in the file, this means that it cannot be run by bash. The next step is to add the “delete metric” code to each line. It appears as if the original file with the list of metrics must have a file extension (eg. *.txt)

This is done with the use of:

awk '{print "text placed in front of each entry" $1}' Original.txt > Final.sh

Where Original is the name of the file that is being augmented. Once this has happened, the file needs to be given execution permission:

sudo chmod +x Final.sh

Now the script can be executed on the server, this will run each line as a command.

Copying

Secure Copy

The scp command will always overwrite any previous files that were previously there before.

pscp -r 'C:\...' username@tsdb.eie.wits.ac.za:'/home/labproj/.../'

Count Files in Folder

ls -1 | wc -l

Size

Size of files in directory

Lists the size of files with the current directory.

ls -l *

Size of Folder

Outputs the size of the folder selected.

du -sh file_path

Size of Files and Folders in CWD

du -sk ./* | sort -nr | \
awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} \
 { total = total + $1; x = $1; y = 1; \
   while( x > 1024 ) { x = (x + 1023)/1024; y++; } \
   printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } \
END { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } \
      printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'

Getting a Shell Script to Work

First create the shell script with:

touch filename.sh

Then edit the file with the contents you want. You have to allow it to have execution ability, you can do this by using

chmod +x filename.sh

Then you can run the script with

./filename.sh

Where the ‘./’ indicates that you are running it from inside the current folder.

Run command and keep it running even when user logs out

Use the screen package. This will run the command, and the user can leave that session with it will running. You can launch a screen by the simply command:

screen

Once the screen is up and running, you can issue your normal commands.

You can disconnect from the screen by using:

Ctrl + A followed by d

To go back into your screen:

screen -r

In order to close the screen one can use either:

exit or:
   
Ctrl + A followed by k

Killing another SSH Login

Search for the other login using: pstree -p

Kill the root process with: kill **

Where ** is the PID of the process you are attempting to kill

Delete all files that do not contain the *** file type

Deletes all files that are of a specific file type:

find . ! -name "*.file_fype" | xargs rm

Run specified command on multiple files

Finds all files with a specified extension (***), then runs a command on those files:

sudo find . -name "*.***" -exec "command here" {} \;

SSH Server

Bash:

ssh -L 13000:localhost:22 username@IP_Address

Using Python (this creates an SSH tunnel from Remote_Port to Local_Port):

python -m sshtunnel -U Username -P Password :Remote_Port -R 
127.0.0.1:Local_Port -p 22 IP_Address

Rename Folder/File

mv oldName newName

Recursively replace text

The following grep command will recursively replace all occurrences of ‘foo’ with ‘bar’

egrep -lRZ 'foo' . | xargs -0 -l sed -i -e 's/foo/bar/g'

egrep -R is what enables the recursive search, and sed -i enables Sed’s ‘in-place’ mode to modify the files directly.

wget

Downloading from the internet, and other resources.

wget URL

Mirror the source directory and recursively dive into any directories found:

wget --mirror --recursive URL

OR

wget -m -r URL

wget -e robots=off -r -nc -mp URL

  • -e robots=off: Navigates through all folders, even if the server doesn’t want it to.
  • -nc or –no-clobber: Skip downloading duplicate files.
  • -np or –n-parent: Stops wget from ascending into a parent directory.
  • -accept: Accept only file types that you are looking for.
  • –level=: Select the number of folder levels in which to go down. (–level=0 does not specify a level, so it will keep going)
  • –reject: Ignore specified file types

Recursively loop through folders A through F and only get the text files:

FOR %g IN (A B C D E F) DO wget -e robots=off -r --level=0 -nc -np --accept txt URL

Big

bash logo

Table of Contents

  1. Basic Operations
    1.1. File Operations
    1.2. Text Operations
    1.3. Directory Operations
    1.4. SSH, System Info & Network Operations
    1.5. Process Monitoring Operations
  2. Basic Shell Programming
    2.1. Variables
    2.2. Array
    2.3. String Substitution
    2.4. Functions
    2.5. Conditionals
    2.6. Loops
  3. Tricks
  4. Debugging

1. Basic Operations

a. export

Displays all environment variables. If you want to get details of a specific variable, use echo $VARIABLE_NAME.

export

Example:

$ export
AWS_HOME=/Users/adnanadnan/.aws
LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8
LESS=-R

$ echo $AWS_HOME
/Users/adnanadnan/.aws

b. whatis

whatis shows description for user commands, system calls, library functions, and others in manual pages

whatis something

Example:

$ whatis bash
bash (1)             - GNU Bourne-Again SHell

c. whereis

whereis searches for executables, source files, and manual pages using a database built by system automatically.

whereis name

Example:

$ whereis php
/usr/bin/php

d. which

which searches for executables in the directories specified by the environment variable PATH. This command will print the full path of the executable(s).

which program_name 

Example:

$ which php
/c/xampp/php/php

e. clear

Clears content on window.

1.1. File Operations

cat chmod chown cp diff file find gunzip gzcat gzip head
lpq lpr lprm ls more mv rm tail touch

a. cat

It can be used for the following purposes under UNIX or Linux.

  • Display text files on screen
  • Copy text files
  • Combine text files
  • Create new text files
    cat filename
    cat file1 file2 
    cat file1 file2 > newcombinedfile
    cat < file1 > file2 #copy file1 to file2
    

b. chmod

The chmod command stands for “change mode” and allows you to change the read, write, and execute permissions on your files and folders. For more information on this command check this link.

chmod -options filename

c. chown

The chown command stands for “change owner”, and allows you to change the owner of a given file or folder, which can be a user and a group. Basic usage is simple forward first comes the user (owner), and then the group, delimited by a colon.

chown -options user:group filename

d. cp

Copies a file from one location to other.

cp filename1 filename2

Where filename1 is the source path to the file and filename2 is the destination path to the file.

e. diff

Compares files, and lists their differences.

diff filename1 filename2

f. file

Determine file type.

file filename

Example:

$ file index.html
 index.html: HTML document, ASCII text

g. find

Find files in directory

find directory options pattern

Example:

$ find . -name README.md
$ find /home/user1 -name '*.png'

h. gunzip

Un-compresses files compressed by gzip.

gunzip filename

i. gzcat

Lets you look at gzipped file without actually having to gunzip it.

gzcat filename

j. gzip

Compresses files.

gzip filename

k. head

Outputs the first 10 lines of file

head filename

l. lpq

Check out the printer queue.

lpq

Example:

$ lpq
Rank    Owner   Job     File(s)                         Total Size
active  adnanad 59      demo                            399360 bytes
1st     adnanad 60      (stdin)                         0 bytes

m. lpr

Print the file.

lpr filename

n. lprm

Remove something from the printer queue.

lprm jobnumber

o. ls

Lists your files. ls has many options: -l lists files in ‘long format’, which contains the exact size of the file, who owns the file, who has the right to look at it, and when it was last modified. -a lists all files, including hidden files. For more information on this command check this link.

ls option

Example:

$ ls -la
rwxr-xr-x   33 adnan  staff    1122 Mar 27 18:44 .
drwxrwxrwx  60 adnan  staff    2040 Mar 21 15:06 ..
-rw-r--r--@  1 adnan  staff   14340 Mar 23 15:05 .DS_Store
-rw-r--r--   1 adnan  staff     157 Mar 25 18:08 .bumpversion.cfg
-rw-r--r--   1 adnan  staff    6515 Mar 25 18:08 .config.ini
-rw-r--r--   1 adnan  staff    5805 Mar 27 18:44 .config.override.ini
drwxr-xr-x  17 adnan  staff     578 Mar 27 23:36 .git
-rwxr-xr-x   1 adnan  staff    2702 Mar 25 18:08 .gitignore

p. more

Shows the first part of a file (move with space and type q to quit).

more filename

q. mv

Moves a file from one location to other.

mv filename1 filename2

Where filename1 is the source path to the file and filename2 is the destination path to the file.

Also it can be used for rename a file.

mv old_name new_name

r. rm

Removes a file. Using this command on a directory gives you an error. rm: directory: is a directory To remove a directory you have to pass -r which will remove the content of the directory recursively. Optionally you can use -f flag to force the deletion i.e. without any confirmations etc.

rm filename

s. tail

Outputs the last 10 lines of file. Use -f to output appended data as the file grows.

tail filename

t. touch

Updates access and modification time stamps of your file. If it doesn’t exists, it’ll be created.

touch filename

Example:

$ touch trick.md

1.2. Text Operations

awk cut echo egrep fgrep fmt grep nl sed sort
tr uniq wc

a. awk

awk is the most useful command for handling text files. It operates on an entire file line by line. By default it uses whitespace to separate the fields. The most common syntax for awk command is

awk '/search_pattern/ { action_to_take_if_pattern_matches; }' file_to_parse

Lets take following file /etc/passwd. Here’s the sample data that this file contains:

root:x:0:0:root:/root:/usr/bin/zsh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync

So now lets get only username from this file. Where -F specifies that on which base we are going to separate the fields. In our case it’s :. { print $1 } means print out the first matching field.

awk -F':' '{ print $1 }' /etc/passwd

After running the above command you will get following output.

root
daemon
bin
sys
sync

For more detail on how to use awk, check following link.

b. cut

Remove sections from each line of files

example.txt

red riding hood went to the park to play

show me columns 2 , 7 , and 9 with a space as a separator

cut -d " " -f2,7,9 example.txt
riding park play

c. echo

Display a line of text

display “Hello World”

echo Hello World
Hello World

display “Hello World” with newlines between words

echo -ne "Hello\nWorld\n"
Hello
World

d. egrep

Print lines matching a pattern - Extended Expression (alias for: ‘grep -E’)

example.txt

Lorem ipsum
dolor sit amet, 
consetetur
sadipscing elitr,
sed diam nonumy
eirmod tempor
invidunt ut labore
et dolore magna
aliquyam erat, sed
diam voluptua. At
vero eos et
accusam et justo
duo dolores et ea
rebum. Stet clita
kasd gubergren,
no sea takimata
sanctus est Lorem
ipsum dolor sit
amet.

display lines that have either “Lorem” or “dolor” in them.

egrep '(Lorem|dolor)' example.txt
or
grep -E '(Lorem|dolor)' example.txt
Lorem ipsum
dolor sit amet,
et dolore magna
duo dolores et ea
sanctus est Lorem
ipsum dolor sit

e. fgrep

Print lines matching a pattern - FIXED pattern matching (alias for: ‘grep -F’)

example.txt

Lorem ipsum
dolor sit amet,
consetetur
sadipscing elitr,
sed diam nonumy
eirmod tempor
foo (Lorem|dolor) 
invidunt ut labore
et dolore magna
aliquyam erat, sed
diam voluptua. At
vero eos et
accusam et justo
duo dolores et ea
rebum. Stet clita
kasd gubergren,
no sea takimata
sanctus est Lorem
ipsum dolor sit
amet.

Find the exact string ‘(Lorem|dolor)’ in example.txt

fgrep '(Lorem|dolor)' example.txt
or
grep -F '(Lorem|dolor)' example.txt
foo (Lorem|dolor) 

f. fmt

Simple optimal text formatter

example: example.txt (1 line)

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

output the lines of example.txt to 20 character width

cat example.txt | fmt -w 20
Lorem ipsum
dolor sit amet,
consetetur
sadipscing elitr,
sed diam nonumy
eirmod tempor
invidunt ut labore
et dolore magna
aliquyam erat, sed
diam voluptua. At
vero eos et
accusam et justo
duo dolores et ea
rebum. Stet clita
kasd gubergren,
no sea takimata
sanctus est Lorem
ipsum dolor sit
amet.

g. grep

Looks for text inside files. You can use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.

grep pattern filename

Example:

$ grep admin /etc/passwd
_kadmin_admin:*:218:-2:Kerberos Admin Service:/var/empty:/usr/bin/false
_kadmin_changepw:*:219:-2:Kerberos Change Password Service:/var/empty:/usr/bin/false
_krb_kadmin:*:231:-2:Open Directory Kerberos Admin Service:/var/empty:/usr/bin/false

You can also force grep to ignore word case by using -i option. -r can be used to search all files under the specified directory, for example:

$ grep -r admin /etc/

And -w to search for words only. For more detail on grep, check following link.

h. nl

Number lines of files

example.txt

Lorem ipsum
dolor sit amet,
consetetur
sadipscing elitr,
sed diam nonumy
eirmod tempor
invidunt ut labore
et dolore magna
aliquyam erat, sed
diam voluptua. At
vero eos et
accusam et justo
duo dolores et ea
rebum. Stet clita
kasd gubergren,
no sea takimata
sanctus est Lorem
ipsum dolor sit
amet.

show example.txt with line numbers

nl -s". " example.txt 
     1. Lorem ipsum
     2. dolor sit amet,
     3. consetetur
     4. sadipscing elitr,
     5. sed diam nonumy
     6. eirmod tempor
     7. invidunt ut labore
     8. et dolore magna
     9. aliquyam erat, sed
    10. diam voluptua. At
    11. vero eos et
    12. accusam et justo
    13. duo dolores et ea
    14. rebum. Stet clita
    15. kasd gubergren,
    16. no sea takimata
    17. sanctus est Lorem
    18. ipsum dolor sit
    19. amet.

i. sed

Stream editor for filtering and transforming text

example.txt

Hello This is a Test 1 2 3 4

replace all spaces with hyphens

sed 's/ /-/g' example.txt
Hello-This-is-a-Test-1-2-3-4

replace all digits with “d”

sed 's/[0-9]/d/g' example.txt
Hello This is a Test d d d d

j. sort

Sort lines of text files

example.txt

f
b
c
g
a
e
d

sort example.txt

sort example.txt
a
b
c
d
e
f
g

randomize a sorted example.txt

sort example.txt | sort -R
b
f
a
c
d
g
e

k. tr

Translate or delete characters

example.txt

Hello World Foo Bar Baz!

take all lower case letters and make them upper case

cat example.txt | tr 'a-z' 'A-Z' 
HELLO WORLD FOO BAR BAZ!

take all spaces and make them into newlines

cat example.txt | tr ' ' '\n'
Hello
World
Foo
Bar
Baz!

l. uniq

Report or omit repeated lines

example.txt

a
a
b
a
b
c
d
c

show only unique lines of example.txt (first you need to sort it, otherwise it won’t see the overlap)

sort example.txt | uniq
a
b
c
d

show the unique items for each line, and tell me how many instances it found

sort example.txt | uniq -c
    3 a
    2 b
    2 c
    1 d

m. wc

Tells you how many lines, words and characters there are in a file.

wc filename

Example:

$ wc demo.txt
7459   15915  398400 demo.txt

Where 7459 is lines, 15915 is words and 398400 is characters.

1.3. Directory Operations

cd mkdir pwd

a. cd

Moves you from one directory to other. Running this

$ cd

moves you to home directory. This command accepts an optional dirname, which moves you to that directory.

cd dirname

b. mkdir

Makes a new directory.

mkdir dirname

c. pwd

Tells you which directory you currently are in.

pwd

1.4. SSH, System Info & Network Operations

bg cal date df dig du fg finger jobs last
man passwd ping ps quota scp ssh top uname uptime
w wget whoami whois

a. bg

Lists stopped or background jobs; resume a stopped job in the background.

b. cal

Shows the month’s calendar.

c. date

Shows the current date and time.

d. df

Shows disk usage.

e. dig

Gets DNS information for domain.

dig domain

f. du

Shows the disk usage of files or directories. For more information on this command check this link

du [option] [filename|directory]

Options:

  • -h (human readable) Displays output it in kilobytes (K), megabytes (M) and gigabytes (G).
  • -s (supress or summarize) Outputs total disk space of a directory and supresses reports for subdirectories.

Example:

du -sh pictures
1.4M pictures

g. fg

Brings the most recent job in the foreground.

h. finger

Displays information about user.

finger username

i. jobs

Lists the jobs running in the background, giving the job number.

j. last

Lists your last logins of specified user.

last yourUsername

k. man

Shows the manual for specified command.

man command

l. passwd

Allows the current logged user to change their password.

m. ping

Pings host and outputs results.

ping host

n. ps

Lists your processes.

ps -u yourusername

Use the flags ef. e for every process and f for full listing.

ps -ef

o. quota

Shows what your disk quota is.

quota -v

p. scp

Transfer files between a local host and a remote host or between two remote hosts.

copy from local host to remote host

scp source_file user@host:directory/target_file

copy from remote host to local host

scp user@host:directory/source_file target_file
scp -r user@host:directory/source_folder target_folder

This command also accepts an option -P that can be used to connect to specific port.

scp -P port user@host:directory/source_file target_file

q. ssh

ssh (SSH client) is a program for logging into and executing commands on a remote machine.

ssh user@host

This command also accepts an option -p that can be used to connect to specific port.

ssh -p port user@host

r. top

Displays your currently active processes.

s. uname

Shows kernel information.

uname -a

t. uptime

Shows current uptime.

u. w

Displays who is online.

v. wget

Downloads file.

wget file

w. whoami

Return current logged in username.

x. whois

Gets whois information for domain.

whois domain

1.5. Process Monitoring Operations

kill killall & nohup

a. kill

Kills (ends) the processes with the ID you gave.

kill PID

b. killall

Kill all processes with the name.

killall processname

c. &

The & symbol instructs the command to run as a background process in a subshell.

command &

d. nohup

nohup stands for “No Hang Up”. This allows to run command/process or shell script that can continue running in the background after you log out from a shell.

nohup command

Combine it with & to create background processes

nohup command &

2. Basic Shell Programming

The first line that you will write in bash script files is called shebang. This line in any script determines the script’s ability to be executed like a standalone executable without typing sh, bash, python, php etc beforehand in the terminal.

#!/usr/bin/env bash

2.1. Variables

Creating variables in bash is similar to other languages. There are no data types. A variable in bash can contain a number, a character, a string of characters, etc. You have no need to declare a variable, just assigning a value to its reference will create it.

Example:

str="hello world"

The above line creates a variable str and assigns “hello world” to it. The value of variable is retrieved by putting the $ in the beginning of variable name.

Example:

echo $str   # hello world

2.2. Array

Like other languages bash has also arrays. An array is a variable containing multiple values. There’s no maximum limit on the size of array. Arrays in bash are zero based. The first element is indexed with element 0. There are several ways for creating arrays in bash which are given below.

Examples:

array[0]=val
array[1]=val
array[2]=val
array=([2]=val [0]=val [1]=val)
array=(val val val)

To display a value at specific index use following syntax:

${array[i]}     # where i is the index

If no index is supplied, array element 0 is assumed. To find out how many values there are in the array use the following syntax:

${#array[@]}

Bash has also support for the ternary conditions. Check some examples below.

${varname:-word}    # if varname exists and isn't null, return its value; otherwise return word
${varname:=word}    # if varname exists and isn't null, return its value; otherwise set it word and then return its value
${varname:+word}    # if varname exists and isn't null, return word; otherwise return null
${varname:offset:length}    # performs substring expansion. It returns the substring of $varname starting at offset and up to length characters

2.3 String Substitution

Check some of the syntax on how to manipulate strings

${variable#pattern}         # if the pattern matches the beginning of the variable's value, delete the shortest part that matches and return the rest
${variable##pattern}        # if the pattern matches the beginning of the variable's value, delete the longest part that matches and return the rest
${variable%pattern}         # if the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest
${variable%%pattern}        # if the pattern matches the end of the variable's value, delete the longest part that matches and return the rest
${variable/pattern/string}  # the longest match to pattern in variable is replaced by string. Only the first match is replaced
${variable//pattern/string} # the longest match to pattern in variable is replaced by string. All matches are replaced
${#varname}     # returns the length of the value of the variable as a character string

2.4. Functions

As in almost any programming language, you can use functions to group pieces of code in a more logical way or practice the divine art of recursion. Declaring a function is just a matter of writing function my_func { my_code }. Calling a function is just like calling another program, you just write its name.

function name() {
    shell commands
}

Example:

#!/bin/bash
function hello {
   echo world!
}
hello

function say {
    echo $1
}
say "hello world!"

When you run the above example the hello function will output “world!”. The above two functions hello and say are identical. The main difference is function say. This function, prints the first argument it receives. Arguments, within functions, are treated in the same manner as arguments given to the script.

2.5. Conditionals

The conditional statement in bash is similar to other programming languages. Conditions have many form like the most basic form is if expression then statement where statement is only executed if expression is true.

if [ expression ]; then
    will execute only if expression is true
else
    will execute if expression is false
fi

Sometime if conditions becoming confusing so you can write the same condition using the case statements.

case expression in
    pattern1 )
        statements ;;
    pattern2 )
        statements ;;
    ...
esac

Expression Examples:

statement1 && statement2  # both statements are true
statement1 || statement2  # at least one of the statements is true

str1=str2       # str1 matches str2
str1!=str2      # str1 does not match str2
str1<str2       # str1 is less than str2
str1>str2       # str1 is greater than str2
-n str1         # str1 is not null (has length greater than 0)
-z str1         # str1 is null (has length 0)

-a file         # file exists
-d file         # file exists and is a directory
-e file         # file exists; same -a
-f file         # file exists and is a regular file (i.e., not a directory or other special type of file)
-r file         # you have read permission
-s file         # file exists and is not empty
-w file         # you have write permission
-x file         # you have execute permission on file, or directory search permission if it is a directory
-N file         # file was modified since it was last read
-O file         # you own file
-G file         # file's group ID matches yours (or one of yours, if you are in multiple groups)

file1 -nt file2     # file1 is newer than file2
file1 -ot file2     # file1 is older than file2

-lt     # less than
-le     # less than or equal
-eq     # equal
-ge     # greater than or equal
-gt     # greater than
-ne     # not equal

2.6. Loops

There are three types of loops in bash. for, while and until.

Different for Syntax:

for x := 1 to 10 do
begin
  statements
end

for name [in list]
do
  statements that can use $name
done

for (( initialisation ; ending condition ; update ))
do
  statements...
done

while Syntax:

while condition; do
  statements
done

until Syntax:

until condition; do
  statements
done

3. Tricks

Set an alias

Open bash_profile by running following command nano ~/.bash_profile

alias dockerlogin=’ssh www-data@adnan.local -p2222’ # add your alias in .bash_profile

To quickly go to a specific directory

nano ~/.bashrc

export hotellogs=”/workspace/hotel-api/storage/logs”

source ~/.bashrc
cd $hotellogs

Exit traps

Make your bash scripts more robust by reliably performing cleanup.

function finish {
  # your cleanup here. e.g. kill any forked processes
  jobs -p | xargs kill
}
trap finish EXIT

Saving your environment variables

When you do export FOO = BAR, your variable is only exported in this current shell and all its children, to persist in the future you can simply append in your ~/.bash_profile file the command to export your variable

echo export FOO=BAR >> ~/.bash_profile

Accessing your scripts

You can easily access your scripts by creating a bin folder in your home with mkdir ~/bin, now all the scripts you put in this folder you can access in any directory.

If you can not access, try append the code below in your ~/.bash_profile file and after do source ~/.bash_profile.

    # set PATH so it includes user's private bin if it exists
    if [ -d "$HOME/bin" ] ; then
        PATH="$HOME/bin:$PATH"
    fi

4. Debugging

You can easily debug the bash script by passing different options to bash command. For example -n will not run commands and check for syntax errors only. -v echo commands before running them. -x echo commands after command-line processing.

bash -n scriptname
bash -v scriptname
bash -x scriptname

1 – SYSTEM INFORMATION

Display Linux system information

uname -a

Display kernel release information

uname -r

Show which version of redhat installed

cat /etc/redhat-release

Show how long the system has been running + load

uptime

Show system host name

hostname

Display the IP addresses of the host

hostname -I

Show system reboot history

last reboot

Show the current date and time

date

Show this month’s calendar

cal

Display who is online

w

Who you are logged in as

whoami

2 – HARDWARE INFORMATION

Display messages in kernel ring buffer

dmesg

Display CPU information

cat /proc/cpuinfo

Display memory information

cat /proc/meminfo

Display free and used memory ( -h for human readable, -m for MB, -g for GB.)

free -h

Display PCI devices

lspci -tv

Display USB devices

lsusb -tv

Display DMI/SMBIOS (hardware info) from the BIOS

dmidecode

Show info about disk sda

hdparm -i /dev/sda

Perform a read speed test on disk sda

hdparm -tT /dev/sda

Test for unreadable blocks on disk sda

badblocks -s /dev/sda

3 – PERFORMANCE MONITORING AND STATISTICS

Display and manage the top processes

top

Interactive process viewer (top alternative)

htop

Display processor related statistics

mpstat 1

Display virtual memory statistics

vmstat 1

Display I/O statistics

iostat 1

Display the last 100 syslog messages (Use /var/log/syslog for Debian based systems.)

tail 100 /var/log/messages

Capture and display all packets on interface eth0

tcpdump -i eth0

Monitor all traffic on port 80 ( HTTP )

tcpdump -i eth0 ‘port 80’

List all open files on the system

lsof

List files opened by user

lsof -u user

Display free and used memory ( -h for human readable, -m for MB, -g for GB.)

free -h

Execute “df -h”, showing periodic updates

watch df -h

4 – USER INFORMATION AND MANAGEMENT

Display the user and group ids of your current user.

id

Display the last users who have logged onto the system.

last

Show who is logged into the system.

who

Show who is logged in and what they are doing.

w

Create a group named “test”.

groupadd test

Create an account named john, with a comment of “John Smith” and create the user’s home directory.

useradd -c “John Smith” -m john

Delete the john account.

userdel john

Add the john account to the sales group

usermod -aG sales john

5 – FILE AND DIRECTORY COMMANDS

List all files in a long listing (detailed) format

ls -al

Display the present working directory

pwd

Create a directory

mkdir directory

Remove (delete) file

rm file

Remove the directory and its contents recursively

rm -r directory

Force removal of file without prompting for confirmation

rm -f file

Forcefully remove directory recursively

rm -rf directory

Copy file1 to file2

cp file1 file2

Copy source_directory recursively to destination. If destination exists, copy source_directory into destination, otherwise create destination with the contents of source_directory.

cp -r source_directory destination

Rename or move file1 to file2. If file2 is an existing directory, move file1 into directory file2

mv file1 file2

Create symbolic link to linkname

ln -s /path/to/file linkname

Create an empty file or update the access and modification times of file.

touch file

View the contents of file

cat file

Browse through a text file

less file

Display the first 10 lines of file

head file

Display the last 10 lines of file

tail file

Display the last 10 lines of file and “follow” the file as it grows.

tail -f file

6 – PROCESS MANAGEMENT

Display your currently running processes

ps

Display all the currently running processes on the system.

ps -ef

Display process information for processname

ps -ef | grep processname

Display and manage the top processes

top

Interactive process viewer (top alternative)

htop

Kill process with process ID of pid

kill pid

Kill all processes named processname

killall processname

Start program in the background

program &

Display stopped or background jobs

bg

Brings the most recent background job to foreground

fg

Brings job n to the foreground

fg n

7 – FILE PERMISSIONS

Linux chmod example PERMISSION EXAMPLE

     U   G   W
    rwx rwx rwx     chmod 777 filename
    rwx rwx r-x     chmod 775 filename
    rwx r-x r-x     chmod 755 filename
    rw- rw- r--     chmod 664 filename
    rw- r-- r--     chmod 644 filename

NOTE: Use 777 sparingly!

    LEGEND
    U = User
    G = Group
    W = World

    r = Read
    w = write
    x = execute
    - = no access

8 – NETWORKING

Display all network interfaces and ip address

ifconfig -a

Display eth0 address and details

ifconfig eth0

Query or control network driver and hardware settings

ethtool eth0

Send ICMP echo request to host

ping host

Display whois information for domain

whois domain

Display DNS information for domain

dig domain

Reverse lookup of IP_ADDRESS

dig -x IP_ADDRESS

Display DNS ip address for domain

host domain

Display the network address of the host name.

hostname -i

Display all local ip addresses

hostname -I

Download http://domain.com/file

wget http://domain.com/file

Display listening tcp and udp ports and corresponding programs

netstat -nutlp

9 – ARCHIVES (TAR FILES)

Create tar named archive.tar containing directory.

tar cf archive.tar directory

Extract the contents from archive.tar.

tar xf archive.tar

Create a gzip compressed tar file name archive.tar.gz.

tar czf archive.tar.gz directory

Extract a gzip compressed tar file.

tar xzf archive.tar.gz

Create a tar file with bzip2 compression

tar cjf archive.tar.bz2 directory

Extract a bzip2 compressed tar file.

tar xjf archive.tar.bz2

10 – INSTALLING PACKAGES

Search for a package by keyword.

yum search keyword

Install package.

yum install package

Display description and summary information about package.

yum info package

Install package from local file named package.rpm

rpm -i package.rpm

Remove/uninstall package.

yum remove package

Install software from source code.

tar zxvf sourcecode.tar.gz cd sourcecode ./configure make make install

11 – SEARCH

Search for pattern in file

grep pattern file

Search recursively for pattern in directory

grep -r pattern directory

Find files and directories by name

locate name

Find files in /home/john that start with “prefix”.

find /home/john -name ‘prefix*’

Find files larger than 100MB in /home

find /home -size +100M

12 – SSH LOGINS

Connect to host as your local username.

ssh host

Connect to host as user

ssh user@host

Connect to host using port

ssh -p port user@host

13 – FILE TRANSFERS

Secure copy file.txt to the /tmp folder on server

scp file.txt server:/tmp

Copy *.html files from server to the local /tmp folder.

scp server:/var/www/*.html /tmp

Copy all files and directories recursively from server to the current system’s /tmp folder.

scp -r server:/var/www /tmp

Synchronize /home to /backups/home

rsync -a /home /backups/

Synchronize files/directories between the local and remote system with compression enabled

rsync -avz /home server:/backups/

14 – DISK USAGE

Show free and used space on mounted filesystems

df -h

Show free and used inodes on mounted filesystems

df -i

Display disks partitions sizes and types

fdisk -l

Display disk usage for all files and directories in human readable format

du -ah

Display total disk usage off the current directory

du -sh

15 – DIRECTORY NAVIGATION

To go up one level of the directory tree. (Change into the parent directory.)

cd ..

Go to the $HOME directory

cd

Change to the /etc directory

cd /etc

Conda/Anaconda

This link provides some simple commands for working with the conda CLI.