find files based on extension but display name without extension no basename, sed, awk, grep or ; allowed

huangapple go评论84阅读模式
英文:

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

文件/目录结构:

  1. $ tree -a
  2. .
  3. ├── bar
  4. ├── bar.gif
  5. ├── base
  6. └── foo.gif
  7. ├── foo
  8. └── aaa.gif
  9. └── .qux.gif
  10. 3 个目录,4 个文件

代码

  1. find -type f -name '*.gif' -exec bash -c 'printf "%s\n" "${@%.gif}"' bash {} +

输出

  1. ./bar
  2. ./.qux
  3. ./foo/aaa
  4. ./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:

  1. $ tree -a
  2. .
  3. ├── bar
  4. ├── bar.gif
  5. ├── base
  6. │   └── foo.gif
  7. ├── foo
  8. │   └── aaa.gif
  9. └── .qux.gif
  10. 3 directories, 4 files

Code

  1. find -type f -name '*.gif' -exec bash -c 'printf "%s\n" "${@%.gif}"' bash {} +

Output

  1. ./bar
  2. ./.qux
  3. ./foo/aaa
  4. ./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

  1. 查找 . -name '*.gif' -type f -execdir bash -c 'printf "%s\n" "${@%.*}"' bash {} +
英文:

Something like:

  1. find . -name '*.gif' -type f -execdir bash -c 'printf "%s\n" "${@%.*}"' bash {} +

答案3

得分: 0

使用 [tag:perl]:

  1. say s/\.gif$//r for File::Find::Rule
  2. ->file()
  3. ->name(qr/\.gif\z/)
  4. ->in(".")
  5. '```
  6. 输出:
  7. ```bar
  8. .qux
  9. foo/aaa
  10. base/foo```
  11. <details>
  12. <summary>英文:</summary>
  13. Using [tag:perl]:
  14. perl -MFile::Find::Rule -E &#39;
  15. say s/\.gif$//r for File::Find::Rule
  16. -&gt;file()
  17. -&gt;name(qr/\.gif\z/)
  18. -&gt;in(&quot;.&quot;)
  19. &#39;
  20. Output:
  21. bar
  22. .qux
  23. foo/aaa
  24. base/foo
  25. </details>

huangapple
  • 本文由 发表于 2023年2月16日 13:39:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定