LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2014, 09:29 AM   #1
rohit_shinez
Member
 
Registered: Aug 2013
Posts: 40

Rep: Reputation: Disabled
Replace file name


Hi,

I am having a files in my directory like this:

Code:
2014 1049_file1.txt
2014 1050_file2.txt
2014 1110_file3.txt
2014 1145_file4.txt
2014 2049_file5.txt
I need to replace the above file names like this without changing the content of filename:

Code:
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
I tried something like this but didn't workout:

Code:
for i in *.txt
do
mv $i ${*_i}
done
 
Old 09-29-2014, 09:32 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
since you have stupid spaces in your original filenames maybe you need to put quotes around them so that the shell can interpret them correctly (what is the error you are getting -- file not found ?) ?

there is prolly a better way but this is my quick-and-dirty hax with awk[untested]:
Code:
for i in *.txt
do
mv $i `echo $i | awk -F _ '{print $2}'`
done

Last edited by schneidz; 09-29-2014 at 09:35 AM.
 
Old 09-29-2014, 10:05 AM   #3
rohit_shinez
Member
 
Registered: Aug 2013
Posts: 40

Original Poster
Rep: Reputation: Disabled
Hi,

the above didn't work its giving error and moreover my code gave error like
-bash: ${*_i}: bad substitution
 
Old 09-29-2014, 10:56 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
Code:
for i in *.txt; do
    mv -- "$i" "${i#*_}"
done
 
Old 09-29-2014, 03:57 PM   #5
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,103
Blog Entries: 6

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
My 2 cents

Another way to remove spaces in filenames in a folder
Code:
for i in *.txt; do mv "$i" ${i// /}; done
If those are all the same you also could remove the first 10 characters in the filenames.
Code:
for i in *.txt; do mv $i $(echo $i | sed 's .\{10\}  '); done
Or
Code:
for i in *.txt; do mv $i $(echo $i | cut -c 10-); done
 
Old 09-29-2014, 04:57 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by teckk View Post
If those are all the same you also could remove the first 10 characters in the filenames.
Code:
for i in *.txt; do mv $i $(echo $i | sed 's .\{10\}  '); done
Or
Code:
for i in *.txt; do mv $i $(echo $i | cut -c 10-); done
or
Code:
for i in *.txt; do mv "$i" "${i:10}"; done
 
Old 09-30-2014, 09:05 AM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by rohit_shinez View Post
Hi,

the above didn't work its giving error and moreover my code gave error like
-bash: ${*_i}: bad substitution
the only way to fix the error is to know what the error is ?
 
  


Reply

Tags
unix


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to do search & replace on a text file--need to extract URLs from a sitemap file Mountain Linux - General 4 08-07-2015 10:52 AM
[SOLVED] replace matched pattern from 1st file into 2nd file using awk sopier Programming 6 12-13-2011 09:58 AM
[SOLVED] Need help with script to replace certain text in file with part of the file's name kmkocot Linux - Newbie 2 08-23-2009 04:06 PM
Perl find file and then replace string in file moos3 Programming 5 07-29-2009 07:10 AM
replace line in CSV file and rename file connected to that name wademac Linux - Newbie 3 07-15-2009 01:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration