find and remove files with space using find command on Linux
I'd do it this way:
find . -iname 'thumbs.db' -exec rm -rfv {} +
This way, it still works even if your directories contain whitespace in their names.
Linux Bash, remove a file seperated by a space
Instead of using xargs
use find's -delete
instead, ie:
find . name .zip -delete
Or tell find to output with null seperators instead so that xargs is not confused by the spaces, ie:
find . name .zip -print0 | xargs -0 rm
Read & Remove files containing space in its name
for i in $(cat)
is a common antipattern in bash. You should not use a for loop to read lines. This doesn't work because:
First
$(cat A.txt)
is expanded to20150726140135_Content 1.mp4
(no quotes).
20150726162955_Content 1.mp4
20150726163056_Content 3.mp4
20150726164057_Content 5.mp4
20150726170315_Tab main 2.mp4Then shell executes the command
for i in 20150726140135_Content 1.mp4 20150726162955_Content 1.mp4 20150726163056_Content 3.mp4
. Newlines and spaces are intepreted as arguments separators.
20150726164057_Content 5.mp4 20150726170315_Tab main 2.mp4
To read a file line by line in bash use a while IFS= read -r
loop:
while IFS= read -r f; do
rm "/tmp/$f";
done < A.txt
How to remove files using find and rm command?
You need space between the command and \;
find -mmin -19 -exec rm {} \;
find
already provide -delete
option, so you don't need to use -exec rm ..
:
find -mmin -19 -delete
-delete
Delete files; true if removal succeeded. If the removal failed, an
error message is issued. If -delete fails, find's exit status will
be nonzero (when it eventually exits). Use of -delete automatically
turns on the -depth option.Warnings: Don't forget that the find command line is evaluated as an
expression, so putting -delete first will make find try to delete
everything below the starting points you specified. When testing a
find command line that you later intend to use with
-delete, you should explicitly specify -depth in order to avoid later surprises. Because -delete implies -depth, you cannot usefully
use -prune and -delete together.
Find files with spaces in the bash
Use find command with a space between two wildcards. It will match files with single or multiple spaces. "find ." will find all files in current folder and all the sub-folders. "-type f" will only look for files and not folders.
find . -type f -name "* *"
EDIT
To replace the spaces with underscores, try this
find . -type f -name "* *" | while read file; do mv "$file" ${file// /_}; done
Delete a list of files with find and grep
find . -name '*car*' -exec rm -f {} \;
or pass the output of your pipeline to xargs
:
find | grep car | xargs rm -f
Note that these are very blunt tools, and you are likely to remove files that you did not intend to remove. Also, no effort is made here to deal with files that contain characters such as whitespace (including newlines) or leading dashes. Be warned.
Shell Script to Delete Files with Spaces
Here's man find
:
When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.
Use -mtime +0
to delete files that are 24+ hours old. Spaces don't matter.
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.
Script to remove spaces in all files and folders?
Try this
ls | grep " " | while read file_name
do
mv "$file_name" "$(echo $file_name | sed -E 's/ +//g')"
done
sed -E
is so that you can use some simple regex, and / +/
so it can work in case of multiple consecutive spaces such as
. And /g
so it replaces every occurrences such as foo baa .txt
.
Related Topics
Linux: Block Until a String Is Matched in a File ("Tail + Grep with Blocking")
Why Do the Md5 Hashes of Two Tarballs of the Same File Differ
Branch-Specific Configuration File Maintenance with Git
How to Display Only Files from Aws S3 Ls Command
Why Does Gcc Force Pic for X64 Shared Libs
Where to Get Msbuild for Linux
"Stdin: Is Not a Tty" from Cronjob
Convert a Text File into Columns
Public Key Is Not Available: No_Pubkey F76221572C52609D
System Wide Keyboard Hook on X Under Linux
Apache Server Keeps Crashing, "Caught Sigterm, Shutting Down"
How to Use Both 64 Bit and 32 Bit Instructions in the Same Executable in 64 Bit Linux
Setup Cron Tab to Specific Time of During Weekdays
Deceive the Jvm About the Number of Available Cores (On Linux)
Rename File Command in Unix with Timestamp
Performance - Multithreaded or Multiprocess Applications
In Linux, How to Test Whether the Output of a Program Is Going to a Live Terminal or to a File