file name shortening for multiple directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file name shortening for multiple directories
# 1  
Old 08-24-2012
file name shortening for multiple directories

there was a post previously about this from around 2010
but i was unable to get the suggested scripts there to work.

the following code works for me when it's saved inside the
directory of files whose names i want to shorten, but i would
like to be able to store it in a file with a list of
directories and have it enter into each directory, shorten the
file names in that directory, and then move to the next
directory.

basically enter directory1 and perform the code below,
move to directory2 and do it again, then directory3, etc. etc.

thanks in advance for any help!

Code:
rename_seq () 
{   
seq=1   
oldname="$1"   
newname="$2"   
ext="$3"   
while [ -e "$newname$(printf "%02d" $seq).$ext" ]; do     
(( seq ++ ))   
done   
mv "$oldname.$ext" "$newname$(printf "%02d" $seq).$ext" 
}  

ls | while read name; do   
ext="${name##*.}"   
name="${name%.*}"   
newname="${name//[<>=?:;\"*+,|]/_}"   
newname="${newname:0:38}"   
if [ -e "${newname}.$ext" ]; then     
rename_seq "$newname" "$newname" "$ext"   
fi   
if [ -e "${newname}"??".$ext" ]; then     
rename_seq "$name" "$newname" "$ext"   
else     mv "$name.$ext" "$newname.$ext"   
fi 
done


Last edited by methyl; 08-24-2012 at 09:08 PM.. Reason: please use code tags for code and data.
# 2  
Old 08-24-2012
What Operating System and version are you running?
Which Shell is this?

I can't follow your code because it is much too complex.
Idea:
Code:
for dir in "directory1" "directory2" "directory3"
do
       cd "${dir}"
       # Rest of the code
done


Or:
Code:
cat list_of_directories.txt | while read dir
       cd "${dir}"
       # Rest of the code
done


Last edited by methyl; 08-24-2012 at 09:25 PM..
This User Gave Thanks to methyl For This Post:
# 3  
Old 08-24-2012
hi! thanks for the fast response.
the first example you gave wouldn't work because i need to shorten the file names in about 1500 different directories. hence i would like a program that can start at the first directory perform the code. move to the next directory, perform the code. move to the next, perform the code, and so on until it gets to the end of the list of directories.
in the second example you gave i'm not sure what "list_of_directories.txt" means or where i would find that. i'm very new to this whole bash thing so if you don't mind giving explanations that would be great!
as far as the code that i gave before (copied from an earlier thread), all it does is takes all of the files in a directory in which it is saved and shortens them to 30 characters and adds a 1 or 2 if some of the files have the same name after they've been shortened. so my end goal is to apply that code to files in many different directories so i don't have to go into each of the 1500 directories i have and run the code. thanks again for any help! hopefully that made sense?
# 4  
Old 08-24-2012
how do you know the 1500 directories. That is the "list_of_dir"

I agree with methyl, your code looks like someone who can program but does not know shell. FORTRAN comes to mind.

Assume the directories you want to mess with are under two main directories. Assume they are /data1 /old_data1. you have many and we will assume those directories lie in a series of subdirectories under the two main ones. I will show you to expand them.
file name matching (expansion) looks like this to find sub directories /directory/*.
This gets everything, all files, including directories

This gets JUST directories, ~/ is shorthand in bash for your login directory.
Code:
find /data1/  /old_data1/ -type d > ~/list_of_dir.txt

Now you have a list of directories. Assuming you think your code works correctly
Code:

while read dirname
do
  cd $dirname 
  ls | while read name
  do   
    ext="${name##*.}"   
    name="${name%.*}"   
    newname="${name//[<>=?:;\"*+,|]/_}"   
    newname="${newname:0:38}"   
    if [ -e "${newname}.$ext" ]; then     
      rename_seq "$newname" "$newname" "$ext"   
    fi   
    if [ -e "${newname}"??".$ext" ]; then     
       rename_seq "$name" "$newname" "$ext"   
    else     
         mv "$name.$ext" "$newname.$ext"   
    fi 
done  < ~/list_of_dir.txt

I added the code in red. Change the find command to include your directories.
If you already have a list, use that instead.

Just like in other languages, indentation helps readability....

Also modify whatever script created those directory atrocities to start with. Why? UNIX filesystems have limits to the length of filenames and they have length limits to the entire path: [/directorynames..../.../../../../,../loooongfilename]. If you can show us the output of
Code:
uname -a

Also a caveat - lots of directories using lots of files use LOTS and LOTS of inodes. Each entry in a directory plus the directory name use an inode. You hit the limit you can't do much with the disk.... You run out of them, you lose. You have to be proactive. Really large directory files are a bad sign.
Code:
ls -ld directory_name

will show you how large a directory is. When you have directories in the MB range it is time to think about it long and hard.

Last edited by jim mcnamara; 08-24-2012 at 11:34 PM..
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 08-27-2012
thank you!!!!! you have made my life about a million times easier. happy monday. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Command to Search a FILE for a STRING in multiple DIRECTORIES

Hi, Can someone please help me with a Command to Search a FILE for a STRING in multiple DIRECTORIES. I am searching for the VIP in HTTPD.CONF in multiple httpd instances. I am using find ./ -name "httpd.conf" | xargs grep 10.22.0.141 cut -d: -f3- | cut -d ' ' -f4 | sort | uniq -c ... (1 Reply)
Discussion started by: crosairs
1 Replies

3. Shell Programming and Scripting

Help with create multiple directories under diff file systems

Hi, Need help ...I want to create multiple directories in different /file systems using for loop..eg.../ORCL_data01/oradata/orcl/ctl. ../ORCL_data01/oradata/orcl/data. ../ORCL_data01/oradata/orcl/redo. Script :- ========= for dir in `ls -d... (8 Replies)
Discussion started by: Linux6.5
8 Replies

4. Shell Programming and Scripting

Reading multiple directories and file from them

Hello I'm making script for Dallas temperature sensors (DS1820). When a sensor is connected, it shows up as a directory in /sys/bus/w1/devices in format 10-xxxxxxx. Inside the directory is a file called w1_slave which holds the temperature in format t=xxxxx. Each sensor has unique... (2 Replies)
Discussion started by: Klipeti
2 Replies

5. UNIX for Dummies Questions & Answers

copy file using unix in multiple directories

Hi All Genious, I want to copy a file name XYZ .In a directory /HOME/dir/IXOS1/dir1 which contain multiple directories named not in pattern want to copy the XYZ in all of the directories available on path /HOME/dir/IXOS1/dir1 . Thanks in advance . (2 Replies)
Discussion started by: mumakhij
2 Replies

6. UNIX for Dummies Questions & Answers

Deleting multiple directories inside multiple directories

Hi, Very unfamiliar with unix/linux stuff. Our admin is on vacation so, need help very quickly. I have directories (eg 40001, 40002, etc) that each have one subdirectory (01). Each subdir 01 has multiple subdirs (001, 002, 003, etc). They are same in each dir. I need to keep the top and... (7 Replies)
Discussion started by: kkouraus1
7 Replies

7. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

8. Shell Programming and Scripting

read list of filenames from text file and remove these files in multiple directories

I have a large list of filenames from an Excel sheet, which I then translate into a simple text file. I'd like to use this list, which contains various file extensions , to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like... (5 Replies)
Discussion started by: fxvisions
5 Replies

9. UNIX for Dummies Questions & Answers

Copy single file to multiple directories

Please help - I need to copy a single file to multiple directories. Dir structure: Parent_Directoy Filename1 Child_Directory1 Child_Directory2 Child_Directory3 Child_Directory4 .... So I need to copy Filename1 to all of the... (2 Replies)
Discussion started by: kthatch
2 Replies

10. Shell Programming and Scripting

moving directories to new directories on multiple servers

Hi - I am new to unix scripts...I need to move several directories on multiple servers to new directories. (0 Replies)
Discussion started by: mackdaddy07
0 Replies
Login or Register to Ask a Question