Bash

如何對沒有副檔名的文件使用萬用字元

  • April 30, 2022

我完全被這個驚呆了。我正在執行一個每週腳本來將日誌文件移動到一個目錄中。

mkdir 2022-04-30
mv *.* 2022-04-30/ 

當我這樣做時-僅移動帶有副檔名的文件,而沒有移動的文件。

如果我嘗試*.說沒有找到文件

如果我嘗試*它會做我想要的,但它也會嘗試將目錄移動2022-04-30到自身並列印錯誤。它不影響腳本,但我想了解發生了什麼。

我很確定*意味著零個或多個字元。所以*.*應該移動所有文件而不是目錄。

發生了什麼以及如何在不移動目錄的情況下製作我需要的腳本?

CentOS 9 / 重擊

編輯:

文件名範例

2022-04-29abc.cde
2022-04-29 App

``means zero or more of any character,.means the single character dot. So.means any names with a(t least one) dot in there somewhere. On Unix-likes, the dot is just a regular character in filenames, and also there's no difference between files and directories in a regular glob. (If you wanted to match directories only, you could use/`.)

Here, it looks like the names of your files contain letters while the directories don’t, so to target on that, you could use *[[:alpha:]]* to match names that have at least one letter. That would still match directories, but would avoid names like 2022-04-30.

Alternatively, if what you’re doing is something like mv 2022-04-29* 2022-04-29 (i.e. move files starting with the date to a matching directory), you could instead use

mv 2022-04-29?* 2022-04-29

The ? matches exactly one character, so ?* requires at least one character after the date.

In zsh (but not in Bash), you could use *(.) to match just regular files (and similarly *(/) to match just directories). In all shells, */ would match only directories, but there’s no equivalent for matching just regular files. (The slight difference is that with */ the names are produced with the trailing slashe.)`

引用自:https://unix.stackexchange.com/questions/700952