Scripting names from a file

Nifty Hat Mitch mitch48 at sbcglobal.net
Wed Sep 29 04:32:34 UTC 2004


On Tue, Sep 28, 2004 at 04:53:54PM +0200, Chadley Wilson wrote:
> >
> > You don't need a script for that. Something simple as:

> OK Maybe I am trying to be too clever in my simplification of the
> senario, so maybe this helps: I have a directory with files in it, I
> also have a text file with a list of certian files, I need to mv the
> files listed in the text files out of the dir into a new dir which I
> have created.  Does that make more sense?

I am lazy and feel handy with in vi.

Since you have a file full of file names
And since I have no clue how the file was generated
and validated.... I want to inspect the file first.

And, if this is a one time action....

I tend to just make a copy of the file
and edit it to be a script.  Working in the 
editor can permit me to see problems.

    cp ./listOfiles listOfiles.sh

    vi ./listOfiles.sh
 Check the first and last lines, 
 these not smiley faces but checking will make ya happy.
    :{
    :0
    :}
    :$
 search for white space and meta characters ... 6 ways to sunday...
 a meta char discovery script line would be handy I can never 
 recall them all.  Mostly I just read the file... I do search for the
 obvious.

    / 
    /\t 
    /'
    /"
    /\$
    /&
    /@
    /#
    /?
    />
    /<
    /-
    /(
    /)
    /{
    /}
    /[
    /]
    /+
    /\*
    /\.
    /:
    /;
    /|
    /!
 
 turn it into a scrip.
    :1,$s/^/mv -i /
    :1,$s/$/ new-dir/

 If ok...
    :wq

    bash -n < ./listOfiles.sh | less
    bash < ./listOfiles.sh


The advantage is that looking at the listOfiles
is part of the process.   My partial list of meta 
char searches might indicate the scope of potential
pitfalls.

I can search for spaces and meta characters with vim I can also build it in
safe steps to validate the lines...  i.e. replace "mv " with "echo" or
"stat"....

I can insert "set -x" to make it noisy/verbose.

If I am going to do this again and again then 
I would do some more interesting looping and scripting.
Clearly some safety testing of the input should be 
added.    

 cat listOfiles | while read i
 do
	echo ===========================
 	echo $i
	mv -i $i newdir
 done | less

or...

 while read i < listOfiles
 do
	echo ===========================
 	echo $i
	mv -i $i newdir
 done | less


YMMV.


--
	T o m  M i t c h e l l 
	Me, I would "Rather" Not.




More information about the fedora-list mailing list