Solved

Need help manipulating files (removing, renaming) in cygwin

Posted on 2004-10-01
8
219 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 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.

688 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