0

I have several sub-directories with no unanimous naming pattern within my home directory (for example ~/123, ~/456, ~/789).

Within each of these sub-directories, I have two folders named alignment1 and alignment2. In the folders alignment1 and alignment2 there are several files.

The files of interest to me are named alignment1 (no extension) in the alignment1 folder and the file alignment2 (no extension) in the alignment2 folder.

Please remember that in the folders alignment1 and alignment2 there are other files named alignment, but they have extensions (for example, alignment1.backbone, alignment1.bbcol and alignment2.backbone, alignment2.bbcol in respective alignment1 and alignment2 folders), but I am not interested in these files.

~/123/alignment1/alignment1
~/123/alignment2/alignment2
~/456/alignment1/alignment1
~/456/alignment2/alignment2
etc...

Question:

  1. My struggle is to rename the folders alignment1 and alignment2 to subdirectory_alignment1 and subdirectory_alignment2 (for example, 123_alignment1 and 123_alignment2).

  2. Then, in the folders alignment1 and alignment2, the alignment files, named alignment1 and alignment2 respectively, need to be renamed to subdirectory_1.aln and subdirectory_2.aln.

  3. Move the subdirectory_1.aln and subdirectory_2.aln to home directory.

I think it is closely related to this and this question, but I have been trying to amend the answers in the above postings for last few hours with no success.

Community
  • 1
  • 1
Abi
  • 11

1 Answers1

0

Trying to break the problem into small parts. This piece of code should find all occurences. Perhaps you should change something in the regex. \1 will match something that already matched inside \( and \). mindepth should find */*/* and below. The * is important here because of the number of / you will have on the result.

find * -mindepth 2 -type f | grep '/\([a-z]*[0-9]\)/\1' | while read f; do
  <process>
done

Now you are iterating all files of your interest. echo "$f" inside the loop is a nice idea before moving forward.

If the filter is working, below is some ideas to break the filename into small parts:

d1=`cut -d'/' -f1 <<< "$f"`
d2=`cut -d'/' -f2 <<< "$f"`
newName="${d1}/${d1}_${d2}"

Now just some simple mv "$from" "$to" here and there.

Joao Morais
  • 1,885
  • 13
  • 20
  • Sorry, but the code doesn't seem to be working for me. I have tried using echo "$f" but no matchings found. I think even find is not finding the files -: ..I know that I am not being helpful here as there is no error message to relate to..Any thoughts? – Abi Jan 29 '16 at 14:40
  • @Abi Try only find * -mindepth 2 -type f (from the root dir of your search), simplify the grep. The trick here is build your script one part at a time. – Joao Morais Jan 29 '16 at 15:01
  • So, this is how far I have got so far; find . -name "alignment1" | sed 's#./\([A-Za-z0-9_-]*\)/alignment1#mv \1/alignment1 \1/\1_alignment1#'. This code is renaming the folder alignment1 to subdirectory_alignment1 but as you said is finding all occurrences of alignment1, including the files alignment1. I can't get it to recognize only the folder alignment1. Can you think of an amend? – Abi Jan 29 '16 at 15:59
  • I have used the selective commands it prints out to rename alignment1 to subdirectory_alignment1 BUT I am not being successful in changing the script to rename the alignment1 files, now in the subdirectory_alignment1 folder to subdirectory_alignment1.aln..Any idea? – Abi Jan 29 '16 at 16:03
  • Further development; now this command-line find . -name "alignment1" | sed 's#./\([A-Za-z0-9]*\)/alignment1#mv \1/alignment1 \1/\1.aln#' is finding the alignment1 file in the subdirectory_alignment1 but the mv part of it is not working?? – Abi Jan 29 '16 at 16:11
  • @Abi one solution I can think is egrep '/(ab)/\1', which will match /ab/ab but not /ab/cd. Don't know your directory tree but it sounds to me that find -type f (and without -name) | grep is the way to go. Furthermore: you cannot use mv inside sed; use the syntax "| while read f; do" (on my answer) to iterate the filenames. – Joao Morais Jan 29 '16 at 16:16