LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-08-2014, 02:20 PM   #1
TalkingRice
LQ Newbie
 
Registered: Jun 2014
Posts: 10

Rep: Reputation: Disabled
How to grep and rename files in their original directory


Hello,

I'm trying to rename multiple files contain same string but of different file types. I have 20 case folders. Each on contain two type of files (*.nii and *.mat). They all contain the same string in the file name (say "reo-"). I hope to rename all of them to "rest.nii" and "rest.mat" respectively and put them in their original case folder.

I don't know how to keep the extension when rename the file. So I'm trying to use the code bellow to rename one type of file then another.
Code:
#!/bin/bash

for files in `find . -type f -name '*.nii' | grep reo
`; do
echo files $file
mv $file rest.nii

done
But I don't know what to add into the script to put the renamed files in their original directory.
Could anyone help to edit it? If anyone know how can I rename both types of file at once, it will be perfect. Thanks!!!
 
Old 07-08-2014, 09:16 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

there are many different ways to do this. Here is one based on the approach you are trying.
Code:
#!/bin/bash
for f in $(find . -type f -name 'reo-.*') ; do
  p=${f%/*}
  x=${f##*.}
  echo mv $f ${p}/rest.${x}
done
Remove the "echo" if it looks like it will do what you want.

See http://spin.atomicobject.com/2014/02...-maniuplation/ for a nice overview of the string matching an manipulation done to set the variables p and x.

HTH,

Evo2.

Last edited by evo2; 07-08-2014 at 09:21 PM.
 
1 members found this post helpful.
Old 07-09-2014, 11:07 AM   #3
TalkingRice
LQ Newbie
 
Registered: Jun 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hi evo2,

I tried your code.
Code:
find . -type f -name 'reo-.*'
part seems not work. So I changed it to a complicated way
Code:
find . -type f -name '*.nii' -o -name '*.mat' | grep reo
. Now it works well. Thank you so much!
 
Old 07-09-2014, 08:01 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

I guess I misunderstood what the original file names actually are. Perhaps the following is what you want
Code:
find . -type f -name '*reo*'
Evo2.
 
1 members found this post helpful.
Old 07-10-2014, 09:52 AM   #5
TalkingRice
LQ Newbie
 
Registered: Jun 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
Now this works! Thanks, evo2!
 
  


Reply


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
Rename pdf files after their directory name and acoording to their total number IlIl7 Linux - Newbie 3 07-06-2012 05:09 PM
Bash script rename files based on directory name cupofnestor Linux - General 7 03-31-2010 09:20 AM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 02:16 PM
rename all files in a directory merlin23 Linux - Newbie 4 03-24-2005 12:02 PM
rename all files in my directory BabaKali Linux - Newbie 2 11-10-2004 05:27 PM


All times are GMT -5. The time now is 01:02 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration