rm -r -f doesn't delete inner folder - bash

I have large projects and some scripts to compile them. I can't add all code here, so I'll try to simplify the problem: in the cleaning part, I need to clean folder named directory which contains other directory named innerDir. I have this bash command for cleaning directory:
clean:
rm -r -f directory
When directory is a folder that I created with mkdir -p beforehand. When I clean, I get this error:
rm: cannot remove 'directory': Directory not empty
But when I try to enter directory , I see that it's empty. So for debugging, I modified my cleanning part to be:
rm -r -f directory/*
find directory
rmdir directory
(it's suppose to do the same, but here I also get the chance to see if all the content of directory was really deleted).
Now I get this error:
find: 'directory/innerDir': Permission denied
There are two things that unclear for me here:
(1). innerDir was created with makedir -p before the clening part, without any change to the permissions of it later in the code. Why don't I have permission to delete it?
(2). If I try to clean again- the cleaning succeed and I don't have any permission problem. So, if I got permission error in the first time I tried to delete it, why don't I get it in the second time?

If your permissions are valid down the directory tree, rm -fr directory ought to work.
If you don't have read access on innerDir, then is it possible/likely (depending on running processes, perhaps) that something has written to innerDir, but the file gets cleaned up after so that the directory becomes free?
Can you give examples of permissions, ownership, and some scope of the operations happening between each step?
Could you rename the parent folder while working, and/or lock it's permissions to prevent other users or processes from altering things?

Related

What are .dat.nosyncb77b.* files and how to remove them?

I am trying to remove the project directory but receive a Permission denied for .dat.nosyncb77b.* files. What are these files and how can I remove them?
After trying a number of different approaches to solve an Xcode issue with Could not load authors for file 'Pods/Pods.xcodeproj/project.pbxproj' I have tried removing the whole project directory in an attempt to clone the project fresh from git.
However, when running rm -rf project-dir I got Permission denied for .dat.nosyncb77b. files. What is strange is that I am the owner of the files. I have tried changing the permissions to 755 and 775 but no luck. All the other directories and files were successfully removed apart from these .dat.nosyncb77b. files.
None of my other projects have those files. I have cloned the repo again under a different directory and could see that those files do not exist in the same project.xcworkspace/xcuserdata/username.xcuserdatad/ where those files are for that project.
It could be ACLs preventing deletion, these are shown with the e option to ls eg ls -le However I'd put money on the Locked flag being set, as I've encountered the occasional file with the Locked flag.
The easiest way to see this is to perform a Get Info (from Finder) on the files.
In the General Panel and uncheck the Locked checkbox.
It can also be changed on the command line with the command GetFileInfo look at the attributes: line and look for the uppercase L, this means the locked flag is set. It shows as lowercase if unset.
file: "/Users/mark/Documents/20200605-pdftext/test"
type: "\0\0\0\0"
creator: "\0\0\0\0"
attributes: avbstcLinmedz
created: 01/30/2021 14:43:54
modified: 01/30/2021 14:43:54
To change the locked status use SetFile -a l filename

Error copying directories at command line using cp

Mac OS X Yosemite v.10.10.5.
I am trying to use the cp command to copy one Git directory to another.
This command-line statement:
cp -r /path/to/dir/from/ /path/to/dir/to/
Returns this error:
cp: /path/to/dir/to/.git/objects/00/00ad2afeb304e18870d4509efc89fedcb3f128: Permission denied
This error is returned one time each for (what I believe, but haven't verified, is) every file in the directory.
The first time I ran the command it worked properly, as expected, without error. But, without making any changes to any files, the second (and subsequent) times I ran the command, I got the error.
What's going on? And how can I fix this?
Edit:
In response to a question in the comment:
What does ls -l /path/to/dir/to/.git/objects/00/00ad2afeb304e18870d4509efc89fedcb3f128 show?
The answer is it shows:
-r--r--r-- 1 myusername staff 6151 May 6 00:45 /path/to/dir/to/.git/objects/00/00ad2afeb304e18870d4509efc89fedcb3f128
The reason you are getting Permission Denied is because you are trying to overwrite a file that already exists in the destination directory that has read only permissions set on it. Since it appears you're trying to overwrite it you could just remove the destination directory if it exists before the copy operation. Also you should use -R, not -r ...
Historic versions of the cp utility had a -r option. This
implementation
supports that option; however, its use is strongly discouraged, as it
does not correctly copy special files, symbolic links, or fifo's.
Using a command such as this should resolve your issue:
[[ ! -d dest ]] || rm -rf dest ; cp -R src dest
The above checks if dest exists; if it does recursively remove it, then copy the source to dest,
You may want cp -rp for this operation. -p preserves the user and group IDs associated with the file. Try starting over using -p and see if that solves the issue.
Anther reason you might be seeing this issue is if the permission really is denied. That is, if you're trying to copy into a folder owned by another user without superuser privileges.

Shell script CP cannot overwrite directory

our users have written a shell script to copy an application into into the /Applications folder on OSX. it works great the first time, but the second time they get an error. This is a new development, it apparently used to work fine before we changed the App name.
The shell script runs the following:
cp -a ApplicationName.app /Applications
open -a '/Applications/ApplicationName.app/Contents/MacOS/ApplicationName' --args -LSRC autolaunch
The first time it runs, it works fine, the application is copied over and then it launches. the second time it comes back with the following errors
[jrivera#chamomile] $ sudo ./InstallScript.sh /SRNM ABC1234567
cp: cannot overwrite directory /Applications/ApplicationName.app/Contents/Frameworks/Sparkle.framework/Headers with non-directory ApplicationName.app/Contents/Frameworks/Sparkle.framework/Headers
cp: cannot overwrite directory /Applications/ApplicationName.app/Contents/Frameworks/Sparkle.framework/Resources with non-directory ApplicationName.app/Contents/Frameworks/Sparkle.framework/Resources
cp: cannot overwrite directory /Applications/ApplicationName.app/Contents/Frameworks/Sparkle.framework/Versions/A/Resources/fr_CA.lproj with non-directory ApplicationName.app/Contents/Frameworks/Sparkle.framework/Versions/A/Resources/fr_CA.lproj
cp: cannot overwrite directory /Applications/ApplicationName.app/Contents/Frameworks/Sparkle.framework/Versions/A/Resources/pt.lproj with non-directory ApplicationName.app/Contents/Frameworks/Sparkle.framework/Versions/A/Resources/pt.lproj
cp: cannot overwrite directory /Applications/ApplicationName.app/Contents/Frameworks/Sparkle.framework/Versions/Current with non-directory ApplicationName.app/Contents/Frameworks/Sparkle.framework/Versions/Current
I'm not exactly sure why that's happening. it's the exact same script in the exact same location copying the exact same things 30 seconds apart. I dug into each and the directories and files all appear the exact same file type. I tried adding other commands to the cp to force it (-RfXv) but got the same thing. Any ideas? maybe it's a strange thing with sparkle?
I would suspect that the problematic files/directories have some extended attributes, and that cp is having problems overwriting the target when it has those attributes. (cp when preserving permissions often seems unreliable on different platforms).
Given that, there are a couple of workarounds to explore:
remove the target /Applications/ApplicationName.app before re-copying it.
use rsync, e.g.,
rsync -vaz ApplicationName.app/ /Applications/ApplicationName.app
Removing the target first may interfere with people using it while you are updating it; rsync works incrementally (and almost always updates more rapidly than cp).

Why am i getting "Directory not empty" error in terminal when using rmdir?

I am a walking through a tutorial and it lets me delete two directories(im using one 10 deep, all empty) but once i try to remove the third it gives me that error message, even though there is not content in the directory and i am in the directory above it. Why is this? By the way i am using terminal.
That error is reported when the directory is not empty.
To find out what files are in that directory use ls -a. The -a flag tells ls to list hidden files (aka "dot files"). Remove those files then rmdir can be used.
Another option is to simply use rm -rf to recursively delete the directory and all of its files. NOTE: this can be dangerous if you give the wrong parameters - resulting in deleting more than you intended.
you can remove all hidden files by using rm -R ./.* you have to be in the directory the hidden file is in for it to work
On Windows I recently has the same problem, and deleting everything in {UserFolder}\AppData\Local\Composer\files didn't helped.
What I've done is to launch multiple times the composer install --dry-run command until it listed all dependencies, then I successfully ran the composer install command.

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