I'm working on a bash script and for a part of the bash script I want to take the most recent file in a directory and make a new file in the same directory , but the new file should be increased by one. So for example if the most recent file is file1234.txt I want to create a new file in the directory called file1235.txt. So my code is as follows.
file=$(ls -ltr /home/dir |tail -n 1 | awk '{print $9}' | cut -f1 -d "." | cut -c 5- )
echo $file
newfile=$((file +1 ))
echo $newfile
touch file$newfile.txt
The issue is that the new file that is created is file1.txt not file1235.txt. Is this an issue with subshells being created?
lsthen check ifls -ltr /home/dir |tail -n 1prints what you expect in the format you expect. Maybe there is an unrelated file (possibly directory) that is most recent at the moment. – Kamil Maciorowski Mar 03 '21 at 22:29cd "$(mktemp --directory)" && touch file123.txt && touch file1234.txt && file=$(ls -ltr |tail -n 1 | awk '{print $9}' | cut -f1 -d "." | cut -c 5- ) && echo $((file +1 ))prints 1235. Something else is going on. – l0b0 Mar 04 '21 at 02:24fileis empty andnewfile=1– DuncG Mar 04 '21 at 10:07