Reading extension of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading extension of files
# 1  
Old 06-05-2009
Reading extension of files

Hi,
I need a command to read extension of files. Could anyone please help me?
# 2  
Old 06-05-2009
ls -l |grep *.tgz

this will grep for the particular extension you need.
# 3  
Old 06-05-2009
if I get your question right... then you need to read the extn of all the files? Am I correct?

If so, above code will be of less help.

You can try this:

Code:
 
var1=abcd.xyz
var2=$(echo $var1 | sed 's/.*\(.\{3\}\)$/\1/')
echo $var2

What this will to is read the last three characters of your file name[assuming that your file extn is of 3 char only]
# 4  
Old 06-05-2009
Many Thanaks,
# 5  
Old 06-05-2009
try it

abc=test.txt
ext=$(echo $abc|awk -F "\." '{print $2}')

print $ext;---> txt

If you have to cut extension from all the files, U can using loop to list out all the file names, then inside that loop , u can use it.
# 6  
Old 06-05-2009
below could be more generic.

Code:
/home->echo "myfile.tmp" | sed '/\.[^.]*$/ s/.*\.\([^.]*$\).*/\1/p'
tmp
/home->echo "myfile.tmp2" | sed '/\.[^.]*$/ s/.*\.\([^.]*$\).*/\1/p'
tmp2
/home->echo "myfile.tmp2.tmp345" | sed '/\.[^.]*$/ s/.*\.\([^.]*$\).*/\1/p'
tmp345

# 7  
Old 06-05-2009
Hi Anchal,

Can you please explain the code above.....

Thanks in advance.

Last edited by Ygor; 06-05-2009 at 05:21 AM.. Reason: Removed email address
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. Shell Programming and Scripting

Replacing extension of files

hi, i am having a certain files in a folder where i need to replace the extension with similar file name for eg 1.csv 2.csv 3.csv 4.csv i need to replace the extension 1.csv 1.txt 2.csv 2.txt 3.csv 3.txt (3 Replies)
Discussion started by: rohit_shinez
3 Replies

4. Shell Programming and Scripting

Moving files extension one by one

Hi, I'm trying to do a script that move .Gz extension (one by one) in a new repertory called old-logs and then copy what's inside old-logs to a new.log but adding a date like this (something. gz: 2012:12:09). thanks in advance for your answers. (5 Replies)
Discussion started by: Froob
5 Replies

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

6. Shell Programming and Scripting

gzip the files with particular extension

Is there any way to compress only the files with .xml extension within a folder which in turn has many sub folders? gzip -r9 path/name/*.xml is not working This compression is done in the Windows server using Batch script. (2 Replies)
Discussion started by: Codesearcher
2 Replies

7. UNIX for Dummies Questions & Answers

Checking files extension

Hi, I need one line command to display all files that ends with .scr. Example: In a directory I have 10 files, out of that 4 files have filetype extension .dat and 4 files with .scr and 2 files with .txt.... In this i want to display only files that ends with .scr. I tried some commands,... (2 Replies)
Discussion started by: gwgreen1
2 Replies

8. HP-UX

Files without extension

I am brand new to hp unix systems. I see some files without extension on this system. If I type name of the file it shows me so many detail but does not take me back to command prompt. What are these files and how do I come back to command prompt? Please help (1 Reply)
Discussion started by: rajahindustani
1 Replies

9. Shell Programming and Scripting

Reading filenames with extension .xml

Hi, I want to write a script to read all the filenames with extension .xml in a directory and pass the name of the file, one by one, to another function. Please help me out. Regards. Saurabh (3 Replies)
Discussion started by: bhalotias
3 Replies

10. UNIX for Dummies Questions & Answers

How to test for files with a particular extension.

Hi!!, Is there any way on Ksh to test the files having a particular Extension? My program looks something like: for i in *$1* do if "$i" != files with ext of .Z then compress -f $i fi done Any suggestions??? :):) (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question