combine cp / chmod to modify perms during cp - bash

I was looking for a way to cp a file and mod its perms to 400 at the same time... after some testing in the public_html folder...
public_html >> ls -lah
-rw-r--r-- 1 user user 0 Feb 27 14:21 a.txt
public_html >> cp a.txt{,.bak}
-rw-r--r-- 1 user user 0 Feb 27 14:21 a.txt
-rw-r--r-- 1 root root 0 Feb 27 14:23 a.txt.bak
perms are still the same (644) and although the file is owned by root, it is still readable via public_html
public_html >> cp a.txt{,.bak} && chmod 400 a.txt.bak
-rw-r--r-- 1 user user 653 Feb 27 14:26 a.txt
-r-------- 1 root root 653 Feb 27 14:30 a.txt.bak
this works but looking for something for a set newbs to use
awk/sed command possibly?
dont think I'm missing a cp flag that could modify the perms, wasn't seeing anything and don't think there are but wanted to pick the collective brain
thanks...

install(1) can both copy files and create directories, and set their permissions at the same time.
install -m 0400 foo bar/

Related

Wrong owner upon file creation in particular directory

I'm facing #subj . If I try to create a file/dir in my home directory it gets created as root:daemon instead of user:staff. I found this behaviour only for one directory ( all the other dirs aren't affected).
It used to create files properly before and now it sets root:daemon with 644.
I can't see any guid or sticky bits, etc.
What do I miss?
$ whoami
user
$ pwd
/home/user
$ touch 1
$ ll 1
-rw-r--r-- 1 root daemon 0 Jul 31 09:50 1
$ ls -ld /home/user/
drwxr-xr-x 13 user staff 4096 Jul 31 09:50 /home/user/
$ ls -ld /home/
drwxr-xr-x 5778 root staff 450560 Jul 31 08:21 /home/
$ umask
0022
I might be due to file access control set to root:daemon. If you run
getfacl /home/user
it should tell you if that was the problem. If yes, then you can set per-folder with the command setfacl with the parameters you prefer.
Another cause that comes to my mind is if that is a mountpoint masked with those particular user and group; you can check that with cat /etc/fstab.

TAR override the contents of the directory

My understanding of tar command that it will override the content of the file if file exist. Otherwise it would keep as existing.
[root#something~]# ls -al /etc/init.d/
total XX
drwxr-xr-x. 2 root root 83 Jun 14 2018 .
drwxr-xr-x. 10 root root 127 Jun 6 2017 ..
-rwxr-xr-x. 1 root root 7293 Jan 2 2018 network
-rw-r--r--. 1 root root 1160 Feb 20 2018 README
[root#something~]# tar tvf /tmp/env_pkg_1.tar
drwxr-xr-x staff 0 2020-05-29 19:42 etc/
drwxr-xr-x user/staff 0 2020-05-29 18:04 etc/init.d/
-rw-r--r-- user/staff 3383 2020-05-29 18:04 etc/init.d/sshd
[root#something~]# cd /
[root#something /]# tar xf /tmp/env_pkg_1.tar
[root#something/]# ls -al /etc/init.d/
total 16
drwxr-xr-x 2 XXXXXX XXXXXX 18 May 29 18:04 .
drwxr-xr-x. 85 XXXXXX XXXXXX 8192 May 29 19:42 ..
-rw-r--r-- 1 XXXXXX XXXXXX 3383 May 29 18:04 sshd
I am not understand why tar is replacing the entire contents of /etc/init.d
Any inputs would be helpful ?
I belive that /etc/init.d is a link to /etc/rc.d/init.d.
When you untarred that file, it overwrote the link with a directory. All of your files are still in /etc/rc.d/init.d.
To fix your situation, remove /etc/init.d, relink it, and add a h to the tar command:
rm -rf /etc/init.d
cd /etc
ln -s ./rc.d/init.d
cd /
tar xhf /tmp/env_pkg_1.tar
You can use -k or --keep-old-files, so it does not touch any files that are already within the destination. Judging by your output in /etc/init.d/ you want to keep network and README and next to them extract sshd, so in your case, they do not overlap.
Alternatively --keep-newer-files will have tar replace files that are newer from the tar archive, than what's on the destination..

I have a problem copying files in bash from subdirectories to parent directories

Hello everyone
I have a bash script to install my bash based application (they are just bash scripts that I wrapped to work as an application with Platypus.
I have my applications that I need to copy to the parent directory in the resources folder of the "installing application".
It goes something like this:
AFolderWhereItGotDownloaded/
Install.app/
Contents/
MacOS/
Info.plist
Resources/
Application1.app/ <-- This is the application that I want to move to "AFolderItGotDownloaded"
Application2.app/ <-- This is the application that I want to move to "AFolderItGotDownloaded"
AppSettings.plist
AppIcon.icns
MainMenu.nib
script <-- This is the script that runs when I double click the app
The only thing I want to do is cp -r or rsync -r the two applications to AFolderWhereItGotDownloaded/
What I've done so far:
#!/bin/bash
#The directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &>/dev/null 2>&1 && pwd )"
#The "parent" directory
PARENTDIR="../../.."
cp -r "${DIR}/Application1.app" "${PARENTDIR}/Application1.app"
cp -r "${DIR}/Application2.app" "${PARENTDIR}/Application2.app"
What I get:
cp: ../../../Application1.app: Permission denied
cp: ~/Downloads/AFolderWhereItGotDownloaded/Install.app/Contents/Resources/Application1.app: unable to copy extended attributes to ../../../Application1.app: Permission denied
With rsync
#!/bin/bash
#The directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &>/dev/null 2>&1 && pwd )"
#The "parent" directory
PARENTDIR="../../.."
rsync -r "${DIR}/Application1.app" "${PARENTDIR}/Application1.app"
rsync -r "${DIR}/Application2.app" "${PARENTDIR}/Application2.app"
What I get:
rsync: mkdir "~/../../../Application1.app" failed: Permission denied (13)
rsync error: error in file IO (code 11) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-52.200.1/rsync/main.c(545) [receiver=2.6.9]
rsync: connection unexpectedly closed (8 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-52.200.1/rsync/io.c(453) [sender=2.6.9]
If someone knows what's going on it would be nice...
OS: macOS Mojave (10.14.6)
The file information window states that I have permissions to read and write the folders.
Results of ls -l
Inside AFolderWhereItGotDownloaded
total 16
drwxr-xr-x# 3 <myusername> staff 96 4 mai 00:13 Install.app
-rwxr-xr-x# 1 <myusername> staff 1069 28 avr 13:20 LICENSE.txt
-rwxr-xr-x# 1 <myusername> staff 2586 3 mai 15:05 README.md
Inside Install.app
total 0
drwxr-xr-x# 5 <myusername> staff 160 4 mai 00:13 Contents
Inside Contents
total 8
-rwxr-xr-x# 1 <myusername> staff 632 3 mai 14:56 Info.plist
drwxr-xr-x# 3 <myusername> staff 96 4 mai 00:13 MacOS
drwxrwxrwx# 17 <myusername> staff 544 4 mai 13:43 Resources
Inside Resources
total 728 (I have other apps too but to keep it clean I only stated two "Applications" here)
-rwxrwxrwx# 1 <myusername> staff 313202 3 mai 14:54 AppIcon.icns
-rwxrwxrwx# 1 <myusername> staff 499 3 mai 14:56 AppSettings.plist
drwxrwxrwx# 3 <myusername> staff 96 4 mai 00:13 Application1.app
drwxrwxrwx# 3 <myusername> staff 96 4 mai 00:13 Application2.app
-rwxrwxrwx# 1 <myusername> staff 42236 3 mai 14:56 MainMenu.nib
-rwxrwxrwx# 1 <myusername> staff 940 4 mai 14:29 script
PARENTDIR="../../.." is wrong. If you execute cd ~; pwd, it will show something like /home/user. Since this is two deep, you should use PARENTDIR="../.." instead.
Also, you have to have root-like privileges (root, sudo) to create files in the root directory. It's better to the software in /opt instead and add it to the users PATH variable, eg export PATH=$PATH:/opt/myapp/bin/. For /opt you also need root-like privileges.
Actually, the problem was that macOS sandboxes applications downloaded from the web: The only way of getting it working is to put the apps into the Applications folder.

Bash script acts differently depending on what executes it

I have a bash script which acts as a post-process script for utorrent-server that passes on variables to a media renamed called FileBot.
Script:
#!/bin/bash
TORRENT_NAME=$1
TORRENT_PATH=$2
TORRENT_LABEL=$3
TORRENT_KIND=$4
TORRENT_TITLE=$5
/usr/share/filebot/bin/filebot.sh -script fn:amc --output "/mnt/Storage/" \
--log-file "amc.log" --action move --conflict override -non-strict \
--def music=n subtitles=en artwork=n xbmc="192.168.0.123" deleteAfterExtract=y \
clean=y "ut_dir=$TORRENT_PATH" "ut_file=$TORRENT_NAME" "ut_kind=$TORRENT_KIND" \
"ut_title=$TORRENT_TITLE" "ut_label=$TORRENT_LABEL" "ut_state=5" "seriesFormat=TV \
Shows/{n}/Season {s.pad(2)}/{n} - {s00e00} - {t}" "movieFormat=Movies/{n} ({y})/{n} ({y})" \
&>> /home/xbmc/run.log
If i run this script manually, it works as intended, however when uTorrent executes it, it returns "No such file or directory." via stderr. I originally had uTorrent calling this script directly however I was having the same issue.
Does anyone know what could cause this?
UPDATE (Permissions for all directories/folders):
drwxr-xr-x 3 root root 4096 Nov 27 23:52 /home
drwxr-xr-x 20 xbmc xbmc 4096 Dec 15 21:46 /home/xbmc
drwxr-xr-x 10 root root 4096 Oct 17 06:51 /usr
drwxr-xr-x 218 root root 4096 Dec 13 15:32 /usr/share
drwxr-xr-x 3 root root 4096 Dec 15 15:55 /usr/share/filebot
drwxr-xr-x 2 root root 4096 Dec 15 18:56 /usr/share/filebot/bin
-rwxr-xr-x 1 xbmc xbmc 615 Dec 15 21:44 /home/xbmc/run.sh
-rwxr-xr-x 1 root root 552 Dec 15 18:56 /usr/share/filebot/bin/filebot.sh
Change the current working directory.
IF filebot.sh is the no-such-file, I suggest you to try with this:
chmod -R a+x /usr/share/filebot/bin/filebot.sh
IF it is your run.sh,
chmod -R a+x /home/xbmc/run.sh
You can try running filebot.sh as the owner. I think it worths a shot.
chown YOURUSERNAME /usr/share/filebot/bin/filebot.sh
chmod u+s /usr/share/filebot/bin/filebot.sh

Bash/Shell scripting

What does the command cp $1/. $2 do? I know cp is used for copying from source(stored in variable $1) to destination(stored in variable $2). I am just confused with the /. used along with the variable. Can someone please help me understand this?
The command:
$ cp -R $1/. $2
copies contents of directory pointed by $1 to the directory $2.
Without -R switch this command would fail both when $1 is a file or directory.
In general, . points to the current directory. You can see that by comparing inode's shown by ls:
$ mkdir test
$ ls -ali
9525121 drwxr-xr-x 3 IU wheel 102 23 mar 12:31 .
771046 drwxrwxrwt 21 root wheel 714 23 mar 12:30 ..
9525312 drwxr-xr-x 2 IU wheel 68 23 mar 12:31 test
$ cd test
$ ls -ali
9525312 drwxr-xr-x 2 IU wheel 68 23 mar 12:31 .
9525121 drwxr-xr-x 3 IU wheel 102 23 mar 12:31 ..
Note that inode 9525312 points to test when viewed from the parent directory, and points to . when viewed from inside the test directory.

Resources