Fetch the last two days directory names and rename them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch the last two days directory names and rename them
# 1  
Old 09-30-2014
Question 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.

Code:
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')

Code:
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.
# 2  
Old 09-30-2014
Given all relevant directories only have alphanumeric characters in their names you might try this:
Code:
find * -maxdepth 1 -mtime -2 | xargs -I{} echo mv {} 0{}

Once happy with the result, remove the echo.
These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 09-30-2014
Hello,

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

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

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-30-2014 at 09:22 AM.. Reason: Added type for directory
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 09-30-2014
Or even
Code:
find * -maxdepth 1 -mtime -2 -exec echo mv {} "0"{} \;

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to fetch matched records from files between two different directory?

awk 'NR==FNR{arr;next} $0 in arr' /tmp/Data_mismatch.sh /prd/HK/ACCTCARD_20160115.txt edit by bakunin: seems that one CODE-tag got lost somewhere. i corrected that, but please check your posts more carefully. Thank you. (5 Replies)
Discussion started by: suresh_target
5 Replies

2. Shell Programming and Scripting

How to rename multiple file names?

Hi all, I need to rename more file name in one command or script. The files have this structure: XxY - filename.doc where X and Y are numbers and the x is the letter itself. I need to rename these files with this structure: string.S0XEY.filename.doc the string is a suffix that... (8 Replies)
Discussion started by: idro
8 Replies

3. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

4. UNIX for Dummies Questions & Answers

Which Unix & Linux Certifications Names In This Days ??

Hello All My Questions About Which Unix & Linux Certifications Names In This Days ? I Mean Red Hat Certifications , Solaris Certifications , IBM Certifications , HP Certifications and BSD Certifications just i need the names of these Certifications and How can i enter to Certifications... (1 Reply)
Discussion started by: Eng/G.Mohammad
1 Replies

5. UNIX for Dummies Questions & Answers

Loop through directory and extract sub directory names

I am trying to loop through folders and extract the name of the lowest level subfolder I was running the script below, it returns /bb/bin/prd/newyork /bb/bin/prd/london /bb/bin/prd/tokyo I really want newyork london tokyo I couldn't find a standard variable for the lowest level... (1 Reply)
Discussion started by: personalt
1 Replies

6. Shell Programming and Scripting

Rename multiple file names in a directory

I hope some one can help me I have multiple files in a directory with out extension like as below mentioned. But i want to change all the file names along .DDMMYYYYHHMISS format. And all files should have same DDMMYYYYHHMISS. Scenario: direcory name = /vol/best/srcfiles files in a... (4 Replies)
Discussion started by: hari001
4 Replies

7. UNIX for Dummies Questions & Answers

How to display only Owner and directory/sub directory names under particular root

hai, I am new to Unix, I have a requirement to display owner name , directory or sub directory name, who's owner name is not equal to "oasitqtc". (here "oasitqtc" is the owner of the directory or sub directory.) i have a command (below) which will display all folders and sub folders, but i... (6 Replies)
Discussion started by: gagan4599
6 Replies

8. Shell Programming and Scripting

Find and rename long file names (html)

Hi Guys, I need a help. I have 1130 zip files. Each one of them has files including 1 html file with long file name (includes special charactors, Alphabetic and numbers). I have copied all 1130 zip files to my linux system and extracted using below command. Find . -name "*.zip" -exec... (7 Replies)
Discussion started by: Rajmani
7 Replies

9. UNIX for Advanced & Expert Users

fetch dates for last 36 days in format yyyy-mm-dd

can anyone please suggest me some ideas for writing a korn shell script which will go back to 36 days from current day and print each day (incremented by 1) in the format yyyy-mm-dd until the current day. Thanks Mark (7 Replies)
Discussion started by: pavan_test
7 Replies

10. Shell Programming and Scripting

ls latest 4 days or specify days of files in the directory

Hi, I would like to list latest 2 days, 3 days or 4 days,etc of files in the directory... how? is it using ls? (3 Replies)
Discussion started by: happyv
3 Replies
Login or Register to Ask a Question