I do and get a list of files where I would like to delete many duplicate backup files
find . -type f -name '._*'
I would like to find those files which have a corresponding filename
/home/masi/._test.texmatches/home/masi/test.tex/home/masi/math/lorem.pngmatches/home/masi/math/._lorem.png
Pseudocode about files wanted to be saved filename which has corresponding ._filename but also save filename without ._filename
find . -type f -name '._*' -exec \
find filenameWithoutDotUnderscore, if yes, print the filename
Pseudocode 2 clarification about files wanted to be removed = ._filename if there is a corresponding filename
- If there is
filenameand._filenamein the same directory, print._filenamesuch that I can remove the duplicate =._filename. - Exclude
filenamePart1_.filenamePart2,bok_3A.pdf, ... in._filename. - Do not remove
._filenameif there is no correspondingfilenamein the same directory.
Reviewing Wildcard's command
I do find . -type f -name '._*' -exec sh -c 'for a; do f="${a%/*}/${a##*/._}"; [ -e "$f" ] && printf "rm -- %s\n" "$a"; done' find-sh {} + but it returns too many files. I think I need more && conditions beside the existence check ([ -e "$f" ]). It would be great to get here some content comparison and lastly diff if suspicion of much difference.
Systems: Ubuntu 16.04 and Debian 8.25
Bash: 4.3.42
Find: 4.7.0
._and if there is a file in the same directory without the._, print that file's name. They are system independent. – Wildcard Jun 10 '16 at 17:33${a%/*}expands to the value of the variableawith the last slash and everything after it removed;${a##*/._}expands to the value of the variableawith everything up to the last occurrence of/._removed. The/in between is a literal slash. SeeLESS='+/Parameter Expansion' man bash. – Wildcard Jun 10 '16 at 17:37._filenamefiles or thefilenamefiles? I've bolded the relevant lines of my answer; I already included a "dry run" version.... – Wildcard Jun 10 '16 at 21:09[ -e "$f" ]is an existence check. Make a backup first. (You should have backups anyway.) Use the command in subdirectories first. Delete them manually, if you like. But your original exact question has been exactly answered. Certainly you should study up enough to understand what the command is doing before you blindly run it, but have you even attempted to understand what the command I posted does? – Wildcard Jun 21 '16 at 17:31filenameexists. However, I think it is not enough. I think other&&conditions are required. What do you think? – Léo Léopold Hertz 준영 Jun 21 '16 at 18:39