LinuxQuestions.org
Visit Jeremy's Blog.
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 05-21-2019, 03:11 PM   #1
stozi
Member
 
Registered: Mar 2010
Distribution: Void Linux
Posts: 39

Rep: Reputation: 0
How to recursively merge directories or folders into one, retaining folder names in file names


Basically, I have a directory tree. I want to find and replace all "/" within that tree with "-".

can i use mv or find?

I want a directory tree to become one directory with lot of files with long names which include the names of the sub-directories which they used to be in.

for example these paths

/a/file.ex
/a/b/file.ex
/a/b/file2.ex
/a/c/file.ex
/a/c/d/file.ex

will become:

/a/file.ex
/a/b-file.ex
/a/b-file2.ex
/a/c-file.ex
/a/c-d-file.ex
 
Old 05-21-2019, 03:46 PM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,097

Rep: Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075
Quote:
Originally Posted by stozi View Post
Basically, I have a directory tree. I want to find and replace all "/" within that tree with "-".

can i use mv or find?

I want a directory tree to become one directory with lot of files with long names which include the names of the sub-directories which they used to be in.

for example these paths

/a/file.ex
/a/b/file.ex
/a/b/file2.ex
/a/c/file.ex
/a/c/d/file.ex

will become:

/a/file.ex
/a/b-file.ex
/a/b-file2.ex
/a/c-file.ex
/a/c-d-file.ex
Yes, you should be able to use either mv or find
I played with it a little...something like
  • ls -1 the files in /a
  • use sed to change the /'s to -'s sed 's./.-.g
  • mv the original file to the name output by sed
Code:
file.ex
file.ex
b/file.ex
b-file.ex
b/file2.ex
b-file2.ex
c/file.ex
c-file.ex
c/d/file.ex
c-d-file.ex
I suspect there's an awk solution as well.
 
1 members found this post helpful.
Old 05-21-2019, 05:25 PM   #3
stozi
Member
 
Registered: Mar 2010
Distribution: Void Linux
Posts: 39

Original Poster
Rep: Reputation: 0
Thanks, I'm hoping for a single command to deal with the whole directory tree if possible
 
Old 05-21-2019, 06:30 PM   #4
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,097

Rep: Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075
Quote:
Originally Posted by stozi View Post
Thanks, I'm hoping for a single command to deal with the whole directory tree if possible
There’s probably not a single command, but you can build a short script:

For each file in the ls, do the grep, do the mv

Give it a shot...use echo instead of mv until you get it right. Ask if you run into trouble.
 
1 members found this post helpful.
Old 05-21-2019, 07:38 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware 14.2 current / ArcoLinux / Void Linux
Posts: 8,669

Rep: Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802
Code:
#!/bin/bash

working_dir=/media/ntfs1/Music2
move2=~/goo

while read f
do
#replace directory dividers / with -
newName=${f////-}
#remove leading - on filename
newName=${newName:1}

echo "$newName"
mkdir -p "$move2"
#echos results 
#remove the echo and first set of quotes to
#put it into production. 
echo "mv -fv "$f" "$move2"/"$newName" "

done <<<"$(find "$working_dir" -type f)"
if you got more than one dir to move to a central dir
Code:
#!/bin/bash

d1= 
d2=
d3=
d4=
move2=

while read f
do

#code goes here

done <<<"$(find "$d1" "$d2" "$d3" "$d4" -type f)"

Last edited by BW-userx; 05-21-2019 at 07:44 PM.
 
Old 05-21-2019, 09:26 PM   #6
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 511

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
On the off chance that there is an existing file of the same name, use mv -i or mv -n, and do not use mv -f, so as to avoid overwriting.

Last edited by Beryllos; 05-21-2019 at 09:47 PM.
 
Old 05-22-2019, 01:16 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,096

Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
The overcomplicated
Code:
while read ...
do
  ...
done <<<"$(find ...)"
runs the find first and must store the output somewhere (in memory or in a tmp file).
It should be simplified by
Code:
while read ...
do
  ...
done < <(find ...)
where the two processes run simultaneously.
This while loop still runs in the foreground,
unlike the classic
Code:
find ... |
while read ...
do
  ...
done
where the pipe forces the loop to run in the background, and any variables in the loop are lost after the loop.
 
1 members found this post helpful.
  


Reply

Tags
directory, folder, rename


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
Compare two folders(recursively) for file names and file contents alaios Linux - Software 9 10-28-2017 04:20 AM
[SOLVED] How to remove all hidden directories and folders, and only hidden directories and folders rm_-rf_windows Linux - General 5 04-12-2016 07:28 AM
find -exec command to recursively delete files and folders in folders with X name Joan Murt Linux - Newbie 2 07-08-2009 04:35 PM
can't recursively copy directories/files with non-ascii names on freeNAS Rascale *BSD 2 03-30-2009 01:07 PM
Merge many files in to one big file. like 20 file merge in one big file Jmor Linux - Newbie 2 10-29-2008 09:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:28 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration