1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

substring filenames

Discussion in 'Linux and Unix' started by cgjoker, Sep 11, 2003.

Thread Status:
Not open for further replies.
  1. cgjoker

    cgjoker Thread Starter

    Joined:
    Aug 13, 2003
    Messages:
    205
    I would like to substring a bunch of files that would have various names but would always end with .dct.

    For example...

    file1.dct
    file2.dct
    dog1.dct
    cat1.dct

    I would like to rename them all to

    file1
    file2
    dog1
    cat1

    These filenames would always change though so I wouldn't be able to just do a mv.

    Here is some code that I would need to incorporate into the substring function.

    for DCT1 in `ls`;

    do
    cp $DCT1 $DCT1.dat
    done
     
  2. codejockey

    codejockey

    Joined:
    Feb 11, 2002
    Messages:
    1,405
    How about something like this:

    Code:
    #!/bin/bash
    for FILE in `ls`
    do
        NEWFILE=`echo $FILE | sed 's/\.dct//'`
        cp $FILE $NEWFILE
    done
    
    This is only one approach, and doesn't consider any error cases (what if the file is a directory, for example?) but the code should give you the basic idea.

    Hope this helps.
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/164061

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice