How to Delete Multiple Files at Once in Bash on Linux

How to delete multiple files at once in Bash on Linux?

Bash supports all sorts of wildcards and expansions.

Your exact case would be handled by brace expansion, like so:

$ rm -rf abc.log.2012-03-{14,27,28}

The above would expand to a single command with all three arguments, and be equivalent to typing:

$ rm -rf abc.log.2012-03-14 abc.log.2012-03-27 abc.log.2012-03-28

It's important to note that this expansion is done by the shell, before rm is even loaded.

Command to delete multiple files

Your approach was not totally wrong. Since you're dealing with a list coming to STDIN and rm expects parameters, you need to use xargs.

Another thing is that you have to escape a dot when you grep for a filename. Your Command should look sth. like this.:

ls | grep -v 'logtest\.' | grep 'logtest' | xargs rm 

Note that you do a doubled grep. The first one to exclude your logtest.* itself and the second to include your remaining files with logtest.

Deleting multiple files in Linux?

simply use rm -f file*.txt to delete all files which starts with file and ends with the extention .txt

How to delete multiple files and directories in UNIX

Yes you can do it this way.

The rm command can take multiple arguments to delete multiple files/directory in one command.
So, instead of calling rm once per entry in your array, you can call it only one time with all the files in your array as parameter.

If you have a lot of files to delete, be careful because there is a command line length limit. (you can get it by executing "getconf ARG_MAX" )

UNIX how to make my script delete multiple files and wildcards?

I'm not going to do a detailed code review of your whole script, but here are a few notes.

  • You are looping over the arguments in the main part of your script, but then you're calling the delete function with multiple arguments. That function has no looping in it. Move the loop from main() to delete_files() (and note that I pluralized its name for clarity).
  • And speaking of main(), you might as well encapsulate that code (option processing, function dispatch, etc.) in a function of that name, then at the bottom of your script have a line that calls it: main "$@"
  • Don't use $* unless you need what it does and understand its use - instead use "$@" almost always and always quote it (with very rare exceptions)
  • Use indentation consistently
  • If your script doesn't need to be portable to shells other than Bash, then use Bash-specific features such as [[ ]] instead of [ ]
  • You're using both methods of naming a function at the same time (function f()). Use one or the other - parens are preferred over using function - so f () { ...; }
  • Use more quotes, some examples:

    pwd=$(readlink -e "$filename")
    mv "$filename" ~/deleted/"${filename}_$inode"
    echo "${filename}_$inode:$pwd" >> ~/.restore.info
  • But I don't recommend using tilde (~) in scripts - use $HOME instead. And if you need to look up a user's home directory, use getent instead of other methods.

How to mass remove files that contain special characters in file name

This will delete every file whose name ends in (1), recursively:

find . -name '*(1)' -exec rm {} +
  • -name '*(1) (1)' to only delete files ending with a double 1.
  • -name '*([0-9])' will match any single digit.
  • find . -mindepth 1 -maxdepth 1 -name '*(1)' -exec rm {} + for no recursion.
  • I would do find . -name '*(1)' -exec echo rm {} \; to print a neat list of the rm commands to be executed. Review it, then remove echo to run for real.
  • Changing \; back to + is optional, + is more efficient.

I want to delete all the files under a directory using shell script, passing file directory as an argument

Use the find command for this:

find "$1" -maxdepth 1 -type f -delete

Pass the directory as a parameter in the scripts ($1) and then ensure you are only searching the directory at one level with -maxdepth 1 and searching for files only with -f. Delete what ever is found with -delete.

How to recursively delete multiple files with different extensions?

find . \( -name "*.extension1" -o -name "*.extension2" \) -type f -delete

find Documents ( -name ".py" -o -name ".html" ) -exec file {} \;

OR

find . -regextype posix-egrep -regex ".*\.(extension1|extension2)$" -type f -delete


Related Topics



Leave a reply



Submit