nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Moving files between folders

This is a discussion on Moving files between folders within the Shell scripting forums, part of the Development/Scripting category; Hello, I have a folder/file structure like this: Folder1/Folder11/file11.avi Folder1/Folder11/file11.srt Folder1/Folder12/file12.avi Folder1/Folder12/file12.srt . . .etc I would like to move ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 19-02-2009, 12:20 AM
Junior Member
User
 
Join Date: Feb 2009
OS: SUSE 11
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
taylormia is on a distinguished road
Default Moving files between folders

Hello,

I have a folder/file structure like this:

Folder1/Folder11/file11.avi
Folder1/Folder11/file11.srt
Folder1/Folder12/file12.avi
Folder1/Folder12/file12.srt
.
.
.etc

I would like to move all the .srt and .avi files into the root Folder1 and delete the subfolders Folder11, Folder12 etc. even if they have contents. The new folder/file structure should look like this:

Folder1/file11.avi
Folder1/file11.srt
Folder1/file12.avi
Folder1/file12.srt


Also, how would I modify this script if I wanted to move only one file (example: file11.avi) rather than two (example: file11.avi and file11.srt) from the subfolders into the root folder.

What if I want to move three files instead of one or two, from each subfolder into the root folder?

I would appreciate any help.

I am sunning SUSE Linux 11

Thanks

Last edited by taylormia; 19-02-2009 at 12:46 AM.
Reply With Quote
  #2 (permalink)  
Old 20-02-2009, 02:31 PM
Junior Member
User
 
Join Date: Feb 2009
OS: Debian
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
ravisola is on a distinguished road
Default

Try something like:
NOTE: You will be better off taking backups first.

Code:
#!/bin/bash

#run this script from Folder1

#find all the files with .srt or .avi extension only in the immediately next subfolder
MATCHING_FILES=$(find . -mindepth 2 -maxdepth 2 -name '*.avi' -o -name '*.srt')
for i in $MATCHING_FILES
do
     CURRENT_DIR=`dirname $i`
     #move the file up one level
     mv $i $CURRENT_DIR/..
done

#delete the folder only when all files with the
#required extension have been moved up one level
for i in $MATCHING_FILES
do
     CURRENT_DIR=`dirname $i`
     rm -rf $CURRENT_DIR
done
>>>Also, how would I modify this script if I wanted to move only one file (example: file11.avi) rather than two (example: file11.avi and file11.srt) from the subfolders into the root folder.

Just remove the condition in find to check for files with srt extension
Code:
MATCHING_FILES=$(find . -mindepth 2 -maxdepth 2 -name '*.avi')
>>>What if I want to move three files instead of one or two, from each subfolder into the root folder?

Add more -o -name '*.<extension>' condition to find
refer to # find --help for details

Hope that helps,
~R
Reply With Quote
  #3 (permalink)  
Old 21-02-2009, 03:55 AM
Junior Member
User
 
Join Date: Feb 2009
OS: SUSE 11
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
taylormia is on a distinguished road
Default

Thank you very much !!!!

It worked perfectly.....except for folders whose names have spaces in them i.e "Episode 9" vs. "Episode_9"

Anyway...I only had a couple of folders like that and did them manually....

Thanks again.....You saved me countless hours of work and I have a new appreciation for the power of shell scripts.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
On the risk of deleting folders or files in /tmp. nosnix Linux software 6 29-10-2008 01:22 PM
Moving a Linux system device John Linux software 5 15-05-2008 02:43 PM
How to archive files along with folders using TAR? Charlie82 Shell scripting 1 31-07-2007 05:26 AM
Need help on moving files and something else karabaja Shell scripting 12 25-08-2006 04:28 PM
moving files based on size kavi Shell scripting 2 11-11-2005 05:17 PM


All times are GMT +5.5. The time now is 11:47 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38