substring filenames

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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
 
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.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Members online

Top