search for a file extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search for a file extension
# 1  
Old 12-08-2009
search for a file extension

hi all,

i'm new to shell scripting, i need help from u guys to do my task now..

i just need to check a file extension existence in a directory, and if it exists then i have to continue my processing.

pls give me the command to check the extension of the files
# 2  
Old 12-08-2009
Wrench

Hi

try the following to get the extensions of all the files in a dir

Code:
dirLoc=""   // initialize this
cd $dirLoc
for FILENAME in `ls`
 do
  FILEEXTENSION=$(echo $FILENAME | awk -F '.' '{if (NF>1)print $NF}')
  if [ -z $FILEEXTENSION ] ; then
    continue
 fi
  echo "$FILENAME    ::::    $FILEEXTENSION"
done

now using the value of variable FILEEXTENSION you can branch again if needed.

Cheers
# 3  
Old 12-08-2009
hello
you can use find that searches files recursively.. the right combination find/exec|xargs
for e.g. if you want to find all text file that contains pattern "hello".
do
Code:
find . -iname "*.txt" -exec egrep -i '\bhello\b' {} \;

and it would do fine.
you can capture the output of the output use backticks `` or $() .
regards.
# 4  
Old 12-08-2009
OK, here's my entry. Replace .xml with whatever extension you want:
Code:
if ls *.xml >/dev/null 2>&1; then
    echo Do whatever you need to do here
fi

We don't want the ls command to print the files, so we direct both stdout and stderr to null. We use the side effect of ls to tell us if it found any files.
# 5  
Old 12-08-2009
if you know the extension, use shell expansion. don't use for loop with ls
Code:
for file in *.extension
do
   # do processing
done

# 6  
Old 12-08-2009
Quote:
Originally Posted by ghostdog74
if you know the extension, use shell expansion. don't use for loop with ls
Interesting. Of the four answers given, three assumed he wants to loop through the files with that extension.

But he only said he needs to check if the extension exists in the directory or not. That shouldn't require a loop.

So how would you use shell expansion for a simple test? I experimented with things like this, but I couldn't get it to work reliably.
Code:
if [ *.xml != "*.xml" ]; then

# 7  
Old 12-08-2009
Quote:
Originally Posted by KenJackson
Interesting. Of the four answers given, three assumed he wants to loop through the files with that extension.

But he only said he needs to check if the extension exists in the directory or not. That shouldn't require a loop.
you used ls, which is the same as "looping through the files", except that ls "abstracted" the process for you.
with the shell, if he knows what extension he is searching for.
Code:
for file in *extension
do
  echo "found $file"
done

or another way
Code:
shopt -s nullglob
a=$(echo *extension)
[ -z "$a" ] && echo "null"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

File Extension Missing in file-$var.csv

I'm afraid this is a silly question but I can't figure it out. I have a script like so... echo "Enter DRDL Signature Version Number" read DRDL_Number mv signature_output.csv SERVICE_OBJECTS_S-$DRDL_Number.csv The resultant filename does not contain the .csv as follows.... (3 Replies)
Discussion started by: Cludgie
3 Replies

3. 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

4. Shell Programming and Scripting

Getting the file extension

I have a file n06-z30-sr65-rgdt0p25-varp0.25-8x6drw-test.cmod and I want to get the extension. At the moment I have set filextension = `echo $f | awk 'BEGIN {FS="."} {print $2}'` which of course does not work as there is a point in varp0.25 (13 Replies)
Discussion started by: kristinu
13 Replies

5. UNIX for Dummies Questions & Answers

creating separate directories according to file extension and keeping file in different directory as

unix program to which a directory name will be passed as parameter. This directory will contain files with various extensions. This script will create directories with the names of the extention of the files and then put the files in the corresponding folder. All files which do not have any... (2 Replies)
Discussion started by: Deekay.p
2 Replies

6. UNIX for Dummies Questions & Answers

About the file extension

Hi everyone, Can we know that which type of file is there without opening a file thanks in advance.... kunal patil (3 Replies)
Discussion started by: kunalpatil09
3 Replies

7. Shell Programming and Scripting

Recursicely search and rename file extension

Greetings to all!!:b: I have one root folder containing several other folders inside it. This tree structure is deep. And the files are of similar extension. I need to start at the top level and recursively search and rename all the files with say .a extension to .b . This is the code to... (7 Replies)
Discussion started by: riverside
7 Replies

8. Shell Programming and Scripting

Search for all the unique file extension

Greetings to All ... :b: I have one root folder containing different other folders within it. I need to get the list of all different types of file extensions residing in those folders. Could anyone help me providing some shell script? (1 Reply)
Discussion started by: riverside
1 Replies

9. UNIX for Advanced & Expert Users

File extension search and copy

Hi need to know if we can write a shell script to find files for a particular format;s ie both .csv and .txt in a particular folder and then copy them to a new folder on a dialy basis. Does anyone know how this can be accomplished? Thanks, Sandeep (20 Replies)
Discussion started by: bsandeep_80
20 Replies

10. UNIX for Dummies Questions & Answers

string search in folders with particular multiple file extension

Hi, I am newbie in UNIX so please excuse for my questions. Is there a a way to search for string in files within folder and sub folder in particluar file extensions. Ex. search for ABC in folder 'A'(including it's sub folders) in html, xml files. Thanks, Ani (2 Replies)
Discussion started by: anikanch
2 Replies
Login or Register to Ask a Question