How to Delete Read Only Files on Linux
I demand to delete all files in a directory, but exclude some of them. For example, in a directory with the files a b c ... z
, I need to delete all except for u
and p
. Is there an piece of cake way to do this?
asked January 8, 2013 at 12:17
AshotAshot
1,449 three gold badges xiv silver badges 25 bronze badges
2
What I do in those cases is to type
rm *
So I press Ctrl+Ten,* to expand *
into all visible file names.
Then I tin just remove the two files I similar to keep from the list and finally execute the command line.
answered Jan 8, 2013 at 12:18
Oliver SalzburgOliver Salzburg
83.2k 57 gilt badges 255 silver badges 299 bronze badges
nine
To rm
all only u,p
in bash just blazon:
rm !(u|p)
This requires the following choice to exist set:
shopt -s extglob
Meet more: glob - Greg's Wiki
slhck
210k 62 gilt badges 564 silver badges 561 bronze badges
answered Jan viii, 2013 at 12:34
sparkiesparkie
ii,200 1 gilt badge 10 silver badges xi bronze badges
3
You can employ find
observe . ! -name u ! -proper noun p -maxdepth 1 -type f -delete
-
!
negates the next expression -
-name
specifies a filename -
-maxdepth 1
will make find process the specified directory only (notice
by default traverses directories) -
-type f
will process only files (and not for example directories) -
-delete
will delete the files
You can so tune the conditions looking at the man folio of find
Update
- Keep in listen that the order of the elements of the expressions is significant (encounter the documentation)
-
Examination your command start past using
-print
instead of-delete
find . ! -proper noun u ! -name p -maxdepth 1 -type f -print
answered Jan eight, 2013 at 12:35
MatteoMatteo
7,237 ii gold badges 39 silver badges 54 bronze badges
6
Simple:
mv
the files you desire in a upper directory, rm
the directory and so mv
them back.
answered Jan eight, 2013 at 12:22
user176581user176581
617 4 silver badges 6 bronze badges
5
Somewhat similar to this answer but no special options are needed, as far every bit I know the post-obit is "ancient" functionality supported past whatsoever (vaguely) /bin/sh resembling shell (e.one thousand. bash, zsh, ksh, etc)
rm [^upwards]
answered Jan eight, 2013 at fourteen:22
hlovdalhlovdal
2,950 4 gilded badges 30 silverish badges 38 bronze badges
iv
Doing information technology without find:
ls | grep -v '(u|p)' | xargs rm
(Edit: "u" and "five", every bit in other places here, are being used as generic versions of entire regexes. Obviously you lot'll want to be careful to anchor your regexes to avoid matching also many things.)
You lot're definitely going to want a script if you lot're going to be doing much of this, as others have suggested.
answered January 9, 2013 at three:54
tquidtquid
177 3 bronze badges
half dozen
In zsh:
setopt extended_glob # probably in your .zshrc
then
rm ^(u|p)
or
rm *~(u|p)
The second volition work even if yous have ^
in $histchars
for history commutation, and of course you can put an arbitrary glob before the ~
.
answered January 9, 2013 at 22:34
pooliepoolie
183 seven bronze badges
GLOBIGNORE takes a colon-separated list
GLOBIGNORE=u:p rm *
answered Jan 8, 2013 at 16:32
W_WhalleyW_Whalley
3,302 1 gold bluecoat 16 silverish badges 16 statuary badges
i
Dorsum in the floppy era I had a dos executable called "Except" that would motion things out of the current directory temporarially and execute a command, so you could say:
except *.txt del *.*
to delete everything but your text files.
This would be a pretty trivial thing to implement as a beat out script and if this is the kind of thing y'all are likely to do more than than twice it seems like it would be a good idea.
answered Jan eight, 2013 at 17:02
Neb One thousandBill 1000
287 one silver bluecoat 7 bronze badges
ane
find . -maxdepth 1 ! -name "u" ! -name "p" -type f -exec rm -rf {} \;
This will delete all files except u and p in unix
James Mertz
25.8k 41 gold badges 106 silver badges 163 bronze badges
answered Jan 8, 2013 at 17:58
For those preferring to specify capricious complex exclude patterns (spanning all affected filenames) in a full blown regexp emacs, posix-awk or posix-extended style (encounter find man page) I would recommend this one. It excludes u
and p
in current dir in this example. This may be handy for scripts.
find -regextype posix-awk ! -regex './(u|p)' -print0 | xargs -0 rm -rf
answered Jan 8, 2013 at 18:16
sparkiesparkie
two,200 i gold badge 10 silver badges 11 statuary badges
5
Yet another:
for FILE in ./*; do if [[ $FILE != ./u* ]] || [[ $FILE != ./p* ]];then rm $FILE; fi; done;
It'south kind of lengthy and I don't know if you could hands make it into an part that could hands accommodate and arbitrary number of arguments, merely information technology works well.
And it'south pure bash goodness.
answered Sep 24, 2014 at 3:05
dylnmcdylnmc
231 2 silvery badges 5 bronze badges
I e'er use:
rm [a-o,q-t,v-z]*
This will allow you to ascertain how granular you want to make it. And so if you want to delete a through o and Z files you tin can use:
rm [a-o,z]*
answered Jan nine, 2013 at 20:54
J BaronJ Baron
880 7 silver badges seven bronze badges
Yet another version using xargs
:
ls -i | grep -v do_not_delete | xargs -I files rm "files"
Note that xargs -I
is needed to handle filenames including spaces correctly.
answered April 1, 2013 at 12:44
Here's another variant. You can type:
rm -i *
or:
rm --interactive *
So rm
will ask you to confirm deleting of each file.
answered Oct 22, 2014 at 12:37
Use:
find . -blazon f ! -proper name 'u' ! -name 'p' ! -name '*.ext' -delete find . -type d ! -name 'u' ! -name 'p' ! -name '*.ext' -delete
in lodge to delete all files including directories, except u, p and .ext files.
answered Sep 11, 2016 at 2:33
A simple manner that is hard to mess up: permit'south say you desire to delete everything except *.pdf:
mkdir tmp mv *.pdf tmp rm * mv tmp/* . rm -r tmp
answered Oct 22, 2013 at 7:06
Not the answer y'all're looking for? Browse other questions tagged linux fustigate rm or ask your own question.
Source: https://superuser.com/questions/529854/how-to-delete-all-files-in-a-directory-except-some
0 Response to "How to Delete Read Only Files on Linux"
Post a Comment