Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

60%
+1 −0
Q&A Recursively remove files with the same name as the ones that end in `.part`

I might be inclined to try... find . -type f -name '*.part' -exec sh -c ' [ -f "${1%.part}" ] && rm -i -- "${1%.part}"; for f in "${1%.part}".*; do [ -f "$f" ] && rm -...

posted 1y ago by jimbobmcgee‭  ·  edited 1y ago by jimbobmcgee‭

Answer
#5: Post edited by user avatar jimbobmcgee‭ · 2023-06-16T02:05:52Z (about 1 year ago)
  • I might be inclined to try...
  • ```
  • find . -type f -name '*.part' -exec sh -c '
  • [ -f "${1%.part}" ] && rm -i -- "${1%.part}";
  • for f in "${1%.part}".*; do
  • [ -f "$f" ] && rm -i -- "$f";
  • done
  • ' -- {} \;
  • ```
  • (newlines for readability; can be elided if *one-liner* means something to you...)
  • 1. `find . -type f -name '*.part'` &mdash; find files ending with _.part_
  • 2. `-exec sh -c '...' -- {} \;` &mdash; run a shell script ... for each found file
  • 3. `"${1%.part}"` &mdash; strip _.part_ from the end of the filename in _$1_
  • 4. `[ -f "${1%.part}" ] && ...;` &mdash; if a file exists with no extension, do the ... bit
  • 5. `rm -i -- "${1%.part}"` &mdash; delete the file with no extension
  • 6. `for f in "${1%.part}".*; do ... done` &mdash; loop each found path matching the filename with any extension; path is stored in _$f_ (this includes the one with the .part extension)
  • 7. `[ -f "$f" ] && ...;` &mdash; if the path in _$f_ exists and is a file, do the ... bit
  • 8. `rm -i -- "$f"` &mdash; remove the file in _$f_
  • Note that I'm using various checks that the thing I'm asking to delete is a *file*, not a directory, link, fifo, etc.
  • If limiting only to files is less of a concern, you might well be able to shorten this to...
  • ```
  • find . -name '*.part' -exec sh -c 'rm -i -- "${1%.part}" "${1%.part}".*' -- {} \;
  • ```
  • The shell may write errors if the args to `rm` don't expand to existing paths, hide that with judicious use of `2>/dev/null` redirection, if you care.
  • For fewer subshells, you may be able to pass all found files to the same shell in one go, with...
  • ```
  • find . -name '*.part' -exec sh -c 'while [ -n "$1" ]; do rm -i -- "${1%.part}" "${1%.part}".*; shift; done' -- {} \+
  • ```
  • ...but this might be painful for larger file lists.
  • In general, note there's is technically a race condition between the various tests and the eventual delete, but that's only a concern if multiple processes are acting on that directory tree. Not sure how to avoid that.
  • Finally, `rm -i` is used to prompt `y/n` for each file to delete, as a safety net. Remove the `-i` switch from the `rm` calls if you are confident.
  • I might be inclined to try...
  • ```
  • find . -type f -name '*.part' -exec sh -c '
  • [ -f "${1%.part}" ] && rm -i -- "${1%.part}";
  • for f in "${1%.part}".*; do
  • [ -f "$f" ] && rm -i -- "$f";
  • done
  • ' -- {} \;
  • ```
  • (newlines for readability; can be elided if *one-liner* means something to you...)
  • 1. `find . -type f -name '*.part'` &mdash; find files ending with _.part_
  • 2. `-exec sh -c '...' -- {} \;` &mdash; run a shell script ... for each found file; path to file is in _$1_ in child script
  • 3. `"${1%.part}"` &mdash; strip _.part_ from the end of the filename in _$1_ (same as `basename` but without the extra process)
  • 4. `[ -f "${1%.part}" ] && ...;` &mdash; if a file exists with no extension, do the ... bit
  • 5. `rm -i -- "${1%.part}"` &mdash; delete the file with no extension
  • 6. `for f in "${1%.part}".*; do ... done` &mdash; loop each found path matching the filename with any extension; path is stored in _$f_ (this includes the one with the .part extension)
  • 7. `[ -f "$f" ] && ...;` &mdash; if the path in _$f_ exists and is a file, do the ... bit
  • 8. `rm -i -- "$f"` &mdash; remove the file in _$f_
  • Note that I'm using various checks that the thing I'm asking to delete is a *file*, not a directory, link, fifo, etc.
  • If limiting only to files is less of a concern, you might well be able to shorten this to...
  • ```
  • find . -name '*.part' -exec sh -c 'rm -i -- "${1%.part}" "${1%.part}".*' -- {} \;
  • ```
  • The shell may write errors if the args to `rm` don't expand to existing paths, hide that with judicious use of `2>/dev/null` redirection, if you care.
  • For fewer subshells, you may be able to pass all found files to the same shell in one go, with...
  • ```
  • find . -name '*.part' -exec sh -c 'while [ -n "$1" ]; do rm -i -- "${1%.part}" "${1%.part}".*; shift; done' -- {} \+
  • ```
  • ...but this might be painful for larger file lists.
  • In general, note there's is technically a race condition between the various tests and the eventual delete, but that's only a concern if multiple processes are acting on that directory tree. Not sure how to avoid that.
  • Finally, `rm -i` is used to prompt `y/n` for each file to delete, as a safety net. Remove the `-i` switch from the `rm` calls if you are confident.
#4: Post edited by user avatar jimbobmcgee‭ · 2023-06-16T02:03:34Z (about 1 year ago)
  • I might be inclined to try...
  • ```
  • find . -type f -name '*.part' -exec sh -c '
  • [ -f "${1%.part}" ] && rm -i -- "${1%.part}";
  • for f in "${1%.part}".*; do
  • [ -f "$f" ] && rm -i -- "$f";
  • done
  • ' -- {} \;
  • ```
  • (newlines for readability; can be elided if *one-liner* means something to you...)
  • 1. `find . -type f -name '*.part'` &mdash; find files ending with _.part_
  • 2. `-exec sh -c '...' -- {} \;` &mdash; run a shell script ... for each found file
  • 3. `"${1%.part}"` &mdash; strip _.part_ from the end of the filename in _$1_
  • 4. `[ -f "${1%.part}" ] && ...;` &mdash; if a file exists with no extension, do the ... bit
  • 5. `rm -i -- "${1%.part}"` &mdash; delete the file with no extension
  • 6. `for f in "${1%.part}".*; do ... done` &mdash; loop each found path matching #3 + any extension; path is stored in _$f_ (this includes the one with the .part extension)
  • 7. `[ -f "$f" ] && ...;` &mdash; if the path in _$f_ exists and is a file, do the ... bit
  • 8. `rm -i -- "$f"` &mdash; remove the file in _$f_
  • Note that I'm using various checks that the thing I'm asking to delete is a *file*, not a directory, link, fifo, etc.
  • If limiting only to files is less of a concern, you might well be able to shorten this to...
  • ```
  • find . -name '*.part' -exec sh -c 'rm -i -- "${1%.part}" "${1%.part}".*' -- {} \;
  • ```
  • The shell may write errors if the args to `rm` don't expand to existing paths, hide that with judicious use of `2>/dev/null` redirection, if you care.
  • For fewer subshells, you may be able to pass all found files to the same shell in one go, with...
  • ```
  • find . -name '*.part' -exec sh -c 'while [ -n "$1" ]; do rm -i -- "${1%.part}" "${1%.part}".*; shift; done' -- {} \+
  • ```
  • ...but this might be painful for larger file lists.
  • In general, note there's is technically a race condition between the various tests and the eventual delete, but that's only a concern if multiple processes are acting on that directory tree. Not sure how to avoid that.
  • Finally, `rm -i` is used to prompt `y/n` for each file to delete, as a safety net. Remove the `-i` switch from the `rm` calls if you are confident.
  • I might be inclined to try...
  • ```
  • find . -type f -name '*.part' -exec sh -c '
  • [ -f "${1%.part}" ] && rm -i -- "${1%.part}";
  • for f in "${1%.part}".*; do
  • [ -f "$f" ] && rm -i -- "$f";
  • done
  • ' -- {} \;
  • ```
  • (newlines for readability; can be elided if *one-liner* means something to you...)
  • 1. `find . -type f -name '*.part'` &mdash; find files ending with _.part_
  • 2. `-exec sh -c '...' -- {} \;` &mdash; run a shell script ... for each found file
  • 3. `"${1%.part}"` &mdash; strip _.part_ from the end of the filename in _$1_
  • 4. `[ -f "${1%.part}" ] && ...;` &mdash; if a file exists with no extension, do the ... bit
  • 5. `rm -i -- "${1%.part}"` &mdash; delete the file with no extension
  • 6. `for f in "${1%.part}".*; do ... done` &mdash; loop each found path matching the filename with any extension; path is stored in _$f_ (this includes the one with the .part extension)
  • 7. `[ -f "$f" ] && ...;` &mdash; if the path in _$f_ exists and is a file, do the ... bit
  • 8. `rm -i -- "$f"` &mdash; remove the file in _$f_
  • Note that I'm using various checks that the thing I'm asking to delete is a *file*, not a directory, link, fifo, etc.
  • If limiting only to files is less of a concern, you might well be able to shorten this to...
  • ```
  • find . -name '*.part' -exec sh -c 'rm -i -- "${1%.part}" "${1%.part}".*' -- {} \;
  • ```
  • The shell may write errors if the args to `rm` don't expand to existing paths, hide that with judicious use of `2>/dev/null` redirection, if you care.
  • For fewer subshells, you may be able to pass all found files to the same shell in one go, with...
  • ```
  • find . -name '*.part' -exec sh -c 'while [ -n "$1" ]; do rm -i -- "${1%.part}" "${1%.part}".*; shift; done' -- {} \+
  • ```
  • ...but this might be painful for larger file lists.
  • In general, note there's is technically a race condition between the various tests and the eventual delete, but that's only a concern if multiple processes are acting on that directory tree. Not sure how to avoid that.
  • Finally, `rm -i` is used to prompt `y/n` for each file to delete, as a safety net. Remove the `-i` switch from the `rm` calls if you are confident.
#3: Post edited by user avatar jimbobmcgee‭ · 2023-06-16T02:02:51Z (about 1 year ago)
  • I might be inclined to try...
  • ```
  • find . -type f -name '*.part' -exec sh -c '
  • [ -f "${1%.part}" ] && rm -i -- "${1%.part}";
  • for f in "${1%.part}".*; do
  • [ -f "$f" ] && rm -i -- "$f";
  • done
  • ' -- {} \;
  • ```
  • (newlines for readability; can be elided if *one-liner* means something to you...)
  • 1. `find . -type f -name '*.part'` &mdash; find files ending with _.part_
  • 2. `-exec sh -c '...' -- {} \;` &mdash; run a shell script ... for each found file
  • 3. `"${1%.part}"` &mdash; strip _.part_ from the end of the filename in _$1_
  • 4. `[ -f "${1%.part}" ] && ...;` &mdash; if a file named literally as per #3 exists, do the ... bit
  • 5. `rm -i -- "${1%.part}"` &mdash; delete the file named as per #3
  • 6. `for f in "${1%.part}".*; do ... done` &mdash; loop each found path matching #3 + any extension; path is stored in _$f_
  • 7. `[ -f "$f" ] && ...;` &mdash; if the path in _$f_ exists and is a file, do the ... bit
  • 8. `rm -i -- "$f"` &mdash; remove the file in _$f_
  • Note that I'm using various checks that the thing I'm asking to delete is a *file*, not a directory, link, fifo, etc.
  • If limiting only to files is less of a concern, you might well be able to shorten this to...
  • ```
  • find . -name '*.part' -exec sh -c 'rm -i -- "${1%.part}" "${1%.part}".*' -- {} \;
  • ```
  • The shell may write errors if the args to `rm` don't expand to existing paths, hide that with judicious use of `2>/dev/null` redirection, if you care.
  • For fewer subshells, you may be able to pass all found files to the same shell in one go, with...
  • ```
  • find . -name '*.part' -exec sh -c 'while [ -n "$1" ]; do rm -i -- "${1%.part}" "${1%.part}".*; shift; done' -- {} \+
  • ```
  • ...but this might be painful for larger file lists.
  • In general, note there's is technically a race condition between the various tests and the eventual delete, but that's only a concern if multiple processes are acting on that directory tree. Not sure how to avoid that.
  • Finally, `rm -i` is used to prompt `y/n` for each file to delete, as a safety net. Remove the `-i` switch from the `rm` calls if you are confident.
  • I might be inclined to try...
  • ```
  • find . -type f -name '*.part' -exec sh -c '
  • [ -f "${1%.part}" ] && rm -i -- "${1%.part}";
  • for f in "${1%.part}".*; do
  • [ -f "$f" ] && rm -i -- "$f";
  • done
  • ' -- {} \;
  • ```
  • (newlines for readability; can be elided if *one-liner* means something to you...)
  • 1. `find . -type f -name '*.part'` &mdash; find files ending with _.part_
  • 2. `-exec sh -c '...' -- {} \;` &mdash; run a shell script ... for each found file
  • 3. `"${1%.part}"` &mdash; strip _.part_ from the end of the filename in _$1_
  • 4. `[ -f "${1%.part}" ] && ...;` &mdash; if a file exists with no extension, do the ... bit
  • 5. `rm -i -- "${1%.part}"` &mdash; delete the file with no extension
  • 6. `for f in "${1%.part}".*; do ... done` &mdash; loop each found path matching #3 + any extension; path is stored in _$f_ (this includes the one with the .part extension)
  • 7. `[ -f "$f" ] && ...;` &mdash; if the path in _$f_ exists and is a file, do the ... bit
  • 8. `rm -i -- "$f"` &mdash; remove the file in _$f_
  • Note that I'm using various checks that the thing I'm asking to delete is a *file*, not a directory, link, fifo, etc.
  • If limiting only to files is less of a concern, you might well be able to shorten this to...
  • ```
  • find . -name '*.part' -exec sh -c 'rm -i -- "${1%.part}" "${1%.part}".*' -- {} \;
  • ```
  • The shell may write errors if the args to `rm` don't expand to existing paths, hide that with judicious use of `2>/dev/null` redirection, if you care.
  • For fewer subshells, you may be able to pass all found files to the same shell in one go, with...
  • ```
  • find . -name '*.part' -exec sh -c 'while [ -n "$1" ]; do rm -i -- "${1%.part}" "${1%.part}".*; shift; done' -- {} \+
  • ```
  • ...but this might be painful for larger file lists.
  • In general, note there's is technically a race condition between the various tests and the eventual delete, but that's only a concern if multiple processes are acting on that directory tree. Not sure how to avoid that.
  • Finally, `rm -i` is used to prompt `y/n` for each file to delete, as a safety net. Remove the `-i` switch from the `rm` calls if you are confident.
#2: Post edited by user avatar jimbobmcgee‭ · 2023-06-16T02:00:45Z (about 1 year ago)
  • I might be inclined to try...
  • ```
  • find . -type f -name '*.part' -exec sh -c '
  • [ -f "${1%.part}" ] && rm -i -- "${1%.part}";
  • for f in "${1%.part}".*; do
  • [ -f "$f" ] && rm -i -- "$f";
  • done
  • ' -- {} \;
  • ```
  • (newlines for readability; can be elided if *one-liner* means something to you...)
  • 1. `find . -type f -name '*.part'` &mdash; find files ending with _.part_
  • 2. `-exec sh -c '...' {} \;` &mdash; run a shell script ... for each found file
  • 3. `"${1%.part}"` &mdash; strip _.part_ from the end of the filename in _$1_
  • 4. `[ -f "${1%.part}" ] && ...;` &mdash; if a file named literally as per #3 exists, do the ... bit
  • 5. `rm -i -- "${1%.part}"` &mdash; delete the file named as per #3
  • 6. `for f in "${1%.part}".*; do ... done` &mdash; loop each found path matching #3 + any extension; path is stored in _$f_
  • 7. `[ -f "$f" ] && ...;` &mdash; if the path in _$f_ exists and is a file, do the ... bit
  • 8. `rm -i -- "$f"` &mdash; remove the file in _$f_
  • Note that I'm using various checks that the thing I'm asking to delete is a *file*, not a directory, link, fifo, etc.
  • If limiting only to files is less of a concern, you might well be able to shorten this to...
  • ```
  • find . -name '*.part' -exec sh -c 'rm -i -- "${1%.part}" "${1%.part}".*' -- {} \;
  • ```
  • The shell may write errors if the args to `rm` don't expand to existing paths, hide that with judicious use of `2>/dev/null` redirection, if you care.
  • For fewer subshells, you may be able to pass all found files to the same shell in one go, with...
  • ```
  • find . -name '*.part' -exec sh -c 'while [ -n "$1" ]; do rm -i -- "${1%.part}" "${1%.part}".*; shift; done' -- {} \+
  • ```
  • ...but this might be painful for larger file lists.
  • In general, note there's is technically a race condition between the various tests and the eventual delete, but that's only a concern if multiple processes are acting on that directory tree. Not sure how to avoid that.
  • Finally, `rm -i` is used to prompt `y/n` for each file to delete, as a safety net. Remove the `-i` switch from the `rm` calls if you are confident.
  • I might be inclined to try...
  • ```
  • find . -type f -name '*.part' -exec sh -c '
  • [ -f "${1%.part}" ] && rm -i -- "${1%.part}";
  • for f in "${1%.part}".*; do
  • [ -f "$f" ] && rm -i -- "$f";
  • done
  • ' -- {} \;
  • ```
  • (newlines for readability; can be elided if *one-liner* means something to you...)
  • 1. `find . -type f -name '*.part'` &mdash; find files ending with _.part_
  • 2. `-exec sh -c '...' -- {} \;` &mdash; run a shell script ... for each found file
  • 3. `"${1%.part}"` &mdash; strip _.part_ from the end of the filename in _$1_
  • 4. `[ -f "${1%.part}" ] && ...;` &mdash; if a file named literally as per #3 exists, do the ... bit
  • 5. `rm -i -- "${1%.part}"` &mdash; delete the file named as per #3
  • 6. `for f in "${1%.part}".*; do ... done` &mdash; loop each found path matching #3 + any extension; path is stored in _$f_
  • 7. `[ -f "$f" ] && ...;` &mdash; if the path in _$f_ exists and is a file, do the ... bit
  • 8. `rm -i -- "$f"` &mdash; remove the file in _$f_
  • Note that I'm using various checks that the thing I'm asking to delete is a *file*, not a directory, link, fifo, etc.
  • If limiting only to files is less of a concern, you might well be able to shorten this to...
  • ```
  • find . -name '*.part' -exec sh -c 'rm -i -- "${1%.part}" "${1%.part}".*' -- {} \;
  • ```
  • The shell may write errors if the args to `rm` don't expand to existing paths, hide that with judicious use of `2>/dev/null` redirection, if you care.
  • For fewer subshells, you may be able to pass all found files to the same shell in one go, with...
  • ```
  • find . -name '*.part' -exec sh -c 'while [ -n "$1" ]; do rm -i -- "${1%.part}" "${1%.part}".*; shift; done' -- {} \+
  • ```
  • ...but this might be painful for larger file lists.
  • In general, note there's is technically a race condition between the various tests and the eventual delete, but that's only a concern if multiple processes are acting on that directory tree. Not sure how to avoid that.
  • Finally, `rm -i` is used to prompt `y/n` for each file to delete, as a safety net. Remove the `-i` switch from the `rm` calls if you are confident.
#1: Initial revision by user avatar jimbobmcgee‭ · 2023-06-16T01:59:01Z (about 1 year ago)
I might be inclined to try...

```
find . -type f -name '*.part' -exec sh -c '
  [ -f "${1%.part}" ] && rm -i -- "${1%.part}"; 
  for f in "${1%.part}".*; do 
    [ -f "$f" ] && rm -i -- "$f"; 
  done
' -- {} \;
```

(newlines for readability; can be elided if *one-liner* means something to you...)

 1. `find . -type f -name '*.part'` &mdash; find files ending with _.part_
 2. `-exec sh -c '...' {} \;` &mdash; run a shell script ... for each found file
 3. `"${1%.part}"` &mdash; strip _.part_ from the end of the filename in _$1_
 4. `[ -f "${1%.part}" ] && ...;` &mdash; if a file named literally as per #3 exists, do the ... bit
 5. `rm -i -- "${1%.part}"` &mdash; delete the file named as per #3
 6. `for f in "${1%.part}".*; do ... done` &mdash; loop each found path matching #3 + any extension; path is stored in _$f_
 7. `[ -f  "$f" ] && ...;` &mdash; if the path in _$f_ exists and is a file, do the ... bit
 8. `rm -i -- "$f"` &mdash; remove the file in _$f_

Note that I'm using various checks that the thing I'm asking to delete is a *file*, not a directory, link, fifo, etc.  

If limiting only to files is less of a concern, you might well be able to shorten this to...

```
find . -name '*.part' -exec sh -c 'rm -i -- "${1%.part}" "${1%.part}".*' -- {} \;
```

The shell may write errors if the args to `rm` don't expand to existing paths, hide that with judicious use of `2>/dev/null` redirection, if you care.

For fewer subshells, you may be able to pass all found files to the same shell in one go, with...

```
find . -name '*.part' -exec sh -c 'while [ -n "$1" ]; do rm -i -- "${1%.part}" "${1%.part}".*; shift; done' -- {} \+
```

...but this might be painful for larger file lists.

In general, note there's is technically a race condition between the various tests and the eventual delete, but that's only a concern if multiple processes are acting on that directory tree.  Not sure how to avoid that.

Finally, `rm -i` is used to prompt `y/n` for each file to delete, as a safety net.  Remove the `-i` switch from the `rm` calls if you are confident.