move all files in /a to its subdirectory /a/b?

move all files in /a to its subdirectory /a/b?

Post by Michael Wa » Fri, 13 Nov 1998 04:00:00



I want to move all files in /a to its subdirectory /a/b.
This sounds easy, but to make it work for all conditions:
/a does not contain any files, /a contains too many files, ...
and error free may not be easy.

The best solution that I can come up with is:

touch /a/dummy
find /a/* -prune -type f -print -exec mv {} /a/b \;
rm /a/b/dummy

But this is not elegant. Better solutions wanted. Thank you.

--
unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog

 
 
 

move all files in /a to its subdirectory /a/b?

Post by Andreas Schwa » Sat, 14 Nov 1998 04:00:00


|> I want to move all files in /a to its subdirectory /a/b.
|> This sounds easy, but to make it work for all conditions:
|> /a does not contain any files, /a contains too many files, ...
|> and error free may not be easy.
|>
|> The best solution that I can come up with is:
|>
|> touch /a/dummy
|> find /a/* -prune -type f -print -exec mv {} /a/b \;
|> rm /a/b/dummy
|>
|> But this is not elegant. Better solutions wanted. Thank you.

If you have GNU find you can use -mindepth/-maxdepth:

find /a -mindepth 1 -maxdepth 1 -type f -exec mv {} /a/b \;

--
Andreas Schwab                                      "And now for something



 
 
 

move all files in /a to its subdirectory /a/b?

Post by Randalf J. Mc Murp » Sat, 14 Nov 1998 04:00:00




>|> I want to move all files in /a to its subdirectory /a/b.
>|> This sounds easy, but to make it work for all conditions:
>|> /a does not contain any files, /a contains too many files, ...
>|> and error free may not be easy.
>|>
>|> The best solution that I can come up with is:
>|>
>|> touch /a/dummy
>|> find /a/* -prune -type f -print -exec mv {} /a/b \;
>|> rm /a/b/dummy
>|>
>|> But this is not elegant. Better solutions wanted. Thank you.

>If you have GNU find you can use -mindepth/-maxdepth:

>find /a -mindepth 1 -maxdepth 1 -type f -exec mv {} /a/b \;

#!/bin/ksh
cd a
mv $(\ls -1p | fgrep -v /) b

--
Randalf J. Mc Murphy

 
 
 

move all files in /a to its subdirectory /a/b?

Post by Randalf J. Mc Murp » Sat, 14 Nov 1998 04:00:00






>>|> I want to move all files in /a to its subdirectory /a/b.
>>|> This sounds easy, but to make it work for all conditions:
>>|> /a does not contain any files, /a contains too many files, ...
>>|> and error free may not be easy.

Okay, my previous post didn't take into account "no files"
and "too many files".  Guess I was moving too fast.  This
will work in all cases:

#!/bin/ksh

cd a

\ls -1p | fgrep -v / > /tmp/temp.$$

while read file
do
   mv $file b
done < /tmp/temp.$$

rm /tmp/temp.$$

# End    

--
Randalf J. Mc Murphy

 
 
 

move all files in /a to its subdirectory /a/b?

Post by Gary E. Ans » Sat, 14 Nov 1998 04:00:00




>I want to move all files in /a to its subdirectory /a/b.
>This sounds easy, but to make it work for all conditions:
>/a does not contain any files, /a contains too many files, ...
>and error free may not be easy.

>The best solution that I can come up with is:

>touch /a/dummy
>find /a/* -prune -type f -print -exec mv {} /a/b \;
>rm /a/b/dummy

>But this is not elegant. Better solutions wanted. Thank you.

Move the directory itself, not all the files:

mkdir /newa
mv /a /newa/b
mv /newa /a

(or, mv /a /olda; mkdir /a; mv /olda /a/b)

This won't work if /a is the mount point for a disk, though, or if you
don't have write permission in the / directory.

Note that this will leave you without a /a directory for a very short time;
if programs are frequently creating new files in this directory this may
be a problem.

Two problems with your original:  it won't get files like .cshrc, and it
attempts to rename the directory /a/b into itself (which I think is detected
and prevented by all current systems, but will probably result in an error
message).

Gary Ansok

 
 
 

move all files in /a to its subdirectory /a/b?

Post by Peter Samuels » Sun, 15 Nov 1998 04:00:00



Quote:> I want to move all files in /a to its subdirectory /a/b.  This sounds
> easy, but to make it work for all conditions: /a does not contain any
> files, /a contains too many files, ...  and error free may not be
> easy.

  $ mv /a/* /a/b/

This will complain about moving `/a/b' into `/a/b/' but otherwise
works.  It does not cover (a) too many files or (b) /a/b being a mount
point.

"Too many files" calls for find/xargs:

  $ find /a/* -prune | grep -v '^/a/b' | xargs mv /a/b/

Mountpoints calls for tar:

  $ find /a/* -prune | grep -v '^/a/b' | xargs tar cf - | tar -C /a/b -xf -

That last is sort of iffy.  I'm not sure what happens if xargs needs to
spawn multiple tar's, whether their output all ends up in the right
pipe.  And I'm too lazy to test it.

Quote:> But this is not elegant. Better solutions wanted. Thank you.

Not sure I consider my solutions too elegant either. ...

--
Peter Samuelson
<sampo.creighton.edu!psamuels>

 
 
 

move all files in /a to its subdirectory /a/b?

Post by Michael Wa » Sun, 15 Nov 1998 04:00:00



Quote:

>#!/bin/ksh

>cd a

>\ls -1p | fgrep -v / > /tmp/temp.$$

>while read file
>do
>   mv $file b
>done < /tmp/temp.$$

>rm /tmp/temp.$$

># End    

again the "cd a" part is scary. I do not see that the temp file
is needed. It will be cleaned up when the program is killed.

cd a && {
\ls -1pa | fgrep -v / | while read file
                        do
                          mv $file b
                        done

Quote:}

--
unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog

 
 
 

move all files in /a to its subdirectory /a/b?

Post by Michael Wa » Sun, 15 Nov 1998 04:00:00



Quote:>#!/bin/ksh
>cd a
>mv $(\ls -1p | fgrep -v /) b

This has the following problems:
1. suppose "cd a" fails, disaster occurs.
2. \ls -1p -> \ls -1pa to cover the .files.
3. when $(...) is too long, it will excceed sh argument size and fails.
4. when $(...) does not produce anything, "mv b" causes a syntax error.
--
unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog

 
 
 

move all files in /a to its subdirectory /a/b?

Post by Michael Wa » Sun, 15 Nov 1998 04:00:00




>>touch /a/dummy
>>find /a/* -prune -type f -print -exec mv {} /a/b \;
>>rm /a/b/dummy

>Two problems with your original:  it won't get files like .cshrc, and it
>attempts to rename the directory /a/b into itself (which I think is detected
>and prevented by all current systems, but will probably result in an error
>message).

There is no such problem that it will
"rename the directory /a/b into itself".
-prune and -type f takes care of this.

There is a problem with dot files, but I can modify it to:
touch /a/dummy /a/.dummy
find /a/* /a/.* -prune -type f -print -exec mv {} /a/b \;
rm /a/b/dummy /a/.dummy

The only problem that I worry is that * will be interpreted by shell,
and so it may exceed the sh argument limit.
--
unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog

 
 
 

move all files in /a to its subdirectory /a/b?

Post by KFL » Tue, 17 Nov 1998 04:00:00



*snip*

Quote:> touch /a/dummy /a/.dummy
> find /a/* /a/.* -prune -type f -print -exec mv {} /a/b \;
> rm /a/b/dummy /a/.dummy

> The only problem that I worry is that * will be interpreted by shell,
> and so it may exceed the sh argument limit.

Perhaps you could try piping the output of your find command into a "xargs
-n10" ( or another suitably small number) to enusre that you don't exceed
the sh argument limit ...whatever that might be.

-KFL.

 
 
 

1. Moving *ALL* files from a subdirectory to a parent directory...

Question: Is there a way to move ALL files from a subdirectory to a parent
directory from the UNIX prompt? The command: mv *.* ..  will move all
regular files but NOT ones that start with a . (hidden files). This is a
problem that DOS users don't face. If you know of such a way, please drop
a line. Thanks.

      .      .     .        .          .
    .      .          .         __             .---------------------------.
 _________________    ____.----'--`----.____   | >>>>>>> Abu Wawda <<<<<<< |
 \________________) . `---.____________.---'   |---------------------------|


         ___||___.---/      \_     .       .   |---------------------------|
  .     /_____           ... |(---    .        |         Live Long         |
     .     .  `-----._______.-  .          .   |        and Prosper        |
  .            .                    .          `---------------------------'

2. Where to get all keyboard keysyms names connected to visual character?

3. moving subdirectories

4. Zero dtime errors on /

5. moving subdirectories with tar

6. How to log onto Windows 2000 from Unix/Linux

7. list of files in directory and all subdirectories > files.csv

8. How to install linux on a Win95/FAT32 desktop computer ?

9. Netscape plug-ins, helper files, Acrobat etc.

10. Compare file modification date using tcsh built-ins

11. changing names of all files and subdirectories to lower case, which tools to use

12. FTP subdirectory's files

13. Programming simple file copy, move, and other file mngmnt routines