Giving a script permission to execute mkdir on its own - macos

I have a file I downloaded from the Internet. When I run it in the osx terminal, one of the automated things it does is make a new directory in my /usr/local/bin, but this fails as terminal says that permission is denied. How do I give this file permission to execute the mkdir command? I know how to give myself permission with sudo, but not how to give this file permission to do the same on its own.

You can give your user permission to that folder by running sudo chown -R $(whoami) /usr/local/bin/. Once you make sure you own the directory and sub-directories (ls -l /usr/local and ls -l /usr/local/bin) your script should be able to write to those directories as well.
As a general rule of thumb, sudoing to work around permission errors just makes the problem worse. Fixing the underlying permissions take a few extra minutes but is better in the long run.

Related

Visual Studio Code always asks for sudo permissions

Ok, so I've got a problem with VSCode where every time I try to save a file it asks me for sudo permission and it's really annoying and I can't make folders or files directly in the IDE. Can please someone help me?
Screenshot from VSCode
The running instance of VS Code doesn't seem to have write permission to the file you wish to save.
Since the file is in the directory of user uteu3, first make sure that it is user uteu3 that has started VS Code (it probably is, if uteu3 is the user logged into the system).
make sure uteu3 is the owner of the file and change the permissions by opening a terminal and executing:
sudo chown uteu3 filename && sudo chmod 664 filename
(Replace filename with actual path+name to the file.)
If this doesn't solve the problem, please execute the following two lines in a terminal and provide the output:
ps aux | grep visual-studio-code
stat -f '%A %a %N' filename
Since you indicate that this happens repeatedly also with other files, maybe several of the file permissions in your home directory are problematic. In that case, consider trying to fix them all. See for example: Need to fix file permissions in a user's home directory
.

Download zip file from s3 using s3cmd issue

I have two issues I need help with on bash, linux and s3cmd.
First, I'm running into linux permission issue. I am trying to download zip files from a s3 bucket using s3cmd with following command in a bash script.sh:
/usr/bin/s3cmd get s3://<bucketname>/<folder>/zipfilename.tar.gz
I am seeing following error: permission denied.
If I try to run this command manually from command line on a linux machine, it works and downloads the file:
sudo /usr/bin/s3cmd get s3://<bucketname>/<folder>/zipfilename.tar.gz
I really don't want to use sudo in front of the command in the script. How do I get this command to work? Do I need to give chown permission to the script.sh which is actually sitting in a path i.e /foldername/script.sh or how do I get this get command to work?
Two: Once I get this command to work, How do I get it to download from s3 to the linux home dir: ~/ ? Do I have to specifically issue a command in the bash script: cd ~/ before the above download command?
I really appreciate any help and guidance.
First, determine what's failing and the reason, otherwise you won't find the answer.
You can specify the destination in order to avoid permission problems when the script is invoked using a directory that's not writeable by that process
/usr/bin/s3cmd get s3:////zipfilename.tar.gz /path/to/writeable/destination/zipfilename.tar.gz
Fist of all ask 1 question at a time.
For the first one you can simply change the permission with chown like :
chown “usertorunscript” filename
For the second :
If it is users home directory you can just specify it with
~user
as you said but I think writing the whole directory is safer so it will work for more users (if you need to)

How to compress root folders (/usr/) from user home directory in linux using script

Im writing shell script to take backup of nagios (/usr/local/nagios) directory. I'm unable to compress it from my home directory because its a root folder. i cannot use sudo as it asks for password. Is there any better way to zip /ussr/local/ files without comprimising security?
Someone please help.
Thank you!
Do it in a cron job owned by root.
Create this script and save it as /etc/cron.daily/nagios-backup:
#!/bin/sh
cd /usr/local
tarball="/home/kart/nagios-$(date +%Y-%m-%d).tgz"
tar -pzcf "$tarball" nagios
chmod 400 "$tarball"
Make sure that script is executable (sudo chmod 755 /etc/cron.daily/nagios-backup) and it'll run every night, dumping that log to a dated tarball file in /home/kart. The tarball will not be readable to you without using sudo (since your account can't normally see the contents and you requested preserving security).

mkdir/cp in Mac terminal gives "Permission denied" error

I'm trying to copy a directory in terminal from the Downloads directory to a sub-directory within the Applications/ directory, and I keep getting "Permission denied" error. Why is this so?
try
ls -lt
to see access permission of the folder you want copy file into
then use
chmod 777 your_folder_name
to change the access permissions of the folder
If you "right-click -> Get info" the Applications folder you will notice that the permissions for the Applications folder are Read/Write for 'system' or 'admin'. For 'everyone' it's Read-only. If you are not an admin you need to use 'sudo cp -R Downloads/___Test /Applications'. Obviously, you need to enter a password.
MacOS prevents interaction with downloaded files until you confirm that they are safe. You can effectively do this programmatically by running:
xattr -d com.apple.quarantine ~/Downloads/your_file_here
Then you should be able to copy the file.

Changing folder permissions from command line (on mac)

I'm trying to write a script that will let me add to an existing directory structure and copy a bunch of files into various places within this. However, using mkdir ... and cp... commands alone wont work since I do not have permission to do so. I understand that this can be changed manually in the 'Get Info' window, but this script will be run by others and its whole point is to save time and hassle.
Is there a way of adding to this script to give me permission to copy files to BASEDIR/SUBDIRS?
A bit more detail on what I'm doing:
I want to add to the directory BASEDIR with a bunch of SUBDIRS then copy files into these subdirectories. The problem is that I am receiving these 'permission denied' errors right after the mkdir BASEDIR/SUBDIR1/SUBDIR2 command.
Thanks
The command
sudo chmod -R ugo=rwx BASEDIR/
gives all folder permissions to all users to BASEDIR and all its subdirectories

Resources