Linux Table of Content




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

required
required


Linux Screen

Imagine having to execute a long running process and waiting for it to finish, most likely you are connected via ssh and you can’t do anything else but waiting for that process to finish. When the process is taking too long your connection will time out and you will be kicked out. When that happens your process will stop its execution and you will have to rerun it. This is painful and here is where screen comes into the picture.

For example, you are downloading a big file or a database using mysql dump command. This usually takes 10 to 20 minutes. 

You can use the Linux screen command to push running terminal applications or processes to the background and pull them forward when you want to see them. It also supports split-screen displays and works over SSH connections, even after you disconnect and reconnect. 

How does screen work?

The usual operation with screen is to create a new window with a shell in it, run a command, and then push the window to the background (called “detaching”). When you want to see how your process is doing, you can pull the window to the foreground again (“reattach”) and use it again. This is great for long processes you don’t want to accidentally terminate by closing the terminal window.

Once you’ve got a screen session running, you can create new windows and run other processes in them. You can easily hop between windows to monitor their progress. You can also split your terminal window into vertical or horizontal regions, and display your various screen windows in one window.

You can connect to a remote machine, start a screen session, and launch a process. You can disconnect from the remote host, reconnect, and your process will still be running.

You can share a screen session between two different SSH connections so two people can see the same thing, in real-time.

Intall screen

# ubuntu
sudo apt update
sudo apt install screen

# centos or fedora
sudo yum install screen

Start a screen session

screen

# start screen session with a name
screen -S loading-database

This will open a screen session, create a new window, and start a shell in that window.

Most used commands for a screen window

  • Ctrl+a c Create a new window (with shell).
  • Ctrl+a " List all windows.
  • Ctrl+a 0 Switch to window 0 (by number).
  • Ctrl+a A Rename the current window.
  • Ctrl+a S Split current region horizontally into two regions.
  • Ctrl+a | Split current region vertically into two regions.
  • Ctrl+a tab Switch the input focus to the next region.
  • Ctrl+a Ctrl+a Toggle between the current and previous windows
  • Ctrl+a Q Close all regions but the current one.
  • Ctrl+a X Close the current region.

Detach from a screen session

Ctrl+a Ctrl+d

 

The program running in the screen session will continue to run after you detach from the session.

Reattach to a screen session

# resume current screen session, assuming one screen session
screen -r

In case you have multiple screen sessions running on your machine, you will need to append the screen session ID after the r switch.

# list the current running screen sessions 
screen -ls

# output
There is a screen on:
        30093.pts-0.ip-172-31-8-213     (09/24/21 16:07:23)     (Attached)
1 Socket in /run/screen/S-ubuntu.

# command
screen -r 30093

Terminate a screen session

Make sure you on the screen session and then run the exit command.

exit

 

September 24, 2021

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

SFTP – transfer file from your local to a remote server

This example is from a mac to a ubuntu linux server

sftp folauk-dev

 

 

 

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