Search code examples
linuxbashls

how to read files listed by ls command one by one in bash?


I want to read all the files in a particular directory, and I want to read it one by one.

Here's what i've done so far. ls successfully get all the files from a specified directory, but could not give the file names to me one by one. It Echos the files one time only. I want to get it one by one because I need to do some parsing and use it somehwere.

#!/bin/sh

echo Content-type: application/json
echo ""

for output in "ls /home/myComputer/Desktop/*";
do
echo $output
done

Solution

  • You can do

    for output in /home/myComputer/Desktop/*
    do
        echo $output
    done
    

    If there's any risk that /home/myComputer/Desktop is empty, please follow @JIDs advice in the comments below.