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.

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

  • This would require a separate command for each .example1, .example2 etc. folder though? – K. Grey Aug 1 '17 at 11:17
  • OK, I've amended the answer to use awk instead. – Raman Sailopal Aug 1 '17 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. – K. Grey Aug 1 '17 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.