i,
OS version is below: uname -a
SunOS [hostname] 5.8 Generic_Virtual sun4v sparc sun4v
bash --version
GNU bash, version 2.03.0(1)-release (sparc-sun-solaris)
Copyright 1998 Free Software Foundation, Inc.
Unfortunately, I can't upgrade, I am NOT the sysadmin, just a user of sort.
I have a list of directories that I want to mv every so often.
Directories are from dir0 to dir15.
So, dir15 is the last/max directory
We only want to mv directories dir4 to dir15.
Ideally dir4 to dir15 are sequential with no gap, i.e. dir4,dir5,dir6 until dir15
However, there can be instance where a gap may exist, this is alright so long as we can mv them as should be. I don't know why there's a gap but if there is then the next step will be to create blank directory of the missing directory.
I am uploading 4 text files that are sample listing from running
ls -1d dir* | egrep -v "^dir0$|^dir1$|^dir2$|^dir3$" | sort -k1.4nr
Basically, what I am wanting to achieve is to keep a set of directory from dir4 to dir15 and mv them every so often like below for example
mv dir10 dir11
mv dir9 dir10
mv dir8 dir9
mv dir7 dir8
mv dir6 dir7
...
mv dir4 dir5
recreate dir4 based on directory structure of dir3 without the files? how? :(
Below is what is my script looks so far. It is a bit of a mess but seems to work fine to what I am wanting to do but will have to do more testing before actually doing the mv, at the moment, testing with echo commands.
The script is processing each sample file that has different scenario for testing.
I am posting here in case, there is a simpler way of doing this sort of. I am not sure if I can simply do alternating rows for the mv's.
#!/bin/bash
#
max=15
min=4
switch=0
echo "## USING sample1.txt"
cat sample1.txt | cut -c4- | awk '$1 <= 15 { print "dir"$1 }' > sample1.txt.tmp
cp sample1.txt.tmp sample1.txt
cat sample1.txt
echo
while read -r line
do
vcurrent=$( echo $line | cut -c4- )
if [[ ${vcurrent} -gt ${max} ]] ; then ## this is for sanity in case the filter above fails for some reason
continue
fi
if [[ ${switch} -eq 0 ]] ; then
vnext=$(( vcurrent + 1 ))
if [[ ${vnext} -le ${max} ]] ; then
echo "mv $line dir${vnext}"
else
echo "rm -r $vcurrent"
fi
switch=1
vprev=${line}
else
echo "mv ${line} ${vprev}"
vprev=${line}
fi
done < sample1.txt
if [[ ! -d "dir4" ]] ; then
echo "mkdir dir4"
fi
echo
echo "## DONE processing sample1.txt"
echo "##############################################"
echo
###########################################################################
max=15
min=4
switch=0
echo "## USING sample2.txt"
cat sample2.txt | cut -c4- | awk '$1 <= 15 { print "dir"$1 }' > sample2.txt.tmp
cp sample2.txt.tmp sample2.txt
cat sample2.txt
echo
while read -r line
do
vcurrent=$( echo $line | cut -c4- )
if [[ ${vcurrent} -gt ${max} ]] ; then ## this is for sanity in case the filter above fails for some reason
continue
fi
if [[ ${switch} -eq 0 ]] ; then
vnext=$(( vcurrent + 1 ))
if [[ ${vnext} -le ${max} ]] ; then
echo "mv $line dir${vnext}"
else
echo "rm -r $vcurrent"
fi
switch=1
vprev=${line}
else
echo "mv ${line} ${vprev}"
vprev=${line}
fi
done < sample2.txt
if [[ ! -d "dir4" ]] ; then
echo "mkdir dir4 OR cp -rp dir3 dir4 OR mkdir dir4 that has a similar dir structure as dir3 but no files"
fi
echo
echo "## DONE processing sample2.txt"
echo "##############################################"
echo
###########################################################################
max=15
min=4
switch=0
echo "## USING sample3.txt"
cat sample3.txt | cut -c4- | awk '$1 <= 15 { print "dir"$1 }' > sample3.txt.tmp
cp sample3.txt.tmp sample3.txt
cat sample3.txt
echo
while read -r line
do
vcurrent=$( echo $line | cut -c4- )
if [[ ${vcurrent} -gt ${max} ]] ; then ## this is for sanity in case the filter above fails for some reason
continue
fi
if [[ ${switch} -eq 0 ]] ; then
vnext=$(( vcurrent + 1 ))
if [[ ${vnext} -le ${max} ]] ; then
echo "mv $line dir${vnext}"
else
echo "rm -r $vcurrent"
fi
switch=1
vprev=${line}
else
echo "mv ${line} ${vprev}"
vprev=${line}
fi
done < sample3.txt
if [[ ! -d "dir4" ]] ; then
echo "mkdir dir4 OR cp -rp dir3 dir4 OR mkdir dir4 that has a similar dir structure as dir3 but no files"
fi
echo
echo "## DONE processing sample3.txt"
echo "##############################################"
echo
###########################################################################
max=15
min=4
switch=0
echo "## USING sample4.txt"
cat sample4.txt | cut -c4- | awk '$1 <= 15 { print "dir"$1 }' > sample4.txt.tmp
cp sample4.txt.tmp sample4.txt
cat sample4.txt
echo
while read -r line
do
vcurrent=$( echo $line | cut -c4- )
if [[ ${vcurrent} -gt ${max} ]] ; then ## this is for sanity in case the filter above fails for some reason
continue
fi
if [[ ${switch} -eq 0 ]] ; then
vnext=$(( vcurrent + 1 ))
if [[ ${vnext} -le ${max} ]] ; then
echo "mv $line dir${vnext}"
else
echo "rm -r $vcurrent"
fi
switch=1
vprev=${line}
else
echo "mv ${line} ${vprev}"
vprev=${line}
fi
done < sample4.txt
if [[ ! -d "dir4" ]] ; then
echo "mkdir dir4 OR cp -rp dir3 dir4 OR mkdir dir4 that has a similar dir structure as dir3 but no files"
fi
echo
echo "## DONE processing sample4.txt"
echo "##############################################"
echo
###########################################################################
Sample runtime output below:
$: ./dir_mv.bash
## USING sample1.txt
dir14
dir9
dir8
dir7
dir5
dir4
mv dir14 dir15
mv dir9 dir14
mv dir8 dir9
mv dir7 dir8
mv dir5 dir7
mv dir4 dir5
mkdir dir4
## DONE processing sample1.txt
##############################################
## USING sample2.txt
dir10
dir9
dir8
dir7
dir5
mv dir10 dir11
mv dir9 dir10
mv dir8 dir9
mv dir7 dir8
mv dir5 dir7
mkdir dir4 OR cp -rp dir3 dir4 OR mkdir dir4 that has a similar dir structure as dir3 but no files
## DONE processing sample2.txt
##############################################
## USING sample3.txt
dir15
dir9
dir8
dir7
dir5
rm -r 15
mv dir9 dir15
mv dir8 dir9
mv dir7 dir8
mv dir5 dir7
mkdir dir4 OR cp -rp dir3 dir4 OR mkdir dir4 that has a similar dir structure as dir3 but no files
## DONE processing sample3.txt
##############################################
## USING sample4.txt
dir15
dir9
dir8
dir7
dir5
rm -r 15
mv dir9 dir15
mv dir8 dir9
mv dir7 dir8
mv dir5 dir7
mkdir dir4 OR cp -rp dir3 dir4 OR mkdir dir4 that has a similar dir structure as dir3 but no files
## DONE processing sample4.txt
##############################################
After doing the mv, I will want to create missing directories between dir4 to dir15. I have not gone thru that part yet.
Note that I am doing the one below to limit the max directory to dir15 only.
cat sample1.txt | cut -c4- | awk '$1 <= 15 { print "dir"$1 }' > sample1.txt.tmp
cp sample1.txt.tmp sample1.txt
cat sample1.txt
sed -n '/dir15/,$p' sample4.txt
appears to work too because sample4.txt contains dir15 BUT if dir15 is not a pattern in the file, it prints nothing. So, if I do sed -n '/dir15/,$p' sample1.txt
, it prints nothing
dir_mv.sh (3.8 KB)
sample1.txt (31 Bytes)
sample2.txt (26 Bytes)
sample3.txt (26 Bytes)
sample4.txt (44 Bytes)