Running OSX 10.11.2, I need to rm -rf the file in the location indicated in the symbolic links below for atom, npm and node as well as the links. I am currently log in as a user but terminal is in su mode.
I tried few commands for no avail. I tried to go to those locations but do not know how to.
Thank you
To start, rm itself does not have a suitable option to remove the files that the links point to. That would make it cumbersome to do this in a single command. A script helps:
#!/bin/sh
for name in "$#"
do
if [ -L "$name" ]
then
target=$(stat -f '%Y' "$name")
$0 "$target"
fi
[ -e "$name" ] && rm -rf "$name"
done
The script uses the OSX stat command to obtain the link target, and recurs to itself, to remove the target (which could be another link), and after removing the target, removes the link (or non-link, as the case may be).
In a comment, OP clarified that the link itself should not be removed. That can be done by changing the test:
#!/bin/sh
for name in "$#"
do
if [ -L "$name" ]
then
target=$(stat -f '%Y' "$name")
$0 "$target"
fi
[ -e "$name" ] && [ ! -L "$name" ] && rm -rf "$name"
done
Related
I don't know a lot about scripting, but I was attempting to write my own.
Context:
I have 2 servers. When server 1 (ubuntu server) automatically adds files to server 2 (synology) (through docker container) permissions of those files are wrong, so some applications I'm running can't access them.
I wanted to write a script that checks for permissions periodically and then changes them to what I want.
I've been messing with it for some days and this is what I've got for now:
#!/bin/bash
shopt -s nullglob
FOLDERS=(/volume1/files/videos/* /volume1/files/photos/*)
for folder in "${FOLDERS[#]}"; do
# [[ -d "$folder" ]]
if [ "$(stat -c '%a' "$folder")" != "755" ] || [ "$(stat -c '%U' "$folder")" != "my_user" ]
then
# echo "Change user permissions of $folder"
chown -R my_user:users "$folder" && chmod 755 -R "$folder"
fi
done
shopt -u nullglob
The problem whit this script is that when files (and not folders) are added it won't detect those files.
So I change the script to this: (find files in directories and the change top directory's permissions recursively)
#!/bin/bash
shopt -s nullglob
FOLDERS=(/volume1/files/videos/* /volume1/files/photos/*)
FILES=(/volume1/files/videos/*/*.mp4 /volume1/files/photos/*/*/*.jpg)
for file in "${FILES[#]}"; do
if [ "$(stat -c '%a' "$file")" != "755" ] || [ "$(stat -c '%U' "$file")" != "my_user" ]
then
# echo "$file" | (cut -d "/" -f5) # --> WORKS PERFECTLY
rootfolder=$(("$file") | (cut -d "/" -f5))
# echo "$rootfolder" # --> ERROR: cannot execute binary file: Exec format error
# chown -R my_user:users "$rootfolder" && chmod 755 -R "$rootfolder"
echo "chown -R my_user:users "$rootfolder" && chmod 755 -R "$rootfolder" "
fi
done
When I add "$file" | (cut -d "/" -f5) into a variable "rootfolder" ("/volume1/files/videos" and "/volume1/files/photos", I'm getting this error when executing the script:
./modify_media_permissions-test.sh: line 12: /volume1/files/videos/my_video.mp4: cannot execute binary file: Exec format error
Tried different thing, but can't resolve it.
Could someone help me further?
I know it is probably not the best and most efficient script, but I'm learning :)
Thanks a lot!
I am running below commands in a script
move_jobs() {
cd $JOB_DIR
for i in `cat $JOBS_FILE`
do
if [ `ls | grep -i ^${i}- | wc -l` -gt 0 ]; then
cd $i
if [ ! -d jobs ]; then
mkdir jobs && cd .. && mv "${i}"-* "${i}"/jobs/
else
cd .. && mv "${i}"-* "${i}"/jobs/
fi
error_handler $?
fi
done
}
but it failing as
mv: cannot stat `folder-*': No such file or directory
Not sure why move command is failing with regular expression
Your script is overly complicated and has several issues, one of which will be the problem, I guess it's the ls | grep ... part, but to find that out, you should include some debug logging.
for i in $(cat ...) loops through words, not lines.
Do not parse ls
And if you still do, do not ever grep for filenames but include it in your ls call: ls "${i}"-* | wc -l.
You do not need to check if a folder exists when the only thing that is different then is that you create it. You can use mkdir -p instead.
Jumping around folders in your script makes it almost unreadable, as you need to keep track of all cd commands when reading your script.
You could simply write the following, which I think will do what you want:
xargs -a "$JOBS_FILE" -I{} \
sh -c "
mkdir -p '$JOB_DIR/{}/jobs';
mv '$JOB_DIR/{}-'* '$JOB_DIR/{}/jobs';
"
or if you need more control:
while IFS= read -r jid; do
if ls "$JOB_DIR/$jid-"* &>/dev/null; then
TARGET_DIR="$JOB_DIR/$jid/jobs"
mkdir -p "$TARGET_DIR"
mv "$JOB_DIR/$jid-"* "$TARGET_DIR"
echo "OK"
else
echo "No files to move."
fi
done < "$JOBS_FILE"
I need a script that will search for a file in a applications directory and delete it. If it's not there it will continue with the install.
What I'm needing deleted:
/Applications/Cydia.app/Sections/Messages(D3#TH's-Repo).png
If that's not found I want it to continue on the install. If it finds that file I want it to delete it before continuing the installation.
This is what I've got:
#!/bin/bash
file="/Applications/Cydia.app/Sections/Messages(D3#TH's-Repo).png"
if [ -f "$file" ]
then
echo "$file delteling old icon"
rm -rf /Applications/Cydia.app/Sections/Messages(D3#TH's-Repo).png
else
echo "$file old icon deleted already moving on"
fi
try this
#!/bin/bash
if [ -e <your_file> ]; then
rm -f <your_file>
fi
this should do.
Parentheses are used to start a subshell in bash, so you'll need to put your filename in double-quotes (as you did in the file test).
Change the line:
rm -rf /Applications/Cydia.app/Sections/Messages(D3#TH's-Repo).png
To:
rm -rf "${file}"
And this will remove the file (assuming no permissions problems).
I have a shell (ksh) script. I want to determine whether a certain directory is present in /tmp, and if it is present then I have to delete it. My script is:
test
#!/usr/bin/ksh
# what should I write here?
if [[ -f /tmp/dir.lock ]]; then
echo "Removing Lock"
rm -rf /tmp/dir.lock
fi
How can I proceed? I'm not getting the wanted result: the directory is not removed when I execute the script and I'm not getting Removing Lock output on my screen.
I checked manually and the lock file is present in the location.
The lock file is created with set MUTEX_LOCK "/tmp/dir.lock" by a TCL program.
In addition to -f versus -d note that [[ ]] is not POSIX, while [ ] is. Any string or path you use more than once should be in a variable to avoid typing errors, especially when you use rm -rf which deletes with extreme prejudice. The most portable solution would be
DIR=/tmp/dir.lock
if [ -d "$DIR" ]; then
printf '%s\n' "Removing Lock ($DIR)"
rm -rf "$DIR"
fi
For directory check, you should use -d:
if [[ -d /tmp/dir.lock ]]; then
echo "Removing Lock"
rm -rf /tmp/dir.lock
fi
I am working on a local login script for some Macintosh clients. I would like to unmount a number of drives on the machine prior to attempting to mount them again, similar to what I do on Windows clients. Listed below is the code that I have; however, I was curious if there was a way in which to optimize this or condense the code?
if [ -d "/Volumes/Share1" ]; then
umount -f /Volumes/Share1
fi
if [ -d "/Volumes/Share2" ]; then
umount -f /Volumes/Share2
fi
if [ -d "/Volumes/Share3" ]; then
umount -f /Volumes/Share3
fi
This just seems bulky and kludgy and I am not sure how to make it any better.
Perhaps something like
VOLUMES="/Volumes/Share1 /Volumes/Share2 /Volumes/Share3"
for volume in $VOLUMES ; do
[ -d $volume ] && umount -f $volume
done
(You'd need some modifications to this to handle names with spaces in them though)
A for loop?
for dir in /Volumes/Share[123]; do
if [ -d "$dir" ] ; then
umount -f $dir
fi
done
In real life, it's unlikely to be Share[123] you want to deal with, so I'd expect you want to put the actual list there, like
for dir in "/Volumes/My Share" "/Volumes/pr0n" "/Volumes/warez"; do
etc.
You can always use functions to reduce duplication:
unmount_if_necessary() {
[ -d "$1" ] && umount -f "$1"
}
unmount_if_necessary /Volumes/Share1
unmount_if_necessary /Volumes/Share2
#...