Operating System - Linux
1748076 Members
5480 Online
108758 Solutions
New Discussion юеВ

Re: Finding file extension

 
hsrirama
New Member

Finding file extension

Hi,

We get a file with different filenames like abc.123.ddmmyy.dat.gz or ddmmyy.hhmiss.dat or something.tar... etc.

I need to copy these file based on their extension. Can you help me get the extension of the filenames? I tried using cut , awk. but they didn't help me much. Please advise.

8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: Finding file extension

Hi:

One way:

# echo abc.123.ddmmyy.dat.gz | perl -nle 'print $2 if /(.+)\.(.+)/'

...returns 'gz'

Regards!

...JRF...
Dave La Mar
Honored Contributor

Re: Finding file extension

Here's a start -
ll /your_directory/ | awk '{print $9}' | grep -e gz -e dat -e zip -e tar > Some_file

From there you will have the file names that match your criteria and it is elemetary to read this file for the file names and perform your copies.

Best of luck.

Regards,

-dl
"I'm not dumb. I just have a command of thoroughly useless information."
James R. Ferguson
Acclaimed Contributor

Re: Finding file extension

Hi (again):

Actually, we can use the Posix shell parameter substitution to do it the fastest:

# F=abc.123.ddmmyy.data.gz
# echo ${F##*.}
gz

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Finding file extension

It would be helpful if you provided a few examples but something like this:

cp *.gz /xxx/yyy/gzdir/
cp *.dat /xxx/yyy/tardir/

Note: UNIX has no concept of a file extension; that belongs to another OS.

If you wish to strip off a suffix then something like this:

#!/usr/bin/sh

typeset BASE=""
typeset FNAME=""
typeset SUFFIX=".gz"

ls | while read FNAME
do
BASE=$(basename "${FNAME}" "${SUFFIX}")
if [[ "${BASE}" != "${FNAME}" ]]
then
echo "Basename: ${BASE}"
# do your copy here
fi
done

------------------------------------
You only see the base part of the filename with the suffix stripped off. Files not matching the suffix are ignored. Man basename for details.
If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: Finding file extension

Dave wrote...

ll /your_directory/ | awk '{print $9}' | grep -e gz -e dat -e zip -e tar > Some_file

Please tell me that you were just teasing and promiss you will never ever run software like that in production!

what about files like:
stars.do_not_process
zippo_lighters_flyer

You need to anchor those matches to the end of the string!

Also, why pipe to grep when awk is perfectly happy to do regexpr's?

Along the lines of...:

ll | awk '/\.gz$|\.zip$|\.tar$/ {print $9}'

Grins,
Hein.
Hein van den Heuvel
Honored Contributor

Re: Finding file extension

hsrirama,

Combining the above you probably want to build upon something like:
You probably want to deal with case when there is no suffix ($BASE==$FNAME)

fwiw, I _love_ file extentions, as a hint such as used in OpenVMS.
In Windows they are followed overzealously imho.
If I want to call a text file .com no 'security' scan should barf and if I have an executable file called .txt then a security scan should still find it.

#!/usr/bin/sh

typeset FNAME=""
typeset BASE=""

ls | while read FNAME
do
BASE=${FNAME##*.}
case $BASE in
gz) print Processing gunzip file $FNAME;;
tar) print Processing backup file $FNAME;;
*) print Not processing $BASE file;;
esac
done
Dennis Handly
Acclaimed Contributor

Re: Finding file extension

>Hein: BASE=${FNAME##*.}

A slight nitpick. The variable name should be SUFFIX. Otherwise if you look to reuse the code you may think it is the basename.
Dave La Mar
Honored Contributor

Re: Finding file extension

Hein -
Good point, well noted, and hands thoroughly slapped.
I'll crawl back in my hole now......

-dl
"I'm not dumb. I just have a command of thoroughly useless information."