How to move the files from sub folder and sub-sub folders into one other folder?
0
0
Entering edit mode
2.9 years ago

Dear All

I have list of thousand folder containing files in sub folder and sub-sub folder. I want to transfer all the files only into another folder. Is there any way to do it command line.

bash • 3.0k views
ADD COMMENT
1
Entering edit mode

I agree with Vincent, this question is more of a linux admin question, and since I'm not a linux admin I don't have the best answer. I can't think of a simple one-liner, but a sample shell script could do it:

#!/bin/bash

# find all files recursively in your source directory
FILES=$(find SomeDir -name \* -type f)

# loop through them and get just the file name
# then mv the file from the original path to target location
for f in $FILES
do
  base=$(basename $f)
  mv $f target_dir/$base
done

I'm not sure what you mean by "a list of thousand folder"...if all the folders are in one directory, the solution above would work. If they're in various directories in an actual list, you could modify the script above by assigning your list to a variable and looping over it to recursively find files before getting their basename so you can move them. This solution assumes that none of the files will clash (they must all have unique names, otherwise you'll be writing files over each other).

ADD REPLY
0
Entering edit mode

Why the loop though? find -exec should do the job. Something like this should suffice:

find /path/to/dir -name "*" -type f -exec mv {} /path/to/out \;
ADD REPLY
1
Entering edit mode

Ha ha :) that would be the "I don't know the best answer" part of my answer. I didn't know about the exec option for find, and if the OP really does have a list of directories (in a file), he could convert that to a shell script. I've certainly dealt with messy unwieldy detritus from an analysis that had no easy answer.

ADD REPLY
0
Entering edit mode

Hi Mathavan - a post like this is best addressed to forum like stack overflow. However, for this post there is no need to create a new post. You can read the man page for mv, or you can google the question. I found this post: https://unix.stackexchange.com/questions/67503/move-all-files-with-a-certain-extension-from-multiple-subdirectories-into-one-di

ADD REPLY
0
Entering edit mode

They'd downvote the question and close it over at Stack Overflow. It's just not sufficiently programming related. The forum you linked to--the Unix StackExchange--is where this question should be asked IMHO.

ADD REPLY
1
Entering edit mode

That is why I wrote "there is no need to create a new post. You can read the man page for mv, or you can google the question. I found this post:" etc...

ADD REPLY
1
Entering edit mode

Apologies Vincent Laufer , I had overlooked that part. Thank you for pointing that out. I did not mean to be snarky or something.

ADD REPLY

Login before adding your answer.

Traffic: 2398 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6