Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Linux and Unix
Tag Cloud
access acer asus bios bsod computer crash desktop dns driver drivers error ethernet excel freeze gaming graphics hard drive hardware hdmi internet laptop malware memory monitor motherboard network printer problem ram registry repair router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
rename file to substring last 7 characters

Reply  
Thread Tools
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
28-Feb-2005, 05:37 PM #1
rename file to substring last 7 characters
I know this should be basic unix but im having a heck of a time getting this to work for me for some reason.

I have a file called john1234567.dat

I want to rename it to 1234567

How do I do this?

here is what im trying ...

for FILE in `ls | grep john_*.dat`;
do
$newname="`expr "$FILE" : ':$s/\([john_])([])'`"
mv $FILE $newname
done
surfnschultz's Avatar
Member with 191 posts.
 
Join Date: Nov 2003
28-Feb-2005, 06:03 PM #2
not sure what you are going to use it for
cp john1234567.dat 1234567

cat john1234567.dat > 1234567

These are easy, but might not meet your needs. Still looking for more help? let me know.
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
01-Mar-2005, 10:35 AM #3
not quite
I guess I should have elaborated a bit more.

The filename will never be the same. Each filename will have 7 digits on the end that id want to rename the file to.

thanks.
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
01-Mar-2005, 11:01 AM #4
Will there always be an undscore between the name and number? Will the filename always start with John_? Makes it real easy if it does.
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
01-Mar-2005, 03:20 PM #5
no. the filename will always be different. i just need to get the last 7 characters of the filename, and rename that file to just those 7 characters.

that is kind of why i was trying to do a for loop in my attempt.
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
01-Mar-2005, 03:21 PM #6
in my example i know i have john_, but this was just to get it to work. really it should not even have the john_ in the substring.
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
01-Mar-2005, 07:37 PM #7
Hmmm, not sure. So you want the 7 characters before the file extension and the file extension dropped. Let me clarify one more thing. Will those 7 characters be numbers or random characters.
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
02-Mar-2005, 11:08 AM #8
I am assuming that the 7 digits will always be there and there will not be any characters in between them.

Code:
for FILE in `ls | grep .dat`;
do
newname=`echo $FILE | tr -d "[:alpha:]" | tr -d "."`
mv $FILE $newname
done
I am not sure if this will work for you or not. Basically what this script does is remove all alpha characters from the filename and the period. So hopefully those 7 digits are the only numerics in your filename.

hopefully that is the last time I edit this.

Last edited by LwdSquashman; 02-Mar-2005 at 02:05 PM..
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
01-Apr-2005, 05:08 PM #9
I know its a really late reply, but there is a slight change to my requirement.

I actually need to keep the first 8 characters of a filename and drop the rest.

so ET345678file.dat would become ET345678

the first 8 characters would change constantly.

thanks.
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
01-Apr-2005, 06:04 PM #10
here is my latest attempt
for FILE in `ls *`;
do
newname=`echo $FILE | head -c9`
mv $FILE $newname
done
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
03-Apr-2005, 11:46 AM #11
Quote:
Originally Posted by cgjoker
here is my latest attempt
for FILE in `ls *`;
do
newname=`echo $FILE | head -c9`
mv $FILE $newname
done
Read the manual page for head and you will see why that wont work. But you were close. If you have your script in this directory it will rename your script as well. So I would suggest you move it into a different directory and chang the ls command to the directory you want listed.

Code:
for FILENAME in `ls *`;
do
newname=`echo $FILENAME | cut -b-8`
mv $FILENAME $newname
done
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
04-Apr-2005, 12:33 PM #12
thanks... I refined it a bit.

for FILENAME in `ls *`;
do
newname=`echo $FILENAME | head -c8`
mv $FILENAME $newname
done

Im finding that your code is doing the same as mine. The problem im having is that both codes are only doing one file. The second or third file is deleted.

So if I have 12345678_file.dat, 34567890_file2.dat....

I get only one file afterwards

12345678
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
04-Apr-2005, 02:57 PM #13
Nevermind,... both are working fine.

Thanks for your help
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
04-Apr-2005, 03:13 PM #14
That is weird that the head command works for you. It is suppose to read the contents of a file and pull the first couple lines or bytes out of it.
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
04-Apr-2005, 03:18 PM #15
so... here is my dilemma now.

I have two files with the same first 8 characters so I want to add a number to each filename to make it unique.

Here is what I came up with.

cd /data
for FILENAME in `ls *`;
do
name=`date +%b +%d`
for x in 1 2 3 4 5 6 7 8 9
do
#newname=`echo $FILENAME | head -c8`
newname=`echo $FILENAME | cut -b-8`
mv $FILENAME $newname$name$x
done
done
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:53 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.