Delete all files except files with the extension pdf in a directory

One way is to use the find command, for example:


find . -maxdepth 1 -type f \! \( -name "A" -o -name "B" \) -deleteThis command finds all files in the current directory (.) and deletes all regular files (-type f) not named "A" or "B". The -maxdepth parameter ensures that find doesn't descend into sub-directories and deletes stuff.

The -name parameters may contain wildcards, e.g -name "tmp*".

The back-slashes are needed because some of the characters in the find expression are also interpreted by the shell, so you'll have to excape them with the \.

Have fun, and experiment in a test-directory before you delete left and right

Edit: Oh, and the -o means OR - I only put this in the example in case simple wildcarding isn't enough for...

0 0

This is the first way I come up to, there is more elegants or efficients
for sure.
Move to top dir where dirs to be delete are:

cd /somedir


ls -1 | grep -v -E 'dir_to_exclude_1|dir_to_exclude_2|^a*' | xargs rm -R

ls -1 lists dirs and files in the current directory but one per line,
which is needed for grep. Then we redirect output to a pipe to grep
command. Option -v is for excluding those patterns that matches our
regexp (regular expression), and -E is for using extended regexp.

Our regexp 'dir_to_exclude_1|dir_to_exclude_2|^a*' matches all
filenames or dirnames who contains dir_to_exclude_1 or (that is the
meaning of |) dir_to_exclude_2 or those filenames that begins with a (^
means begin of line) and have zero or more characters after a (this is

0 0

Let us say you have 100+ files in a folder. You want to delete all of them except one or few specific files. How would you do it? You can copy the files you wanted to keep, and save them in a different location, and then delete the rest of the files or the entire folder. But wait, I know an easiest way to do this. You can delete everything in a folder except one or certain files in one go with a single line command. Want to know how? Read on.

Remove All Files In A Folder Except One Specific File

Let us picture the following example. We have a folder called ‘test’ that contains 10 text files.

ls test/

Sample output:

file10.txt file2.txt file4.txt file6.txt file8.txt file1.txt file3.txt file5.txt file7.txt file9.txt

Now, I want to delete everything in this folder except file10.txt.

There might be many commands to do this. But these are the five commands that I am aware of.

First, go to the test folder:

cd test/

And run the following...

0 0


In a Linux terminal, how to delete all files from a folder except one or two?

For example.

I have 100 image files in a directory and one .txt file.
I want to delete all files except that .txt file.


From within the directory, list the files, filter out all not containing ‘file-to-keep’, and remove all files left on the list.

ls | grep -v 'file-to-keep' | xargs rm

To avoid issues with spaces in filenames (remember to never use spaces in filenames), use find and -0 option.

find 'path' -maxdepth 1 -not -name 'file-to-keep' -print0 | xargs -0 rm

Or mixing both, use grep option -z to manage the -print0 names from find



In general, using an inverted pattern search with grep should do the job. As you didn’t define any pattern, I’d just give you a general code example:

ls -1 | grep -v 'name_of_file_to_keep.txt' | xargs rm -f

The ls -1 lists one file per line, so that grep...

0 0


Remove all files except files with certain extension

This removes all files that end with .a or .b $ ls *.a a.a b.a c.a $ ls *.b a.b b.b c.b $ rm *.a *.b How would I do the opposite and remove all files that end with *.* except the ones that end with *.a and *.b?
The linked answer has useful info, though the question is somewhat ambiguous and the answers use differing interpretations. The simplest approach in your case is probably (a streamlined version of (GLOBIGNORE='*.a:*.b'; rm *.*) Note the use of a subshell ((...)) to localize setting the GLOBIGNORE variable. The patterns assigned to GLOBIGNORE must be :-separated. The appeal of this approach is that you can use a single subshell without changing global state. By contrast, getting away with a single subshell with shopt -s extglob requires a bit of trickery: (shopt -s extglob; glob='*.!(a|b)'; echo $glob) Note the mandatory use of an intermediate...
0 0

‘m a new Linux system user. I need to cleanup in a download directory i.e. delete all files from ~/Downloads/ folders except the following types:

*.iso – All iso images files.
*.zip – All zip files.

How do I delete all file except some in bash shell on a Linux, OS X or Unix-like systems?

Bash shell supports rich file pattern matching such as follows:

* - Match any files.? - Matches any single character in filenames.[...] - Matches any one of the enclosed characters.

Method #1: Say hello to extended pattern matching operators

You need to use the extglob shell option using the shopt builtin command to use extended pattern matching operators such as:

?(pattern-list) - Matches zero or one occurrence of the given patterns.*(pattern-list) - Matches zero or more occurrences of the given patterns.+(pattern-list) - Matches one or more occurrences of the given patterns.@(pattern-list) - Matches one of the given patterns.!(pattern-list) -...
0 0
0 0