- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Finding file extension
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2007 09:19 AM
тАО03-29-2007 09:19 AM
Finding file extension
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.
- Tags:
- basename
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2007 09:31 AM
тАО03-29-2007 09:31 AM
Re: Finding file extension
One way:
# echo abc.123.ddmmyy.dat.gz | perl -nle 'print $2 if /(.+)\.(.+)/'
...returns 'gz'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2007 09:36 AM
тАО03-29-2007 09:36 AM
Re: Finding file extension
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2007 09:39 AM
тАО03-29-2007 09:39 AM
Re: Finding file extension
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2007 09:46 AM
тАО03-29-2007 09:46 AM
Re: Finding file extension
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2007 11:11 AM
тАО03-29-2007 11:11 AM
Re: Finding file extension
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.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2007 12:01 PM
тАО03-29-2007 12:01 PM
Re: Finding file extension
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2007 03:52 PM
тАО03-29-2007 03:52 PM
Re: Finding file extension
A slight nitpick. The variable name should be SUFFIX. Otherwise if you look to reuse the code you may think it is the basename.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2007 05:47 AM
тАО04-20-2007 05:47 AM
Re: Finding file extension
Good point, well noted, and hands thoroughly slapped.
I'll crawl back in my hole now......
-dl