0

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 :)

Community
  • 1
  • 1
MLSC
  • 5,872
  • 8
  • 55
  • 89
  • I just updated. Let me know if that does what you are trying to do. – merlin2011 Apr 13 '14 at 05:39
  • So you want to pass `/path/to/dir1/2010/file.db` to awk and search `2010`? If `2010` is found then what do you want to do next? – anubhava Apr 13 '14 at 05:40
  • Thanks... If 2010 [second argument] found I want to rename it to the parent directory... – MLSC Apr 13 '14 at 05:41
  • So you want: `mv /path/to/dir1/2010/file.db dir1`? – anubhava Apr 13 '14 at 05:41
  • I want to do: mv $2 [parent directory of $2] – MLSC Apr 13 '14 at 05:43
  • But why awk? This can be done in BASH itself – anubhava Apr 13 '14 at 05:44
  • Ok I thought it is good using awk...If it is possible with bash please help.. – MLSC Apr 13 '14 at 05:46
  • How do you see this [one](https://github.com/MortezaLSC/Programming/blob/master/test.sh)?: – MLSC Apr 13 '14 at 05:50
  • @MortezaLSC, You make a good point. I thought he was just trying to use `awk` to learn it. :P – merlin2011 Apr 13 '14 at 05:54
  • The last 4 lines of your posted script each contain semantic errors and your descriptive text is far too focused on HOW you think you need to do whatever you're trying do do (e.g. `I want to check the result of find command from array` and `I want to know how to get a variable inversely in awk`) rather than WHAT you're trying to do. Please just clean it up to show some sample input, expected output and a brief description of WHAT it is you need to do. As-is, you'll probably get an answer telling you how to do the wrong thing in a valid syntax. – Ed Morton Apr 13 '14 at 15:39
  • Thanks...Firstly I thought I could do that with arrays, after that our friends told me other ways...So I asked it again in other question...You and other guys answered my question...Thank you for your help... – MLSC Apr 14 '14 at 03:33

3 Answers3

2

This answer addresses only the OP's update. My best interpretation is that you are trying to get awk to print the value dir1 inside the string /path/to/dir1/2010/file.db. The following line will achieve it.

awk -F"/" '{for (i = 1; i < NF; i++) if ($i == "2010") print $(i-1)}'

I tested using the following command, which will output dir1.

echo /path/to/dir1/2010/file.db | awk -F"/" '{for (i = 1; i < NF; i++) if ($i == "2010") print $(i-1)}'

Based on your update, you should surround the awk command with the backtic operator.

mv $2 `awk -F"/" '{for (i = 1; i < NF; i++) if ($i == "$2") print $(i-1)}'`
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • Thanks..very good..but: How to tell rename 2010 to the result of awk here? – MLSC Apr 13 '14 at 05:33
  • 1
    I will update based on the assumption that your array is correct. – merlin2011 Apr 13 '14 at 05:36
  • i liked your solution. Way to go @merlin2011 – Sireesh Yarlagadda Apr 13 '14 at 05:41
  • By your help: It [THIS](https://github.com/MortezaLSC/Programming/blob/master/test2.sh) working for me? – MLSC Apr 13 '14 at 05:57
  • @MortezaLSC, I just updated. I screwed up the backtic placement lol. – merlin2011 Apr 13 '14 at 06:04
  • I don't have linux now..I want to be sure this is working or not...sorry..I would be thankful if you put the whole script in your answer... – MLSC Apr 13 '14 at 06:06
  • @MortezaLSC, I can only test this part. I do not have your directory structure on my system, so I can't verify whether the other parts of your script are correct. – merlin2011 Apr 13 '14 at 06:13
  • 1
    @MortezaLSC, Use the last line in my updated answer. Keep in mind I am assuming that `echo $line` always has a `2010` in it. If that is not true, then the `mv` command will simply throw and error because it will only have one argument. – merlin2011 Apr 13 '14 at 06:22
1
        To implement, we have to do the recursive folder search.

        It should be combination of two commands like find and mv




        find . -name "2010" -exec mv {} /some/path/here \;


Other way shared by merlin2011
    mv $2 awk -F"/" `'{for (i = 1; i < NF; i++) if ($i == "$2") print $(i-1)}'`
Sireesh Yarlagadda
  • 12,978
  • 3
  • 74
  • 76
  • This should be: `find $1 -name "$2" -exec mv {} [Here should be the parent directory of 2010] \;` – MLSC Apr 13 '14 at 05:08
  • Ok...I want to use awk to find the parent directory...I am not good at `awk` – MLSC Apr 13 '14 at 05:10
1

Here is awk command:

awk -F"/$2/" '{split($1, a, "/"); system("echo mv " $0 " " a[length(a)]);}' <<< "$1"
mv /path/to/dir1/2010/file.db dir1

Once you're satisfied remove echo in system command.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • please check [this](https://github.com/MortezaLSC/Programming/blob/master/test.sh) – MLSC Apr 13 '14 at 05:52
  • That's what it does. You can set `s="$2"`. Isn't `dir1` parent dir of `2010`? – anubhava Apr 13 '14 at 05:54
  • Yes but this is a variable itself...may be dir1 maybe anything else...so the command `mv /path/to/dir1/2010/file.db dir1` I think shouldn't work – MLSC Apr 13 '14 at 05:56
  • 1
    Yes it can be anything. In above command if `s='/path/to/anything/2010/file.db'` then output will be: `mv /path/to/anything/2010/file.db anything` What else do you expect? – anubhava Apr 13 '14 at 06:00
  • Ok...Could you possibly put the whole script? I am not able write it because of some problems...Thank you very much – MLSC Apr 13 '14 at 06:01
  • I actually don't know what is `$1` and `$2` in your script and why are you doing `find $1`? – anubhava Apr 13 '14 at 06:05
  • $1 is absolute path that I want to find all subdirs...$2 is a number that can be 2010 or anything can be founded in my absolute path to filnaly find parent dirs – MLSC Apr 13 '14 at 06:08
  • find $1 is showing all dirs and subdirs and files of an absolute path. After that I want to tell script main dir is 2010($2) than I want to remove all 2010 dirs to their parent dir. Firstly I used arrays for doing that.But I think with `for loop` it is possible as well – MLSC Apr 13 '14 at 06:11
  • Thank you for your help..Yes I tried and doesn't work..now please see [this one](http://stackoverflow.com/questions/23040109/rename-specific-pattern-of-files-in-bash) surely you can understand what I exactly want...thank you – MLSC Apr 13 '14 at 07:45
  • I just got back, posted an answer on other question also. – anubhava Apr 13 '14 at 08:17