Getting current folder name appended to all desired files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Getting current folder name appended to all desired files

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-17-2011
c_bg1 c_bg1 is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 22 April 2011, 8:50 AM EDT
Posts: 4
Thanks: 5
Thanked 0 Times in 0 Posts
Getting current folder name appended to all desired files

Hello everyone,

Just registered here, I'm kinda new to Unix

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:


Code:
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'`.

2. 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!!!!

Last edited by c_bg1; 04-17-2011 at 03:32 AM..
Sponsored Links
    #2  
Old 04-17-2011
ahamed101's Avatar
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 31 October 2014, 5:13 AM EDT
Location: San Jose, CA
Posts: 1,910
Thanks: 54
Thanked 486 Times in 479 Posts
Try this...
The script is placed in the parent directory where the subfolders are present and ths script name is "run.sh"


Code:
#!/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
The Following User Says Thank You to ahamed101 For This Useful Post:
c_bg1 (04-17-2011)
Sponsored Links
    #3  
Old 04-17-2011
c_bg1 c_bg1 is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 22 April 2011, 8:50 AM EDT
Posts: 4
Thanks: 5
Thanked 0 Times in 0 Posts
Worked great, thanks Ahamed!

I have one last question

Now the script looks like this:

Code:
#!/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:

Code:
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.
    #4  
Old 04-17-2011
ahamed101's Avatar
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 31 October 2014, 5:13 AM EDT
Location: San Jose, CA
Posts: 1,910
Thanks: 54
Thanked 486 Times in 479 Posts
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


Code:
#!/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
The Following User Says Thank You to ahamed101 For This Useful Post:
c_bg1 (04-17-2011)
Sponsored Links
    #5  
Old 04-17-2011
c_bg1 c_bg1 is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 22 April 2011, 8:50 AM EDT
Posts: 4
Thanks: 5
Thanked 0 Times in 0 Posts
I was thinking of something like this:

Code:
#!/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 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
Sponsored Links
    #6  
Old 04-17-2011
ahamed101's Avatar
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 31 October 2014, 5:13 AM EDT
Location: San Jose, CA
Posts: 1,910
Thanks: 54
Thanked 486 Times in 479 Posts
I am not sure why it creates the merged file in the main folder.
Anyways you can try this giving the full folder path


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

regards,
Ahamed
The Following User Says Thank You to ahamed101 For This Useful Post:
c_bg1 (04-17-2011)
Sponsored Links
    #7  
Old 04-17-2011
c_bg1 c_bg1 is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 22 April 2011, 8:50 AM EDT
Posts: 4
Thanks: 5
Thanked 0 Times in 0 Posts
Thank you again!

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

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

each time the script loops.

Is there a way of disabling those messages?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Copy current date files from FTP to local folder Chicklet UNIX for Dummies Questions & Answers 3 08-18-2010 08:02 AM
How to find files in current folder only? Hangman2 Shell Programming and Scripting 7 01-04-2009 01:31 AM
How to copy set of files with date appended to their name sish78 UNIX for Dummies Questions & Answers 7 07-07-2008 05:21 PM
Use awk to create new folder in current directory ccox85 Shell Programming and Scripting 6 01-28-2008 02:59 AM
Pack current folder WebWatch UNIX for Dummies Questions & Answers 3 12-17-2007 04:46 AM



All times are GMT -4. The time now is 07:03 AM.