Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: syntax error near unexpected token `|'

This command works well in command prompt :

 ]$ ls -ltr ../cmxserver.log*|grep "`date | awk '{print $2" "$3}'`"|cut -d "/" -f2
 cmxserver.log.2
 cmxserver.log.1
 cmxserver.log

However, using this in for loop gives error bash: syntax error near unexpected token `|'

   ]$for y in `ls -ltr ../cmxserver.log*|grep "`date | awk '{print $2" "$3}'`"|cut -d "/" -f2`

 -bash: syntax error near unexpected token `|'

Any idea ?

Thanks

like image 463
Sourabh Avatar asked Apr 11 '26 04:04

Sourabh


1 Answers

The whole line can be solved by find command:

find .. -maxdepth 1 -mtime -1 -daystart -name 'cmxserver.log*' -printf "%f\n"
  • .. - directory where to search
  • -maxdepth 1 - don't go recursive to subdirectories
  • -mtime -1 - only today's files
  • -daystart - count the day since midnight, not last 24 hrs
  • -name 'cmxserver.log*' - filenames
  • -printf "%f\n" - print only basename
like image 77
mitroo Avatar answered Apr 13 '26 04:04

mitroo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!