简体   繁体   中英

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? 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.

ls -1 will only show the filenames without any of the other attributes.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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