I have main DIR X and inside this I have many sub directories like A,B,C,D. I have main DIR Y and inside this i have many sub directories with same name as X main directory sub directories like A,B,C,D.
Now I need to MOVE only files from X main dir sub directories to Y main directory sub directory.
Eg:
Inside main directory X ,sub directory A has 100 files
and B has 1 file
and C has 5 files
and D has 50 files...
my cursor should be at main DIR X , from there I need to MOVE all the sub directory files to same named sub directories(A,B,C,D) which is there inside Y main directory.
how can I do this in SHELL SCRIPTING(KSH) or unix??
Note: Fundamentally revised to come up with a simpler solution. One-liners (more complex) at the bottom.
A POSIX-compliant solution:
The above will therefore work with
ksh
as well, and also other POSIX-compatible shells such asbash
andzsh
.There are two potential problems with this solution:
.
) are ignored.*
that does not match anything is left as-is, resulting in non-existent paths being passed tomv
.More robust Bash version
Bash - unlike
ksh
, unfortunately - has options that address both issues:shopt -s dotglob
causes hidden items to be included when globbing is performed.shopt -s nullglob
causes patterns that don't match anything to expand to the empty string.shopt -s nullglob
by itself was not enough - we still had to collect globbing matches in an array first, so we could determine if anything matched.2>/dev/null
to letmv
fail silently if there are no matches, this is not advisable, because it could mask true error conditions.If you're interested in one-liners, here are
find
-based commands; they are, however, quite complex.You can use script as below:
Replace X, Y with your directory
Here is a shell script which should work. I have not had time to check it though. Please check and confirm.