JoshPark
JoshPark

Reputation: 29

How would I create a Bash script that moves files?

So, I have 209 text files (numbered 0-208.txt) that I want to move from the directory "data" to "query".

I wrote this simple bash script, but it keeps giving me an error:

#!/bin/bash
counter=0
while [$counter -le 209]
do
  mv /data/$counter.txt /query
  ((counter++))
done

It gives me the error message:

generate_all_predictions.bash: line 6: syntax error near unexpected token '$'\r''
'enerate_all_predictions.bash: line 6: ' ((counter++))

Shouldn't the code I have just loop through /data and move all the text files into /query?

Upvotes: 0

Views: 74

Answers (3)

user1934428
user1934428

Reputation: 22217

Since according to your query, the numbers 0 and 208 are fixed, how about

 mv /data/{0..208}.txt /query

Upvotes: 0

l0b0
l0b0

Reputation: 58808

You have at least two syntax errors:

  1. A carriage return somewhere in your code. Fix by running the script through unix2dos.
  2. [$counter -le 209] needs to be [ "$counter" -le 209 ] - the whitespace is crucial because [ is actually a command, not just syntax like in many other languages.

You also have a logical error, because -le means "less than or equal",so the script will try to move file 209 as well.

Since your file list is relatively small you can also use mv /data/{0..208}.txt /query to move everything in one go.

Upvotes: 2

Mars
Mars

Reputation: 4995

Seems like there is a carriage return after $.

What text editor did you use? Did you create the script in a Windows editor?

Removing the carriage return and including spaces within the brackets as others have mentioned should get the script to work.

Upvotes: 0

Related Questions