bash 将 LS 用于特定路径和文件类型而不返回完整文件路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18176113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using LS for a specific path & file type without returing full file path?
提问by ms.Ashlee
I am writing a bash script and would like to use LS giving it a path/*.tar and have it return just file names instead of full path and file name to pass the file names through read into an array, below is what happens in terminal:
我正在编写一个 bash 脚本,并想使用 LS 给它一个路径/*.tar 并让它只返回文件名而不是完整的路径和文件名,以将文件名通过 read 传递到数组中,下面是发生了什么终端:
If i just use LS with a directory it returns:
如果我只是将 LS 与一个目录一起使用,它会返回:
admin@linuxbox:~$ ls /home/admin/Backup/
0_BuIndex.txt.backup Backup1376238064.tar Backup1376239611.tar Backup1376241919.tar
Backup1376167035.tar Backup1376238960.tar Backup1376240158.tar Backup1376243097.tar
Backup1376168581.tar Backup1376239110.tar Backup1376241421.tar
Backup1376237070.tar Backup1376239350.tar Backup1376241489.tar
Backup1376237928.tar Backup1376239479.tar Backup1376241608.tar
Giving path & file type it returns:
给出它返回的路径和文件类型:
admin@linuxbox:~$ ls /home/admin/Backup/*.tar
/home/admin/Backup/Backup1376167035.tar /home/admin/Backup/Backup1376239479.tar
/home/admin/Backup/Backup1376168581.tar /home/admin/Backup/Backup1376239611.tar
/home/admin/Backup/Backup1376237070.tar /home/admin/Backup/Backup1376240158.tar
/home/admin/Backup/Backup1376237928.tar /home/admin/Backup/Backup1376241421.tar
/home/admin/Backup/Backup1376238064.tar /home/admin/Backup/Backup1376241489.tar
/home/admin/Backup/Backup1376238960.tar /home/admin/Backup/Backup1376241608.tar
/home/admin/Backup/Backup1376239110.tar /home/admin/Backup/Backup1376241919.tar
/home/admin/Backup/Backup1376239350.tar /home/admin/Backup/Backup1376243097.tar
I would like to use a command like the second but have it only return the file names without the path, my work around right now is to use
我想使用第二个命令,但它只返回没有路径的文件名,我现在的工作是使用
read -r -a TarArray <<< `ls -1 /home/admin/Backup | grep "tar"`
but I am hoping to find a solution that doesn't require me to pipe through grep.
但我希望找到一个不需要我通过 grep 管道的解决方案。
Any ideas? Thanks all!
有任何想法吗?谢谢大家!
回答by lmjohns3
You could potentially use find
for this :
您可能会find
为此使用:
find /home/admin/Backup -name \*.tar -printf '%f\n'
From the man page :
从手册页:
-printf format
True; print format on the standard output,
interpreting `\' escapes and `%' directives.
...
%f File's name with any leading directories
removed (only the last element).
回答by Bryan
So I know the answer has already been chosen but here is an option for using ls
just in case you were curious:
所以我知道已经选择了答案,但这里有一个选项,ls
以防万一你好奇:
ls /home/admin/Backup/*.tar | awk -F/ '{print $NF}'
回答by Keith Thompson
Run the ls
command from the directory:
ls
从目录运行命令:
( cd /home/admin/Backup ; ls *.tar )
But you really don't need to use the ls
command for this. The shell already expands the *tar
wildcard for you; all ls
does in this case is print the arguments. So:
但是您真的不需要为此使用该ls
命令。shell 已经*tar
为你扩展了通配符;所有ls
做在这种情况下打印的参数。所以:
( cd /home/admin/Backup ; echo *.tar )
(Incidentally, your command with grep "tar"
will list all files with "tar"
anywhere in their names, not just files whose names end in .tar
. If you wanted to use grep
, you'd want grep '\.tar$'
.)
(顺便说一句,您的命令 withgrep "tar"
将列出"tar"
名称中包含任何位置的所有文件,而不仅仅是名称以.tar
.结尾的文件。如果您想使用grep
,则需要grep '\.tar$'
.)
回答by tripleee
You should not use ls
for this. Instead, simply create an array with the full names:
你不应该ls
为此使用。相反,只需创建一个具有全名的数组:
files=(/path/to/dir/*.tar)
... and run basename
on each element when you process the array.
...并basename
在处理数组时在每个元素上运行。