1833323 Members
2879 Online
110051 Solutions
New Discussion

Remove files & subdirs

 
SOLVED
Go to solution
Janko Meskovski
Occasional Contributor

Remove files & subdirs

#1) I need to remove all files and subdirs in /dir, symbolic links should not be followed and /dir should not be deleted. Will 'rm -rf /dir/*' do the job? Also if there are subdirs that exceed the maximum arguments what would be a better way to do this?

#2) Will 'cp -rp /olddir/* /newdir' copy all files and subdirs from /olddir to /newdir(symbolic links too)? Is there a better way?
5 REPLIES 5
Robert-Jan Goossens
Honored Contributor

Re: Remove files & subdirs

Hi,

-1-

check your file to be removed with

# cd /dir
# find . -xdev -exec ls -l {} \;

Remove files

# fine . -xdev -exec rm -f {} \;

-2-

# cd /dir
# fine . | cpio -pcmudv /newdir

Hope it helps,

Robert-Jan.
Stefan Farrelly
Honored Contributor

Re: Remove files & subdirs

1) yes, cd to /dir then rm -rf ./* will do the job - it wont follow symbolic links by default and as you are in /dir it wont delete that either. You only use find if you want to exclude something or add extra rules - eg. if you want to keep all symbolc links then use; cd /dir; find . -xdev ! -type l -exec rm -rf {} \;

2) yes, cp -rp will copy all files and subdirs and symbolic links aok.
Im from Palmerston North, New Zealand, but somehow ended up in London...
RolandH
Honored Contributor
Solution

Re: Remove files & subdirs

Hi Janko,

1) Your method is right and will work fine.
rm does not follow links.
But if you mean a second mount point it will follow this mount point and removes everything there. Then you need the method from Robert-Jan
# find . -xdev -exec rm -f {} \;

xdev prevents to descend in directories from other filesystems.


2)
a)
# cd fromdir ; tar cf - . | ( cd todir ; tar xf - )

This method will also hold your users/groups permissions if you do it as root but be carefully if you are not root. Then you must also use the -p switch. Read manpage from tar.

b)
# find . -print|cpio -pvumod | (cd todir;cpio -pvumid)
Keeps the original user/group and access rights on your files.

c) for remote moving
# find . -print|cpio -pvumod | remsh dest_host "cd todir; cpio -pvumid"


HTH
Roland
Sometimes you lose and sometimes the others win
Tim Adamson_1
Honored Contributor

Re: Remove files & subdirs

Hi,

Answer for 1)

cd /dir
pwd <<<< ensure you are in /dir

To get around the max arguments use xargs:

find . | xargs -n 1 rm -r


Answer for 2)

Yes. You could also specify:

cp -rp /olddir /newdir


Cheers!
Yesterday is history, tomorrow is a mystery, today is a gift. That's why it's called the present.
Janko Meskovski
Occasional Contributor

Re: Remove files & subdirs

Thx for your answers.

Tim - I originally did 'cp -r /olddir /newdir' and it copied /olddir into /newdir (it created /newdir/olddir and copied the contents). Thats why I think

#cp -rp /olddir/* /newdir

is required (later I also realised that I need to preserve user/group permissions :).