bash script to rename multiple directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash script to rename multiple directories
# 1  
Old 11-21-2009
bash script to rename multiple directories

Hello

I have a directory structure with year in format 4 digits, e.g 2009, below which is month format 1 or 2 digits, e.g 1 or 12, blow which is day format 1 or 2 digits, e.g 1 or 31.

I want to change the names of lots of directories to the be
Year - 4 digits , e.g 2009 - No change here
Month 4 digi year, underscore, 2 digit month - e.g 2009_10 for October 2009
Day 4 digi year, underscore, 2 digit month, underscore, 2 digit day - e.g 2009_10_31 for 31st October 2009

Some files already have the new naming structure so there must be a check before renaming somehow. there are files within the day directories which should be unchanged.

So far I thought to use the find -d command to check for directories and check for directories named with only a single character and place a zero at the start of the name.

How do I check how many characters are in the directory name?

And after that I got kind of lost......any bored script writers out there?

Thanks in advance people
# 2  
Old 11-21-2009
Show some sample input, and expected output, as there is some confusions in your explanation.
# 3  
Old 11-21-2009
I'm assuming you have something like this:

Code:
2009  # year dir
    # month dir
  -  1
  -  2
        # days
      - 01  # already partly correct
      - 02
      - 3
      - 2009_02_04  #fully correct
      ...
  -  3
  - 2009_04
   -05

If you can assume that anything with a "_" is already fully correct, you don't have to test for length.

I'd do this in Perl. Something like

Code:
#!/usr/bin/perl
foreach(<200*>) {
  $year=$_;
  opendir(YEAR,$year);
  while(defined($month=readdir(YEAR))) {
     next if $month =~ /^\./;
     next if $month =~ /_/;
     opendir(MONTH, "$year/$month") ;
     while(defined($day=readdir(MONTH))) {
        next if $day =~ /^\./;
        next if $day =~ /_/;
        $newname=sprintf("%d_%0.2d_%0.2d",$year,$month,$day);
        print " --  $year $month $day is $year/$month/$newname\n"; 
        # rename "$year/$month/$day", "$year/$month/$newname";
     }
     
     $newname=sprintf("%d_%0.2d",$year,$month);
     print "$year $month is $year/$newname\n"; 
     # rename "$year/$month", "$year/$newname";
    
  }
}

I wrote this to be obvious rather than clever. If I've misunderstood your directory structure, of course this won't be right, but you can modify it.

When you are satisfied this will do what you want, uncomment the rename lines.
# 4  
Old 11-21-2009
Tony

The perl script you wrote does almost exactly as wanted, it's just the rename command that does not work with no error message.

Can't seem to see why, I've opened up permissions etc but no luck.

Any ideas?

Thanks
# 5  
Old 11-23-2009
Done

Anyone interested here is my script.

#!/bin/bash
cd ~/tst2
## set variable year_dir as first year
year_dir=2007

## checks dir exists and is a directory and into FIRST LOOP
while [ -d "$year_dir" ] && [ "$year_dir" -le 2010 ]
do
cd $year_dir
YEAR=`basename $PWD` ;echo "YEAR IS "$YEAR
## list all directories and into SECOND LOOP
for month_dir in `ls -d *`
do
cd $month_dir
MONTH=`basename $PWD | awk '{printf "%02i",substr($0,length($0)-1)}'` ;echo "MONTH IS "$MONTH
## lists all single char directories,i.e. 1 to 9 and into THIRD LOOP
for day_dir_1char in `ls -d [1-9]`
do
echo "mv "$day_dir_1char" 0"$day_dir_1char
mv $day_dir_1char 0$day_dir_1char
done
## exit THIRD LOOP
## list all two char dirs and rename all days (all in 2 char format). in to FOURTH LOOP
for day_dir_2char in `ls -d [0-3][0-9]`
do
echo "mv $day_dir_2char to "$YEAR"_"$MONTH"_"$day_dir_2char
mv $day_dir_2char $YEAR\_$MONTH\_$day_dir_2char
done
## exit FOUTH LOOP
cd ..
echo "mv $month_dir to "$YEAR"_"$MONTH
mv $month_dir $YEAR\_$MONTH
# echo "cmv "$month_dir $YEAR"_"$MONTH
# DAY=`basename $PWD`; echo "DAY IS "$DAY
done
let year_dir=$year_dir+1
cd ..
done
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files from multiple directories along with directory indicator

Hi, Friends, i have a requirement where i need to rename my files residing in multiple sub directories and move them to one different directory along with some kind of directory indicator. For eg: test--is my parent directory and it has many files such as a1.txt a2.txt a3.txt ... (5 Replies)
Discussion started by: gnnsprapa
5 Replies

2. Shell Programming and Scripting

Merge values from multiple directories into one file in awk or bash

I am trying to merge or combine all $1 values in validation.txt from multiple directories into one new file and output it here tab-delimited:/home/cmccabe/Desktop/20x/total/total.txt. Each $2 value and the header would then be a new field in total.txt. I am not sure how to go about this as cat is... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. UNIX for Dummies Questions & Answers

Rename multiple files in shell bash, changing elements order.

Hi, I want to rename several files like this: example: A0805120817.BHN A0805120818.BHN ..... to: 20120817.0805.N 20120818.0805.N ...... How can i do this via terminal or in shell bash script ? thanks, (6 Replies)
Discussion started by: pintolcv
6 Replies

4. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

5. UNIX for Dummies Questions & Answers

Script to rename directories

I am trying to create a script that will search for a directory named bob, and then rename that directory to peter. I do not want the directory path to change. so /folders/bob would become /folders/peter It seems to be renaming the bob folder to peter correctly, but the peter folder ends... (2 Replies)
Discussion started by: gumby456m
2 Replies

6. UNIX for Dummies Questions & Answers

Bash script to rename files in a directory

Dear friends, I have created a script to rename all files in a directory by appending the file name with username (who created the file), the date it was created. For example, "apple.doc" should be renamed to "johnFeb23apple.doc" where "john" is the owner and "Feb23" is file created date. It... (4 Replies)
Discussion started by: djsnifer
4 Replies

7. UNIX for Dummies Questions & Answers

Bash script to rename all files within a folder...

Hi. I don't have any experience with making scripts in bash. I need a simple script to rename all files in a folder to the format file1.avi, file2.avi, file3.avi, and so on..... Please note that the original files have different filenames and different extensions. But they all need to be... (2 Replies)
Discussion started by: dranzer
2 Replies

8. Shell Programming and Scripting

bash script to rename in mass

Basically, I have a huge amount of files (ripped audiobooks) that all have the same garbage in their filenames. I'm wondering how to go about writing a bash script to mass rename them. Example filenames as they stand now: The First CD - 1x01 - Title 1.mp3 The First CD - 1x02 - Title 2.mp3... (4 Replies)
Discussion started by: audiophile
4 Replies

9. Shell Programming and Scripting

Hello - new here - bash script - need to rename and zip files.

I'm working on a project that basically unzips three zip files. When these unzip they create about 70+ directories with subdirectories of year/month with about 3 to 9 pdf files in each directory. Basically, I'm needing to figure out a way to zip these pdf files up. for instance the script... (1 Reply)
Discussion started by: Aixia
1 Replies
Login or Register to Ask a Question