Linux Table of Content




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Linux SSH

SSH stands for “Secure Shell”. It is a protocol used to securely connect to a remote server. ssh is secure in the sense that it transfers the data in encrypted form between the host and the client. It transfers inputs from the client to the host and relays back the output. ssh runs at TCP/IP port 22.

SSH with username and password

ssh {options} username@host

host can be ip address or domain name. You will be prompted to enter password

ssh ubuntu@19423455

 

SSH with private and public keys

ssh -i “/path-to-IdentityFile” username@host

With aws ec2, you have a .pem file as the identity file.

ssh -i "test.perm" ubuntu@folauk.com

 

Add custom connection options

When you ssh into a server, you most likely use a key. But it takes time to type out the key and other options. It would be nice to have to option of just typing out something like ssh my-linux-tester in which case you know exactly where to go. It turns out you can do this kind of thing. SSH has a config file in the ~/.ssh directory. This config file can be configured for your custom connections with these options:

  • HostName: The actual hostname that should be used to establish the connection. This replaces any alias defined in the Host header. This option is not necessary if the Host definition specifies the actual valid hostname to connect to.
  • User: The username to be used for the connection.
  • Port: The port that the remote SSH daemon is running on. It’s default to port 22 if not specified.
  • IdentityFile : The public identity file.
# Personal linux server for testing
Host my-linux-tester
  HostName ec2-tester.folaukaveinga.com
  User ubuntu
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/personal/test.perm

Now you can just do this to ssh into server. ssh {Host} in which case you will ssh into the ec2-tester.folaukaveinga.com server

ssh {Host}

ssh my-linux-tester

 

 

November 15, 2020

Linux prepend content to a file

Prepend content to a file

Use sed with -i option. You can also specify the line to which the content will prepend to.

sed -i '' '1s/^/package home;/" Home.java

 

Prepend content to multiple files

Here I need to add a package to 200 java files. To do this manual would be tedios. 

for f in *.java
do
  sed -i "" "1s/^/package home;/" $f 
done

 

May 3, 2020

Linux Search/Filters

 

Grep

Grep command stands for “global regular expression print”. grep command filters the content of a file which makes our search easy.

grep {search-key-word} {filename}

// search for word lisa in test.txt file
grep 'lisa' test.txt

grep -n

The -n option display the line number

grep -n 'lisa' test.txt

grep -v

The -v option displays lines not matching to the specified word.

// search for any word that is not lisa in test.txt file 
grep -v 'lisa' test.txt

grep -i

The -i option  filters output in a case-insensitive way.

grep -i 'lisa' test.txt

 

grep -w

By default, grep matches the given string/pattern even if it found as a substring in a file. The -w option to grep makes it match only the whole words.

grep -w 'lisa' test.txt

 

Sed

SED command in UNIX is stands for stream editor and it can perform lot’s of function on file like, searching, find and replace, insertion or deletion. Though most common use of SED command in UNIX is for substitution or for find and replace. By using SED you can edit files even without opening it, which is much quicker way to find and replace something in file, than first opening that file in VI Editor and then changing it.

Replace a string

// replace the word lisa with lisak
sed 's/lisa/lisak/' test.txt

// To edit every word, we have to use a global replacement 'g'
sed 's/lisa/lisak/g' test.txt

// replace the second occurence of lisa with lisak
sed 's/lisa/lisak/2' test.txt

// replace from 2nd occurence to the all occurrences
sed 's/lisa/lisak/2g' test.txt

// replace lisa with lisak on line 2
sed '2 s/lisa/lisak/' test.txt
 

Delete lines from a file

sed ‘nd’ {filename}

// delete 3rd line in test.txt
sed '3d' test.txt


// delete from 3rd to 6th line
sed '3,6d' test.txt

// delete from 3rd to the last line
// sed '3,$d' test.txt

 

 

 

April 6, 2020

Linux Zip

Zip

Zip is a compression tool. Zip files have the .zip extension. zip is very useful when you are on a limited bandwidth and need to send a big file over the internet.

zip {options} {zipFilename} {files…}

zip zipfile.zip test.txt test1.txt

unzip {zipFilename}

unzip myfile.zip

 

Gzip

gzip command compresses files. Each single file is compressed into a single file. The compressed file consists of a GNU zip header and deflated data.

If given a file as an argument, gzip compresses the file, adds a “.gz” suffix, and deletes the original file. With no arguments, gzip compresses the standard input and writes the compressed file to standard output.

 

gzip {options} {files…}

gzip test.txt test1.txt

gunzip {gzipFile}

gunzip test.gz

 

 

 

 

March 6, 2020

Linux Vi Editor

vi stands for visual editor and comes as one of default applications in every Linux/Unix system. The vi editor has two modes.

  • Command Mode: In command mode, actions are taken on the file. The vi editor starts in command mode. Here, the typed words will act as commands in vi editor. To pass a command, you need to be in command mode.
  • Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key will take you to the command mode from insert mode.

If you are not sure which mode you are in, press Esc key twice and you’ll be in command mode.

 

Create a file with vi {filename}

vi test.txt
~                                                                                                                                                                                                     
~                                                                                                                                                                                                     
~                                                                                                                                                                                                     
~                                                                                                                                                                                                     
~                                                                                                                                                                                                     

 

Save file

// save and quit
:wq

// save
:w

// quit
:q

// save as filename
:w fname

// save and quit
ZZ

// quit and disregard work
:q!

// quit and save qork
:wq!

 

How to delete on command mode

// delete current character
x

// Replace the current character
r

// Switch two characters
xp

// Delete the current line
dd

// Delete the current line from current character to the end of the line
D

// delete from the current line to the end of the file
dG

 

Repeat and undo

// Undo the last command
u

// repeat the last command
.

 

Copy, Cut, and Paste

// Delete a line
dd

// (yank yank) copy a line
yy

// Paste after the current line
P

// Paste before the current line
p

// Delete the specified n number of lines
{n}dd

// Copy the specified n number of lines
{n}yy


 

Search string

// Forward search for given string
/{string}

// Backward search for given string
?{string}

// Forward search string at beginning of a line
/^{string}

// Forward search string at end of a line
/{string}$

// Go to next occurrence of searched string
n

// Search for the word he (and not for there, here, etc.)
/\<he\>

// Search for place, plbce, and plcce
/pl[abc]ce

 

Replace all

:{startLine,endLine} s/{oldString}/{newString}/g

// Replace forward with backward from first line to the last line
:1,$ s/readable/changed/

 

  

 

 

March 6, 2020