문제

I have many files without file extention. Now I want to add .txt to all files. I tried the following but it gives an error, mv: rename . to ..txt: Invalid argument.

How can I achieve this?

find . -iname "*.*" -exec bash -c 'mv "$0" "$0.txt"' {} \;
도움이 되었습니까?

해결책

You're nearly there!

Just add -type f to only deal with files:

find . -type f -exec bash -c 'mv "$0" "$0.txt"' {} \;

If your mv handles the -n option, you might want to use it (that's the option to not overwrite existing files).


The error your having is because . is one of the first found by found, and your system complains (rightly) when you want to rename .! with the -type f you're sure this won't happen. Now if you wanted to act on everything inside your directory, you would, e.g., add -mindepth 1 at the beginning of the find command (as . is considered depth 0).


It is not very clear in your question, but what if you want to add the extension .txt to all files that don't have an extension? (we'll agree that to have an extension means to have a period in the name). In this case, you'll use the negation of -name '*.*' as follows:

find . -type f \! -name '*.*' -exec bash -c 'mv "$0" "$0.txt"' {} \;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top