Re: Finding file extension (263 Views)
Reply
Occasional Visitor
hsrirama
Posts: 1
Registered: ‎03-29-2007
Message 1 of 9 (263 Views)

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.

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 9 (263 Views)

Re: Finding file extension

Hi:

One way:

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

...returns 'gz'

Regards!

...JRF...
Honored Contributor
Dave La Mar
Posts: 829
Registered: ‎03-27-2001
Message 3 of 9 (263 Views)

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."
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 9 (263 Views)

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...
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 5 of 9 (263 Views)

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.
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 6 of 9 (263 Views)

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.
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 7 of 9 (263 Views)

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
Acclaimed Contributor
Dennis Handly
Posts: 25,188
Registered: ‎03-06-2006
Message 8 of 9 (263 Views)

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.
Honored Contributor
Dave La Mar
Posts: 829
Registered: ‎03-27-2001
Message 9 of 9 (263 Views)

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."
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.