Removing files exclusively


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing files exclusively
# 1  
Old 03-16-2005
Removing files exclusively

Do we have a command, which removes all the files in a file excluding the one's, I dont want to delete?


Like - I am having file - XXXCopyXXX

I want to remove all files not having 'Copy' in its name.

rm works on all files, there I couldnt find exclusion option.

Can some throw light to have a best suitable command?
# 2  
Old 03-16-2005
Smilie find . -name "*Copy*" -prune -o -print -exec rm {} \;
# 3  
Old 03-17-2005
Though this command working as expected, I am getting following message.

.
rm: 0653-600 Cannot remove '.' or '..'.

How I can get rid of this message? I am ready to use if thats a 2nd command, as a part of shell script.
# 4  
Old 03-17-2005
It will not remove the current directory so you can do the following;
find . -name '*Copy*' -prune -o -exec rm -r {} \; 2>/dev/null

Example;
# ls -lia
total 40
129053 drwxr-xr-x 5 root system 512 Mar 17 06:58 .
63492 drwxr-xr-x 9 pthompso staff 1536 Mar 16 19:16 ..
129054 drwxr-xr-x 2 root system 512 Mar 17 06:53 Copy
129109 -rw-r--r-- 1 root system 0 Mar 17 06:55 Copyaaa
129106 -rw-r--r-- 1 root system 0 Mar 17 06:57 Copyxxx
129108 -rw-r--r-- 1 root system 0 Mar 17 06:54 aaaCopyaaa
133120 drwxr-xr-x 2 root system 512 Mar 17 06:58 cat
129111 drwxr-xr-x 2 root system 512 Mar 17 06:58 dog
129107 -rw-r--r-- 1 root system 0 Mar 17 06:57 xxxCopy
129110 -rw-r--r-- 1 root system 0 Mar 17 06:58 xxxCopyxxx
# find . -name '*Copy*' -prune -o -exec rm -r {} \; 2>/dev/null
# ls -lia
total 24
129053 drwxr-xr-x 3 root system 512 Mar 17 06:59 .
63492 drwxr-xr-x 9 pthompso staff 1536 Mar 16 19:16 ..
129054 drwxr-xr-x 2 root system 512 Mar 17 06:53 Copy
129109 -rw-r--r-- 1 root system 0 Mar 17 06:55 Copyaaa
129106 -rw-r--r-- 1 root system 0 Mar 17 06:57 Copyxxx
129108 -rw-r--r-- 1 root system 0 Mar 17 06:54 aaaCopyaaa
129107 -rw-r--r-- 1 root system 0 Mar 17 06:57 xxxCopy
129110 -rw-r--r-- 1 root system 0 Mar 17 06:58 xxxCopyxxx
#
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find and removing the old files and zipping the files using shell script

Hi, I am trying to removing the old files which were older than 10 days and same g zipping the files using the shell script. script was return as follows. find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f | xargs rm -f find /cer_skyliv??/log... (6 Replies)
Discussion started by: venkat918
6 Replies

2. Red Hat

Pvcreate can't open /dev/xvda2 exclusively mounted filesystem

I downloaded the RH Linux 6.7 from RHEL, I wanted to extend the storage for "/" from default 16GB to say 200GB. Here’s the steps I tried is : 1. Init 1 2. Using “fdisk /dev/xvda” , delete the swap /dev/xvda3 as well as /dev/xvda2 3. Re-create /dev/xvda2 with linux LVM using new end... (7 Replies)
Discussion started by: alnhk
7 Replies

3. UNIX for Advanced & Expert Users

Pvcreate can't open /dev/xvda2 exclusively mounted filesystem

I downloaded the oracle linux 6.7 from RHEL, I wanted to extend the storage for "/" from default 16GB to say 200GB. Here's the steps I tried is : 1. Init 1 2. Using “fdisk /dev/xvda” , delete the swap /dev/xvda3 as well as /dev/xvda2 3. Re-create /dev/xvda2 with linux LVM using new end blocks ... (1 Reply)
Discussion started by: alnhk
1 Replies

4. UNIX for Dummies Questions & Answers

Help in Removing the Old files

Hi Gurus, we are planning to clear the old log files based on the year and i need help on this and i searched in google and i came up with the scripts but i am stuck with this. (1) wroks fine How many files exist in based on the extension find -type f | sed -e 's/.*\.//' | sort | uniq... (1 Reply)
Discussion started by: SeenuGuddu
1 Replies

5. UNIX for Dummies Questions & Answers

Removing files

How do you delete/remove multiple files ? (5 Replies)
Discussion started by: nosuchluck
5 Replies

6. SuSE

can't open /dev/sda3 exclusively

Hi, please help!!! I'm installing an openSUSE 10.3 into a computer, after creating the following partitions: Primay Partition swap / LVM(volume manager) - /dev/sda3 /opt, /var, /tmp, /usr, /local when i tried to continue installing, after creating the partitions i am prompted with the... (2 Replies)
Discussion started by: etcpasswd
2 Replies

7. UNIX for Dummies Questions & Answers

Removing Files

Hi, This might seem like a really stupid question but i have created a shell script and at the end of the script i want to remove most of the file i create. So I have: rm file test sorted However this does not work for the sorted one. The only way i can get this to work is if i do: rm... (4 Replies)
Discussion started by: jazz8146
4 Replies

8. Shell Programming and Scripting

removing old files except configuration files and folders

Dear all, I want to remove files older than 2 months in the /home/member directory. But except the configuration files (like .bash_profile .config/ .openoffice/ .local/ .kde/ etc..) I have tried with the command find . -mtime +60 -wholename './.*' -prune -o -print -exec mv {} \; but it... (1 Reply)
Discussion started by: jamcalicut
1 Replies

9. UNIX for Advanced & Expert Users

Removing files

Hi, It seems someone has created files instead of actually running the commands, as below: -rw-r--r-- 1 oracle92 dba 0 Mar 2 11:19 PRIML_070302.ok -rw-r--r-- 1 oracle92 dba 557 Mar 2 11:20 PRIVH_070302.dat -rw-r--r-- 1 oracle92 dba 0 Mar 12 11:57... (2 Replies)
Discussion started by: LiquidChild
2 Replies

10. UNIX for Dummies Questions & Answers

Removing Files

I am trying to remove a file so that I can delete a directory. Unfortuantely the file looks like this --------.--- and the system is not allowing me to remove the file. System message is rm: Error: Illegal option -- - usage: rm file... I have tried rm * and rm *.* to no avail. Any... (5 Replies)
Discussion started by: Blondie
5 Replies
Login or Register to Ask a Question