Fetch the last two days directory names and rename them

Hi All,

I have below directory structure and from these directories, I would like to fetch the last two days list of directories and append a '0' (zero) to those directories.

bash-4.1$ ls -lrt
total 32
drwxr-xr-x+ 6 root root 9 Sep  5 01:05 tested-597
drwxr-xr-x+ 6 root root 9 Sep  9 01:04 tested-605
drwxr-xr-x+ 6 root root 9 Sep  9 13:34 tested-606
drwxr-xr-x+ 6 root root 9 Sep 10 13:35 tested-608
drwxr-xr-x+ 6 root root 9 Sep 11 13:47 tested-614
drwxr-xr-x+ 4 root root 4 Sep 22 16:13 647
drwxr-xr-x+ 4 root root 4 Sep 23 00:49 650
drwxr-xr-x+ 6 root root 9 Sep 23 15:19 tested-652
drwxr-xr-x+ 8 root root 8 Sep 24 11:52 659
drwxr-xr-x+ 8 root root 8 Sep 24 13:12 660
drwxr-xr-x+ 8 root root 8 Sep 24 23:26 661
drwxr-xr-x+ 8 root root 8 Sep 25 02:59 664
drwxr-xr-x+ 8 root root 8 Sep 25 13:58 tested-665
drwxr-xr-x+ 8 root root 8 Sep 26 01:17 666
drwxr-xr-x+ 8 root root 8 Sep 26 13:52 667
drwxr-xr-x+ 8 root root 8 Sep 27 01:19 668
drwxr-xr-x+ 8 root root 8 Sep 27 13:40 669
drwxr-xr-x+ 8 root root 8 Sep 28 01:12 670
drwxr-xr-x+ 8 root root 8 Sep 28 13:36 671
drwxr-xr-x+ 8 root root 8 Sep 29 01:12 672
drwxr-xr-x+ 8 root root 8 Sep 29 13:45 673
bash-4.1$
bash-4.1$
bash-4.1$
bash-4.1$ find . -maxdepth 1 -mtime -2
.
./673
./672
./671
bash-4.1$

With the below find command, I am able to fetch the last two days list of directories but unable to combine this with 'mv' command in a shell script (what ever the output from find cmd, those dir should be appended with a '0')

find . -maxdepth 1 -mtime -2

For e.g. from above list, I am able to fetch the last two days directory names 671, 672, 673 and I would like rename these dir as 0671, 0672, 0673.

Any advise is much appreciated.

Given all relevant directories only have alphanumeric characters in their names you might try this:

find * -maxdepth 1 -mtime -2 | xargs -I{} echo mv {} 0{}

Once happy with the result, remove the echo .

2 Likes

Hello,

Following may help also, remove echo after cross checking the output, if output is correct.

find * -maxdepth 1 -type d -mtime -2 -exec bash -c 'echo mv $0 "0"${0}' {} \;

Thanks,
R. Singh

1 Like

Or even

find * -maxdepth 1 -mtime -2 -exec echo mv {} "0"{} \;
1 Like