Shell script help for listing files

Hi,

I need to work on script which will give me the selective list of files in a directory into another file. I will explain , I have a script abc.sh , the shell is ksh. I have a directory /home/anand/output. The output will have files by the name file1.txt , file2.txt and file1.txt.1 and file2.txt.2. Now I want to get a list of files without the .1 and .2 extensions only. and put them into a list.txt file. So my list.txt will have file1.txtand file2.txt. Can someone help me with this. I am not being able to figure out how to not select files with the last alphabet as a numeral.

Regards,
Anand
anandabrataAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
You don't need a shell script, a simple Unix command will do:
# ls -l |  grep '.*\.txt$' > listing.txt
This fill list all files with name ending with ".txt" and put this list into file listing.txt
0
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
If you want to see only files _not_ ending with ".<number>", use
 # ls -l | egrep -v '\.[0-9]$'
0
anandabrataAuthor Commented:
Would ls -l | egrep -v '\.[0-9]$' give me the same result if used in script or do i need to do something extra.

Regards
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

anandabrataAuthor Commented:
Also my files can have extension from 1 to 99 so in that case ls -l | egrep -v '\.[0-9]$ does not work. I tried ls -l | egrep -v '\.[0-99]$ and even that does not work.

regards,
anand
0
anandabrataAuthor Commented:
Thanks this # ls -l |  grep '.*\.txt$' > listing.txt solves the problem. Let me try it out in a script and I will get back to you.
0
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
Try
  # ls -l | egrep -v '\.[0-9][0-9]*$'

All file _not_ ending with a number (at least one digit long)
0
anandabrataAuthor Commented:
Oh no!! I just figured that the files might end with CXX. Which means C01, C02. so just giving "txt" will not help. I need something with egrep.  
0
anandabrataAuthor Commented:
Hi,

This command ( ls -l | egrep -v '\.[0-9][0-9]*$') works great excepting that it will also list the directories in which it runs. I have another saving grace i.e all the files that I want will always start with 2112657. So in addition in the above condition , even this needs to be checked. I tried
ls | egrep -v '\.[0-9][0-9]*$' | ls | grep '2112657*' and
ls | egrep -v '\.[0-9][0-9]*$' > ls | grep '2112657*'
but they don't work.

Regards,
Anand
0
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
The RE (regular expression)
    [0-99]
is not valid! You must use
    [0-9][0-9]*
instead. Therefore:
  # ls -l  | egrep -v '\.[0-9][0-9]*$'
lists all files _not_ ending with a number (at least one digit).
0
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
a) The "ls" lists *all* files and the "grep ..." filters out what you want to see (or not to see using -v)

Therefore:
  ls | egrep -v '\.[0-9][0-9]*$' | ls | grep '2112657*
should read
  ls | egrep -v '\.[0-9][0-9]*$' |  grep '^211'
  (everything NOT ending with "dot" "number") AND ( starting with 211)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anandabrataAuthor Commented:
Thanks that helps a lot. I just need help with another thing, I want make a recursive call to the ls command used above. Like this
acty_loa_cnt=do a select from database which will retrun me a count value
if [ $acty_loa_cnt -eq 11 ]
      then
        ls | egrep -v '\.[0-9][0-9]*$' |  grep '^211'
        else
       sleep 1000
fi
#do the select and if condition block above again. Basically make it recursive with a sleep of 1 min. how is that possible. Btw appreciate your help.

Regards,
Anand

0
amit_gCommented:
If these are the only lines in your script, you can call the script again

acty_loa_cnt=do a select from database which will retrun me a count value
if [ $acty_loa_cnt -eq 11 ]
      then
        ls | egrep -v '\.[0-9][0-9]*$' |  grep '^211'
        else
       sleep 1000
fi
#Call the same script again
$0

Or you could put all this in a loop

while :
do
       acty_loa_cnt=do a select from database which will retrun me a count value
       if [ $acty_loa_cnt -eq 11 ]
             then
               ls | egrep -v '\.[0-9][0-9]*$' |  grep '^211'
               else
              sleep 1000
       fi
done

The only way it would stop is via Ctrl-C or if some error occurs. If you really want to run this every minute indefinitely, you should schedule it as a cron job (without sleep command).

BTW, sleep 1000 should be sleep 60 for 1 minute sleep.

0
TintinCommented:
So you're saying you want a list of all .txt files (without a number on the end) and any file ending in CXX, if so then a simple

ls -l *.txt *C[0-9][0-9]

will do that.
0
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
Tintin,

I think he also has files ending in Cx as well as Cxx

BTW: If we got all required information in the beginning it would have been somewhat easier and quicker coming up with the right solution ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.