Solved

Shell script help for listing files

Posted on 2007-03-22
14
313 Views
Last Modified: 2013-12-27
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
0
Comment
Question by:anandabrata
14 Comments
 
LVL 16

Expert Comment

by:Hanno Schröder
ID: 18771455
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
 
LVL 16

Expert Comment

by:Hanno Schröder
ID: 18771502
If you want to see only files _not_ ending with ".<number>", use
 # ls -l | egrep -v '\.[0-9]$'
0
 

Author Comment

by:anandabrata
ID: 18771544
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
 

Author Comment

by:anandabrata
ID: 18771574
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
 

Author Comment

by:anandabrata
ID: 18771666
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
 
LVL 16

Expert Comment

by:Hanno Schröder
ID: 18771713
Try
  # ls -l | egrep -v '\.[0-9][0-9]*$'

All file _not_ ending with a number (at least one digit long)
0
 

Author Comment

by:anandabrata
ID: 18771737
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:anandabrata
ID: 18772290
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
 
LVL 16

Expert Comment

by:Hanno Schröder
ID: 18772484
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
 
LVL 16

Accepted Solution

by:
Hanno Schröder earned 500 total points
ID: 18772524
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
 

Author Comment

by:anandabrata
ID: 18773324
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
 
LVL 58

Expert Comment

by:amit_g
ID: 18773828
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
 
LVL 48

Expert Comment

by:Tintin
ID: 18775078
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
 
LVL 16

Expert Comment

by:Hanno Schröder
ID: 18778095
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
This article will show, step by step, how to integrate R code into a R Sweave document
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.:
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now