Modify permissions for scripts recursively - unix

Trying to set permissions recursively for multiple scripts:
chmod -R /export/home/*.sh
But some files do not have .sh extension, e.g. .ksh and some scripts have no extension at all.
How can I make all scripts/files executable? (any type, extension or not)

This command should do the trick:
find /export/home -type f -exec chmod +x {} \;
However, I don't think you really want to make all files executable - there is significant security risk in this. You'd be better off just determining what files should be executable, or putting them in a "bin/" subdirectory, which you could then search for:
find /export/home -type d -name bin -exec chmod -R +x {} \;

Related

Generate entity in symfony 4 (methods)

when I tried to run the command php bin/console doctrine:generate:entities App:Center in symfony 4.0.6 I get this error:
Can't find base path for "App\Entity\Center" (path:
"/home/USER/foo/bar/src/Entity",
destination: "/home/USER/foo/bar/src/Entity").
the Center is my entity name!
how can I fix this?
Doctrine developers are deprecating the generation commands for Symfony 4. The reason why it doesn't work for you is because the command is hardwired to find the destination directory under
projectRoot/src/{namespace}/{App}Bundle/Entity/{Center.php}
but the Symfony 4 directory structure is different than that, hence it cannot find it. If you are dead-set on using the command, you could probably extend it and create your own, in which you could change the destination path(s) for your entities. You can just skip the command part and generate getters and setters on your own.
Doctrine issue: https://github.com/doctrine/DoctrineBundle/issues/729
Symfony issue: https://github.com/symfony/symfony-docs/issues/8893
From the maintainers of Doctrine:
https://github.com/doctrine/DoctrineBundle/pull/790
So, from the various questions, I think your first problem is files permissions...
Meanwhile, you need to first correct your files permissions.
Here is how to do it.
chown /home/alireza/Projects/www/ www-data:www-data /
find /home/alireza/Projects/www/ -type d -exec chmod 0755 "{}" \;
find /home/alireza/Projects/www/ -type f -exec chmod 0664 "{}" \;
find /home/alireza/Projects/www/ -type d -exec chmod g-s "{}" \;
find /home/alireza/Projects/www/ -type d -exec chmod g+s "{}" \;
As I assume that www/ is where all your projects are stored, I stopped the path at the www/ folder. It will thus correct files permissions for all of your projects.
Also, make sure to add your current user to the www-data group.
usermod -a -G www-data user_name
Try this, and tell us if you still have your problem.

Change permission recursively for all directory and exclude only one directory

I would like to change the permission recursively all files and directories but exclude some directories.
find . -name user -prune -o -type d -exec chmod 755 {} \;
But using this, its only change the directories but not recursively. The content of directories not change.
while,
find . -name user -prune -o -type d -exec chmod -R 755 {} \;
This will change all directories and its contents and include the 'user'directory which i want to exclude.
So, how to change the permission recursively all files and directories but exclude user directory?
You need to also exclude the specific user directory, while including everything else.
Switch the file search to wholename on all sub-things of user
On the "or directory", add an exclusion for user
I think this does it
find . -wholename "*user/*" -prune -o \
-type d -a -wholename^"./user$" -exec chmod -R 755 {} \;

Move only directories and not files in Unix OS

I had a directory with many files and sub-directories. To move only the sub-directories, I just learned you can use:
ls -d BASEDIR/*/ | xargs -n1 -I% mv % TARGETDIR/
I use the following:
$ mv ./*/ DirToMoveTo
For example:
Say I wanted to move all directories with "Old" in the name to a folder called "Old_Dirs" on /data.
The command would look like this:
mv ./*Old*/ /data/
Why not use find?
find . -maxdepth 1 -type d -exec mv '{}' /tmp \;
-maxdepth 1 makes sure find won't go deeper than current directory
-type d tells find to only find directories
-exec execute a command with the result of the find referenced by {}
In my opinion a cleaner solution and it also works better then using xargs when you have files with white space or tabs in them.
With a file structure like this:
/dir2move2
/dir
/subdir1
/subdir2
index.js
To move only the sub directories and not the files you could just do:
mv ./dir/*/ ./dir2move2
Possible solution:
find BASEDIR/ -maxdepth 1 -mindepth 1 -type d -exec mv '{}' TARGETDIR \;
you can simply use the same command for moving a file but put a slash after the name of the subdirectory
sudo mv */ path/to/destination
sudo mv subdir/ path/to/subdirectory

Unix 'find' without descending into matched directories

I was trying to remove all git files from a repository with this:
find . -name ".git*" -exec rm -rf {} \;
However, rm warns that files could not be deleted (because their parent directory has already been deleted).
Is there a way to get find to stop recursing when it finds a matching directory?
E.g. find...
/.gitmodules
/.git/stuff
/.git/.gitfile
... produces
/.gitmodules
/.git
Use -depth:
find . -depth -name ".git*" -exec rm -rf {} \;
This would allow you to process the files or subdirectories first before their parent directories.

How to delete only directories and leave files untouched

I have hundreds of directories and files in one directory.
What is the best way deleting only directories (no matter if the directories have anything in it or not, just delete them all)
Currently I use ls -1 -d */, and record them in a file, and do sed, and then run it. It rather long way. I'm looking for better way deleting only directories
To delete all directories and subdirectories and leave only files in the working directory, I have found this concise command works for me:
rm -r */
It makes use of bash wildcard */ where star followed by slash will match only directories and subdirectories.
find . -maxdepth 1 -mindepth 1 -type d
then
find . -maxdepth 1 -mindepth 1 -type d -exec rm -rf '{}' \;
To add an explanation:
find starts in the current directory due to . and stays within the current directory only with -maxdepth and -mindepth both set to 1. -type d tells find to only match on things that are directories.
find also has an -exec flag that can pass its results to another function, in this case rm. the '{}' \; is the way these results are passed. See this answer for a more complete explanation of what {} and \; do
First, run:
find /path -d -type d
to make sure the output looks sane, then:
find /path -d -type d -exec rm -rf '{}' \;
-type d looks only for directories, then -d makes sure to put child directories before the parent.
Simple way :-
rm -rf `ls -d */`
find command only (it support file deletion)\
find /path -depth -type d -delete
-type d looks only for directories, then -depth makes sure to put child directories before the parent. -delete removing filtered files/folders
In one line:
rm -R `ls -1 -d */`
(backquotes)

Resources