简体   繁体   English

将来自Linux“ ls”命令的文件名存储到单独的数组/列表元素中

[英]Store filenames from linux “ls” command into separate array/list elements

How do I store "just" the file names and their associated extension into an array or list in a "bash" script? 如何将文件名及其关联的扩展名“仅”存储到“ bash”脚本中的数组或列表中? In a way that every filename is stored in a separate element WITHOUT other file information that ls spits out like the date created or the permission levels... 以某种方式将每个文件名存储在单独的元素中,而不会产生其他文件信息,例如创建日期或权限级别……

I like to do: 我喜欢做:

filelist=`ls -1 /somedir/`

and then iterate over $filelist. 然后遍历$ filelist。

ls -1 will only show the filenames without any of the other attributes. ls -1仅显示文件名,不包含任何其他属性。

Something like this: 像这样:

root@myserver-1-00:~# filelist=($(ls))
root@myserver-1-00:~# echo $filelist
Desktop
root@myserver-1-00:~# echo ${filelist[0]}
Desktop
root@myserver-1-00:~# echo ${filelist[1]}
Documents
root@myserver-1-00:~# echo ${filelist[2]}
Downloads

variable=($(yourcommand)) --> makes the output to be assigned as an array variable = {$(yourcommand))->使输出被分配为数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM