Remove all files except the subdirectory(without pattern) in a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove all files except the subdirectory(without pattern) in a directory
# 1  
Old 08-21-2013
Remove all files except the subdirectory(without pattern) in a directory

I used rm * and it deleted the files in the directory but gives and error message for unsuccessful subdirectory deletion.

"rm: cannot remove 'DirectoryName': Is a directory"

I dont want to explicitly get the above error.

What are the modifications I have to do in the rm command?
# 2  
Old 08-21-2013
Code:
rm -rf *

Be very very careful on what you are deleting. This command deletes everything under the current dir.
This User Gave Thanks to rajamadhavan For This Post:
# 3  
Old 08-21-2013
I've had problems with the suggested rm -r because it will follow symbolic links out to other filesystems. Imagine what fun we had when we ran that from /opt/BOB/new only to find that there was a link to /usr/lib contained within it. Yes, it went off an cleared that too. Good thing we didn't have a link to /dev, /usr or /.

If you have few enough files, you could:-
Code:
for object in *
do
   if [ -f $object ]
   then
      rm $object
   fi
done

This tests that the item is a file. It will not traverse down directories though. Do you want to do that too, or does the above suffice?

If you have lots of files, then you may have to write a stronger loop. We can get to that if you need it.



Robin
Liverpool/Blackburn
UK
These 2 Users Gave Thanks to rbatte1 For This Post:
# 4  
Old 08-21-2013
Quote:
Originally Posted by rbatte1
I've had problems with the suggested rm -r because it will follow symbolic links out to other filesystems. Imagine what fun we had when we ran that from /opt/BOB/new only to find that there was a link to /usr/lib contained within it. Yes, it went off an cleared that too. Good thing we didn't have a link to /dev, /usr or /.
Hopefully you cannot replicate that with anything relatively current. If so, you should report it.

It's been nearly ten years since POSIX explicitly addressed symlinks during directory traversal. Released in 2004, the Issue 6 rm manual requires:
Quote:
The rm utility shall not traverse directories by following symbolic links into other parts of the hierarchy, but shall remove the links themselves.
Rationale for the inclusion of that clarification follows later on:
Quote:
The rm utility removes symbolic links themselves, not the files they refer to, as a consequence of the dependence on the unlink() functionality, per the DESCRIPTION. When removing hierarchies with -r or -R, the prohibition on following symbolic links has to be made explicit.
Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compressing & removing files in a directory & subdirectory

Hi, I want a simple line of code that will compress files within a directory specified (parameter) and its subdirectories and also i want to remove files which are exactly 365 days old from the sysdate after this compression. Please help. Thanks, JD (8 Replies)
Discussion started by: Jesshelle David
8 Replies

2. Shell Programming and Scripting

Globbling files in the direct subdirectory of the current directory

I want to list files that end with .c in the direct subdirectory of the current directory. I have tried the following command: find ./ -mindepth 2 -maxdepth 2 -name "*.c" Is that right? Or is there any easier way to handle that problem? Another problem is that I want to grep in a file to find... (5 Replies)
Discussion started by: Ray Sun
5 Replies

3. Shell Programming and Scripting

Changing ownership of a directory, subdirectory and files as same as in another server

accidentally i have changed ownership of a directory,subdirectory and files wil below command. I want to the change ownership back as same as in same directory on another server. How can i do it? chown -R user:group /u01 is there any simple script? it is really an urgent need.. (2 Replies)
Discussion started by: johnveslin
2 Replies

4. Solaris

Display the number of files in a directory and recursively in each subdirectory

Display the number of files in a directory and recursively in each subdirectory To look something like below, for example /var 35 /var/tmp 56 /var/adm 46Any ideas how can we do this? Got a sun cluser global mount point which takes ages to mount everytime, need to understand... (5 Replies)
Discussion started by: jakerock
5 Replies

5. Solaris

Display the number of files in a directory and recursively in each subdirectory

Display the number of files in a directory and recursively in each subdirectory To look something like below, for example /var 35 /var/tmp 56 /var/adm 46 Any ideas how can we do this? :wall: (1 Reply)
Discussion started by: jakerock
1 Replies

6. Shell Programming and Scripting

Move all files not in a directory into a subdirectory named for each given file

Hi Everyone! Looking for some help with a script that will take all files in any given root folder (which are not already in a folder) and put them into separate folders with the name of each given file. Any ideas? Thank you! (1 Reply)
Discussion started by: DanTheMan
1 Replies

7. UNIX for Dummies Questions & Answers

How to move all files in a directory and subdirectory?

I'm trying to organize my MB Pro by moving all my jpeg files to a single folder from the desktop. There are some on the desktop that are not in any folder. I was at the command line and typed mv *.jpg "Jpeg files" but it only moved the files that were on the desktop, not any of the ones that... (3 Replies)
Discussion started by: Straitsfan
3 Replies

8. Shell Programming and Scripting

Regex to remove subdirectory

I need to remove subdirectories that are empty and I've not done this before. First I am going through the files to remove old records. Then if the directory is empty I want to delete it. There are files in /direcotry/images/fs* - 0-9 and a-z The fs* directories need to stay, but any directories... (4 Replies)
Discussion started by: janel10
4 Replies

9. UNIX for Dummies Questions & Answers

copying a pattern of files in one directory into other with new pattern names...

Hi, I have to copy a set of files abc* in /path/ to /path1/ as abc*_bkp. The list of files appear as follows in /path/: abc1 xyszd abc2 re2345 abcx .. . abcxyz I have to copy them (abc* files only) into /path1/ as: abc1_bkp abc2_bkp abcx_bkp .. . (6 Replies)
Discussion started by: new_learner
6 Replies

10. Shell Programming and Scripting

Find files in directory and its subdirectory

I am writing a script which reads a file line by line and then assigns it to a variable like this 1090373422_4028715212.jpg. I have images with file name of this format in some other directory. In my script I want to assign variable with this file name and then find this filename in some other... (11 Replies)
Discussion started by: jyotib
11 Replies
Login or Register to Ask a Question