对于目录中的文件,只有文件名回声(无路径) [英] For files in directory, only echo filename (no path)

查看:101
本文介绍了对于目录中的文件,只有文件名回声(无路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何去呼应只有文件的文件名,如果我遍历目录与一个循环?

 用于在/ home文件名/用户/ *

  回声$文件名
完成的;

将拉动全路径与文件名。我只想文件名。


解决方案

如果您希望本机庆典解决方案

 在/文件家庭/用户/ *;做
  回声$ {文件## * /}
DONE

以上使用参数扩展这是原产于壳,也不需要如基本名

不过,也许我建议只使用找到

 查找/ home / user的型的F -printf%F \\ N

How do I go about echoing only the filename of a file if I iterate a directory with a for loop?

for filename in /home/user/*
do
  echo $filename
done;

will pull the full path with the file name. I just want the file name.

解决方案

If you want a native bash solution

for file in /home/user/*; do
  echo ${file##*/}
done

The above uses Parameter Expansion which is native to the shell and does not require a call to an external binary such as basename

However, might I suggest just using find

find /home/user -type f -printf "%f\n"

这篇关于对于目录中的文件,只有文件名回声(无路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆