recursively removing permissions under root folder - linux

i am new to linux platform and now i wanted to remove write permissions of all the php files under my root folder. It would be appreciable if somebody can suggest a solution.
thanks and regards
tismon

find / -name '*.php' -exec chmod a-w {} \;

An alternate option would be to use these 2 commands
cd /
chmod -R a-w *.php
But I also recommend (like Matthieu did) to have a look first, what files you are really modifying.

This will work:
find /root -type f -name "*php" | xargs chmod 664

Related

how to find all directories in /home that are not owned by root and change their permissions to ensure they have 711 permission?

I try to find all directories in /home that are not owned by root and change their permissions to ensure they have 711 permission in the same command.
find \home type -d -not -user root -ls | chmod 711 {} \
But the command I used doesn't work.
The following should work:
find /home -type d -not -user root -exec chmod 711 {} +
The -exec action allows you to run a separate executable (in this case chmod) and supply the found names to it. The + at the end allows find to run chmod with multiple names at once.
The above includes fixes for a few typos: \home should be /home, type -d should be -type d.
The first instruction has wrong syntax. You could try:
find /home -type d -not -user root -ls
Also, you shouldn't use "ls" if you care about performance. Instead I suggest using the -exec switch.
Good luck

How to find all .sh files and make them executable using bash in linux?

also when I launch I want to pass path to folder when located these .sh files
I started with this
#!/bin/bash
find /home/user_name -name "*.sh"
And after script has to write in logo list with executable files
The safest and way both in terms of security and in terms of weird file names (spaces, weird characters, and so forth) is to use find directly:
find /home/user -name "*.sh" -execdir chmod u+x {} +
You can check the comments and the manual of find why this is safe, but in short, it makes sure your file is properly quoted in the chmod command. execdir (rather then -exec) is an extra security feature making sure the command is executed in the directory the file was found in avoiding race conditions (elaborated in the manual).
another way :
find . -name "*.sh" -exec chmod ux+y {} \;
you can first check your command by using
find . -name "*.sh" -print
If you want to make all files executable for the current user, you can use the command as follows (assuming that you have permission for all files in target home folder) :
find /home/user_name -name "*.sh" -print0 | xargs -0 chmod u+x
By #kabanus command
#!/bin/bash
# chmod u+x $(find $1 -name "*.sh")
# ls -1 $1/*.sh
find $1 -name "*.sh" -print -exec chmod u+x {} +
And use as
$ ./script.sh /your_directory
/your_directory - first argument ($1) in the script.

How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?

How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?
Here is what I have so far:
find -type d -name "*.ABC" -exec {} /Desktop/NewFile \;
I get this as output:
find: './GAE/.ABC: PERMISSION DENIED
Please Help, Thanks!
Your error here above has nothing to do with file read permission. You're trying to execute the directories you find! Avoid running commands as root or sudo unless: (1) you really need it and (2) you really know what you're doing. Quite often people asking for root or sudo privileges are exactly the ones should not have it.
That said... there are several ways to copy a directory tree under *nix. This is just one possible approach:
$ find <start> -type d -name \*.ABC -exec cp -av {} <target> \;
Where:
<start> is a directory name. It's used to tell find where to start its search (for example /usr/local or $HOME)
<target> is another directory name to define the final destination of your copied directories
UPDATE
In case you want to search for multiple paths...
$ find <start> -type d \( -name \*.ABC -o -name \*.DEF \) -exec cp -av {} <target> \;
This should work:
find ./source_dir -name \*.png -print0 | xargs -0 cp -t path/to/destination
For more info, you can look up here.

Bash - Recursively change ownership of only the directories which belong to a certain user/group

I have a directory (we will call /files) with ~1300 subdirectories, each of which contains further subdirectories and files.
90% of the top level directories in /files belong to apache:apache and the rest belong to root:root. I need everything to belong to apache:apache.
I think if I do a recursive chown on the whole lot it will be quite extreme, so I was wondering if there's a more efficient way to recursively change ownership of just the root:root directories to apache:apache.
Bonus if chmod can be done on these directories in the same way.
Your recursive chown would have probably been done already, but you could use this instead:
find . -type d \( ! -user apache -o ! -group apache \) -print0 | xargs -0 chown apache:apache
To change directories that have the wrong permission:
find . -type d ! -perm 755 -print0 | xargs -0 chmod 755
Using linux's find command is going to help there:
find /files -user root -group root -type d \
-exec chmod something {} \; -exec chown apache.apache {} \;
for more details on WHY that works there is http://www.explainshell.com/explain?cmd=find+%2Ffiles+-user+root+-group+root+-type+d+-exec+foo+\%3B

changing permissions of files in a directory recursively

I am trying to change the permissions of a files present in a directory and subdirectories using the below command and running into below error..can anyone help?
user#machine:/local/mnt/workspace$ find . -type f -exec chmod 644 {} \;
chmod: changing permissions of `./halimpl/ncihal/adaptation/NonVolatileStore.cpp': Operation not permitted
you can run the following command:
#chown -R directory_path
But it will change the permissions of directories also.
For only files, you can run.
#find directory_path -type f -exec chmod 644 {} \;
It also looks like you dont have enough permissions. try
#sudo find directory_path -type f -exec chmod 644 {} \;
or run the command as root user.
It looks to me like you don't have permission to change NonVolatileStore.cpp.
Are you aware of chmod's -R switch that recursively changes permissions?
if you have the root privilege, try:
sudo find . -type f -exec chmod 644 {} \;
It could be that you simply don't own that file. Run an ls -l on it to see full permissions and who the owner is.
It could also be the filesystem is read only.

Resources