[英]Using LS for a specific path & file type without returing full file path?
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给它提供一个path / *。tar,并使其仅返回文件名而不是完整路径和文件名,以将文件名通过读入数组进行传递,以下是发生的情况终奌站:
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
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!
谢谢大家!
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).
所以我知道答案已经选择了,但是这里是一个使用ls
的选项,以防万一您好奇:
ls /home/admin/Backup/*.tar | awk -F/ '{print $NF}'
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. 但是您确实不需要为此使用
ls
命令。 The shell already expands the *tar
wildcard for you; Shell已经为您扩展了
*tar
通配符; all ls
does in this case is print the arguments. ls
在这种情况下ls
就是打印参数。 So: 所以:
( 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$'
.) (顺便说一句,使用
grep "tar"
命令将在名称中任何位置列出所有带有"tar"
文件,而不仅仅是名称以.tar
结尾的文件。如果要使用grep
,则需要grep '\\.tar$'
。
You should not use ls
for this . 您不应为此使用
ls
。 Instead, simply create an array with the full names: 相反,只需使用全名创建一个数组:
files=(/path/to/dir/*.tar)
... and run basename
on each element when you process the array. ...,并在处理数组时在每个元素上运行
basename
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.