Linux / UNIX Tech Support Forum
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 ...
|
|||||||
| Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Sponsored Links | ||
|
|
|
|||
|
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
Just remove the condition in find to check for files with srt extension Code:
MATCHING_FILES=$(find . -mindepth 2 -maxdepth 2 -name '*.avi') Add more -o -name '*.<extension>' condition to find refer to # find --help for details Hope that helps, ~R |
|
|||
|
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. |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |