I have this folder structure:
├── foo1
│ ├── bar1.txt
│ └── bar2.txt
├── foo2
│ ├── bar3.txt
│ └── bar4 with a space.txt
└── foo3
└── qux1
├── bar5.txt
└── bar6.txt
that I would like to flatten into this, with an underscore between each folder level:
├── foo1_bar1.txt ├── foo1_bar2.txt ├── foo2_bar3.txt ├── foo2_bar4 with a space.txt ├── foo3_qux1.bar6.txt └── foo3_qux1_bar5.txt
I’ve looked around and I haven’t found any solution that work, mostly I think because my problem has two particularities: there might be more than one folder level inside the root one and also because some files might have spaces.
Any idea how to accomplish this in bash? Thanks!
Edit: Running gleen jackman proposed answer I get this:

There are two underscores for the first level folder. Any idea how to either avoid this or just rename it so that is just one underscore? Thanks.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
find */ -type f -exec bash -c 'file=${1#./}; echo mv "$file" "${file////_}"' _ '{}' ;
remove echo if you’re satisfied it’s working. Don’t worry that the echo’ed commands don’t show quotes, the script will handle files with spaces properly.
If you want to remove the now empty subdirectories:
find */ -depth -type d -exec echo rmdir '{}' ;
Method 2
Using perl’s rename :
find . -depth -type f -exec rename '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="176457">[email protected]</a>(?<!.)/@<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="643b2403">[email protected]</a>' -- {} ;
OUTPUT
$ find -type f ./foo2_bar4 whit a space.txt ./foo3_qux1_bar6.txt ./foo2_bar3.txt ./foo3_qux1_bar5.txt ./foo1_bar2.txt ./foo1_bar1.txt
NOTE
- I use a negative look behind
(?<!.)to don’t touch the first./ -
I keep the empty dirs, feel free to :
find . -depth -type d -exec rm {} ;
There are other tools with the same name which may or may not be able to do this, so be careful.
If you run the following command (GNU)
$ file "$(readlink -f "$(type -p rename)")"
and you have a result like
.../rename: Perl script, ASCII text executable
and not containing:
ELF
then this seems to be the right tool =)
If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :
$ sudo update-alternatives --set rename /path/to/rename
(replace /path/to/rename to the path of your perl's rename command.
If you don’t have this command, search your package manager to install it or do it manually
Last but not least, this tool was originally written by Larry Wall, the Perl’s dad.
Method 3
with pax…
pax -Xrwls '|/|_|g' */ "$PWD"
That will create a hardlink in the current directory to all files in its child directories with a _ substituted for /. You can then inspect the results and remove all child directories with…
rm -rf */
…once you have verified the results are to your liking.
Method 4
You can try with below command.
$ find -type f -exec bash -c 'mv $0 $(echo $0|sed "s///_/2")' {} ;
Above command will move the files and the directory will remain which can be removed later.
$ ls foo1_bar1.txt foo1_bar2.txt foo2_bar3.txt foo2_bar4.txt foo3_bar5.txt foo3_bar6.txt
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0