Getting current folder name appended to all desired files

Hello everyone,

Just registered here, I'm kinda new to Unix :o

I've been trying to automate some processes with various Windows tools. I found that using unix scripts the result would be closest to my needs. So I installed Cygwin on Windows 7.

My folders and files are structured like this:

folder1
-file1_temp.txt
-file2_temp.txt

folder2
-file1_temp.txt
-file2_temp.txt

The desired outcome should look like this:

folder1
-file1_folder1.txt
-file2_folder1.txt

folder2
-file1_folder2.txt
-file2_folder2.txt

Now, here's what's bothering me:

  1. I have a loop script for renaming files which contain the "temp" string for the current opened folder:
for i1 in *temp.*
do
i2=`echo $i1 | sed 's/temp/<FOLDER_NAME>/g'`
mv $i1 $i2
done

I want to automate the process of renaming files and replacing "temp" string in each file with the current folder name, but I'm failing to find a command which would automatically find the current folder name and put it in sed 's/temp/<FOLDER_NAME>/g'`.

  1. I would also like to automate the above loop so that it could be applied to all folders (folder1, folder2, etc).

Thank you in advance guys!!!!

Try this...
The script is placed in the parent directory where the subfolders are present and ths script name is "run.sh"

#!/bin/ksh

HOMEDIR=/user/ahamed/test
dirs=`ls | grep -v run`

for dir in $dirs
do
  cd $HOMEDIR/$dir
  files=`ls`
  dirname=`echo $dir | sed 's=/==g'`
  for file in $files
  do
    newfile=`echo $file | sed "s/temp/$dirname/g"`
        mv $file $newfile
  done
done

regards,
Ahamed

1 Like

Worked great, thanks Ahamed!

I have one last question :o:o

Now the script looks like this:

#!/bin/ksh
HOMEDIR=/cygdrive/n/test
dirs=`ls | grep -v run`
for dir in $dirs
do
  cd $HOMEDIR/$dir
  files=`ls`
  dirname=`echo $dir | sed 's=/==g'`
  for file in $files
  do
    newfile=`echo $file | sed "s/temp/$dirname/g"`
        mv $file $newfile
  done
done

I have one other script with grep and sed commands which creates the mentioned above temp files. It looks like this:

grep '.*' *.txt > merged_temp.txt #merging all text files in the opened subfolder to one file
sed -i 's/.txt:/ /g' merged_temp.txt #trimming the merged file for better looks

Is there a way to join the above two scripts together, so that the script would execute the command in the following order for every subfolder in the main directory:

  1. enter first subfolder and execute the grep and sed commands, creating the merged file in the current folder;
  2. rename the merged file in the first subfolder to the name of the subfolder;
  3. exit the first subfolder;
  4. loop script with all subfolders until the end.

Thanks again, I appreciate the help.:b::b:

If you are using the grep and sed command to merge all the file in the subfolder to one big file, then you can try this

#!/bin/ksh
HOMEDIR=/cygdrive/n/test
dirs=`ls | grep -v run`
for dir in $dirs
do
  cd $HOMEDIR/$dir
  files=`ls`
  dirname=`echo $dir | sed 's=/==g'`
  for file in $files
  do
    echo $file >> merged_$dir.txt    #this will have the file name first
    cat $file >> merged_$dir.txt    # and then the contents.
    newfile=`echo $file | sed "s/temp/$dirname/g"`
    mv $file $newfile
  done
done

regards,
Ahamed

1 Like

I was thinking of something like this:

#!/bin/ksh
HOMEDIR=/cygdrive/n/test
dirs=`ls | grep -v run`
for dir in $dirs
do
  cd $HOMEDIR/$dir
  files=`ls`
  dirname=`echo $dir | sed 's=/==g'`
  for file in $files
  do
	grep '.*' *.txt > _merged_temp.txt
	sed -i 's/.txt:/ /g' _merged_temp.txt
	newfile=`echo $file | sed "s/temp/$dirname/g"`
    mv $file $newfile
  done
done

But the above script creates the merged file in the main directory, not in the subfolder where it needs to be :frowning: And if there's another folder, it copies it into the selected subfolder.

Doh.. it's getting complicated... I started this yesterday. Be easy on me :slight_smile:

I am not sure why it creates the merged file in the main folder.
Anyways you can try this giving the full folder path

    ...
    grep '.*' *.txt > $HOMEDIR/$dir/_merged_temp.txt
    sed -i 's/.txt:/ /g' $HOMEDIR/$dir/_merged_temp.txt
    ...

regards,
Ahamed

1 Like

Thank you again!

Everything is fine now, only thing that bothers me is the message:

mv: `file1.txt' and `file1.txt' are the same file

each time the script loops.

Is there a way of disabling those messages?

...
mv $file $newfile >/dev/null 2>&1
...

regards,
Ahamed

1 Like