Chmod on files and folders not already on that bit mask - find

Is there a way to change perms only on files/folders not on those perms already?
Say, if executing find . -type f -exec chmod 0640 {} \; on a file that is already in 0640, skip it and don't overwrite.
I'm syncing (trough lsyncd) files from one to many hosts and when executing that command, ALL files are modified and so, all files are also synced. Thanks.

The correct and fastest way is
find . -type f ! -perm 644 -exec chmod 644 {} \;

Related

Copy directories and their sub directories created day ago

I tried to copy Copy directories and their subdirectories created a day ago as follows:
find /application/work/ -type d -mtime -1 -exec cp -r {} /tmp/backup \;
But it is copying all directories (Not only the ones created a day ago).
Would you please advise?
find is also finding the working directory /application/work/ and is copying it, see How to exclude this / current / dot folder from find "type d". Since you're executing cp -r, it recursively copies everything in . before also copying the subset of directories you've found via -mtime. You need to set the -mindepth to exclude the working directory from the paths on which find will operate.
Modify your command to:
find /application/work -mindepth 1 -type d -mtime -1 -exec cp -r {} /tmp/backup \;

find command behaves weird on AIX

Found something weird with the find command on AIX 7.1. I executed find command to list files of specific permissions. It works when I execute as below
find . -type f -perm 600 -exec ls -la {} \;
But fails to pull the files when I execute as below
find /user -type f -perm 600 -exec ls -la {} \;
Is anyone familiar with this kind of error?
Thanks,

How to use find to find a file in specified subdirectory

I have multiple sites that user my own template, now I have updated my template and I want to update the template using find.
if I do
find . -name template.css -print -exec cp NEW_FILE {} \;
Then all other template.css wil overwritten to.
I only want template.css to be overwritten if they are in the directory mytemplate/css.
Any one an idea?
Example:
/site1/templates/mytemplate/css/template.css*
/site1/templates/othertemplate/css/template.css
/site1/templates/other2template/css/template.css
/site2/templates/mytemplate/css/template.css*
/site2/templates/othertemplate/css/template.css
/site2/templates/other2template/css/template.css
/site3/templates/mytemplate/css/template.css*
/site3/templates/othertemplate/css/template.css
/site3/templates/other2template/css/template.css
The files with an * should only be overwritten
You can use -wholename flag and proper regular expression. This should work for your example:
find . -wholename "*/mytemplate/css/template.css" -print -exec cp NEW_FILE {} \;

mv: "Directory not Empty" - how do you merge directories with `mv`?

I tried to deploy my personal blog website to my remote server recently. When I tried to move a few files and directories to another place by executing mv, some unexpected errors happened. The command line echoed "Directory not Empty". After doing some googling, I tried again with '-f' switch or '-v', the same result showed.
I logged in on the root account, and the process is here:
root#danielpan:~# shopt -s dotglob
root#danielpan:~# mv /var/www/html/wordpress/* /var/www/html
mv: cannot move `/var/www/html/wordpress/wp-content` to `/var/www/html/wp-content`:
Directory not empty
root#danielpan:~# mv -f /var/www/html/wordpress/* /var/www/html
mv: cannot move `/var/www/html/wordpress/wp-content` to `/var/www/html/wp-content`:
Directory not empty
Anybody know why?
(I'm running Ubuntu 14.04)
If You have sub-directories and "mv" is not working:
cp -R source/* destination/
rm -R source/
I found the solution finally. Because the /var/www/html/wp-content already exists, then when you try to copy /var/www/html/wordpress/wp-content there, error of Directory not Empty happens. So you need to copy /var/www/html/wordpress/wp-content/* to /var/www/html/wp-content.
Just execute this:
mv /var/www/html/wordpress/wp-content/* /var/www/html/wp-content
rmdir /var/www/html/wordpress/wp-content
rmdir /var/www/html/wordpress
Instead of copying directories by cp or rsync, I prefer
cd ${source_path}
find . -type d -exec mkdir -p ${destination_path}/{} \;
find . -type f -exec mv {} ${destination_path}/{} \;
cd $oldpwd
moves files (actually renames them) and overwrites existing ones. So it's fast enough.
But when ${source_path} contains empty subfolders you can cleanup by rm -rf ${source_path}

How to create links to all subfolders containing specified text in their names

As specified in title I am looking for a way how to create links to all subfolders containing specified text in their names, so for example for all subfolders of root directory containing ".app" in their names an link will be created to "/AppLinks" directory. I would like to use it in bash script (open source, free).
Does anyone know how to do that?
I searched it by google with no luck.
find yourdir -type d -name '*.app' -exec ln -s {} /AppLinks \;
Find all directories named something.app in yourdir, and create a symlink to them in /AppLinks.
single line bash-fu
function FUNCsymlink() { echo "$1"; fileName=`basename "$1"`; ln -s "$1" "/AppLinks/$fileName"; }; export -f FUNCsymlink; find `pwd`/ -maxdepth 1 -type d -iname "*.app" -exec bash -c "FUNCsymlink '{}'" \;
to easy reading:
function FUNCsymlink() {
echo "$1";
fileName=`basename "$1"`;
ln -s "$1" "/AppLinks/$fileName";
};
export -f FUNCsymlink;
find `pwd`/ -maxdepth 1 -type d -iname "*.app" -exec bash -c "FUNCsymlink '{}'" \;
you may have to adjust it a bit for your specific solution.
wherever you run it, it will create the symlinks to /AppLinks
it will only look for direct subfolders, not subfolders of subfolders, thats what I believe you need..