I am writing a bash script and need help. This is what I tried:
With the help of @merlin2011
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: `basename $0` <absolute-path> <number>"
exit 1
fi
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
#find . -name "$2" -exec mv {} /some/path/here \;
find $1 >> /tmp/test
for line in $(cat `/tmp/test`); do
echo $line | mv $2 awk -F"/" '{for (i = 1; i < NF; i++) if ($i == "$2") print $(i-1)}'
done
Now I want to check the result of find
command from array and then if there were a directory named 2010
then get the absolute path of it. For ecxample:
arr[1]="/path/to/2010/file.db"
Then I want to rename 2010
to parent directory to
. My pattern is:
arr[1]="/path/to/2010/file.db"
arr[2]="/path/test/2010/fileee.db"
arr[3]="/path/tt/2010/fileeeee.db"
.
.
.
arr[100]="/path/last/2010/fileeeeeee.db"
Result should be:
mv /path/to/2010/ to
mv /path/test/2010 test
mv /path/tt/2010/ tt
.
.
.
mv /path/last/2010 last
UPDATE:
Totally I want to know how to get a variable inversely in awk
...
/path/to/dir1/2010/file.db
I want to search in absolute path
then find 2010 and rename it in previous path with /
pattern like : awk -F"/" {print [what?]}
tell awk my state is 2010 then print one variable before it by knowing splitter is /
UPDATE
The files dirs and subdirs pattern are:
/path/to/file/efsef/2010/1.db
/path/to/file/hfjh/sdfsf/2010/2.db
/path/to/file/dsf/sdhher/aqwe/sfrt/2010/3.db
.
.
.
/path/to/file/kldf/2010/100.db
I want to rename all 2010 dirs to their parent then tar
all .db
This is what exactly I want :)