LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 09-13-2008, 01:26 PM   #1
jsstevenson
LQ Newbie
 
Registered: Jul 2006
Location: Scotland
Distribution: Gentoo
Posts: 21

Rep: Reputation: 0
How to flatten contents of sub-directories down to one directory?


Hello...

I have, today, had to use dd_rescue and Photorec to retrieve data from a corrupted drive.

The files that I have retrieved are now in 111 folders (approx 60,000 files of various formats) within 1 master folder, e.g:

master_folder -->
sub-folder_1
sub-folder_1
sub-folder_1
etc...
sub-folder_111
What I would like to do is issue a command at the cli that will descend into each sub-folder and move (not copy) every file from the sub-folder into the master_folder until the contents of all 111 sub-folders have been relocated to the master_folder.

I know this would be easy enough with cp but I am dealing with a large amount of data and do not have the space to duplicate it using copy prior to deleting the original sub-folders and their contents hence why I want to use the mv command.

Once I have moved everything into a flat structure I would then intend to create a folder for each file type and then move all files of a given type into the corresponding folder, e.g:

master_folder -->
music
pictures
films
etc...
misc
Any help would be very appreciated.

Thanks for reading.
Jamie.
 
Old 09-13-2008, 04:51 PM   #2
klearview
Member
 
Registered: Aug 2006
Location: London
Distribution: Debian, Kubuntu
Posts: 572

Rep: Reputation: 75
Assuming you are in master_folder ('pwd' responds with 'master_folder') than you can:
Quote:
mv ./*/* .
That will move everything from subfolders to master_folder leaving subfolders empty.
 
Old 09-13-2008, 04:59 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
If there are too many files or directories, you may run out of memory, because bash will expand ./*/* to all of the files and directories in the ARGV array it presents to move. If that is the case you could use find instead.

find ./ -mindepth 2 -type f -exec mv '{}' . \;

You could also pipe the output of find to the xargs command to limit the number of arguments that "mv" needs to handle at once:
find ./ -mindepth 2 -type f -print0 | xargs -0 -L50 mv --target-directory .
 
Old 09-13-2008, 05:01 PM   #4
Foldarn
LQ Newbie
 
Registered: Sep 2008
Location: Northern UT
Posts: 13

Rep: Reputation: 0
if the number of subdirectories varies or reside on multiple levels of subdirectories, you can do this in 2 steps. First, put yourself in the top level directory. Create an empty directory entitled something like "all_files" beside your master files directory. Then this:

find . -name "*" -exec mv {} ../all_files \;

then this:

mv ../all_files/* .
 
  


Reply


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
list contents of directory without listing contents baddah Linux - Newbie 2 06-12-2006 04:02 AM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM
I want ls to not display contents of directories GameboyHippo Linux - Newbie 2 01-26-2004 03:47 AM
Contents of multiple directories in one playroll Linux - Newbie 2 11-17-2003 02:17 PM
mv the contents of one directory to the parent directory warkrime Linux - Newbie 4 07-14-2003 07:03 PM


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