rm -rf

inux anti-election deleting files

 

The easiest way is

# Shopt -s extglob (open extglob mode)
# rm -fr !(file1)
 

If it is more than to be excluded, it can be:

# rm -rf !(file1|file2) 
Other documents in addition to a file to delete all of the linux command

Linuxrm delete other files outside the specified file method summary

First, delete files and folders under Linux common commands as follows: delete the file: rm file
delete folder: rm -rf dir
should be noted that, rmdir can only delete empty folders. Second, delete all the files in the development files (folder) outside of it?
1, 1, is too much trouble approach is:
file copy you want to keep to another folder, and then delete the directory, and then move want to keep coming back. mv keep ../ # reserved file (folder) the Keep
RM -rf * # delete the current folder of all the files mv ../keep ./ # move back to the original thing
2, Method 2, you need to clip in the current file were:
RM -rf (keep) # delete all the files other than keep files!
RM -rf (keep1 | keep2) # delete files beyond keep1 and keep2 all files!
3, method 3, the current folder in conjunction with grep and xargs to deal with filenames: ls | grep -v keep | xargs rm # delete all the files in a folder other than keep
Description: ls to get the name of the current all files and folders, grep -v keep, carry out regular grep match lookup keep, -v parameters determine the result of the matching result is outside, i.e. to the outside Keep all file names, and xargs for obtaining parameters from the standard input and passed back to the command, which is used herein rm, then delete the files previously selected by the rm.
Benefits: Use the grep regular expressions to match the file name, keep multiple files at once, so that more accurate processing.
4, method 4, instead of using the find command LS, improved method of processing 3 can be developed in the folder:
find ./test/ | grep -v keep | xargs rm # delete all the files in the current folder test outside of the keep file
description, use grep instead find -name select the name, because the name chosen is too much trouble find, for the regular expression type support is not sufficient to exclude the specified file name.
5, method of directly using the find command to delete other files:
find ./ -name '[K ^] [^ E] [^ E] [P ^] *' -exec RM -rf {} \; # keep outside deleted other documents. find ./ -name '[^ k] [^ e] [^ e] [^ p] *' | xargs rm -rf # other files deleted keep away. recommend!
Description: The high efficiency of the second line of code above some because -exec starts multiple processes when deleting multiple files to deal with, and xargs rm will start a process to deal with.
Comparing the effectiveness and efficiency of command borrow xargs find execute commands initiated details, please refer to: HTTP:? Q = //www.linuxsir.org/main/ the Node / 137
Linux file search command find, xargs detailed http: / /www.linuxsir.org/main/?q=node/137 article reproduced from: http: //blog.sina.com.cn/s/blog_70ffb5c501011rrk.html

rm Delete to remove all remaining files in the specified file (reverse delete rm)

@ zhou zhou: ~ / LinuxC / File / LS the Test $
1 23sdfwe 88888888 aabb AG ghdda MMM
2 3 aaaaaaaa abc ASDG llllllll wwwww
zhou zhou @: ~ / LinuxC / File / $ the Test

and then I want to remove all the outside in addition to containing the string aa the document, which is want to stay aabb, aaaaaaaa, these two files, the other all deleted
Here is my command:

zhou zhou @: ~ / LinuxC / file / RM `the Test $ LS | grep -v" AA "`
@ zhou zhou: ~ / LinuxC / File / LS the Test $
aaaaaaaa aabb
zhou zhou @: ~ / LinuxC / File / $ the Test
so, and succeeded.
Simply explain that command it: rm delete files specified after
`ls | grep -v" aa " ` Remember the outside anti quotes (backticks position on the left of the numeric keypad standard 1),
ls: See all the files in the current directory, use the grep command to filter what grep -v "aa" is to find out the string without the "aa" of.

Then along the overall look: lists the file name without the "aa" string file, and then delete them. OK.
In fact, that sounds simple, then I have done for a long time, because no how previous contact grep, so from the beginning I think the approach is to use regular table
expression, but in the process of doing suddenly found grep to make a good thing , therefore use.

The above command to delete with "aa" string file, that if I just want to leave it aa file? Very simple

zhou @ zhou: ~ / LinuxC / file / test $ rm `ls | grep -v" ^ aa $ "`
in front of aa plus ^, followed by the $ symbol represents the end mean, this is the exact match.
Well, so much. I hope in the future to use this useful command
Transfer from: http: //blog.sina.com.cn/s/blog_67e34ceb01014930.html

linux delete other files

http://zhidao.baidu.com/link?url=uvHfrb3kSnM_8p5ILhZyc39U0h3md-Ncrm3iaygeYTLU-zjthNBlqO674VulVGTnPiNcl2nj7wo5vn08N4481_

Neighborhoods linux centos I want to delete a directory that does not comply with the file name "* 20100330 *" This rule should be how to delete a file?

And a directory called myTest assumed for the next one of the current directory, using the following command:
cd ./myTest && rm `ls | grep -v '20100330'` && cd .. 

enter myTest directory, delete the file after the return to the current directory. 

grep -v parameter indicates the reverse selection. 

General use rm to delete files when there will be a confirmation prompt, if not confirmed, direct force delete, you can use the rm -f parameter.

Other one answer

find ./ -type f \! -name "*20100330*" -exec rm -rf {} \;

How to select the file and delete reverse

http://bbs.csdn.net/topics/390077765

Case: a folder I want to delete all files except abc files, command how to write (under linux)

find . -maxdepth 1 -type f -not -name 'abc' -exec rm '{}' ';'

ls | grep -v abc | xargs -i rm -rf {}

rm -f `ls | grep -v abc`
if a lot of cases file, do not use this method ......
use the second floor of the method ......

mv abc /tmp
rm *
mv /tmp/abc .

 

Reverse display file

 

File using the ls command only displays the current directory does not include..

Ls and grep filter way: ls -al | grep -v '\ .' (Ls --ignore = * -al can be achieved.).

In the Open extglob mode (default is open), ls can also be achieved, but also more flexible.

Shopt -u extglob # close
shopt -s extglob # Open

LS -al! (*. *)
LS -al -d! ( *. *)

Guess you like

Origin www.cnblogs.com/lelin/p/12034534.html