0

I have a loop that iterates over a series of text files (all are in the form name.txt):

for txt in *txt; do
    sed -i '$ d' $txt 
done

But whenever there is no .txt file in the directory, I get the following error message:

sed: can't read ‘*txt’: No such file or directory

codeforester
  • 39,467
  • 16
  • 112
  • 140
D1X
  • 5,025
  • 5
  • 21
  • 36

2 Answers2

0

It's because it isn't matching any files and is leaving the 'txt' instead of doing what you expect which is to skip the for-loop. On some implementations (e.g. macOS), it will leave the string as '*txt' and run the for-loop with the variable txt set to *txt. You need to test for the existence of the file pattern first before attempting to run the for-loop. See Check if a file exists with wildcard in shell script.

Community
  • 1
  • 1
Nick
  • 4,820
  • 18
  • 31
  • 47
0

You can solve this issue in two ways:

a) check for file existence before doing anything with it

for txt in *txt; do
  [[ -f "$txt" ]] || continue # skip if file doesn't exist or if it isn't a regular file
  # your logic here
done

b) use the shell option shopt -s nullglob which will make sure glob expands to nothing when there are no matching files

shopt -s nullglob
for txt in *txt; do
  # your logic here
done

See also:

Community
  • 1
  • 1
codeforester
  • 39,467
  • 16
  • 112
  • 140