How can I get the current date and time in the terminal and set a custom command in the terminal ...

Learn how can i get the current date and time in the terminal and set a custom command in the terminal for it? with practical examples, diagrams, and best practices. Covers linux, command-line, ter...

Mastering Date and Time in Your Linux Terminal

Hero image for How can I get the current date and time in the terminal and set a custom command in the terminal ...

Learn how to display the current date and time in the Linux terminal and create custom, memorable commands for quick access.

The command line is a powerful tool for system administrators and developers alike. One of the most fundamental pieces of information you often need is the current date and time. While the date command is straightforward, customizing it and creating aliases can significantly boost your productivity. This article will guide you through displaying the current date and time, formatting it, and setting up your own custom commands for convenience.

Displaying Current Date and Time

The primary command for displaying date and time in Linux is date. By default, it provides a comprehensive output including the day of the week, month, day, time, timezone, and year. However, its true power lies in its formatting options, allowing you to tailor the output precisely to your needs.

date

Basic date command output

The date command supports a wide array of format specifiers, each preceded by a percentage sign (%). These specifiers allow you to extract specific components of the date and time or arrange them in a custom order. For instance, %Y gives the full year, %m the month as a number, %d the day of the month, %H the hour (24-hour format), %M the minute, and %S the second.

date +"%Y-%m-%d %H:%M:%S"
date +"Today is %A, %B %d, %Y and the time is %I:%M:%S %p"

Examples of custom date and time formatting

Creating Custom Commands (Aliases)

Typing long date format strings repeatedly can be cumbersome. This is where shell aliases come in handy. An alias is a shortcut that allows you to define a new command that executes a longer command string. Once defined, you can use your custom, shorter command instead of the full one.

To create a temporary alias, you can use the alias command directly in your terminal. This alias will only be active for the current terminal session. If you close the terminal or open a new one, the alias will be gone.

alias now='date +"%Y-%m-%d %H:%M:%S"'
now

Creating and using a temporary alias for current date and time

Making Aliases Permanent

For aliases you want to use regularly, you need to make them permanent. This involves adding the alias definition to your shell's configuration file. Common shell configuration files include ~/.bashrc for Bash, ~/.zshrc for Zsh, or ~/.profile (which is often sourced by both).

flowchart TD
    A[Start] --> B{Open Shell Config File};
    B --> C[Add 'alias' Command];
    C --> D{Save File};
    D --> E[Source Config File];
    E --> F[Use Custom Command];
    F --> G[End];

Workflow for making a custom command permanent

1. Open your shell's configuration file

Use a text editor like nano or vim to open your shell's configuration file. For Bash, this is typically ~/.bashrc.

2. Add the alias definition

Append your desired alias definition to the end of the file. For example, alias mytime='date +"%H:%M:%S"'.

3. Save and close the file

Save the changes and exit your text editor.

4. Source the configuration file

Apply the changes to your current session without restarting the terminal by running source ~/.bashrc (or your respective config file).

5. Test your new command

Now you can type your custom command, e.g., mytime, and it should display the formatted time.

# Example for ~/.bashrc or ~/.zshrc

alias current_datetime='date +"%Y-%m-%d %H:%M:%S"'
alias just_time='date +"%H:%M:%S"'
alias full_date='date +"%A, %B %d, %Y"'

Adding permanent aliases to a shell configuration file

By leveraging the date command's formatting capabilities and the power of shell aliases, you can significantly streamline your workflow and make interacting with your terminal more efficient and personalized.