Getting current folder name appended to all desired files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting current folder name appended to all desired files
# 1  
Old 04-17-2011
Getting current folder name appended to all desired files

Hello everyone,

Just registered here, I'm kinda new to Unix Smilie

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 04:32 AM..
# 2  
Old 04-17-2011
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
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 04-17-2011
Worked great, thanks Ahamed!

I have one last question SmilieSmilie

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.SmilieSmilie
# 4  
Old 04-17-2011
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
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 04-17-2011
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 Smilie 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 Smilie
# 6  
Old 04-17-2011
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
This User Gave Thanks to ahamed101 For This Post:
# 7  
Old 04-17-2011
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

2. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. UNIX for Dummies Questions & Answers

Move txt file to with current date appended to filename

I have multiple txt files which begin with the word "orders" in folder C:\source. I need to move the files to folder C:\dest and rename them to "process_<date>_<count>" So for example , if there are 3 files ordersa.txt , ordersb.txt and ordersc.txt in C:\source , after running the script I want... (7 Replies)
Discussion started by: johannd
7 Replies

4. Shell Programming and Scripting

Move txt file to with current date appended to filename

I have multiple txt files which begin with the word "orders" in folder C:\source. I need to move the files to folder C:\dest and rename them to "process_<date>_<count>" So for example , if there are 3 files ordersa.txt , ordersb.txt and ordersc.txt in C:\source , after running the script I want... (1 Reply)
Discussion started by: johannd
1 Replies

5. Shell Programming and Scripting

Convert all files in current folder from UTF8 to ANSI, name unchanged.

Asking for a Linux command line to convert all files in current folder from UTF8 to ANSI, name unchanged. Best Regards Pei (3 Replies)
Discussion started by: jiapei100
3 Replies

6. UNIX for Dummies Questions & Answers

tar file from current folder

Hello guys, I am sure this has been asked before, but honestly, I cant find post talking about it. Here is what I need: - A tar file will be generated manually by user - This tar file is then used within a bash shell script My source folder structure is like this: ... (2 Replies)
Discussion started by: manolain
2 Replies

7. Shell Programming and Scripting

How to find files in current folder only?

How do I find files in current folder only? We are on AIX 5.3, so maxdepth is not supported. I tried to do this find /dir1/dir2/dir3/dir4 -prune -type f to display all files in /dir1/dir2/dir3/dir4 only but it does not show any files. Somehow the -prune option works for dir3 level... (7 Replies)
Discussion started by: Hangman2
7 Replies

8. UNIX for Dummies Questions & Answers

How to copy set of files with date appended to their name

Hi gurus, I have set of files in a directory ex : test_file1.csv test_file2.csv test_file3.csv my requirement is to copy these files into another directory but with date appended to the name : like after copying the files should look like : test_file1_07072008.csv ... (7 Replies)
Discussion started by: sish78
7 Replies

9. Shell Programming and Scripting

Use awk to create new folder in current directory

Alright, I am sure this is a laughable question, but I don't know so I am going to ask anyway. I have a little script I am writing to take information from one source, recode it in a certain way, and print to files for each subject I have data for. This all works perfectly. I just want to put... (6 Replies)
Discussion started by: ccox85
6 Replies

10. UNIX for Dummies Questions & Answers

Pack current folder

How do I pack (using tar zcvf ?) the current folder inluding all files and folders ?? I need to be sure to get all files and folders/subfolders... Later I will unpack into a new folder on a new server.. Appreciate any help.. (3 Replies)
Discussion started by: WebWatch
3 Replies
Login or Register to Ask a Question