How to get files with a specific extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get files with a specific extension
# 1  
Old 03-12-2004
How to get files with a specific extension

Hi All,
How do I get only the files with the .csv extension. I have a shell script something like below:

#!/usr/bin/
#Directory to scan for files
SCANDIR="/cmb/data/exstream/scriptslogs/";
LOGFILE="/cmb/data/exstream/scriptslogs/test.log";
cd $SCANDIR
for FILE in * ; do
FILENAME=$FILE
if [ FILE = "*.csv" ]
then
echo "$FILE" >> $LOGFILE
fi
done

The condition I gave in IF doesn't seem like working.
Please help!!

Thanks in Advance
Jana
# 2  
Old 03-12-2004
First, you will need to specify a shell in the first line, not simply the path. Second, your variable declarations do not need the semi-colon at the end of the line. You are trying to get to fancy with your script.

#!/usr/bin/ksh
#Directory to scan for files
SCANDIR="/cmb/data/exstream/scriptslogs/"
LOGFILE="/cmb/data/exstream/scriptslogs/test.log"

ll $SCANDIR/*.csv >> $LOGFILE

or if you simply want a list of file names only, replace the line above with

ls -1 $SCANDIR/*.csv >> $LOGFILE

Last edited by google; 03-12-2004 at 08:35 PM..
# 3  
Old 03-12-2004
What about

for FILE in `ls *.csv`; do
echo $FILE >> $LOGFILE
done
# 4  
Old 03-12-2004
That would certainly work, but only in the present working directory. Why is a script needed for such a simple, mundane task?
# 5  
Old 03-12-2004
Hi cbkihong,
I tried what you said and it works.

Hi google
I need this task as a part of a bigger shell script, I have just posted a part of the whole script just to make it easy.

Thanks both for your suggestions.

Jana

Last edited by janavenki; 03-12-2004 at 08:57 PM..
# 6  
Old 03-13-2004
Quote:
Originally posted by google
That would certainly work, but only in the present working directory. Why is a script needed for such a simple, mundane task?
I would personally not get myself involved in shell scripting either for this very case, but who knows if the poster will later on change his/her mind and add more commands in the loop to do some extra things, or did (s)he actually show a simplified version of the script in the question, as this is indeed the case?

I don't know, so I don't mind being more verbose.
# 7  
Old 09-19-2007
Unix command to list files with a specific extension

If all you need is to find files with a specific extension, perhaps the "FIND" Unix command can do the job:
Code:
find . -name "*.ext"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash to remove find and remove specific extension

The bash below executes and does find all the .bam files in each R_2019 folder. However set -x shows that the .bam extension only gets removed from one .bam file in each folder (appears to be the last in each). Why is it not removing the extension from each (this is $SAMPLE)? Thank you :). set... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Bash to create sub directories from specific file extension

In the bash below I am trying to create sub-directories inside a directory from files with specific .bam extensions. There may be more then one $RDIR ing the directory and the .bam file(s) are trimmed (removing the extension and IonCode_0000_) and the result is the folder name that is saved in... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

4. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

5. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

6. Shell Programming and Scripting

Python - glob () - How to grep same files with different extension files

Hi I Have a directory and i have some files below abc.txt abc.gif gtee.txt ghod.pid umni.log unmi.tar How can use glob function to grep abc files , i have created a variable "text" and i assigned value as "abc", please suggest me how can we use glob.glob( ) to get the output as below... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

7. Shell Programming and Scripting

Delete all files with specific extension in directory tree

I'm sure this has been asked many times, but a search didn't turn up a definitive best method for this (if there ever is such a thing). I have been using rsync to back up my main data directory, but I have accumulated a large number of older backups that I don't need. All of the files I don't... (14 Replies)
Discussion started by: LMHmedchem
14 Replies

8. Shell Programming and Scripting

Passinng specific file extension to while loop in ksh

hello, i have the below while loop wherein i am passig list of filenames to be scped. this is in unix ksh - filenamelist.txt has list of files names, including .dat and .txt files but i want to pass only the .txt filenames to the while loop so that only .txt files gets scped. how can... (4 Replies)
Discussion started by: billpeter3010
4 Replies

9. UNIX for Dummies Questions & Answers

How to list files with no extension together with *.prog files?

Hi, I know that to list files with no extension, we can use.. ls -1 | grep -v "\." And to list .prog files, we can use.. ls -1 *.prog or ls -1 | grep '.prog$' (4 Replies)
Discussion started by: adshocker
4 Replies

10. Shell Programming and Scripting

While file exists with specific extension

Hi, I need some help to see if I this is posible with a while loop. I need to run a script once a day that will take all of the *.TXT files in a folder and rename them to a specific file name structure with a .dat extension. I wrote this script, but I get an error that says " line 3: too many... (2 Replies)
Discussion started by: strpwr
2 Replies
Login or Register to Ask a Question