cancel
Showing results for 
Search instead for 
Did you mean: 
jeesshnasree
Starfighter Starfighter
Starfighter
  • 1,063 Views

how to create folders more than 30 days in linux

Jump to solution

Hello All,

 

How to remove older than one month folders &  only current path only .

Please provide command for create folders more than 30 days for Learning & housekeep  practice purpose .

if possible any command then please provide .

I am waiting for your valuable information . 

 

 

Thanks & Regards,

Jeesshnasree

Labels (1)
1 Solution

Accepted Solutions
shashi01
Moderator
Moderator
  • 399 Views

@jeesshnasree 

Thanks for sharing the screenshot and your question!

What you're seeing is normal behavior in Linux. When you create folders using mkdir, Linux sets the timestamp (shown in ls -lrt) to the current date and time, not based on the folder name. This is because Linux filesystems like ext4 and xfs track creation time (btime) internally, but do not allow users to manually set or change it.

The only timestamps that can be changed from userspace are:

Modification time (mtime)

Access time (atime)

So, even if your folders are named like rundeck-01092025, rundeck-02092025, etc., the timestamps you see are simply the moment the folder was created, in your case, all showing September 1st.

If you want the visible timestamps to match the dates in the folder names (just for clarity or for alignment with future schedule logic), you can use the touch command to set the modification time manually.

Here’s how you can do it all in a single line:

sudo bash -c "for offset in {0..30}; do folder=\"rundeck-\$(date -d \"+\$offset day\" +%d%m%Y)\"; mkdir -p \"\$folder\"; chown rundeck:rundeck \"\$folder\"; touch -d \"+\$offset day\" \"\$folder\"; done"

This command:

Creates 31 folders with future-dated names

Assigns them to the rundeck user and group

Sets the folder’s mtime to match its name (e.g., rundeck-01092025 will show a timestamp of Sep 1, 2025)

Note: This changes only the mtime, not the true btime (creation time), which Linux does not allow you to modify.

Screenshot From 2025-09-01 19-41-50.png

You can use the stat command to view btime

View solution in original post

15 Replies
shashi01
Moderator
Moderator
  • 877 Views

@jeesshnasree 

Hello,  just to make sure I understand, when you say 'how to create folders more than 30 days in Linux', do you mean:

Finding folders that are older than 30 days?

Creating a folder and making it look like it was created over 30 days ago (i.e., backdating the timestamp)?

Or scheduling a folder to be created 30 days from now?

Let me know what you're trying to do, and I’ll help you out accordingly!

Ok got it

mkdir test1 test2 test3 && \
touch -d "35 days ago" test1 && \
touch -d "40 days ago" test2 && \
touch -d "10 days ago" test3 && \
echo "Older than 30 days (current path):" && \
find . -mindepth 1 -maxdepth 1 -type d -mtime +30 && \
echo "Deleting those…" && \
find . -mindepth 1 -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \; && \
echo "Leftover:" && ls -d */

NOTE:  Always test with the echo safe-run before deleting:

find . -mindepth 1 -maxdepth 1 -type d -mtime +30 -exec echo {} \;

Only when you are sure >>  run with rm -rf

For your own testing purpose, you can break down the commands step by step and try them individually to better understand how they work

jeesshnasree
Starfighter Starfighter
Starfighter
  • 489 Views

Hello @shashi01 ,

 

 

Thank you @shashi01 for your valuable information .

Please find below details need to create folders .

Examples :  from today to  more than 30 days create folders with single command with for loop , if possible could you please share  command .


I provide below folder details for reference purpose .
if one single create these all folders need to create more than 

 

 

sample:

drwxrwxr-x 2 rundeck rundeck 4096 Jun 27 19:39 rundeck-27062025

drwxrwxr-x 2 rundeck rundeck 4096 Jun 28 19:39 rundeck-28062025

drwxrwxr-x 2 rundeck rundeck 4096 Jun 29 19:39 rundeck-29062025

drwxrwxr-x 2 rundeck rundeck 4096 Jun 30 19:39 rundeck-30062025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 01 19:39 rundeck-01072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 02 19:39 rundeck-02072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 03 19:39 rundeck-03072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 04 19:39 rundeck-04072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 05 19:39 rundeck-05072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 06 19:39 rundeck-06072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 07 19:39 rundeck-07072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 08 19:39 rundeck-08072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 09 19:39 rundeck-09072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 10 19:39 rundeck-10072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 11 19:39 rundeck-11072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 12 19:39 rundeck-12072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 13 19:39 rundeck-13072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 14 19:39 rundeck-14072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 15 19:39 rundeck-15072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 16 19:39 rundeck-16072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 17 19:39 rundeck-17072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 18 19:39 rundeck-18072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 19 19:39 rundeck-19072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 20 19:39 rundeck-20072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 21 19:39 rundeck-21072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 22 19:39 rundeck-22072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 23 19:39 rundeck-23072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 24 19:39 rundeck-24072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 25 19:39 rundeck-25072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 26 19:39 rundeck-26072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 27 19:39 rundeck-27072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 28 19:39 rundeck-28072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 29 19:39 rundeck-29072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 30 19:39 rundeck-30072025

drwxrwxr-x 2 rundeck rundeck 4096 Jul 31 19:39 rundeck-31072025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 01 19:39 rundeck-01082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 02 19:39 rundeck-02082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 03 19:39 rundeck-03082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 04 19:39 rundeck-04082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 05 19:39 rundeck-05082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 06 19:39 rundeck-06082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 07 19:39 rundeck-07082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 08 19:39 rundeck-08082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 09 19:39 rundeck-09082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 10 19:39 rundeck-10082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 11 19:39 rundeck-11082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 12 19:39 rundeck-12082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 13 19:39 rundeck-13082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 14 19:39 rundeck-14082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 15 19:39 rundeck-15082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 16 19:39 rundeck-16082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 17 19:39 rundeck-17082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 18 19:39 rundeck-18082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 19 19:39 rundeck-19082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 20 19:39 rundeck-20082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 21 19:39 rundeck-21082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 22 19:39 rundeck-22082025

drwxrwxr-x 2 rundeck rundeck 4096 Aug 23 19:39 rundeck-23082025

 

 

-------------
I am waiting for your valuable information .

 

Thanks & Regards,

Jeesshnasree

 

0 Kudos
shashi01
Moderator
Moderator
  • 487 Views

@jeesshnasree 

sudo bash -c 'for offset in {0..30}; do folder="rundeck-$(date -d "+${offset} day" +%d%m%Y)"; mkdir -p "$folder" && chown rundeck:rundeck "$folder"; done'

(I wrapped multiple commands inside sudo bash -c to run the whole loop as root in a clean and efficient way)

ls -ld rundeck-* (List all folders matching the pattern)

ls -d rundeck-* 2>/dev/null | wc -l (Count total folders created)

ls -ld rundeck-* | awk '{print $3, $4}' | sort | uniq -c (Verify folder ownership)

0 Kudos
jeesshnasree
Starfighter Starfighter
Starfighter
  • 422 Views

Hello @shashi01 ,

Thank you @shashi01  .

 

All folders showing same creation date timestamp ( Sept 1 )  , Please find attached screenshot .

folders creation date is showing current date and not getting like from Sept 1 to Aug 1 . could you please find attached screenshot. Please check & provide date time also need to change while create folders .  Please provide command for folders create time need to change time stamp also as per folders . if possible please share the command and if not possible then please ignore it . 

folder-creation.png

0 Kudos
shashi01
Moderator
Moderator
  • 400 Views

@jeesshnasree 

Thanks for sharing the screenshot and your question!

What you're seeing is normal behavior in Linux. When you create folders using mkdir, Linux sets the timestamp (shown in ls -lrt) to the current date and time, not based on the folder name. This is because Linux filesystems like ext4 and xfs track creation time (btime) internally, but do not allow users to manually set or change it.

The only timestamps that can be changed from userspace are:

Modification time (mtime)

Access time (atime)

So, even if your folders are named like rundeck-01092025, rundeck-02092025, etc., the timestamps you see are simply the moment the folder was created, in your case, all showing September 1st.

If you want the visible timestamps to match the dates in the folder names (just for clarity or for alignment with future schedule logic), you can use the touch command to set the modification time manually.

Here’s how you can do it all in a single line:

sudo bash -c "for offset in {0..30}; do folder=\"rundeck-\$(date -d \"+\$offset day\" +%d%m%Y)\"; mkdir -p \"\$folder\"; chown rundeck:rundeck \"\$folder\"; touch -d \"+\$offset day\" \"\$folder\"; done"

This command:

Creates 31 folders with future-dated names

Assigns them to the rundeck user and group

Sets the folder’s mtime to match its name (e.g., rundeck-01092025 will show a timestamp of Sep 1, 2025)

Note: This changes only the mtime, not the true btime (creation time), which Linux does not allow you to modify.

Screenshot From 2025-09-01 19-41-50.png

You can use the stat command to view btime

jeesshnasree
Starfighter Starfighter
Starfighter
  • 316 Views

Hello @shashi01 ,


Thank you for sharing . 

actually , I mentioned from Sept 1 to Aug 1  . I saw your command after modify then i got my desired output  from Sept 3 to Aug 3, Please find attached screenshot. thank you @shashi01 .Screenshot 2025-09-03 085114-before.png

jeesshnasree
Starfighter Starfighter
Starfighter
  • 316 Views

I modified your shared command then I got desired output . Thank you @shashi01 . 


command:

sudo bash -c "for offset in {-30..0}; do folder=\"rundeck-\$(date -d \"+\$offset day\" +%d%m%Y)\"; mkdir -p \"\$folder\"; chown rundeck:rundeck \"\$folder\"; touch -d \"+\$offset day\" \"\$folder\"; done"
student@my-play-ground:/tmp/rundeck/test-3$ ls -lrt
total 124
drwxr-xr-x 2 rundeck rundeck 4096 Aug 4 03:20 rundeck-04082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 5 03:20 rundeck-05082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 6 03:20 rundeck-06082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 7 03:20 rundeck-07082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 8 03:20 rundeck-08082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 9 03:20 rundeck-09082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 10 03:20 rundeck-10082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 11 03:20 rundeck-11082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 12 03:20 rundeck-12082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 13 03:20 rundeck-13082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 14 03:20 rundeck-14082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 15 03:20 rundeck-15082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 16 03:20 rundeck-16082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 17 03:20 rundeck-17082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 18 03:20 rundeck-18082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 19 03:20 rundeck-19082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 20 03:20 rundeck-20082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 21 03:20 rundeck-21082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 22 03:20 rundeck-22082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 23 03:20 rundeck-23082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 24 03:20 rundeck-24082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 25 03:20 rundeck-25082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 26 03:20 rundeck-26082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 27 03:20 rundeck-27082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 28 03:20 rundeck-28082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 29 03:20 rundeck-29082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 30 03:20 rundeck-30082025
drwxr-xr-x 2 rundeck rundeck 4096 Aug 31 03:20 rundeck-31082025
drwxr-xr-x 2 rundeck rundeck 4096 Sep 1 03:20 rundeck-01092025
drwxr-xr-x 2 rundeck rundeck 4096 Sep 2 03:20 rundeck-02092025
drwxr-xr-x 2 rundeck rundeck 4096 Sep 3 03:20 rundeck-03092025
student@my-play-ground:/tmp/rundeck/test-3$ pwd
/tmp/rundeck/test-3

jeesshnasree
Starfighter Starfighter
Starfighter
  • 760 Views

Hi @shashi01 ,

 

Thank you @shashi01 for share valuable information. 

If any command with for loop - create folders from today to more than 30 days & folder name is "YYYYMMDD". 

Example : today - 23-AUG-2025 - 20250823 
                 between 22,21,20,19,18,17,16,15,14,13,12,11...
                              23-MAY-2025 -- 20250523

How to create folders mention format from today to more than 30 days folders need to create . If possible please provide command .

Apologies to all , not mentioned clearly for create folders & format . 
if not posiible then please ignore it . 

0 Kudos
Chetan_Tiwary_
Community Manager
Community Manager
  • 855 Views

@jeesshnasree for creating the folder with old timestamp :

#mkdir <dir>

#touch -d '30 days ago' <dir>

Chetan_Tiwary__0-1755879286261.png

 

To remove folders which are older than a month and in the current path , use :

#find . -mindepth 1 -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \;

Chetan_Tiwary__1-1755879487677.png

refer man page touch : https://man7.org/linux/man-pages/man1/touch.1.html 

https://www.man7.org/linux/man-pages/man1/find.1.html 

Chetan_Tiwary__2-1755879557889.png

 

Join the discussion
You must log in to join this conversation.