Solved

Need help manipulating files (removing, renaming) in cygwin

Posted on 2004-10-01
8
216 Views
Last Modified: 2013-12-16
First, I'm doing this in cygwin, on a win2k machine.  However, it seems that i have enough linux tools to get the job done (i hope), if I could just figure out how to use them!

The problem:
I need to delete a group of files that match a pattern.  Basically, I'm running a process that alters a bunch of files and appends _fill to the file name, e.g. myfile.txt -> myfile_fill.txt.  I need to delete the original files and rename the processed ones, leaving any unprocessed ones alone.

I.e. if the folder contains
myfile1.txt
myfile1_fill.txt
myfile2.txt
myfile2_fill.txt
myfile3.txt

After I'm done, I need it to look like this:
myfile1_fill.done
myfile2_fill.done
myfile3.txt

This is deleting the myfile1.txt and myfile2.txt and renaming myfile1_fill.txt to myfile1_fill.hld, etc. This will leave the unprocessed one in a state to easily retry the process.

Any help would be appreciated!

Thanks!
Teiwaz

0
Comment
Question by:teiwaz
  • 5
  • 3
8 Comments
 
LVL 10

Expert Comment

by:Luxana
ID: 12206045
#!/bin/sh
for file in `ls | egrep \_ `
do {
        nm=${file%\.txt}
        rm=`echo $file | sed 's/_fill//'`
                echo "$file $nmdone"
                         mv $file $nm.done
                         rm $rm
                        } done

______
works fom me...
0
 
LVL 10

Accepted Solution

by:
Luxana earned 500 total points
ID: 12206456
one more thing if you will run that script in directory where you already have files like myfile1_fill.done  myfile2_fill.done you better:

 #!/bin/sh
for file in `ls | egrep \_fill\.txt `
do {
        nm=${file%\.txt}
        rm=`echo $file | sed 's/_fill//'`
                echo "$file $nmdone"
                         mv $file $nm.done
                         rm $rm
                        } done

LUXANA
0
 
LVL 1

Author Comment

by:teiwaz
ID: 12208890
very nice!  worked like a charm.

now, if you could do a bit of explaining....

  nm=${file%\.txt}

I understand that this is assigning the file name to the var nm, but don't get syntax.  It is somehow stripping off the extension. How?

Guess that is the only part I don't understand.

Thanks again!
Teiwaz
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 10

Expert Comment

by:Luxana
ID: 12209504
Hi Teiwaz

nm=${file%\.txt} in other words you assing variable to nm from variable file but in that case we took just part of variable file and rest of the string .txt we throw away.

1) for file in `ls | egrep \_fill\.txt `
we need just files in current directory with "_fill.txt" ( myfile1_fill.txt )
file=myfile1_fill.txt and so on...
2) nm=${file%\.txt}
mn=myfile1_fill
3) rm=`echo $file | sed 's/_fill//'`
because we need to delete files which belongs to myfile1_fill.txt I used sed to change strin and assign value to
rm=myfile1.txt
4) echo "$file $nmdone"
I forgot "." there so to make it perfect put it there $nm.done so replace that line with
echo "renaming $file to  $nm.done"
5)mv $file $nm.done
renaming
6) rm $rm
remove files

Glad to help !!

LUXANA
0
 
LVL 1

Author Comment

by:teiwaz
ID: 12209586
Thanks. I'm clear on what everthing is doing.  However, I'm still clear on what the ${%/} syntax is.

so does

   ${}

mean we are going to do some sort of manipulation on the value of the variable?

And does the % mean we are going to truncate? or ignore anything that matches \.txt?

Is the \ used with % or do they have separate meaning?

Ya, I'm a curious one!
Teiwaz
0
 
LVL 10

Expert Comment

by:Luxana
ID: 12209662
\ is confusing you in fact you can run that scrip without \ because I used \ just in case like escape character for "." to avoid any possible errors :-)

a=foo.bar
b=${a%r}
so
b=foo.ba

a=foo.bar
b=${a%bar}
so
b=foo.

and so on...







0
 
LVL 1

Author Comment

by:teiwaz
ID: 12210241
Thanks Luxana! Great work!
0
 
LVL 10

Expert Comment

by:Luxana
ID: 12213852
My pleasure !!

LUXANA
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question