Solved

Shell script help for listing files

Posted on 2007-03-22
14
312 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

746 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

9 Experts available now in Live!

Get 1:1 Help Now