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()
todelete_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 usingfunction
- sof () { ...; }
- 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, usegetent
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 therm
commands to be executed. Review it, then removeecho
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
Highlight Text Similar to Grep, But Don't Filter Out Text
Linux Cmd to Search for a Class File Among Jars Irrespective of Jar Path
Check Whether a Certain File Type/Extension Exists in Directory
How to Schedule Tcpdump to Run for a Specific Period of Time
Is It Better to Use Git Grep Than Plain Grep If We Want to Search in Versioned Source Code
How to Fix Symbol Lookup Error: Undefined Symbol Errors in a Cluster Environment
When to Use --Dynamic Option in Nm
Install Packages in Alpine Docker
End of Line (New Line) Escapes in Bash
How to Force a Cifs Connection to Unmount
Comparing Two Unsorted Lists in Linux, Listing the Unique in the Second File
I Get "Dquote>" as a Result of Executing a Program in Linux Shell
Sanitize Environment with Command or Bash Script
How to Skip Saturday and Sunday in a Cron Expression
How to Register Fuse Filesystem Type with Mount(8) and Fstab