Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


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

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


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

Pls some1 help to me
Old 04-09-2010, 06:11 AM   #2
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
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
This is a rough attempt using some assumptions.
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
  dirname_prefix=$(echo $dirname | cut -f1-4 -d.)
  cd /tmp/$dirname
  imagename=$(ls -1 *.img|head -1)
  mv $imagename ../${dirname_prefix}.img
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
LQ Newbie
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0

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.



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 > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:15 AM.

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