So Basically I need to find all files and folders in my github project containing the string 'persons'
find . -type f -print | grep "persons"
find . -type d -print | grep "persons"
The above works for me.
But I also need to rename all the above files and folders with 'members'
Can I do the above with a couple of commands? Instead of manually replacing them one by one
i dont know how to do a git mv oldfilename newfilename rescursively to the above
for dir in `find /DIR -type d -iname '*persons*'` ; do
git mv "${dir}" "${dir/persons/members}"
done
Will do. For the files do it with -type f.
find . -depth -name persons | while read F; do mv $F $(dirname $F)/members; done
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 need to find subdirectories called "child" and copy the files in those folders to a new directory. Can't get it to work.
I can get as far as finding the subdirectories but I can't copy the files from them.
> find /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/2016/03_2016 -type d -iname child
The above will find all subdirectories in 03_2016 named "child" but how do I now copy the files inside those directories?
I tried this but the problem is that it seems to want to copy the directories themselves and not just the files:
> find /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/2016/03_2016 -type d -iname child | xargs cp '{}' /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/TEST \;
I can't get it to target only the files.
This might be a little cleaner than the above answer:
find $(find /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/2016/03_2016 -type d -iname child | xargs) -type f -exec cp {} /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/TEST \;
Two calls to find, one call to xargs. Like the other answer, this also will overwrite duplicate file names.
You're close:
> find $YOUR_PATH/03_2016/ -type d -iname child | xargs -I {} find -type f {} | xargs -I {} cp {} /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/TEST
Note: You might overwrite files that share the same name at the final destination
I'm trying to create a cronjob for Ubuntu where:
all empty dir's should be removed
if the dir is not empty then it should be removed if the only filetypes are txt or csv files
Currently I have:
find /path -depth rmdir {} \; 2>dev/null
What do I need to delete the folders which only have txt or csv files?
I don't want to delete all txt or csv files, just those folders which do not contain other filetypes.
Additional example:
Dir1
SubDir1
SubSubDir1
File.txt
File.csv
SubDir2
SubSubDir2
File.xml
SubSubDir1 should be deleted. Since SubDir1 and Dir is now empty they should be deleted as well.
SubSubDir2 contains another filetype and should no be deleted.
You could list the number of files in a folder with something like:
find "$d" -maxdepth 1 -not -iname '*.csv' -a -not -iname '*.txt' | wc -l
If the folder is empty or the folder contains exclusively txt and csv files, it shall print 1.
And to list folders so that they don’t mess up each other if you erase the parents first:
find /path -depth -type d
All in all, you may be able to achieve what you want with:
while read d
do
if [ $(find "$d" -maxdepth 1 -not -iname '*.csv' -a -not -iname '*.txt' | wc -l) -eq 1 ]
then
rm -rf "$d"
fi
done < <(find /path -depth -type d)
But I also advocate a check somewhere so your cron doesn’t wipe your storage without your consent.
I just started to study Bash. I want to do a script to find some specific folders in a directory and its subdirectories and if it exist, rename it into the same folder where we have found it. The same specific folder can be in some subdirectories.
I use this:
file=`find . -name a`
if [ -d $file ]
then
rename 's/a/b/' $file
fi
But don't work. Is there anyway to do this process?
Thanks.
Finally, i solved the problem with this:
find . -name "a" -type d -execdir rename 's/a/b/' {} \; &>/dev/null
You can do this with oneliner:
find . -name "a" -type d -execdir rename 's/a/b/' {} \;
The parameter to name might be regex.
With -type d it will find all directories.
-execdir changes to a matching item's directory and then executes the rename command, passing the filename of the item at hand as an argument ({}).
I'm new to unix shell/bash scripting . My requirement is as follows :
The current directory contains a lot of dynamic folders and the data file is available only in the last sub folder.
I need to move the data file to the home folder and rename the datafile's name as the current directory's name.
Could you please help in writing the bash script for the same.
--update--
I tried the following to move file to the parent directory:
find . -mindepth 2 -type f -print -exec mv {} . \;
After trying out many options , the following worked
find . -mindepth 2 -type f -print -exec mv {} . \;
dirFullPath=`pwd`
fileName=`echo $dirFullPath | awk -F"/" '{print $(NF)}'`
mv *.0 $fileName.tab
Any other better solutions is appreciated, Thanks.!!