How to recursively recode all project files excluding some directories and preserving permissions?
Based on this question, but its solution does not preserve permissions, so I had to modify it.
WARNING: since the recursive removal is a part of the solution, use it on your own risk
Task:
Recursively recode all project files (iso8859-8 -> utf-8) excluding '.git' and '.idea' dirs and preserving permissions.
Solution (worked well in my case):
Backup your project's dir, then cd there. Run:
find . -not -path "./.git/*" -not -path "./.idea/*" -type f -print -exec iconv -f iso8859-8 -t utf-8 -o {}.converted {} \; -exec sh -c 'cat {}.converted > {}' \; -exec rm {}.converted \;
Binary and image files will fail to recode since they aren't text, so files like 'image.jpeg.converted' will be left along with 'image.jpeg'. To clean up this mess:
find . -not -path "./.git/*" -not -path "./.idea/*" -type f -regex '.*\.converted' -exec rm {} \;
Before you do that, you may want just print (without rm) to see that there are only those files listed that you'd really like to remove.
Related
What I want:
In a bash script: Find all files in current directory that contain a certain string "teststring" and cop them into a subfolder "./testfolder"
Found this to find the filenames which im looking for
find . -type f -print0 | xargs -0 grep -l "teststring"
..and this to copy found files to another folder (here selecting by strings in filename):
find . -type f -iname "stringinfilename" -exec cp {} ./testfolder/ \;
Whats the best way to combine both commands to achieve what I described at the top?
Just let find do both:
find . -name subdir -prune -o -type f -exec \
grep -q teststring "{}" \; -exec cp "{}" subdir \;
Note that things like this are much easier if you don't try to add to the directory you're working in. In other words, write to a sibling dir instead of writing to a subdirectory. If you want to wind up with the data in a subdir, mv it when you're done. That way, you don't have to worry about the prune (ie, you don't have to worry about find descending into the subdir and attempting to duplicate the work).
I've looked at numerous articles and I can't seem to figure it out, I suppose I'm a noob.
Anyways I have a directory that I would like to tar, however I want to exclude the shallow directory's files, as well as exclude the folders
"plugins", "backups", and "logs" that are located in the shallow directory.
->
\#!/bin/bash
mkdir -p /path/to/backup/directory/`date +%d%m%y`
cd /path/to/backup/directory/`date +%d%m%y`
cd .. | find . -not \\( -path plugins -prune -o -path backups -prune -o -path logs -prune \\) -mindepth 1 -print0 | xargs -0 tar cpj --directory=$(cd -) -f `date +%H`.tar.gz
The find section is what's wrong, it doesn't exclude anything. This is my 30th (not literally but probably higher than that actually xD ) attempt to prune and what not, with each attempt looking more ridiculous than the last.
If someone could just show me a solution for the find section, that'd be great - thanks
(the '`' characters are around the dates, it just breaks the code view when I try to put them in there)
Use --exclude=PATTERN and */, as it will only catch directories:
tar --exclude=plugins/ --exclude=logs/ --exclude=backups/ -cf /path/to/backup/directory/`date +%d%m%y`/whatever.tar [other options] */
About find excluding directories, try
find . -type d \( -path plugins -o -path backups -o -path logs \) -prune -o -print
Is there a way to remove all temp files and executables under one folder AND its sub-folders?
All that I can think of is:
$rm -rf *.~
but this removes only temp files under current directory, it DOES NOT remove any other temp files under SUB-folders at all, also, it doesn't remove any executables.
I know there are similar questions which get very well answered, like this one:
find specific file type from folder and its sub folder
but that is a java code, I only need a unix command or a short script to do this.
Any help please?
Thanks a lot!
Perl from command line; should delete if file ends with ~ or it is executable,
perl -MFile::Find -e 'find(sub{ unlink if -f and (/~\z/ or (stat)[2] & 0111) }, ".")'
You can achieve the result with find:
find /path/to/directory \( -name '*.~' -o \( -perm /111 -a -type f \) \) -exec rm -f {} +
This will execute rm -f <path> for any <path> under (and including) /path/to/base/directory which:
matches the glob expression *.~
or which has an executable bit set (be it owner, group or world)
The above applies to the GNU version of find.
A more portable version is:
find /path/to/directory \( -name '*.~' -o \( \( -perm -01 -o -perm -010 -o -perm -0100 \) \
-a -type f \) \) -exec rm -f {} +
find . -name "*~" -exec rm {} \;
or whatever pattern is needed to match the tmp files.
If you want to use Perl to do it, use a specific module like File::Remove
This should do the job
find -type f -name "*~" -print0 | xargs -r -0 rm
I want to rename all .hg_gg folders in /var/www to .hg. How can I do it?
I know how to rename .hg to .hg_gg.
find /var/www -name ".hg" -exec bash -c 'mv $0 $0_gg' {} \;
but don't know how to make reverse change.
Try this:
find /var/www -name ".hg_gg" -execdir bash -c 'mv {} .hg' \;
You need to use a special syntax defined by find: {} is the placeholder for the current file name. Check the man page for that. Also it is important to use -execdir instead of -exec. execdir changes the current working directory to the folder where the found directory is located. Otherwise it would do something like this mv /var/www/.hg_gg ./.hg
You can speed up things a bit when restricting find to find folders only using -type d:
find /var/www -type d -name ".hg_gg" -execdir bash -c 'mv {} .hg' \;
Consider this find command with -execdir and -prune options:
find /var/www/ -type d -name ".hg_gg" -execdir mv '{}' '.gg' \; -prune
-execdir will execute the command in each subdirectory
-prune causes find to not descend into the current file
Not a one liner, but you could do this:
for file in `find /var/www -name ".hg_gg"`; do
mv $file `echo $file | sed 's/hg_gg$/hg/'`
done
I need to delete unpacked directories from my /source tree keeping the others with .tar and .patch extensions,
how to do please?
This should work:
find . -not -name "*.tar" -not -name "*.patch" -type f -exec rm {} \;
This is using only one command not using pipes.
Note. This will proceed recursively into subdirectories. If this is unwanted, use the maxdepth switch:
find . -maxdepth 1 -not -name "*.tar" -not -name "*.patch" -type f -exec rm {} \;
BACKUP YOUR DIRECTORY FIRST, I HAVE NOT TESTED THIS, AND IT HAS A BUG AS NOTED IN THE COMMENTS.
WHILE IN THE ACTUAL /source DIRECTORY:
ls|fgrep -v -e .tar -e .patch|xargs rm -rf
You probably want to use the "put echo after xargs" trick to see what this would actually do, before running it:
ls|fgrep -v -e .tar -e .patch|xargs echo rm -rf