I need to rename files to include a string found in the first line. ex: [acoder@test]# head -1 FILE001.DATXYZ123Here's my best guess: for file in /some/dir/*.DATdo        CODE=`head -1 $file`        echo ${file%.DAT}.$CODE.DATdoneoutput: .DATa/some/dir/FILE001.XYZ123expecting to see: /some/dir/FILE001.XYZ123.DATWhat's going wrong?
Add string from file to filename
bash;filenames;string
If the file comes from a Windows system, it may have a carriage-return at the end.You can fix that by assigning to the CODE variable like this:CODE=$( head -n 1 $file | tr -d '\r' )