// avoid using below thing
int mid = (start + end) / 2;
// correct approach
int mid = start + ((end - start) / 2);
Sunday, January 30, 2022
Calculate mid value in indexes/number
Calculate length/number of digits in a Number
int n=12345;
int number_of_digits = (int)Math.log10(n)+1;
GCD/HCF and LCM
Euclidean Theorm: gcd (a, b) = gcd (b, a % b)
HCF * LCM = a * b
public class GCD_LCM {
public static void main(String[] args) {
int a = 36;
int b = 24;
int original_a = a, original_b = b;
while (b > 0) {
int rem = a % b;
a = b;
b = rem;
}
System.out.println("HCF: " + a);
System.out.println("LCM: " + original_a * original_b / a);
}
}
Sunday, January 23, 2022
Run Executable JAR in background on Linux
Nohup means, no hang up.
The
nohup utility makes the command passed as an argument run in the background
even after you log out.
After the command is
run, it is the foreground process
- nohup java -jar xxx.jar
& Let the program
run in the background
- java -jar xxx.jar &
Processing of log files
The
command will generate one by default nohup.out, file to
log, standard output and error output will be in this file.
nohup.out file can be
accessed using the below-mentioned command:
- tail –f nohup.out
Resources:
·
https://developpaper.com/nohup-and-let-the-program-run-in-the-background/#:~:text=use%20nohup%20java%20%2Djar%20xxx,you%20specify%20%3E%20Specify%20your%20file.
Saturday, January 22, 2022
Docker: Copy file from Server to Docker Container and vice versa
The docker cp utility
copies the contents of SRC_PATH to the DEST_PATH. You can copy
from the container’s file system to the local machine or the reverse, from the
local filesystem to the container.
The docker
cp command assumes container paths are relative to the
container’s / (root) directory. This means supplying the initial
forward slash is optional;
Below mentioned commands
are treated as identical:
- myContainer:/tmp/myFile.txt
- myContainer:tmp/myFile.txt
Local machine paths can
be an absolute or relative value.
The command interprets a
local machine’s relative paths as relative to the current working directory
where docker cp is run.
Copying Remote Server File to Docker Container:
docker cp
[source_pathOfFile_RemoteServer] [docker_containerName]:[destination_path_dockerContainer]
docker cp
/tmp/myFile.xls myContainer:/tmp/
Copying Docker Container File to Remote Server:
docker cp
[docker_containerName]:[sourcepathOfFile_dockerContainer] [destination_pathOfFile_RemoteServer]
docker cp
myContainer:/tmp/myFile.xls /tmp/myFile.xls
Note: These commands need to be executed from
Remote Server on which docker is installed and any docker container is up and
running.
Resources:
· https://docs.docker.com/engine/reference/commandline/cp/
Linux: SCP Command to Securely Transfer Files
SCP (secure copy) is a command-line utility that allows you to
securely copy files and directories between two locations.
With scp, you can copy a file or directory:
- From your local system to a remote system.
- From a remote system to your local system.
- Between two remote systems from your local system.
It
uses the same authentication and security as it is used in the Secure Shell
(SSH) protocol
Before
you Begin
The scp command relies on ssh for
data transfer, so it requires an ssh key or password to authenticate on the
remote systems.
The colon (:) is how scp distinguish
between local and remote locations.
To be able to copy files, you must
have at least read permissions on the source file and write permission on the
target system.
Be careful when copying files that
share the same name and location on both systems, scp will overwrite files without warning.
Copying
Local File to Remote System:
scp [source_file_path_LOCAL]
[user]@[Destination_Host_IP]:[destination_file_path_REMOTE]
scp C:\Prabhat\fileName.xls prabhatk@10.10.0.2:/tmp/
Copying
Remote File to Local System:
scp [user]@[Destination_Host_IP]:[source_file_path_REMOTE]
[destination_file_path_LOCAL]
scp prabhatk@10.10.0.2:/tmp/ fileName.xls C:\Prabhat\
Note: These commands need to be executed from Local System
to perform above operation.
Resources:
·
https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/
·
https://www.geeksforgeeks.org/scp-command-in-linux-with-examples/
Popular Posts
-
Clients one which are requesting information they want from somewhere eg. browser, devices, any program - java, etc. generally clients spea...
-
BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in )); int n = Integer . parseInt ( br . readL...
Most Featured Post
GitHub: Squash all commits in PR
Command Meaning git fetch origin Get the latest origin/master . git reset --soft origin/master Move your branch to match origin/master , b...