Operating System - HP-UX
1846766 Members
4582 Online
110256 Solutions
New Discussion

Re: Deleting multiple directories dives errors.

 
SOLVED
Go to solution
Gulam Mohiuddin
Regular Advisor

Deleting multiple directories dives errors.

I am running following commands to remove more than 500+ directories:

#ll |grep dr |xargs rm -r

Gives lots of errors:
rm: drwxr-x--- non-existent
rm: 3 non-existent
rm: root non-existent
rm: sys non-existent
rm: 96 non-existent
rm: Jan non-existent
rm: 16 non-existent
rm: 10:44 non-existent
rm: drwxr-x--- non-existent
rm: 3 non-existent
rm: root non-existent
rm: sys non-existent
.
.
.

Thanks.

Gulam
Everyday Learning.
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: Deleting multiple directories dives errors.

Hi Gulam:

You have passed to 'rm' every space-delimited token reported by 'll'. To see the result of what you asked, do:

# ll | grep dr | xargs

At the very least, you probably want to simply use 'ls' without any options.

# ls |grep dr | ...

Regards!

...JRF...
Gulam Mohiuddin
Regular Advisor

Re: Deleting multiple directories dives errors.

Thanks for your response.

But using ls doesn't returns anything
#ls |grep dr
#

Where "grep dr" I was using to filter only directory names.

Gulam.
Everyday Learning.
Bill Hassell
Honored Contributor

Re: Deleting multiple directories dives errors.

ll produces a long line of text items, not just the list of names in the current directory. So the non-existant filenames are actually portions of the ll listing such as permissions, owner/group, date, size, etc.

When you need to remove a lot of files, *ALWAYS* preview the task. Using your example, it is assumed that you are in the correct directory. Rather than use ll, just use which matches all the filenames in the current directory and grep for dr. To preview this, type:

ls | grep dr

*IF* the result is a list of the files you want to delete, you can then pipe the result to your xargs command:

ls | grep dr | xargs rm -f


Bill Hassell, sysadmin
Patrick Wallek
Honored Contributor

Re: Deleting multiple directories dives errors.

If, by using the 'grep dr' you are looking for the first 2 characters in the permissions list 'drwx------' or whatever, to find a directory, then you may want to rethink your command. By doing this you will also pass the '.' and '..' directories to your 'rm -r' command.

I would try something like this:

# ll | grep ^dr | awk '{print $9}' > dir_fie

Now I would edit the dir_file with vi and remove the '.' and '..' entries and also verify that the other dirs are what you want.

Then you could do something like:

for DIR in $(cat dir_file)
do
echo "Removing ${DIR}"
rm -r ${DIR}
done

James R. Ferguson
Acclaimed Contributor

Re: Deleting multiple directories dives errors.

Hi Gulam:

Instead of 'ls' you could use 'find' :

# find /your_path -type d | xargs ...

Regards!

...JRF...
Gulam Mohiuddin
Regular Advisor

Re: Deleting multiple directories dives errors.

Yes, I want to only remove all dir:

But running following still gives errors:

#find . -type d | xargs rm -r

rm: cannot remove .. or .
rm: ./PSAPPSRV.21455/cache non-existent
rm: ./PSAPPSRV.21518/cache non-existent
rm: ./PSAPPSRV.21979/cache non-existent
rm: ./PSAPPSRV.22244/cache non-existent
rm: ./PSAPPSRV.22935/cache non-existent
.
.
.

Gulam.
Everyday Learning.
James R. Ferguson
Acclaimed Contributor

Re: Deleting multiple directories dives errors.

Hi (again) Gulam:

Please be specific and be careful. What you probably did is what you wanted to do, except that the recursive remove removed upper-level directories before subordinate ones were handled. You could have done:

# find /my_path -xdev -type d -depth xargs rm -r

As already noted, too, things like this should be tested beforehand!

See the manpages for more information.

Regards!

...JRF...

Gulam Mohiuddin
Regular Advisor

Re: Deleting multiple directories dives errors.

Thanks everyone it worked now.

Gulam.
Everyday Learning.