-1

I'm writing a unix shell script that sorts data in ten subdirectories (labelled 1-10) of the home directory. In each subdirectory, the script needs to rename the files hehd.output and fort.hehd.time, as well as copy the file hehd.data to a .data file with a new name.

What I'd like it to do is rename each of these files in the following format:

AA.BB.CC

Where

  AA = a variable in the hehd.data file within the subdirectory containing the file
  BB = the name of the subdirectory containing the file (1-10)
  CC = the original file name

Each subdirectory contains an hehd.data file, and each hehd.data file contains the string ij0=AA, where AA represents the variable I want to use to rename the files in the same subdirectory.

For example: When run, the script should search /home/4/hehd.data for the string ij0=2, then move /home/4/hehd.output to /home/4/2.4.hehd.output.

I'm currently using the grep command to have the script search for the string ij0=* and copy it to a new text file within the subdirectory. Next, the string ij0= is deleted from the text file, and then its contents are used to rename all target files in the same subdirectory. The last line of the shell script deletes the text file.

I'm looking for a better way to accomplish this, preferably such that all ten subdirectories can be sorted at once by the same script. My script seems incredibly inefficient, and doesn't do everything that I want it to by itself.

How can I improve this?

Any advice or suggestions would be appreciated; I'm trying to become a better computer user and that means learning better ways of doing things.

Rhyphte
  • 3
  • 2
  • 1
    Please consider providing actual code, as opposed to only English pseudocode. Implementation details matter, and we can't see what they are without reading the implementation. – Charles Duffy Jul 14 '16 at 17:49
  • 1
    (Also, in general, there's a preference in StackOverflow for focused questions with specific answers; open-ended questions that can be interpreted as "can we discuss X?" aren't entirely welcome here, as opposed to a question of "how can I fix problem X?" that has a concrete solution; for general-purpose "how can I improve this already-working code?" questions, see http://codereview.stackexchange.com/ -- but do read the site rules there, rather than just reposting). – Charles Duffy Jul 14 '16 at 17:52
  • 1
    ... http://stackoverflow.com/help/dont-ask covers similar territory. – Charles Duffy Jul 14 '16 at 17:54

1 Answers1

0

Try this:

fromdir=/home
for i in {1..10};do
  AA=$(sed 's/ij0=\([0-9]*\)/\1/' "$fromdir/$i/hehd.data")
  BB="$i"
  for f in "$fromdir/$i/"*;do
    CC="${f##*/}"
    if [[ "$CC" = "hehd.data" ]]; then 
      echo cp "$f" "$fromdir/$i/$AA.$BB.$CC"
    else
      echo mv "$f" "$fromdir/$i/$AA.$BB.$CC"
    fi
  done
done

It loops over directories using Bash sequence {1..10].

In each directory, with the sed command the ij0 value is assigned to AA variable, the directory name is assigned to BB.

In the file loop, if the file is hehd.data it's copied, else it's renamed with the new name.

You can remove the echo before cp and mv commands if the output meets your needs.

SLePort
  • 15,211
  • 3
  • 34
  • 44
  • Without the OP posting his code, you could end up with the same code he has already written. – alvits Jul 14 '16 at 19:19
  • @alvits He gives a short description of its approach (create file from `grep` output, delete variable name from file, delete file) that is not mine. Let us wait and see. – SLePort Jul 14 '16 at 19:33