Search the FAQ Archives

3 - A - B - C - D - E - F - G - H - I - J - K - L - M
N - O - P - Q - R - S - T - U - V - W - X - Y - Z
faqs.org - Internet FAQ Archives

Unix - Frequently Asked Questions (2/7) [Frequent posting]
Section - How do I rename "*.foo" to "*.bar", or change file names to lowercase?

( Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Single Page )
[ Usenet FAQs | Web FAQs | Documents | RFC Index | Forum archive ]


Top Document: Unix - Frequently Asked Questions (2/7) [Frequent posting]
Previous Document: How do I read characters from the terminal in a shell script?
Next Document: Why do I get [some strange error message] when I "rsh host command" ?
See reader questions & answers on this topic! - Help others by sharing your knowledge

2.6)  How do I rename "*.foo" to "*.bar", or change file names to lowercase?
        
      Why doesn't "mv *.foo *.bar" work?  Think about how the shell
      expands wildcards.   "*.foo" and "*.bar" are expanded before the
      mv command ever sees the arguments.  Depending on your shell,
      this can fail in a couple of ways.  CSH prints "No match."
      because it can't match "*.bar".  SH executes "mv a.foo b.foo
      c.foo *.bar", which will only succeed if you happen to have a
      single directory named "*.bar", which is very unlikely and almost
      certainly not what you had in mind.

      Depending on your shell, you can do it with a loop to "mv" each
      file individually.  If your system has "basename", you can use:

      C Shell:
        foreach f ( *.foo )
            set base=`basename $f .foo`
            mv $f $base.bar
        end

      Bourne Shell:
        for f in *.foo; do
            base=`basename $f .foo`
            mv $f $base.bar
        done

      Some shells have their own variable substitution features, so
      instead of using "basename", you can use simpler loops like:

      C Shell:

        foreach f ( *.foo )
            mv $f $f:r.bar
        end

      Korn Shell:

        for f in *.foo; do
            mv $f ${f%foo}bar
        done

      If you don't have "basename" or want to do something like
      renaming foo.* to bar.*, you can use something like "sed" to
      strip apart the original file name in other ways, but the general
      looping idea is the same.  You can also convert file names into
      "mv" commands with 'sed', and hand the commands off to "sh" for
      execution.  Try

        ls -d *.foo | sed -e 's/.*/mv & &/' -e 's/foo$/bar/' | sh

      A program by Vladimir Lanin called "mmv" that does this job
      nicely was posted to comp.sources.unix (Volume 21, issues 87 and
      88) in April 1990.  It lets you use

        mmv '*.foo' '=1.bar'

      Shell loops like the above can also be used to translate file
      names from upper to lower case or vice versa.  You could use
      something like this to rename uppercase files to lowercase:

        C Shell:
            foreach f ( * )
                mv $f `echo $f | tr '[A-Z]' '[a-z]'`
            end
        Bourne Shell:
            for f in *; do
                mv $f `echo $f | tr '[A-Z]' '[a-z]'`
            done
        Korn Shell:
            typeset -l l
            for f in *; do
                l="$f"
                mv $f $l
            done

      If you wanted to be really thorough and handle files with `funny'
      names (embedded blanks or whatever) you'd need to use

        Bourne Shell:

            for f in *; do
              g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
              mv "$f" "$g"
            done

      The `expr' command will always print the filename, even if it
      equals `-n' or if it contains a System V escape sequence like `\c'.

      Some versions of "tr" require the [ and ], some don't.  It
      happens to be harmless to include them in this particular
      example; versions of tr that don't want the [] will conveniently
      think they are supposed to translate '[' to '[' and ']' to ']'.

      If you have the "perl" language installed, you may find this
      rename script by Larry Wall very useful.  It can be used to
      accomplish a wide variety of filename changes.

        #!/usr/bin/perl
        #
        # rename script examples from lwall:
        #       rename 's/\.orig$//' *.orig
        #       rename 'y/A-Z/a-z/ unless /^Make/' *
        #       rename '$_ .= ".bad"' *.f
        #       rename 'print "$_: "; s/foo/bar/ if <stdin> =~ /^y/i' *

        $op = shift;
        for (@ARGV) {
            $was = $_;
            eval $op;
            die $@ if $@;
            rename($was,$_) unless $was eq $_;
        }

User Contributions:

Hello ... Im looking a lover..
I love oral sex! Write me - tinyurl.com/yz6aajf4
Try Your luck and win a FREE $500 or $1000 coupon! - tinyurl.com/yhxaqmng
Hi baby!! my name is Virginia...
I love oral sex! Write me - is.gd/be2piI
Try Your luck and win a Free Coca-Cola Pack! - tinyurl.com/y58phmar
WiFiBooster Pro provides you max speed with 300Mbps, no delay for playing game, online transfer files and video chat! Works with Any Router and Device (Smartphone, Computer, Smart tv, etc). Get 50% Off Today! Order Here - is.gd/OdxGOK
Fill out the form and win a Free $500 or $1000 voucher! - tinyurl.com/yf9odcpg
hey dear!!! my name is Sophia...
I want sex! Here are my photos - is.gd/kZlpA3
Hi !! my name Emily...
Do you want to see a beautiful female body? Here are my erotic photos - tinyurl.com/yzet8jvr
heey .. my name Rebecca!!!
I love oral sex! Write me - tinyurl.com/ydnrouzb
Hi !! my name Maria.
I love sex. Here are my erotic photos - is.gd/eP3qxP
Hi .. Im looking a man!!!
If you want to meet me, I'm here - is.gd/5kUUii
heey baby!!! my name Margaret..
I want sex! Write me - chilp.it/bf4d037
13
Rapinia
Hi...?! I'm my name is Name.FirstName and I have
A
Good opportunity for Target.Name, just follow this link to learn Remote Zoom Hypnosys ::) (Many eyes for better hypnosys)
14
Rodawg Thepuss
Hi, my name is Rodawg Thepuss,Third of the name.
I am here to present you a new and exciting opportunity to take advantage of others with Hypnosis through TeamsMEET.Hypnosis Hypnosis Hypnosis Hypnosis Hypnosis Hypnosis Hypnosis .

This is a record access board which can hold 30 seconds information about all the Hypnosis teams in Singapore. These teams have made numerous activities in the whole of Singapore, which has led to many mass hypnosiemicy readings.

This is the fastest way you can fast way of getting your classmates' goal on term project , S P MOAR!

so join Hypnosis Team in TeamMeets.net to become team's master key. On my life I swear by the many success stories i have shared above.

To be free of all problems, if you will join my Virtual Talk seminar. No one could ever stop you from achieving success. Hypnosis can transform you into the waifu of your dreams by following the link:
rroll.to/wxjWSC
15
Luc
Apr 12, 2022 @ 11:11 am
Hello Folks,

For old Linux distributions I could suggest this web site - linux-distros.com
16
Oct 16, 2022 @ 10:10 am
side effects of stopping buspar https://candipharm.com/search?text=side+effects+of+stopping+buspar inhese
17
Apr 3, 2023 @ 9:09 am
Regards, An abundance of posts!
funny college essay https://dissertationwritingtops.com newsletter writing service https://essaywritingservicebbc.com
18
Apr 4, 2023 @ 2:02 am
You actually explained that well.
master thesis writer https://essaywritinghelperonline.com custom thesis writing service https://essaywritingservicebbc.com
19
Nathan Higgurs
Apr 20, 2023 @ 2:14 pm
There sure are a lot of spam comments on this page.
20
Sep 29, 2023 @ 3:03 am
hallo dear... Im looking a man.
I want sex! Write me - is.gd/vHiBd4
21
Reader
Oct 1, 2023 @ 4:04 am
Thanks for the command name lore.
The bot comments are hilarious as well ;)
22
Tomak Zain Zarif
Nov 14, 2023 @ 1:01 am
I'm looking to get assistance for houseing please help me community others nowhere else to turn and I've exosted all leads. I'm diabetic,with narapathy in my feet and hands.I'm a strong black man 46 years of age just had birthday Nov.3/which makes me a humbled Scorpio until you piss me off#1im a born and raises decent of Los Angle Cali. If you want to call or email me hit me up #1 @ tomakzarif65@gmail.com. I defianately need a woman in my life. Hit me soon#1

Comment about this article, ask questions, or add new information about this topic:




Top Document: Unix - Frequently Asked Questions (2/7) [Frequent posting]
Previous Document: How do I read characters from the terminal in a shell script?
Next Document: Why do I get [some strange error message] when I "rsh host command" ?

Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Single Page

[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer:
tmatimar@isgtec.com (Ted Timar)





Last Update March 27 2014 @ 02:12 PM