How can I recursively delete all files of a specific extension in the current directory?



Delete all the files which have same name with ${filename}. find . -name "${filename}" -type f -delete Delete all files with .bak file extension in the current dir find . -name "*.bak" -type f -delete


-name pattern True if the last component of the pathname being examined matches pattern. Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'') may be used as part of pattern. These characters may be matched explicitly by escaping them with a backslash (``\'').

or you can use regular expression for file matching.

-regex pattern True if the whole path of the file matches pattern using regular expression. To match a file named ``./foo/xyzzy'', you can use the regular expression ``.*/[xyz]*'' or ``.*/foo/.*'', but not ``xyzzy'' or ``/foo/''. ...
0 0

I am a photographer and generate “raw” and JPG images. At a certain point in my workflow, I need to delete all the JPG images before proceeding. That’s easy enough; in Windows Explorer, one can sort by type, then delete all the JPGs, or one could open up a command prompt in the directory and “erase *.jpg”. But there’s a complication; in some modes of the camera, no raw file is generated. In those cases, I just have one .jpg that I want to keep. Here is an example:


I want to process this directory and end up with one file of each filename, as follows:


The pseudocode would go something like this:

Sort files by filename fname

IF fname.jpg and fname.cr2 both exist THEN delete fname.jpg

How can I easily do this?...

0 0

How can I recursively delete all files of a specific extension in the current directory? You don't even need to use rm in this case if you are afraid. Use find: find . - name "*. But use it with precaution.

Run first: find . Also, make sure that - delete is the last argument in your command. If you put it before the - name *. See man find and man rm for more info and see also this related question on SE.

How to delete files in UNIX using shell script Shell Programming and Scripting. Is there a simple way, in a pretty standard UNIX environment with bash, to run a command to delete all but the most recent X files from a directory? To give a bit more of a concrete example, imagine.

Unix Delete All Files Subdirectories Linux

Unix Delete All Files Subdirectories Definition

How do I safely delete all files with a specific extension (e.g.bak) from current directory and all subfolders using one command-line? Simply, I'm afraid to use rm since I used...

0 0
Mar 4, 2011

I would like to be able to recursively delete specific various files from a directory and sub-directories. For example:

|_ _rm *file1 *file2 *file3


General :: Recursive Search For Files That Hold Some Specific Text? General :: Finding A Recursive Shell Or Perl Script To Delete Files With The Same Name As The Parent Folder? General :: List Files In Specific Directories Older Than 1 Day Or 24 Hours General :: Command For Listing Files/dirs Recursively Of Specific Directories? General :: Assign Read Privileges To User On Specific Directories And Files General :: Delete All Files (except The Four Newest) Throughout Directories Using A Bash Script? General :: If Some Users Modify/delete/create Files/directories? General :: Shell Script To Search For Files Of Specific Extension And Delete Them All? Software :: Create Certain Files And Directories With Specific Owners On The Target Machine Ubuntu Servers...
0 0

am new to Unix and Linux command line. How do I find and delete files under Linux / UNIX operating systems using shell prompt?

Some time it is necessary to find out files and remove them. However, rm command does not support search criteria. You need to use the find command to search for files in a directory and remove them on fly. You can combine find and rm command together.

Linux or UNIX – Find and remove file syntax

The basic find command syntax is:

find dir-name criteria action

dir-name : – Defines the working directory such as look into /tmp/ criteria : Use to select files such as “*.sh” action : The find action (what-to-do on file) such as delete the file.

To remove multiple files such as *.jpg or *.sh with one command find, use:

find . -name "FILE-TO-FIND" -exec rm -rf {} \;


find . -type f -name "FILE-TO-FIND" -exec rm -f {} \;

The only difference between above two syntax is that the first...

0 0

If you are wondering how to copy files of a specific extension (e.g., .txt, .php, .java) recursively, the following guide can help you. By copying files recursively, I mean that you copy files located in the current directory as well as its sub-directories, to another location which has the same directory structure.

Your first (unsuccessful) attempt may be using cp command with "-r" option.

$ cp -r *.txt /path/to/destdir

However, the above command will copy only *.txt files in the top-level current directory, but not in any of its sub-directories. So this is not the right way to do it.

A correct way to copy files recursively is to use cpio command, which is a Linux tool that copies files to and from archives. The command below demonstrates how to copy .txt files from one directory tree to another.

$ find /path/to/srcdir -name '*.txt' | cpio -pdm /path/to/destdir

"-p" option enables a recursive copy operation. "-d" option creates leading...
0 0


Using LM11 x64. How to list a specific file pattern recursively? I have a folder containing a dozen of subfolders. These subfolders contains *.mp3 files. I want to list all the mp3 files

The terminal answers:

ls: cannot access *.mp3: No such file or directory

However if I do

Then ls can list recursively, but all files are listed, regardless of their extension. Reading the help of ls, it appears that ls is able to recognize a file pattern like *.mp3 but somehow I cannot get that working.


: how to use ls to list recursively files of a specific extension?


: how to delete recursively files of a specific extension? Example delete all files having the extension Bogus: rm -r *.Bogus doesn't work. I got the same error than with ls (rm: cannot remove `*.Bogus': No such file or directory).

Thanks in advance for any...

0 0

Author: Deron Eriksson

Description: This Java tutorial describes how to recursively traverse all the files and subdirectories in a directory.

Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)

In another tutorial, we saw how we could get an array of the File objects (which can be files or directories) in a directory File object via a call to its listFiles() method. We also saw how we could determine if a File object is a directory or a file by calling its isDirectory() method. However, sometimes we want to access everything below a certain directory including all subdirectories and their files, not just the immediate level of files. We can do with with recursion.

The RecursiveFileDisplay class demonstrates how to do this. We obtain a File object representing the current directory and pass that File object to displayDirectoryContents(). The displayDirectoryContents() gets the array of File objects that...

0 0

Delete All Files In Unix 5,0/5 3703 votes

Simple Unix commands: deleting files and directories needs to be. Nyu Pre Med Summer Program. Everything else should be typed literally as given in the examples. Almost everything in Unix is in. At the end of each command you must press the Return key.

How to remove all the files in a. hasn't since at least 1994 for the GNU utils and likely since the late 70s for UNIX. To delete all files and. How can I recursively delete all files of a specific extension in the current directory? Unix & Linux; Ask Different. How can I delete all files with a particular extension in a particular folder? will accidentally delete all.xvg-files in the. UNIX is a registered trademark. A note about deleting empty directories. To remove empty directory use rmdir and not the rm command: $ rmdir mydirectory $ rmdir dirNameHere. Read a List of All Files.

Simple Unix commands. This will delete all the files with html after the dot which are in...

0 0
How to delete all files with specific word in filename?

location: - date: May 21, 2011
I need a command to find the all files which filename contains the text "SomeText" and to delete that files! From /home/movie/wp-content/uploads/ this folder I have lots of files and folders . Also I need that for folders and subfolders who contains some text in folder name "someTextInFolderName" Tnx in advance!

How to delete all files expect specific file type in the folder using command line [closed]

location: - date: April 17, 2014
I have lot of files in specific folder. I want to delete all files expect *.html file type in that folder. Is there any way to do this in command line? I am using Linux.

How to recursively delete all files with a specific file extension starting from??

location: - date: September 22, 2008
how do you recursively delete all files with a specific file extension starting...

0 0

OK, I know a lot of people might have asked about this, but, I just can't seem to find my answer. What I wanna do, is that I wanna delete all .jpg and .txt files (for ex) in dir1 and dir2.

What I did was:

@echo off FOR %%p IN (C:\testFolder D:\testFolder) DO FOR %%t IN (*.jpg *.txt) DO del /s %%p\%%t

In some directories it worked, in others it didn't. Like for .e.g this didn't do anything:

@echo off FOR %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t

Could you help me out here, I'm a novice in writing batch files, is this the right way of doing this? if not, then what's the right way? if it is, then what I'm I missing in the second snippet? why didn't it work? thnx for any tips...

0 0

A good replacement Linux tool is rpl, that was originally written for the Debian project, so it is available with apt-get install rpl in any Debian derived distro, and may be for others, but otherwise you can download the tar.gz file in SourgeForge.

Simplest example of use:

$ rpl old_string new_string test.txt

Note that if the string contain spaces it should be enclosed in quotation marks. By default rpl take care of capital letters but not of complete words, but you can change these defaults with options -i (ignore case) and -w (whole words). You can also specify multiple files:

$ rpl -i -w "old string" "new string" test.txt test2.txt

Or even specify the extensions (-x) to search or even search recursively (-R) in the directory:

$ rpl -x .html -x .txt -R old_string new_string test*

You can also search/replace in interactive mode with -p (prompt) option:

The output show the numbers of files/string replaced and the type of search (case...

0 0

I don't know a single-function method for this, but you can use genpath to recurse a list of subdirectories only. This list is returned as a semicolon-delimited string of directories, so you'll have to separate it using strread, i.e.

dirlist = strread(genpath('/path/of/directory'),'%s','delimiter',';')

If you don't want to include the given directory, remove the first entry of dirlist, i.e. dirlist(1)=[]; since it is always the first entry.

Then get the list of files in each directory with a looped dir.

filenamelist=[]; for d=1:length(dirlist) % keep only filenames filelist=dir(dirlist{d}); filelist={}; % remove '.' and '..' entries filelist([strmatch('.',filelist,'exact');strmatch('..',filelist,'exact'))=[]; % or to ignore all hidden files, use filelist(strmatch('.',filelist))=[]; % prepend directory name to each filename entry, separated by filesep* for f=1:length(filelist) filelist{f}=[dirlist{d} filesep filelist{f}]; ...
0 0

Java's interface for reading filesystem folder contents is not very performant (as you've discovered). JDK 7 fixes this with a completely new interface for this sort of thing, which should bring native level performance to these sorts of operations.

The core issue is that Java makes a native system call for every single file. On a low latency interface, this is not that big of a deal - but on a network with even moderate latency, it really adds up. If you profile your algorithm above, you'll find that the bulk of the time is spent in the pesky isDirectory() call - that's because you are incurring a round trip for every single call to isDirectory(). Most modern OSes can provide this sort of information when the list of files/folders was originally requested (as opposed to querying each individual file path for it's properties).

If you can't wait for JDK7, one strategy for addressing this latency is to go multi-threaded and use an ExecutorService with a maximum # of...

0 0

Go to the toplevel directory of the tree containing the zip files (cd …), then run

mv **/*.zip /path/to/single/target/directory

This works out of the box in zsh. If your shell is bash, you'll need to run shopt -s globstar first (you can and should put this command in your ~/.bashrc). If your shell is ksh, you'll need to run set -o globstar first (put it in your ~/.kshrc).

Alternatively, use find, which works everywhere with no special preparation but is more complicated:

find -name '*.zip' -exec mv {} /path/to/single/target/directory \;

If you want to remove empty directories afterwards, in zsh:

rmdir **/*(/^Fod)

In bash or ksh:

rmdir **/*/

and repeat as long as there are empty directories to remove. Alternatively, in any shell

find . -depth -type d -empty -exec rmdir {}...
0 0

We can also show love for LINQ:

using System.IO; using System.Linq; … var directory = Directory.GetParent(TestContext.TestDir); directory.EnumerateFiles() .ToList().ForEach(f => f.Delete()); directory.EnumerateDirectories() .ToList().ForEach(d => d.Delete(true));

Note that my solution here is not performant, because I am using Get*().ToList().ForEach(...) which generates the same IEnumerable twice. I use an extension method to avoid this issue:

using System.IO; using System.Linq; … var directory = Directory.GetParent(TestContext.TestDir); directory.EnumerateFiles() .ForEachInEnumerable(f => f.Delete()); directory.EnumerateDirectories() .ForEachInEnumerable(d => d.Delete(true));

This is the extension method:

/// /// Extensions for . /// public static class IEnumerableOfTExtensions { /// /// Performs the /// on each item in the enumerable object. /// /// The type of the enumerable. /// The enumerable. /// The action. /// /// “I am...
0 0