Databricks throws an error when I try to delete a folder that doe not exist:
databricks workspace delete -r /Shared/myfolder
Error message:
Error: b'{"error_code":"RESOURCE_DOES_NOT_EXIST","message":"Path (/Shared/myfolder) doesn\'t exist."}'
So I would probably need to check if the folder exists before deleting it?
Pseudo code example:
if [ -d "/Shared/myfolder" ]; then databricks workspace delete -r /Shared/myfolder ; fi
How can I implement this using Databricks CLI?
There is no separate function in the CLI (and REST API) to check existence of resource. You have two choices:
Just ignore the error - if you don't want to see it in the script, just add > /dev/null at the end of command
Use ls subcommand to check existence of directory, and then delete (but I personally don't see benefit from that). Something like this:
FOLDER=/Shared/myfolder
databricks workspace ls $FOLDER > /dev/null
RES=$?
if [ $RES -eq 0 ]; then
databricks workspace delete -r $FOLDER
fi
I would personally go with first approach
Related
I wrote a script to monitor a particular directory using inotify. If any new directory got created. I want to replace it with that newly created directory name in 'z.sh' file. But I am getting the whole path instead of directory name while using the below script.
#!/bin/sh
MONITORDIR1="/var/lib/clickhouse/data/"
monitor() {
inotifywait -m -r -q $MONITORDIR1 | while read NEWFILE
do
echo "NEW FOLDER CREATED"
cp /home/boctrainee/clickhousescriptold.sql /home/boctrainee/clickhousescript.sql
echo "${NEWFILE#*/*/*/*/*/}"
sed -i -e "s+NEWFILE#*/*/*/*/*/+$NEWTENANTNAME+g" /home/boctrainee/clickhousescript.sql
done
}
monitor $MONITORDIR1 &
Please give your suggestions
If you want to know in which directory the file $NEWFILE (given with the full path) lives, just issue:
directory=$(dirname "$NEWFILE")
How to simply recreate copy/paste functionality like in gui environments?
My typical scenario for copying file/directory in Linux console is:
cp source_path target_path
Sometimes paths are relative, sometimes absolute, but I need to provide them both. It works, but there are situations where I would like to recreate scenario from gui which is:
1. go to source directory
2. copy file/directory
3. go to target directory
4. paste file/directory
I imagine something like
cd source_directory_path
copy_to_stash source_name
cd target_directory_path
paste_from_stash [optional_new_target_name]
I know that there is a xclip app, but a documentation says that it copies content of a file, not a file handle. Also, I can use $OLDPWD variable and expand it when I copy file, but this is not a solution without some cumbersome.
Is there some simple, general, keyboard only, not awkward to use equivalent?
I've also asked the same question on superuser and answer that I've received is good enough for me.
In short: two additional scripts and temporary variable to hold intermediate value.
Below is a code and link to original answer.
#!/bin/bash
# source me with one of:
# source [file]
# . [file]
# Initialize
sa_file=
sa(){
# Fuction to save a file in the current PWD
if [[ -e "$PWD/$1" ]]; then
sa_file=$PWD/$1
echo "Saved for later: $sa_file"
else
echo "Error: file $PWD/$1 does not exist"
fi
}
pa(){
# Paste if file exists, to $1 if exists
if [[ -e "$sa_file" ]]; then
if [[ $1 ]]; then
cp -v "$sa_file" "$1"
else
cp -v "$sa_file" .
fi
else
echo "Error: file $sa_file does not exist, could not copy"
fi
}
https://superuser.com/a/1405953/614464
The way I see it your only option is to write a script to do all of those steps. You could easily implement the clipboard functionality by copying the file to the /tmp directory before copying again from it.
This should work as intended.
Usage: script [from] [to]
filename=$(basename "$0")
tmpfile=/tmp/$filename.$RANDOM
cd $(dirname "$0")
cp $tmpfile $filename
cd $(dirname "$1")
cp $tmpfile $(basename "$1")
One option: you can either copy-paste the filename using mouse, using copy-paste feature from your terminal emulator (e.g. Konsole or GNOME Terminal), but this: 1) requires a GUI since the terminal emulator software run in GUI; 2) well, requires a mouse.
Another option: utilize shell tab completion. You still need to type the filename, but not all of it.
Third option, and this is closer to how you work in a GUI file explorer: use a TUI-based file explorer, e.g. the dual-pane style Midnight Commander. You can use arrow keys (if you turn on the Lynx-like motion setting, which is very recommended) to quickly navigate the directory tree. Then select files using the Insert, +, -, or * keys, then copy/move files from one pane to another. It's very convenient. In fact half of the time I spend in CLI, I spend in MC.
I'm looking for a command that would create a directory and bring me to it directly after, similar to:
$ mkdir project-one-business-dev-2
$ cd project-one-business-dev-2
I don't want to type the project's name twice because it's too long (I know I can use tab, but what if there are similar names?). Maybe only one command can do it.
A process can't change the working directory of it's parent process. That makes it impossible for an external command like mkdir to set the working directory of the calling shell to the newly created folder.
But you can create a bash function for that purpose. Put this for example into your .bashrc:
mkcd() {
mkdir -p "${1}"
cd "${1}"
}
You can do it like this:
mkdir project-one-business-dev-2 && cd "$_"
for more information check out this post on AskUbuntu
I am trying to create a sh/bash script to rename files according to directory names. For example if there is a directory named Linux, the files inside should be renamed Linux.jpg, Linux2.jpg, Linux3.jpg etc. There is also more then 1 directory within the main example directory with other files also.
it must work within my copy script if possible, i am copying the directory across to another folder, then when that is done i'd like the rename process to happen. Here is my copy script:
#!/bin/sh
if cp -r "$1" "$2"
then echo "copy success!"
else echo "copy failed!"
fi
It seems this question is already answered here, please check it out:
Shell script to rename files based on directory names
I'm trying to run a script that installs some files in a directory a user specifies. Once the user specifies the directory, I'd like to transfer the main file to that directory so it can perform so more tasks there before ultimately deleting itself once complete.
#prompt for directory in which to build project
read -p "Drag and drop the directory in which you'd like to build this project: "
echo "reply is $REPLY"
cp ./myScript.sh $REPLY
/bin/bash $REPLY/myScript.sh
I've got the script to execute the file from this question. I tried doing it with source $REPLY/myScript.sh as well as simply sh $REPLY/myScript.sh. I get the error /path/to/file/ is a directory
It must be that it doesn't known I'm trying to run myScript.sh, but I don't understand how I've given it a directory.
A likely cause is that drag-and-drop is putting whitespace after the directory name.
Thus:
/bin/bash $REPLY/myScript.sh
would be running
/bin/bash /path/to/directory /myScript.sh
A simple fix, if that's only a standard space, would be:
/bin/bash "${REPLY% }/myScript.sh"
You are missing the variable in read command so obiously it will fail as whatever you are reading is not getting stored. You can replace the read command as follows.
#prompt for directory in which to build project
read -p "Drag and drop the directory in which you'd like to build this project: " REPLY