Which of the following commands accept the --preserve-root flag? - bash

Which of the following commands accept the --preserve-root flag?
chgrp
chmod
rm
rsync

for cmd in chgrp chmod rm rsync; do
man "$cmd" | grep -q "preserve-root" && echo "$cmd"
done
OR
$ for i in chgrp chmod rm rsync; do $i --help | grep -q '\-\-preserve\-root' && echo $i; done
chgrp
chmod
rm

Related

How to do auto upgrade to latest github software release version?

Releases uploads every time to url like
https://github.com/ipfs/go-ipfs/releases/tag/v0.9.1
my script is
#!/bin/bash
rm /home/ipfs/go-ipfs -rf
rm go-ipfs.tar.gz
curl -s https://api.github.com/repos/ipfs/go-ipfs/releases/latest | grep linux-amd64.tar.gz\" | grep download | sed 's/.*: \"//g' | sed 's/\"//g' | wget -i - -O /home/ipfs/go-ipfs.tar.gz
if test -f /home/ipfs/go-ipfs.tar.gz then
tar -xf /home/ipfs/go-ipfs.tar.g
newsize=$(wc -c <"/home/ipfs/go-ipfs/ipfs")
cursize=$(wc -c <"/home/ipfs/ipfs")
if [$newsize -ne $cursize]; then
mv /home/ipfs/go-ipfs/ipfs /home/ipfs/ipfs
chmod +x /home/ipfs/ipfs
pkill ipfs
fi
fi
but it has an error i cant fix
Solution is
#!/bin/bash
#remove old repo folder
rm /home/ipfs/go-ipfs -rf
#remove old tar.gz
rm go-ipfs.tar.gz
#try to download new
curl -s https://api.github.com/repos/ipfs/go-ipfs/releases/latest | grep linux-amd64.tar.gz\" | grep download | sed 's/.*: \"//g' | sed 's/\"//g' | wget -i - -O /home/ipfs/go-ipfs.tar.gz
#check file exists
if [ -f /home/ipfs/go-ipfs.tar.gz ]; then
#unpack tar gz
tar -xf /home/ipfs/go-ipfs.tar.gz
#get file sizes
newsize=$(wc -c <"/home/ipfs/go-ipfs/ipfs")
cursize=$(wc -c <"/home/ipfs/ipfs")
#if new file is not as current
if (($newsize != $cursize)); then
#replace it
mv /home/ipfs/go-ipfs/ipfs /home/ipfs/ipfs
chmod +x /home/ipfs/ipfs
#kill old to restart new
pkill ipfs
fi
fi

Run multiple commands, but run the last one in the background

I have 4 commands I want to run:
sudo mkdir -p /data/db && \
sudo chmod 755 /data/db && \
sudo chown -R addison.pan: /data && \
mongod &
I only want to run mongod in the background if the other 3 above it succeed. But when I type this into bash, it runs the whole thing as one background task. How do I only make the mongod run in the background, and only if it gets to it?
To run one or more commands in a separate process, enclose that series of commands in parentheses. As specified in the Single Unix Specification, §2.9.4 “Compound Commands”:
( compound-list )
Execute compound-list in a subshell environment […]
To group one or more commands in the same shell process, enclose that series of commands in curly braces:
{ compound-list ; }
Execute compound-list in the current process environment. […]
That's true for any POSIX shell (so it also works in Bash).
So your example can be changed to:
sudo mkdir -p /data/db && \
sudo chmod 755 /data/db && \
sudo chown -R addison.pan: /data && \
( mongod & )
That may be good because you want the mongod process separated. On the other hand, a more general answer would be to group the list of commands within the same shell process:
sudo mkdir -p /data/db && \
sudo chmod 755 /data/db && \
sudo chown -R addison.pan: /data && \
{ mongod & }
Both these are described in the above documentation references.
Be explicit. There's no need to try to abuse the syntax to use a short-circuit:
if \
sudo mkdir -p /data/db \
&& sudo chmod 755 /data/db \
&& sudo chown -R addison.pan: /data
then
mongod &
fi
Use parens:
sudo mkdir -p /data/db && \
sudo chmod 755 /data/db && \
sudo chown -R addison.pan: /data && \
(mongod &)
You could run multiple commands within sudo, for example:
sudo sh -c 'mkdir -p /data/db && chmod 755 /data/db && chown -R <user>: /data' \
&& mongod &

Change permissions on a user folder - will this shell script work?

I am trying to change permissions on a user's folder that is not an administrator. I was told to run this script:
{
sudo chflags -R nouchg,nouappnd ~ $TMPDIR..
sudo chown -R $UID:staff ~ $_
sudo chmod -R u+rwX ~ $_
chmod -R -N ~ $_
} 2> /dev/null
But I am afraid that it will chown my user folder, instead of the user I am trying to fix.

inotifywait adding multiple files at ones

this shell script should add everything put in the folder to transmission. With one folder it works fine, but when i add more then one folder at the same moment it ignores the second one.
while true;
do
file=$(inotifywait -e moved_to --format %f /srv/watchfolderfilme)
file="/srv/watchfolderfilme/$file"
transmission-create -o $file.torrent -s 16384 -t http://0.0.0.0:6969/announce $file
mv $file /srv/downloads
chmod 0777 $file.torrent
cp $file.torrent /srv/newtorrentfiles
mv $file.torrent /srv/watchfoldertorrents
done
Rethough my solution and found a better one that works fine for multiple adds
inotifywait -m /srv/watchfolderfilme -e create -e moved_to |
while read path action file; do
# echo "The file '$file' appeared in directory '$path' via '$action'"
chmod 0777 $path$file
transmission-create -o /srv/newtorrentfiles/$file.torrent -s 16384 -t http://0.0.0.0:6969/announce $path$file
mv $path$file /srv/downloads
chmod 0777 /srv/newtorrentfiles/$file.torrent
cp /srv/newtorrentfiles/$file.torrent /srv/watchfoldertorrents
done

basic BASH variable call failing in script

Why is this basic variable call in my script failing?
The script is just below and the errors outputted in terminal after execution are below the script.
Line 8 is the first sudo command.
I am executing this script as root in terminal for now. It works just fine if I execute the commands manually, one-at-a-time, within terminal...
I would be grateful for any insight.
#!/bin/bash
echo Enter username
read NAME
echo Enter number
read NUM
sudo (cd /Users/$NAME && tar c .) | (cd /Users/$NUM && tar xf -)
sudo chown -R $NUM:"Domain Users" /Users/$NUM
sudo chmod g+rwx /Users/$NUM
Stephen-Kucker:Desktop root# ./stackoverflowq.txt
Enter username
jsteinberg-c
Enter number
admin
./stackoverflowq.txt: line 8: syntax error near unexpected token `cd'
./stackoverflowq.txt: line 8: `sudo (cd /Users/$NAME && tar c .) | (cd /Users/$NUM && tar xf -)'
Try this:
sudo tar -C /Users/$NAME -c . | sudo tar -C /Users/$NUM -xf -
You need to use the -s option to pass an arbitrary shell command (like the pipeline shown) to the shell with sudo:
sudo -s "(cd /Users/$NAME && tar c .) | (cd /Users/$NUM && tar xf -)"

Resources