Linux Command to Convert an Absolute Path to a Current Existing Relative Path - linux

I already have the relative path: /home/Folder1/Folder2 which its original absolute path is /home/user1/Folder1/Folder2. And I have several scripts that are using /home/Folder1/Folder2. Now, I need to delete user1 so I created user2 with the same structure of user1 so now I have a new path which is /home/user2/Folder1/Folder2. If I delete user1, my scripts will then fail because they are using the relative path /home/Folder1/Folder2 which its original absolute path is /home/user1/Folder1/Folder2. So I want my new path /home/user2/Folder1/Folder2 to point to /home/Folder1/Folder2 so that my scripts won't fail and I don't want to go through the trouble of opening each script and change the relative path to my new created path. Any idea how I can do this?

I guess, you got confused between soft links and absolute/relative path.
I assume you have a soft link created from "/home/Folder1/Folder2" pointing to "/home/user1/Folder1/Folder2" and you want to delete user1 directory and create user2 directory with same structure. If my assumption is right, recreate the softlink "/home/Folder1/Folder2" to point to "/home/user2/Folder1/Folder2". Your existing scripts will work seamlessly.

Related

Node creating file 1 level up from the current path

I was using path.resolve but this command created a monster folder that can't be deleted called lib..
path.resolve(__dirname + "../assets/pic/" + `${fileName}.png`)
Question 1
What is the propper usage to create a folder 1 level up from the current path?
Question 2
How to remove the lib../assets/pic folder? Deleting the entire project or using git reset --hard, git stash doesn't work because Windows 10 says the folder doesn't exist.
Answer to Question 1:
tl;dr
const fs = require('fs')
const folderName1DirUp = '../SomeFolder'
try {
if (!fs.existsSync(folderName1DirUp)){
fs.mkdirSync(folderName1DirUp)
}
} catch (err) {
console.error(err)
}
The back story:
Two ways to reference the current folder in a Node.js script. You can use ./ or __dirname. Note in addition to ./ you can also use ../ which points to the parent folder.
The difference between the two.
__dirname in a Node script will return the path of the folder where the current JavaScript file resides.
./ will give you the current working directory (and ../ the parent). For ./ a call to process.cwd(). returns the same thing.
Initially the current working directory is the path of the folder where you ran the node command, but during the execution of your script it can change by calling process.chdir(...).
There is one place where ./ refers to the current file path which is in a require(...) call. There the ./, for convenience, refers to the JavaScript file path which lets you import other modules based on the folder structure.
Thus the call to fs.mkdirSync('../SomeFolder') with the given folder name makes a folder one level up from the current working directory.
Answer to Question 2:
From a PowerShell prompt Remove-Item './../Folder1UpToDelete' -Force The ./ is for the current folder. The ../ is for one folder up from the current. Finally the Folder1UpToDelete is the one folder up from the current that you want to delete. The -Force makes sure to delete all sub-folders/files under the folder you want deleted including hidden and/or read-only files.
To answer the first question, to create a path 1 level up, you can use path.join():
path.join(__dirname, "../assets/pics", `${fileName}.png`);
For the second question, if deleting it through the explorer doesn't work, you can try:
fs.rmdirSync("E:/path/to/broken/folder..");
Using Git Bash and running
cd /c/path/to/broken/
rmdir folder..

Creating A Folder In the Temp Folder

I'm trying to create a folder within the temp folder that doesn't have a random name.
Here is how I was trying to create the folder within the temp folder.
if not DirExists(ExpandConstant('{%tmp}\Utilities\SDK')) then
CreateDir(ExpandConstant('{%tmp}\Utilities\SDK'));
Log('Temp\Utilities\SDK Folder Has Been Created.');
I had a look at this thread, but even with the %, unfortunately, it still doesn't create the folder.The script compiles and runs as expected, however the folder doesn't create even though it says it has in the log file, I understand that the log file will say that because its told too, however, if the folder was unable to be created, wouldnt it crash? or return a false if an if statement was present?
With CreateDir() You must create dirs one after the other and not a dir structure at once.
if not DirExists(ExpandConstant('{tmp}\Utilities')) then
CreateDir(ExpandConstant('{tmp}\Utilities'));
if not DirExists(ExpandConstant('{tmp}\Utilities\SDK')) then
CreateDir(ExpandConstant('{tmp}\Utilities\SDK'));
if DirExists(ExpandConstant('{tmp}\Utilities\SDK')) then
Log('Temp\Utilities\SDK Folder Has Been Created.') else
Log('Temp\Utilities\SDK Folder ERROR : NOT Created.');
Inno Setup has a function to create a dir structure at once
function ForceDirectories(Dir: string): Boolean;
Example:
if not DirExists(ExpandConstant('{tmp}\Utilities\SDK')) then
ForceDirectories(ExpandConstant('{tmp}\Utilities\SDK'));
Also keep in mind :
{tmp} all is related to the Inno Setup Temp folder is-XXXXX.tmp
C:\Users\...\AppData\Local\Temp\is-XXXXX.tmp
{%temp} is the users Temp folder
C:\Users\...\AppData\Local\Temp
I think you want the Windows Temp and not the tmp from InnoSetup
{tmp}:
Temporary directory used by Setup or Uninstall. This is not the value of the user's TEMP environment variable. It is a subdirectory of the user's temporary directory which is created by Setup or Uninstall at startup (with a name like "C:\WINDOWS\TEMP\IS-xxxxx.tmp"). All files and subdirectories in this directory are deleted when Setup or Uninstall exits. During Setup, this is primarily useful for extracting files that are to be executed in the [Run] section but aren't needed after the installation.
So I think you want to do somethink like this:
if not DirExists(ExpandConstant('{%temp}\Utilities\SDK')) then
CreateDir(ExpandConstant('{%temp}\Utilities\SDK'));
Log('Temp\Utilities\SDK Folder Has Been Created.');

Relative addressing files python3

I have variable address = /data/train/1.jpg, and I'm trying to read file by
im = Image.open(address)
FileNotFoundError: [Errno 2] No such file or directory: '/data/train/1.jpg'
By some reasons I can't use full name of file.
I started jupyter notebook from folder which actually contains file 1.jpg in /data/train/.
How can I fix it?
Relative addressing means from the perspective of the current working directory. So if you script is in the same directory that the data folder is in, your path to the file would be ./data/train/1.jpg. Note the ./, which means the current directory.
use relative path, this one is absolute
address = './data/train/1.jpg'
im = Image.open(address)
in this case . means current location while slash means the environments root (view and explanation) on this depends on your OS

Resolving paths in a custom root directory and avoiding going in parent folders

Using path.resolve we can resolve paths using Node.js.
I want to have a directory containing files and other directories and so on. From the client I receive a path as string and I have to resolve it in this directory.
Because of security, I have to prevent the result path to be outside of the directory:
// This comes from the client
let filePath = "/../../../some-sensitive-data-from/disk";
/* Here I have to check if the path is inside of the directory */
path.resolve(ABSOLUTE_PATH_TO_MY_DIR + filePath);
How to prevent the access to parent directories of ABSOLUTE_PATH_TO_MY_DIR?
Obviously a solution would be to check if the final path is still inside of the ABSOLUTE_PATH_TO_MY_DIR path. But maybe there are other better solutions.

Require file somewhere in the directory node.js

I have a file that is required in many other files, that are on different folders, inside the main directory.
Is there a way to just require the filename without having to write the relative path, or the absolute path? Like require('the_file'). And without having to go to npm and install it?
Create a folder inside your main directory , put the_file.js inside and set the NODE_PATH variable to this folder.
Example :
Let's say you create a ./libs folder within your main directory, you can just use :
export NODE_PATH = /.../main/lib
after that, you can require any module inside this directory using just :
var thefile = require('the_file')
To not have to do that every time, you'd have to add the variable to your .bashrc (assuming you're running a Unix system).
Or you can set a global variable inside your app.js file and store the path of your 'the_file' in it like so :
global.rootPath = __dirname;
Then you can require from any of your files using :
var thefile = require(rootPath+'/the_file')
These are the most convenient methods for me, short of creating a private npm, but there are a few other alternatives that I discovered when looking up an answer to your question, have a look here : https://gist.github.com/branneman/8048520

Resources