?
Solved

Need help manipulating files (removing, renaming) in cygwin

Posted on 2004-10-01
8
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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