I want to rename sub-directories to my new pattern but some results may be dangerous in my own script:
dst=$1
dirs=$( find $dst -type d )
for dir in $dirs; do
path=$( echo $dir | awk 'BEGIN{FS=OFS="/"}{NF--; print}' )
new_pattern=$( mkdir "$(head /dev/random | tr -dc A-Za-z0-9 | head -c 15 ; echo '')
mv -v $dir $path/$new_pattern
done
For example if I use /tmp/etc
as input argument I would have the nasty results in the first iteration:
mv -v /tmp/etc /tmp/<newpattern>
So in next iteration there will not be /tmp/etc
to work at.
The second problem in my script is when I use relative path like etc
as input argument. So the first output is:
mv -v etc /<newpattern>
Which is a very bad practice. My ideal way is to detect all directories and then rename them in my own pattern recursively.
Update
My pattern is: head /dev/random | tr -dc A-Za-z0-9 | head -c 15 ; echo ''
or may be anything else.
All of directories all empty without any files. Then I want to rename all of these directories to a new name base on my pattern.
dst=$1
dirs=$(find $dst -type d | sort -r)
for dir in $dirs; do
path=$( echo $dir | awk 'BEGIN{FS=OFS="/"}{NF--; print}' )
new_pattern=$(head /dev/random | tr -dc A-Za-z0-9 | head -c 15 ; echo '')
mv -v $dir $path/$new_pattern
done
When use relative path in input argument:
# pwd
/tmp
# bash a.sh etc
renamed 'etc/apache2/conf-enabled' -> 'etc/apache2/6jFtEeT27SHre1t'
renamed 'etc/apache2' -> 'etc/0rP0r7p63CzICdE'
renamed 'etc/alternatives' -> 'etc/I3Yog16F2ijlcYe'
renamed 'etc' -> '/zKpmJT351VuDrVK'
The last output renamed 'etc' -> renamed 'etc' -> '/zKpmJT351VuDrVK'
is the main problem.