0

I run this code for getting the list of directories from input file and return last file in each directory

#!/bin/bash
while IFS= read -r line; do
echo "Text read from file: $line"
ls $line -ltrh |tail -1
done < "$1"

but its not working with this error

[root@localhost /]# ./last_file.sh new2.txt
Text read from file: 17609
 No such file or directory
Text read from file: 17601
: No such file or directory
Text read from file: 17608
: No such file or directory
Text read from file: 17610
: No such file or directory
Text read from file: 12465
: No such file or directory
Text read from file: 12356
: No such file or directory
Text read from file: 34562
: No such file or directory
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ryan
  • 1
  • 1

1 Answers1

0

I just inverted ls options and everything works as expected:

#!/bin/bash
while IFS= read -r line; do
        echo "Text read from file: $line"
        ls -ltrh "$line" | tail -1
done < "$1"

Please, tell me what do you get.

 $>  ./last_file.sh new2.txt        
 $>  Text read from file: /xxxxxx/xxxxxx/Downloads/
 $>  -rwxr-xr-x   1 xxxxxxx  staff   115B Sep  3 16:49 last_file.sh
JrZabott
  • 23
  • 7