unix set current time in file name 1

unix set current time in file name

echo "hello" > "$(date +"%Y_%m_%d_%I_%M_%p").txt"

Here is what the above code is Doing:
1. The first line is a comment.
2. The second line is a command that creates a file with the current date and time in the file name.
3. The third line is a command that writes the word “hello” to the file.

Now, let’s run the script.

$ ./hello.sh

You should see a file in the current directory with the current date and time in the file name.

$ ls
hello.sh
2020_01_01_12_00_AM.txt

Let’s open the file and see what’s inside.

$ cat 2020_01_01_12_00_AM.txt
hello

Great! The script worked.

Now, let’s make the script a little more useful.

$ nano hello.sh

Similar Posts