LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-17-2008, 09:16 PM   #1
ratcateme
Member
 
Registered: Feb 2008
Distribution: CentOS 5.4
Posts: 59

Rep: Reputation: 15
loop through directories and move files


I am look to script a bash script that will loop through a directory go into sub directory and move files that are say .fileb to a new directory
it has to work like this
Code:
start/dira/1.filea
start/dira/1.fileb
start/dira/2.filea
start/dira/2.fileb
start/dirb/1.filea
start/dirb/1.fileb
start/dirb/2.filea
start/dirb/2.fileb
//run script
start/dira/1.filea
start/dira/2.filea
start/dirb/1.filea
start/dirb/2.filea
end/dira/1.fileb
end/dira/2.fileb
end/dirb/1.fileb
end/dirb/2.fileb
i tried mv start/*/*.fileb end/*/
but that tried to put all the files in
end/dira/
how can i do this?

Scott.
 
Old 11-17-2008, 11:52 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
IIUIC:
Code:
for file in `find start -type f -print`
do
    echo $file
    fname=`basename $file`
    dir=`dirname $file`
    if [[ $fname =~ "fileb" ]]
    then
        # move it
        if [[ $dir =~ "dira" ]]
        then
            mv $file end/dira
        elif [[ $dir =~ "dirb" ]]
        then
            mv $file end/dirb
        else
            echo "unmatched dir $dir"
        fi
    fi
done
 
Old 11-18-2008, 01:01 AM   #3
ratcateme
Member
 
Registered: Feb 2008
Distribution: CentOS 5.4
Posts: 59

Original Poster
Rep: Reputation: 15
thanks for the reply
but there will be heaps of dirs
so
start/dira
start/folder
start/dirc
start/example
.....
and so i want to do it without having to enter each dir into the code.
and they will change often to.

Scott.
 
Old 11-18-2008, 01:21 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by ratcateme View Post
I am look to script a bash script that will loop through a directory go into sub directory
.....
and so i want to do it without having to enter each dir into the code.
These two statement are contradictory. What are exactly your requirements? The script suggested by chrism01 does not enter any directory (no cd statements).
 
Old 11-18-2008, 01:28 AM   #5
ratcateme
Member
 
Registered: Feb 2008
Distribution: CentOS 5.4
Posts: 59

Original Poster
Rep: Reputation: 15
i want it to loop through say start/ and get all the directory names then go through each directory it found and find all files with the .fileb extension and move them to end/(directory name from start/)/name.fileb

Scott.
 
Old 11-18-2008, 01:46 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If they (dirs) are only start+one level and you want the same end+one level dirs, you just slice the dirname using eg cut
Code:
subdir=`echo start/dirb|cut -d'/' -f2`
gives 'dirb', which you then append to end. Obviously you don't hardcode 'start/dirb' that's just an example.

Last edited by chrism01; 11-18-2008 at 05:59 PM.
 
Old 11-18-2008, 02:04 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A modified version of the Chris' script. This checks for the existence of the destination directory, otherwise create it. Hope this is what you are asking for:
Code:
#!/bin/bash
for file in `find start -type f -name '*.fileb' -print`
do
  # strip out the front of the path till start/
  path=${file#*start/}
  # strip out the file name
  dir=${path%/*}
  # create the destination dir if missing
  test -d end/$dir || mkdir -p end/$dir
  # move it
  echo mv $file end/$dir
done
 
Old 11-18-2008, 02:33 AM   #8
ratcateme
Member
 
Registered: Feb 2008
Distribution: CentOS 5.4
Posts: 59

Original Poster
Rep: Reputation: 15
Thanks that does exactly what i want.

Scott.
 
  


Reply



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
need to move several directories craftereric Linux - Newbie 3 06-02-2008 04:57 PM
Very simple shell script - move files to different directories beley Programming 7 11-02-2006 05:24 AM
loop move files up one directory level Melsync Programming 8 12-19-2005 10:51 PM
Move certain files into different directories facets Programming 6 05-28-2004 01:20 PM
Can I move Untarred directories? Nigel_Tufnel Linux - General 5 08-08-2002 08:45 AM

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

All times are GMT -5. The time now is 04:16 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