Unix How-To: Tricks for Working with Filenames

By Sandra Henry-Stocker  Add a new comment

In this week's column, I answer two readers' questions about working with Unix filenames. One asks how to easily remove all files in a directory except those that match a particular pattern. The other asks how to force the names of files to be all lowercase. The answers may be easier than you think.

[ See also: Unix Commands to Try When You're Bored ]

A reader recently asked me "How do I remove all files in a directory except those that end in a particular suffix, such as .c?". Not hard given a little Unix know how, but there are several ways to exempt files from your Unix commands.

The first trick that comes to mind is using the find command with its special not (!) operator. For example, the command "find . ! -name *.c" would find all the files you want to remove. You could use this in a command like this provided you want to do the same with subdirectories or don't have any:

$ find . -type f ! -name *.c -exec rm {} \;

I threw in the "-type f" clause to ensure that the command would not try to remove directories and generate errors.

If you don't want the file removing operation to work on subdirectories, you can stop it using the -maxdepth option. With a maxdepth of 1, the find command will only work in the current directory.

$ find . -maxdepth 1 ! -name "*.c" -print -exec rm {} \;

A lot of people prefer piping the output of a find command to xargs to using the -exec rm option, but the rm command will complain if it isn't provided with files to remove. Depending on how and when you'll be using the command, you might not want to risk the errors.

Another option is simply to move all the files you want to preserve to a temporary directory, clean out the current directory and then move the preserved files back into place. This solution will remove subdirectories as well if you use "rm -r" or will leave them intact, depending on the options you use to clean out the unwanted files.

$ mkdir ~/tmp$$
$ mv *.c ~/tmp$$
$ rm -r *
$ mv ~/tmp$$/* .
$ rmdir ~/tmp$$

Another reader asked how she could conveniently rename files with uppercase characters in their names to be all lowercase. While tricks with sed and tr are possible, using the typeset command to force a variable to be lowercase makes quick work of this problem. I sent her this script:

#!/bin/ksh
# rename files to be all lowercase

# NOTE: typeset defines $l as lowercase
typeset -l l

for u in [A-Z]*
do
   l=$u
   mv $u $l
done

I used $u (for uppercase) and $l (for lowercase) to make the script a little easier to read. The use of the typescript command avoids any need to map A-Z to a-z in some form of regular expression.

ITworld LIVE

Operating SystemsWhite Papers & Webcasts

White Paper

A Comparison of PowerVM and VMware vSphere (4.1 & 5.0) Virtualization Performance

This technical white paper presents benchmark results showing greater VM consolidation ratios than demonstrated in previous benchmarks and demonstrating the extent of the performance lead that PowerVM virtualization technologies deliver over x86-based add-on virtualization products.

White Paper

Consolidating Lotus Domino x86 Workloads on IBM Power Systems

Read the white paper to learn how moving up to Lotus Domino 8.5 and consolidating with IBM Power Servers can help you boost performance results and ROI.

White Paper

Task, workflow & issue management for teams. Try free!

Need a flexible system for managing team tasks, issue tracking, and automating and managing workflow processes? Comindware® Tracker helps you do it all.

Webcast On Demand

Best Practices in Monitoring VMware

The benefits of virtualization are unassailable: increased agility, scale, and cost savings to name a few. However, so too are the monitoring challenges posed by these environments-including complexities, lack of visibility and control, and inefficiency.

Sponsor: Nimsoft

White Paper

How Nimsoft Service Desk Speeds Deployment and Time to Value

For years, many support teams have been hamstrung by their traditional service desk platforms, which require complex, time-consuming coding for virtually every aspect of customization. This complexity makes it costly and difficult for support organizations to adapt-and places an increasingly substantial burden on the agility and efficiency of the business as a whole.

See more White Papers | Webcasts

Ask a question

Ask a Question