I have lots of point files I need to change for model input. I want to rename lots of these files based on the contents of a file. These files are text files arranged in rows.
file name: data_lat_long.txt
1 data_20_20
2 data_30_40
3 data_50_60
.
.
and so on
my files are named 1
, 2
, 3
, 4
, etc,.
I want 1
to be changed in to data_20_20
, 2
in to data_30_40
and so on.
Best Answer
Redirect the contents of
data_lat_long.txt
to awhile
loop that usesread
to read in the each line of the file and for each line assign it to a variable called$line
. Then runmv
with the contents of$line
as an argument (which is the from and to parts you have specified in yourdata_lat_long.txt
file). Keep on looping until the end of file.Note this implicitly expects that neither file names in your
data_lat_long.txt
contain spaces (so that there are only two columns).If files have spaces in them, then I suggest a comma separator in your file:
and then use:
That makes use of the
-a
option ofread
which assigns the text separated by theIFS
(in this case comma) to an array called$files
. We then index the various elements on themv
and quote the arguments with double quotes""
. This is so the array value is expanded on execution of themv
command and so the space in the file is not seen as argument separator bymv
.mv 1 space file1
does not do the same thing asmv "1" "space file1"
. The former command attempts to move files/directories called1
andspace
to a directory calledfile1
.