|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
doneregards, Ahamed |
| The Following User Says Thank You to ahamed101 For This Useful Post: | ||
c_bg1 (04-17-2011) | ||
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
doneI 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
|
||||
|
||||
|
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
doneregards, Ahamed |
| The Following User Says Thank You to ahamed101 For This Useful Post: | ||
c_bg1 (04-17-2011) | ||
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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
doneBut 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
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|