Search code examples
linuxshellls

Alternative to ls in shell-script compatible with nohup


I have a shell-script which lists all the file names in a directory and store them in a new file.

The problem is that when I execute this script with the nohup command, it lists the first name four times instead of listing the correct names.

Commenting the problem with other programmers they think that the problem may be the ls command.

Part of my code is the following:

for i in $( ls -1 ./Datasets/); do
    awk  '{print $1}'  ./genes.txt | head -$num_lineas | tail -1 >> ./aux   
    let num_lineas=$num_lineas-1    
done

Do you know an alternative to ls that works well with nohup?

Thanks.


Solution

  • Don't use ls to feed the loop, use:

    for i in ./Datasets/*; do
    

    or if subdirectories are of interest

    for i in ./Datasets/*/*; do
    

    Lastly, and more correctly, use find if you need the entire tree below Datasets:

    find ./Datasets -type f | while IFS= read -r file; do
        (do stuff with $file)
    done
    

    Others frown, but there is nothing wrong with also using find as:

    for file in $(find ./Datasets -type f); do
        (do stuff with $file)
    done
    

    Just choose the syntax that most closely meets your needs.