Solved

Need help manipulating files (removing, renaming) in cygwin

Posted on 2004-10-01
8
217 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

730 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