[Linux] – Create a folder with the current date and time – 2019. And make it file system compliant! EASY!

Linux

Command:

current_date_and_time=$(date +%YY.%mM.%dD_%HH_%MM_%SS) && mkdir -p YOUR_FOLDER_NAME_$current_date_and_time

What this command is doing is running ‘date‘ with a specified format of YYYY.MM.DD_HH_MM_SS which is compatible across all operating systems; then it is taking the current date in that format and creating a folder using ‘mkdir‘ and suffixing the string of ‘YOUR_FOLDER_NAME’ with the date value just generated. You can adjust the periods to be underscores or vice versa.

The output ends up looking like this:

YOUR_FOLDER_NAME_2019Y.09M.09D_22H_17M_29S

Most examples of this create folders with spaces or colons; the colons are non compliant with other OSs like Windows. For example Linux is fine with having a colon in the name of a file or folder, Windows does not accept this.

If you wanted to make a function of this to add in your .bashrc or .zshrc you can use this, feel free to rename the function to what you want, I used mkdir_date but you could use use mkdird or something. Then you can use it just like you would with mkdir

function mkdir_date
{
current_date_and_time=$(date +%YY_%mM_%dD_%HH_%MM_%SS);
mkdir -p $1_$current_date_and_time
}

If you wanted to make a folder with this function all you need to do is type:

mkdir_date myfolder
and you’ll see something like this:

myfolder_2019Y_09M_09D_22H_05M_37S

An example of this in use on one of my machines


Comments? Questions? Post them below! 🙂

Leave a Reply

You are currently viewing [Linux] – Create a folder with the current date and time – 2019. And make it file system compliant! EASY!