linux - Shell 脚本 ls 指向第 n 个文件

标签 linux shell

我想为我的 linux 系统开发一个脚本,我需要一个返回路径中第 n 个文件的函数。例如,如果我在路径中有后续文件:

my_path
  |_file1
  |_file2
  |_file3
  |_file4
  |_file5

我想要一个返回第 n 个文件名的函数。是否可以?我不知道怎么...

最佳答案

家庭作业? ;)

应该这样做:

find my_path -type f | head -3 | tail -1

用你的'n'替换'3'。以上将是递归的。如果您只需要“my_path”中的文件,请将 find 替换为简单的 ls。

显然,'n' 的定义取决于排序顺序。您可能需要考虑在 head 之前进行“排序”。

关于linux - Shell 脚本 ls 指向第 n 个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24231621/

相关文章:

bash - 是否可以在 msys/mingw 上模拟进程替换(使用 bash 3.x)

linux - 将特定列中的数值替换为其他数值

linux - 什么时候用引号括起 shell 变量?

bash - 如何在 Apache2 CGI 中捕获参数值

linux - LDAP:slapd 和 openldap

linux - 在Linux中检查可用内存的正确方法是什么

linux - 无法从 Windows 批处理文件运行 shell 脚本

ruby-on-rails - 在 `irb` 中,反引号抛出异常,但在 `rails c` 中,它们不会

linux - unqlite.h :651:15: error: changes meaning of ‘pgno’ from ‘typedef sxu64 pgno’ [-fpermissive]

linux - erlang zip :unzip/1 {error, bad_central_directory} 和 {error, bad_eocd}