473,889 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finding file type

12 New Member
I am newbie... this may be a basic question...

In korn shell script, how to find the type of the file.... here is the pseudo

if (filename is of type ".eml" or ".jpg") then
--- do something

thx in advance.
Feb 21 '07 #1
12 9400
Motoma
3,237 Recognized Expert Specialist
I am newbie... this may be a basic question...

In korn shell script, how to find the type of the file.... here is the pseudo

if (filename is of type ".eml" or ".jpg") then
--- do something

thx in advance.
Do you have a filename to begin with? Could you post your shell script thus far?
Feb 21 '07 #2
trs204
12 New Member
The directory contains the following files
.html
.jpg
.css
.js

I want only the .jpg files checked based on the extension or some file type logic.

1) for x in ${directory}/*.*
2) if (x endswith .jpg) then /// finding a particular type of file.
// do something

I am looking for the exact condition (FOR ENDS WITH) how to keep on the second line.

thx in advance.
Feb 21 '07 #3
Motoma
3,237 Recognized Expert Specialist
The directory contains the following files
.html
.jpg
.css
.js

I want only the .jpg files checked based on the extension or some file type logic.

1) for x in ${directory}/*.*
2) if (x endswith .jpg) then /// finding a particular type of file.
// do something

I am looking for the exact condition (FOR ENDS WITH) how to keep on the second line.

thx in advance.
I can think of ways to do this with filters, but off hand I am not familiar with ksh or shell scripting.

Expand|Select|Wrap|Line Numbers
  1. $ head `ls | grep ".txt"`
  2.  
This will perform "head" on all files in the current directory that have the .txt extension.

Expand|Select|Wrap|Line Numbers
  1. $ grep -i toasty `find . | grep ".txt"`
  2.  
This will perform "grep -i toasty" on all files in the current directory and subdirectories that have the .txt extension.

In these examples, there is no iteration needed. Simple command line filters can do the necessary tasks.

I hope this helps.
Feb 21 '07 #4
trs204
12 New Member
As I have to modify the existing script, is there any way in korn shell script to check... if the filename contains or ends with.

Any references on string functions (like finding word in a string, finding character in a string) is helpful. thanx in advance.
Feb 21 '07 #5
Motoma
3,237 Recognized Expert Specialist
As I have to modify the existing script, is there any way in korn shell script to check... if the filename contains or ends with.
Maybe it would help for you to post the ksh script then.
Feb 21 '07 #6
cybervegan
36 New Member
The directory contains the following files
.html
.jpg
.css
.js

I want only the .jpg files checked based on the extension or some file type logic.

1) for x in ${directory}/*.*
2) if (x endswith .jpg) then /// finding a particular type of file.
// do something

I am looking for the exact condition (FOR ENDS WITH) how to keep on the second line.

thx in advance.
Maybe you can just change the filespec of the for statement to "/*.jpg"?

That would be fine if it's only jpg's you want to process. If it's complicated, I'd personally use Python (but that's just me).

hth
-cybervegan
Feb 22 '07 #7
ghostdog74
511 Recognized Expert Contributor
The directory contains the following files
.html
.jpg
.css
.js

I want only the .jpg files checked based on the extension or some file type logic.

1) for x in ${directory}/*.*
2) if (x endswith .jpg) then /// finding a particular type of file.
// do something

I am looking for the exact condition (FOR ENDS WITH) how to keep on the second line.

thx in advance.
#eg only, find *.jpg files and remove them
find /dir -type f -name "*.jpg" -exec rm {} \;
Feb 23 '07 #8
trs204
12 New Member
sorry motoma... its a very big script to post and has lot of env variables. I tried to post lot of references... will not be clear.

Thanks for the replies.

I had thought of using this
if [[ "`echo ${fname} | cut -d'.' -f2`" = "jpg" ]]; then
-- do some thing.
Feb 23 '07 #9
michaelb
534 Recognized Expert Contributor
find /dir -type f -name "*.jpg" -exec rm {} \;
Note that this command will do a recursive search...
I'm not sure the intention here was to delete all jpg files in the dir hierarchy.

Here's a working example in Bourne shell, I believe it can be easily converted to any other standard shell.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2.  
  3. for fname in `ls`
  4. do
  5.    if echo $fname | egrep -i '.+\.jpg$' >/dev/null 2>&1 ; then 
  6.        # perform the desired action on the file; rm, etc
  7.        echo got $fname
  8.    fi
  9. done
  10.  
  11. exit 0
  12.  
I use case-insensitive match, assuming pic.JPG is the same file-type as pic.jpg
If this is not a desired behavior remove the "-i" option
I make sure that hidden file ".jpg" will not be captured.
Feb 23 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
13812
by: Noam Dekers | last post by:
Hi all, I would like to find a word stored in a text file. Structure: I have one file named keyWords.txt that stores some key words I'm interested in finding. In addition I also have a file named textOrigin.txt in which I store the text to search in. I would like my prog to check if a certain word appears in the text and than to tell me what line it found it in (if it did...). My problem is that the script can't find the words I'm...
17
23188
by: Sean Ross | last post by:
Hi. Recently I made a small script to do some file transferring (among other things). I wanted to monitor the progress of the file transfer, so I needed to know the size of the files I was transferring. Finding out how to get this information took some time (reading the manuals - googling did not prove worthwhile). Anyway, I did eventually figure out how to do it (there are a few ways, including os.path.getsize(filename)). My...
11
4517
by: spideyman99 | last post by:
How can I find the true EOF for a file that contains 0xFF. When I use fgetc and it encounters 0xFF it thinks it's the end of file but it really isn't.
3
2132
by: Jonny | last post by:
Hi all, I need to find out the hex value of a file. Is this possible? I need to be able to quickly access and confirm its extension against its file type (which is as far as i know defined by its hex value). Thanks, /Jonny
1
3226
by: hazz | last post by:
"Value cannot be null.\r\nParameter name: type" is the exception thrown after the CreateInstance method below. Type t = Type.GetType(GetConfigValue("PasswordProvider")); IPasswordProvider ppdr= (IPasswordProvider)Activator.CreateInstance(t); GetConfigValue DOES return the correct value from the config file - "namespace.DBPassword, namespace" It appears that Type.GetType is NOT finding the assembly whose name is the
15
1644
by: pemo | last post by:
Are there standard macros that a (c99) compliant compiler should support? I note that in the c99 std that __FILE__ __LINE__ __func__ are mentioned, but others (that seem to be built in to every compiler I currently use) like __DATE__ __TIME___ don't appear. Presumably, __TIME__ etc are just simply 'popular' rather than required?
4
6363
by: spivee | last post by:
I'm having an odd type of issue. I want to be able to pass an element name in my javascript event and find the location of the element, be it a div, span, img whatever, specifically the top and left attributes. I have defined my element like so... ### .css file... #mydiv { position:absolute;
0
1910
by: NSF12345 | last post by:
Iv developed a small program that looks for a file over our network, and copy it to the location of another computer. Im using the "If FileExists("\\oldpc\main share\Folder\file.txt") Then" way of finding if the file exists, but i want to make it so that it tries to look for the computer, not the file. At the moment this is how i am finding and copying the file: If FileExists("\\oldpc\main share\Folder\file.txt") Then FileCopy "\\oldpc\main...
2
1156
by: Bardo | last post by:
Hi all, I am a newbie to using the XML Schema Object Model (SOM) and would like a few pointers on how to perform a particular task. I am essentially trying to search for elements/attributes via the SOM as they appear in a visual XML editor (as in the tree view). I need to be able to: 1. Start by finding the "root node" of the schema. I.e. the complex type that contains references to all other types. 2. Move to each nested complex type...
5
2171
by: ron.longo | last post by:
Is there any way that I can find the path of the main .py file of my application? For example, I have an application with some resources which are in a subdirectory: myPythonApp.py /resources image1
0
10794
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10896
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7999
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5830
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4650
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.