Solved

Need help manipulating files (removing, renaming) in cygwin

Posted on 2004-10-01
8
213 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now