symlink installed java to /usr/java/latest - linux

A program requires a java directory location of /usr/java/latest. In many cases this directory does not exist. In order to get the program to run correctly I need to do the following steps:
mkdir -p /usr/java
mkdir -p /usr/java/latest
Then I need to find what java installations are available and symlink them to the above directories:
ll /usr/lib/jvm
I usually choose the latest directory avialable...ie:
drwxr-xr-x. 3 root root 16 Feb 12 2016 java-1.8.0-openjdk-1.8.0.71-2.b15.el7_2.x86_64
and then create a link to /usr/java/latest
ln -s /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.71-2.b15.el7_2.x86_64/jre/lib /usr/java/latest/lib
ln -s /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.71-2.b15.el7_2.x86_64/jre/bin /usr/java/latest/bin
I am trying to create an installation script that automates the above process without having to copy and paste the directory name for the symbolic link. Basically I want to parse the /usr/lib/jvm/ directory for the most recent (or any) available java directory. Then I want to take that and link the lib and bin directories within that directory to the /usr/java/latest directory. Thank you in advance for any type of advice.

I am using the below to find the directory I need.
ls -F /usr/lib/jvm | grep / | sed '$!d' | sed 's:/*$::'

Related

How to Create a long listing of all the files in this directory to a file called etcFiles.txt (In Linux)

I started to learn Linux. But I dont know how to solve this problem. I want to Create a long listing of all the files in /etc/ directory to a file called etcFiles.txt.When i try to run this terminal says "Permission denied".enter image description here
To long list a file in Linux, you need to use the command
ls -l
It displays the contents on the console. To store it in the file you need to redirect it using redirection operation > to a file like
ls -l directoryPath > outputFile.txt.
Here, to store the result of long listing /etc/ to file you need to use
ls -l /etc/ > etcFiles.txt
In the image linked, to store the contents of the current directory to a file you need to provide the current directory as the argument to ls command. In Unix/ Linux, the current directory is represented by ., so as shown in the screenshot, you are already in /etc/ directory, to store long listing contents of current directory i.e. /etc/ to the file, you need to use
ls -l . > ~/etcFiles.txt
However ls command takes the current directory as default argument . above can be avoided and the following command will also work
ls -l > ~/etcFiles.txt
Linux /Unix by default does not give any user permission to write/ create files in /etc/ directory and requires elevated permission to make any changes in this directory. Since you do not have permission to create a new file in /etc/ directory, either you need to redirect the output to the file in some directory where you have permission like above, we are storing it in the home directory ~ or else you will have to use sudo for superuser permission to create new file in /etc/ itself.
Since we need redirection operator to write file in /etc/, we can't simply run
sudo ls -l > etcFiles.txt
because ls will run with superuser permission and redirection will be done with default user permission. So you need to club in both to run in elevated permission.
To achieve that spawn a new shell with elevated permission using sudo sh and pass the command as a string with -c option as shown below
Solution 1
sudo sh -c 'ls -l . > etcFiles.txt'
Solution 2
You can make use of pipe | by piping the output of ls -l to a command called tee which basically reads the standard input and writes it to both the standard output and one or more files.
Since you need to write to a file inside /etc/ directory, you need to run tee with sudo for elevated permission.
ls -l | sudo tee etcFiles.txt
This will also print the output to the console. To avoid output to the console, redirect output to /dev/null (take it as dustbin sink to throw unwanted outputs) and your final command becomes
ls -l | sudo tee etcFiles.txt > /dev/null

cp: target '/root/var/www/html/' is not a directory

I am using Ubuntu windows 10 bash and I'd like to move a project from /mnt/i/Projects/Template to run it on Apache server which located in /var/www/html.
I tried to copy a folder from a direct to new directly but unfortunately I got an error which is:
cp -r /mnt/i/Projects/Template ~/var/www/html/
cp: target '/root/var/www/html/' is not a directory
I would like to test those templates with Apache and I tried to change Apache directly.
Another test I did:
root#DESKTOP-4PBGG1N:/var/www# ls -ld ~/var ~/var/www ~/var/www/html
ls: cannot access '/root/var': No such file or directory
ls: cannot access '/root/var/www': No such file or directory
ls: cannot access '/root/var/www/html': No such file or directory
First of all the directory for the apache server is not in root it's just "/var/www/html". If it still doesn't work you probably doesn't have apache installed, you can do that by running these two lines "lsb_release -a" and "sudo apt-get install apache2". There will come an error when trying to launch the apache server (with "sudo service apache2 start"), but just ignore it you can still use it without any problems. Hope it helps ;)
try creating directory if the only problem is '/root/var/www/html/' not being a directory
# mkdir -pv ~/var/www/html/
# cp -r /mnt/i/Projects/Template ~/var/www/html/
before that just make sure that apache is installed and configured
have a nice day
For instance you have a file in Documents called index.php and to be copied in the /root/var/www/html/ directory you have to do it this way:
First don't forget to use sudo to be super user and then
- sudo cp -Rv index.php /var/www/html or
- sudo cp -Rv index.php /root/var/www/html
And you will get this output: 'index.php' -> '/var/www/html/index.php'
-R for copy folders &
-v for see what folders and files are copied

How to Create a path in my shell for windows?

I have a question, I created a script but I need to create a path, to find my cassandra folder to execute cqlsh, this is the route
C:/apache-cassandra-3.11.4/bin/cqlsh
the problem is because I am in windows not in linux, and I want to see if possible to create an a path in case that I need to pass my script to my team. but you know your cassandra db is in another route for that reason I want to do that because I need to execute this script
example
route= /../cassandra3.11.4/bin/cqlsh
$route -k fsainstqual -e "TRUNCATE instrumentmanufacturer"
If you mean you don't know how to mix Windows drive letters and Linux directory handling (which does not have drives), you'll need mounting points, as you can see in following mount excerpt on my PC, where I have a Linux app installed:
Prompt>mount
...
C: on /mnt/c type drvfs (rw,noatime,uid=1000,gid=1000,case=off)
E: on /mnt/e type drvfs (rw,noatime,uid=1000,gid=1000,case=off)
In top of that, on my root directory, I've created symlinks to those mounted directories:
Prompt>cd /
Prompt>$ ls -ltra | grep "\->"
lrwxrwxrwx 2 root root 6 Nov 23 2017 C -> /mnt/c
lrwxrwxrwx 2 root root 6 Nov 14 12:00 E -> /mnt/e
I think it's better to clarify your question buddy.
If you want to change your current directory, you can use "cd" command as in linux shell.
If you want to create a path use "md" or "mkdir" command.
Here is the help in windows OS
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/md

Is it portable to call executable with full path "/bin/..."?

For the RTOS I'm writing ( http://www.distortos.org ) I need to run find as part of the build configuration process (from make menuconfig target). For Windows I assume that user has MSYS2 installed, so find.exe is available. The only problem with this particular file is that Windows also has such file in C:/Windows/system32 (supposedly it is something close to grep). So depending on the order of folders in your PATH environment variable you get one or the other if trying to call the file by just the name.
I've found that calling this program as /bin/find from the Makefile or in shell script works both in Windows and on (my) Linux. What is most important - doing it that way always calls find.exe from MSYS2, no matter what is the order of folders in PATH. So I'm wondering - is it OK to call find this way, or maybe it is not portable and I just had luck that it works for me?
It would probably be more portable to refer to it as /usr/bin/find. For example, on Fedora /bin is actually a symlink to /usr/bin, so either works:
bash-4.3$ ls -l /bin/find
-rwxr-xr-x. 1 root root 222608 Dec 28 18:26 /bin/find
bash-4.3$ ls -l /usr/bin/find
-rwxr-xr-x. 1 root root 222608 Dec 28 18:26 /usr/bin/find
But on a recent Ubuntu:
root#69ca68fbe5c0:/# ls -l /bin/find
ls: cannot access /bin/find: No such file or directory
root#69ca68fbe5c0:/# ls -l /usr/bin/find
-rwxr-xr-x. 1 root root 229992 Jan 6 2014 /usr/bin/find
I would recommend not hard-coding the path to find and instead instruct Windows users that they must run your script inside the MSYS2 environment. MSYS2 will put its own bin directories near the beginning of the path so that find always gets the MSYS2 version instead of the Microsoft version.
If you hard-code the path to find then you make things be more brittle than they need to be.

Copy files from one user home directory to another user home directory in Linux

I have the logins and passwords for two linux users (not root), for example user1 and user2.
How to copy files
from /home/user1/folder1 to /home/user2/folder2, using one single shell script (one single script launching, without manually switching of users).
I think I must use a sudo command but didn't found how exactly.
Just this:
cp -r /home/user1/folder1/ /home/user2/folder2
If you add -p (so cp -pr) it will preserve the attributes of the files (mode, ownership, timestamps).
-r is required to copy hidden files as well. See How to copy with cp to include hidden files and hidden directories and their contents? for further reference.
sudo cp -a /home/user1/folder1 /home/user2/folder2
sudo chown -R user2:user2 /home/user2/folder2
cp -a archive
chown -R act recursively
Copies the files and then gives permissions to user2 to be able to access them.
Copies all files including dot files, all sub-directories and does not require directory /home/user2/folder2 to exist prior to the command.
(shopt -s dotglob; cp -a /home/user1/folder1/* /home/user2/folder2/)
Will copy all files (including those starting with a dot) using the standard cp. The /folder2/ should exist, otherwise the results can be nasty.
Often using a packing tool like tar can be of help as well:
cd /home/user1/folder1
tar cf - . | (cd /home/user2/folder2; tar xf -)
I think you need to use this command
sudo -u username /path1/file1 /path2/file2
This command allows you to copy the contents as a particular user from any file path.
PS: The parent directory should be list-able at least in order to copy files from it.
Just to add to fedorqui 'SO stop harming' answer.
I had this same challenge when I tried to change the default admin user for a server from stage_user to prod_user on an Ubuntu 20.04 machine:
First, I created a prod_user using the command below:
sudo adduser prod_user
And then I added the newly created prod_user to the sudo group:
sudo adduser prod_user sudo
Next, I copied all the directories that I needed from the home directory of the stage_user to the prod_user:
sudo cp -r /home/stage_user/folder1/ /home/prod_user/
Next, I changed the ownership of the copied folders from stage_user to prod_user to avoid permission issues:
sudo chown prod_user:prod_user /home/prod_user/folder1
That's all.
I hope this helps
The question has to to do with permissions across users.
I believe by default home permission does allow all people to do listing and changing working directory into another's home:
eg. drwxr-xr-x
Hence in the previous answers people did not realise what you might have encountered.
With more restricted settings like what I had on my web host, nonowner users cannot do anything
eg. drwx------
Even if you use su/sudo and become the other user, you can still only be ONE USER at one time, so when you copy the file back, the same problem of no enough permission still apply.
So. . . use scp instead, treat the whole thing like a network environment let me put it that way and that's it. By the way this question had already been answered once over here (https://superuser.com/questions/353565/how-do-i-copy-a-file-folder-from-another-users-home-directory-in-linux), only cared to reply because this ranked 1st result from my search.

Resources