英文:
find files based on extension but display name without extension no basename, sed, awk, grep or ; allowed
问题
我需要编写一个脚本,列出当前目录及其所有子目录中具有.gif扩展名的所有文件,但不使用以下任何内容:
- basename
- grep
- egrep
- fgrep
- rgrep
- &&
- ||
- ;
- sed
- awk
并且仍然包括隐藏文件。
我尝试过使用 find . -type f -name '*.gif' -printf '%f\n'
来成功显示.gif文件,但仍然显示扩展名。问题是:如果我尝试使用 cut -d . -f 1
来移除文件扩展名,那么也会移除隐藏文件(这是我不想要的),因为它们的名称以"."开头。
然后我尝试使用 tr -d '.gif'
,但一些文件的名称中包含'g'或'.'。
我还尝试使用一些这些答案中的方法,但它们都包括basename、sed、awk或在其脚本中使用一些";"。
由于有这么多限制,我真的不知道是否可能实现这一目标,但我应该做到。
你会如何实现这个目标?
英文:
I need to write a script that lists all the files with a .gif extension in the current directory and all its sub-directories BUT DO NOT use ANY of:
- basename
- grep
- egrep
- fgrep
- rgrep
- &&
- ||
- ;
- sed
- awk
AND still include hidden files.
I tried find . -type f -name '*.gif' -printf '%f\n'
which will succesfully display .gif files, but still shows extension. Here's the catch: if I try to use cut -d . -f 1
to remove file extension, I also remove hidden files (which I don't want to) because their names start with ".".
Then I turned to use tr -d '.gif'
but some of the files have a 'g' or a '.' in their name.
I also tried to use some of these answers BUT all of them include either basename, sed, awk or use some ";" in their script.
With so many restrictions I really don't know if it's even possible to achieve that but I'm supposed to.
How would you do it?
答案1
得分: 1
文件/目录结构:
$ tree -a
.
├── bar
├── bar.gif
├── base
│ └── foo.gif
├── foo
│ └── aaa.gif
└── .qux.gif
3 个目录,4 个文件
代码
find -type f -name '*.gif' -exec bash -c 'printf "%s\n" "${@%.gif}"' bash {} +
输出
./bar
./.qux
./foo/aaa
./base/foo
解释
参数扩展 扩展参数:$foo
,$1
。您可以使用它来执行字符串或数组操作:${file%.mp3}
,${0##*/}
,${files[@]: -4}
。它们应该始终用引号括起来。查看:http://mywiki.wooledge.org/BashFAQ/073 和 bash 手册中的 "Parameter Expansion"。还请参阅 http://wiki.bash-hackers.org/syntax/pe。
英文:
files/dirs structure:
$ tree -a
.
├── bar
├── bar.gif
├── base
│   └── foo.gif
├── foo
│   └── aaa.gif
└── .qux.gif
3 directories, 4 files
Code
find -type f -name '*.gif' -exec bash -c 'printf "%s\n" "${@%.gif}"' bash {} +
Output
./bar
./.qux
./foo/aaa
./base/foo
Explanations
Parameter Expansion expands parameters: $foo
, $1
. You can use it to perform string or array operations: "${file%.mp3}"
, "${0##*/}"
, "${files[@]: -4}"
. They should always be quoted. See: http://mywiki.wooledge.org/BashFAQ/073 and "Parameter Expansion" in man bash. Also see http://wiki.bash-hackers.org/syntax/pe.
答案2
得分: 0
查找 . -name '*.gif' -type f -execdir bash -c 'printf "%s\n" "${@%.*}"' bash {} +
英文:
Something like:
find . -name '*.gif' -type f -execdir bash -c 'printf "%s\n" "${@%.*}"' bash {} +
答案3
得分: 0
使用 [tag:perl]:
say s/\.gif$//r for File::Find::Rule
->file()
->name(qr/\.gif\z/)
->in(".")
'```
输出:
```bar
.qux
foo/aaa
base/foo```
<details>
<summary>英文:</summary>
Using [tag:perl]:
perl -MFile::Find::Rule -E '
say s/\.gif$//r for File::Find::Rule
->file()
->name(qr/\.gif\z/)
->in(".")
'
Output:
bar
.qux
foo/aaa
base/foo
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论