Bash - Find several specific folders in a directory and its sbdirectories and rename these specific folders - linux

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 ({}).

Related

Linux move files from dir to dir by name mask

I am trying to move all files with names starts with SML from directory to another.
Tried with
find /var/.../Images/ -name SML\* mv /var/.../Images/Small but doesnt work
try find /var/.../Images/ -name SML\* -exec mv {} /var/.../Images/Small/ \;
I guess you want something like this:
dir=/path/to/your/Images
mkdir -p "$dir/Small"
find "$dir" -name "SML*" -not -wholename "$dir/Small/*" -exec mv {} "$dir/Small/" \;
Since the directory you move the files to is a subdirectory of the one you seach in, you need to exclude the files already moved there. So I added -not -wholename "$dir/Small/*"
To execute a command for each found file, you need -exec .... The alternative would be to pipe your find results to a while read loop.
When using -exec, the found name can be referenced by {}.
See man find for a lot more information.

Copy recursive files of all the subdirectories

I want to copy all the log files from a directory which does not contain log files, but it contains other subdirectories with log files. These subdirectories also contain other subdirectories, so I need something recursive.
I tried
cp -R *.log /destination
But it doesn't work because the first directory does not contains log files. The response can be also a loop in bash.
find /path/to/logdir -type f -name "*.log" |xargs -I {} cp {} /path/to/destinationdir
Explanation:
find searches recursively
-type f tells you to search for files
-name specifies the name pattern
xargs executes commands
-I {} indicates an argument substitution symbol
Another version without xargs:
find /path/to/logdir -type f -name '* .log' -exec cp '{}' /path/to/destinationdir \;

Find and replace string in a tree. Recursive

I have tried searching for answer here and I found some but they dont seem to work for me. I want to rename a project file and I find it that the esiest way would be to replace its name in every single file and ALSO every file name as well in the directory.
Could someone please point me a way (in windows or linux) to replace recursively a string in a directory in every file and every file name, with another string?
I would appreciate all help.
Bash
In Unix you can do it in bash shell. You have to make use of find, mv and sed and the following three commands as examples:
Replace string "ABC" with "345" in the name of every directory
in the current directory or in the subdirectories recursively.
find . -depth -type d -name "*ABC*" -exec bash -c 'dir=${1%/*} base=${1##*/}; mv "$1" "$dir/${base//ABC/345}"' par0 {} \;
Replace string "ABC" with "345" in the name of every file in
the current directory or in the subdirectories recursively.
find . -depth -type f -name "*ABC*" -exec bash -c 'dir=${1%/*} base=${1##*/}; mv "$1" "$dir/${base//ABC/345}"' par0 {} \;
Replace string "ABC" with "345" in the content of every file
in the current directory or in the subdirectories recursively.
find . -depth -type f -name "*" -exec bash -c 'sed -i "s/ABC/345/g" "$1"' par0 {} \;

Move and rename the files - bash script

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.!!

Rename multiple directories matching pattern

I would like to rename all directories under a basedir which match a name. For example:
In basedir/, I have:
- foo/bar/blah
- my/bar/foo
- some/bar/foo1
- other/foo/bar
I would like to rename all directories matching bar, but I would like to preserve the prefix part.
With find, I can easily make a list of all the directories like this:
find . -name repositoryunit -type d
However, how can I use -exec mv {} ... (or perhaps combine with another app) so that the prefix is preserved?
Many thanks in advance!
find . -depth -name bar -type d -execdir mv {} baz \;
-execdir changes directory to the parent before executing the command, so the mv here will be local to each parent directory.

Resources