LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 04-09-2010, 04:20 AM   #1
bayaraa_u
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Rep: Reputation: 0
How to changing same file names by their directory names?


hello,

I have a problem with file names changes. I have a hundred of file with same names and different subdirectories.
I want to change that file names by their subdirectery names.
Currently is:
file name------------------------subdirectory name
1_km_16_days_EVI_s2_01200_01200.img --> MOD13A2.A2000049.h23v03.005.dir
1_km_16_days_EVI_s2_01200_01200.img --> MOD13A2.A2000065.h23v03.005.dir
1_km_16_days_EVI_s2_01200_01200.img --> MOD13A2.A2000081.h23v03.005.dir
...
I want to
MOD13A2.A2000049.h23v03.005.img
MOD13A2.A2000065.h23v03.005.img
MOD13A2.A2000081.h23v03.005.img

Pls some1 help to me
 
Old 04-09-2010, 06:11 AM   #2
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
You might like to consider using a for loop:


for dirname in ls -d *; do mv $dirname/filename destination/$dirname; done
 
Old 04-09-2010, 06:14 AM   #3
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 4&5, Fedora 10, CentOS 5.4, IPCop
Posts: 569

Rep: Reputation: 55
This is a rough attempt using some assumptions.
Code:
#!/bin/bash
set -x

cd /tmp
mkdir MOD13A2.A2000049.h23v03.005.dir
mkdir MOD13A2.A2000065.h23v03.005.dir
mkdir MOD13A2.A2000081.h23v03.005.dir
touch MOD13A2.A2000049.h23v03.005.dir/1_km_16_days_EVI_s2_01200_01200.img
touch MOD13A2.A2000065.h23v03.005.dir/1_km_16_days_EVI_s2_01200_01200.img
touch MOD13A2.A2000081.h23v03.005.dir/1_km_16_days_EVI_s2_01200_01200.img

for dirname in *.dir
do
  dirname_prefix=$(echo $dirname | cut -f1-4 -d.)
  cd /tmp/$dirname
  imagename=$(ls -1 *.img|head -1)
  mv $imagename ../${dirname_prefix}.img
done
You won't need most of this script - it creates test data at the start of the script so it has something to work with.

It uses a for loop, as suggested by bakdong, and also "cut" to remove the .dir suffix (which I assumed were really there and not just something to tell us they were directory names). It then looks for image files in that dir, and grabs the first one (as you indicate, there should only be one).

Error handling is absent

Last edited by blacky_5251; 04-09-2010 at 06:19 AM.
 
Old 04-09-2010, 08:26 AM   #4
bayaraa_u
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Solved

thanks for yours valuable suggestions blacky_5251, bakdong. Your are made to my life more easy .
^-^

Last edited by bayaraa_u; 04-09-2010 at 08:28 AM.
 
  


Reply

Tags
filename


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
changing file names scofiled83 Programming 8 05-23-2009 05:31 AM
Need script for changing DIRECTORY names only to upper case PaxRomana Programming 15 05-08-2007 05:04 AM
Weird, file names changing case after restore... s2cuts Linux - General 2 03-03-2007 01:39 PM
Getting file names in a directory using C on linux. rajsun Programming 4 06-10-2005 12:47 AM
Changing global file names. Sapient Linux - General 6 03-31-2005 05:10 AM


All times are GMT -5. The time now is 07:49 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration