How to recursively change permissions with backup file comparing permissions - linux

So in my work i did a mistake and it change all the file permissions (file owner and group) recursively inside a folder. I have the backup of that folder. I'm working inside a nsf server and my idea is to copy the permissions of the backup directory to the directory where i did the mistake recursively. All the folders and files have the same name and i want to grab the permissions from each file/directory from the backup and copy the permissions to the original directory, recursively.

chown can be uded to change ownership of a file or firectort
chown -R root:user /dir
changes the file ownership to user
-R perform the command recursively in a directory
same for
chmod -R 600 /dir
then you specify your permission and ownership in your own format

Related

How does Linux work with file permissions? Regular user can edit content of root file with permisions 644

One thing confuse me about file permissions in Linux.
root creates file root_file_644 with 644 permissions in user Alis home dir.
So as I know Alis can delete this file root_file_644 , as Alis is owner of dir /home/Alis and acording to linux permissions file deletion operation is dir operation, but Alis can't append contents to this file.
But on screenshot below you can see that Alis can change file owner of root file(file root_file_644) and appends contents to this file.
enter image description here

chmod cannot access to 'uploads/' :file or folder doesn't exist

i did this
sudo chmod -R 775 uploads/
and got this
chmod cannot access to 'uploads/' :file or folder doesn't exist
Your command is syntactically correct. The reason why it is reporting file or folder doesn't exist will be that the file or folder doesn't exist.
As you are using a relative path, you may wish to ensure that the current directory when you type the command is the parent directory of where you expect the uploads directory to reside. Note in particular that sudo will run the chmod command from the existing current directory (for example, it would not change to the home directory of the root user merely because it is running the command as root).
If you wish to check whether uploads is a valid path relative to the current directory, then use the ls command (without command-line arguments) to list the contents of the current directory, and see whether uploads (case-sensitive) is mentioned in the output.

How to make a folder/directory undeletable/unremovable for a user but still writable

This is needed for a case when it is necessary to create a folder in home directory of a user, to allow user read/write/remove files in the folder, but not allow to remove the folder itself (considering a regular user and not a sudoer).
In my case (i.e. RedHat) it was not enough just to put a file with root rights inside user`s folder, because the user owns the folder.
In my case if a user owns (or has all permissions on) a folder, he can remove it even with root file or empty folder inside.
I have made quite a number of experiments including playing with chown and permissions like 400, 000, o+t, 1775 etc. Initially I did not want to use chattr.
Meanwhile I have found a solution, which I share as an answer below; a variant that suits my needs so far.
Here is the solution I found myself.
Basically it uses the fact that when root subfolder is not empty, the user cannot remove it anymore.
In the below example superuser (root) each time creates (if does not exist already) a folder with user rights in user`s home directory, then puts inside a folder with root rights (if does not exist already), and inside that root folder puts a file with root rights.
## prepare directory for the external configuration
EXT_CONFIG_PATH="~user/.EXT_CONFIG"
mkdir -p ${EXT_CONFIG_PATH}
chown user:user ${EXT_CONFIG_PATH}
chmod 555 ${EXT_CONFIG_PATH}
mkdir -p ${EXT_CONFIG_PATH}/.rootguard
chmod o+t ${EXT_CONFIG_PATH}/.rootguard
touch ${EXT_CONFIG_PATH}/.rootguard/.rootguard
chmod 400 ~user/.EXT_CONFIG/.rootguard/.rootguard

How to copy files and give them permission of destination directory

I am copying files from source to location. The source is not owned by me and the permission for files at source is ----rwx---. The permission of files coped to destination directory which is owned by me is ----r-x---. The permission of destination directory is drwxrwsrwx. How do I have the files with same permission of destination directory. I tried "cp --no-preserve=all" but it did not work (still the same permission).
Try this:
cp --no-preserve=mode,ownership $backupfile $destination
Let me rephrase that to "How to preserve permissions of destination directory on copy?"
I can't take credit for the answer since I just combined a couple of answers I found on the wild. So here it comes.
Firstly
Permissions are generally not propagated by the directory that files are being copied into, rather new permissions are controlled by the user's umask. However when you copy a file from one location to another it's a bit of a special case where the user's umask is essentially ignored and the existing permissions on the file are preserved.
Which explains why you can't directly propagate the permissions of the src to the dst directory.
However, there is two-step workaround to this.
cp-metadata: Copy the attributes and only the attributes you want to preserve back to the source directory. Here is a quick script that can do this:
#!/bin/bash
# Filename: cp-metadata
myecho=echo
src_path="$1"
dst_path="$2"
find "$src_path" |
while read src_file; do
dst_file="$dst_path${src_file#$src_path}"
$myecho chmod --reference="$src_file" "$dst_file"
$myecho chown --reference="$src_file" "$dst_file"
$myecho touch --reference="$src_file" "$dst_file"
done
You can leave out the touch command if you don't want keep the timestamp.
Replace myecho=echo with myecho= to actually perform the commands.
Mind that this script should be run in sudo mode in order to be able
to run chown and chmod effectively
cp --preserve: After you have successfully run the first command
now it's time to copy the contents along with the attributes to the dst
directory.
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
\cp -rfp $src_dir $dst_dir should do what you want.

How do I fix the uploading error in aptana studios: parent path doesn't exist

I have a quick question. I've been trying to upload my website to blue-host, but every time I try, it says that parent path doesn't exist, is there a way I can change the parent path so the uploads work
Thx,
willmonferno
This is likely due to file permissions, you don't have write access, try updating file owner or file permissions.
I'm guessing you on some type of linux system, so you either need a chown (change owner) shell command or a chmod command (change file permissions) on the parent folder
try:
sudo chmod -R 777 {YOUR_PARENT_FOLDER}
This will recursively change this and all other files and folders below it in the file tree to allow everything. I would change it once you're in a production environment.

Resources