Bash Script Automatitation Name Change Using Date
I'm New to the Shell/Bash Area. I've Created This Little Script to Change a Name Using the Date Command. the Idea Is to Get Something Like This 1...
I'm new to the shell/bash area. I've created this little script to change a name using the date command. The idea is to get something like this
1.txt=>2020-02.txt
And it works, but I get the old file name 2020-02-1.txt added to the output.
This is my code so far:
# FOR LOOP TO RENAME FILES
cd /home/atlas/Documents/bash/CARPETA-ORIGEN
#UBICAR LA CARPETA
FILES=$(ls *.txt)
NEW="$(date +"%Y-%m")"
for FILE in $FILES
do
echo "Renaming $FILE to new-$FILE"
mv $FILE $NEW-$FILE
done
1 Answer
I tried to reproduce your error. The result was:
1.txtis renamed to2020-02-1.txt,2.txtis renamed to2020-02-2.txt,- ...and so on.
If you want to remove the old filename/index from the result (which creates problems because of name clashes - there can only be one file as output), you can change your script to
# FOR LOOP TO RENAME FILES
cd /home/atlas/Documents/bash/CARPETA-ORIGEN
#UBICAR LA CARPETA
FILES=$(ls *.txt)
NEW="$(date +"%Y-%m")"
for FILE in $FILES
do
echo "Renaming $FILE to new-$FILE"
mv $FILE $NEW.txt
done
But I wouldn't recommend this, because you can only rename one .txt file per folder.
However, the output of the above script is
2020-02.txt