open.itworld.com
  Search  
Security Home Page Security Webcasts Security White Papers Security Newsletters Security News Open Topics Careers ITworld Voices ITwhirled The Security site of ITworld.com

Unix Tip: Stripping file extensions with the basename command

ITworld 02/11/2008

Sandra Henry-Stocker, ITworld.com

I recently discovered a new trick for using basename to extract filenames from a full pathnames.

In the normal use of the command, basename strips the path from a file or directory reference, leaving just the name of the particular file or directory. The path "/etc/hosts" becomes "hosts" and "/var/spool/cron/crontabs/adm" becomes "adm". Simple enough. The partner of the basename command, dirname, does just the opposite. It strips off the file or directory name, leaving only the path.

boson> basename /etc/hosts
hosts
boson> dirname /etc/hosts
/etc
You can, however, also strip off a file's (or a directory's) extension by adding the particular extension to the end of your basename command as shown in these examples:

boson> basename /etc/syslog.conf .conf
syslog
boson> basename /etc/init.d .d
init
The extra argument is referred to as a "suffix" in the basename man page.

This beats the socks off having to tack on a pipe and a call to some additional tool, such as this example using sed:

boson>  basename /etc/syslog.conf | sed "s/.conf//"
syslog
Of course, the arguments that you pass to the basename and dirname commands don't actually have to be paths. They don't have to exist on your system or any system for that matter. They just have to look like paths. The basename command will strip off everything up to and including the final slash (/) in just about any string that you feed it and the dirname will strip off everything starting with and including the final slash. These examples prove the point:

boson> numerator=`dirname 1/2`
boson> echo $numerator
1
boson> denominator=`basename 1/2`
boson> echo $denominator
2
If the string, path or otherwise, that you want to reduce contains blanks, just enclose your string in quotes and basename will get to work on it. In this example, basename breaks the string on the slash as agreeably as it would an actual path:

boson> basename "I want to buy 1/4 cow"
4 cow
In fact, the basename suffix can be used to remove larger portions of file names if you are inclined to use it that way. There is no constrain that says you can only remove a string that starts with a dot.

You can remove multiple extensions:

boson> basename /etc/syslog.conf.old .conf.old
syslog
You can remove everything up to the first letter of a file's name:

boson> basename /etc/path_to_inst.old ath_to_inst.old
p
This trick can be useful if you want to extract some portion of a file's name and then reuse it. For example, if you want to find the differences between a number of files with .old extensions and the original files, you could do this:

boson> for file in `ls *.old`
> do
>     diff $file `basename $file .old`
> done
This little trick with basename will probably come in very handy from time to time.

On this topic

 

Sandra Henry-Stocker has been administering Unix systems for more than 18 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She currently works for TeleCommunication Systems, a wireless communications company, in Annapolis, Maryland, where no one else necessarily shares any of her opinions. She lives with her second family on a small farm on Maryland's Eastern Shore. Send comments and suggestions to bugfarm@gmail.com.




Sponsored Links

Sign up for a Microsoft Dynamics® CRM WEBCAST
Hear globally recognized leaders in customer strategy discuss the importance and evolution of CRM.
TRY NEW SUN SERVERS FREE for 60 Days!
Test The Latest Sun Servers In Your Environment BEFORE YOU BUY. Pay Nothing, Not Even Shipping.
Workflow Enabled Help Desk & IT Service Management
Automate service desk activities and integrate processes across IT. Learn more here.
Sign up for a FREE NETWORK RISK ASSESSMENT!
MORE THAN 70% OF NETWORKS ARE INFECTED by hidden Malware. Find out if your network is infected now!
Enterprise IP Goes Mobile
To maximize full productivity, companies must integrate their mobile applications with the IP network.
» Buy a link now

Advertisements
Sponsored links
Top 5 Reasons to Combine App Performance and Security
Locate Hidden Software on business PCs with this free tool
KODAK i1400 Series Scanners stand up to the challenge
Bring harmony to your mix of UNIX-Linux-Windows computing environments
 Home   Open source  Operating systems  Unix
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.