1

I have various folders each containing files as such:

/.example1/example1.txt /.example1/example2.txt /.example2/example1.txt /.example2/example2.txt etc. 

And want to find a way to move each of the files into a subfolder of its parent folder, eg:

/.example1/folder/example1.txt /.example1/folder/example2.txt /.example2/folder/example1.txt /.example2/folder/example2.txt etc. 

Any ideas on a quick and simple way to do this for large amounts of files? I've already tried playing around with find but without much luck.

2 Answers 2

1

You can do it using GNU versions of find and mv like this:

find . -mindepth 2 -maxdepth 2 -type f -execdir mv -vt ./folder/. {} + 

Where we constrain find to look for regular files 2 levels deep and then using the -execdir option to move the selected file(s) into the subdirectory folder which is co-adjacent to the selected file(s).

0
find /.example1 -name example*.txt" -type f | awk -F\/ '{ for(i=1;i<=NF-1;i++) { path=path$i"/" } } END { print "mv "$0" "path"folder/"$NF }' | sh 

Do a find on all the example files in the directory and then parse the returned output using awk, forming a move command that is executed with sh

3
  • This would require a separate command for each .example1, .example2 etc. folder though? Commented Aug 1, 2017 at 11:17
  • OK, I've amended the answer to use awk instead. Commented Aug 1, 2017 at 11:41
  • It still only uses the /.example1 folder to start in (also missing a " from the -name argument). After replacing it with a . instead, it just ends with an interactive shell prompt instead, so I don't think the arguments are being passed correctly from awk. Commented Aug 1, 2017 at 12:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.