Changing cases of multiple files in different folders


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Changing cases of multiple files in different folders
# 8  
Old 11-02-2014
junior, your code works great
aia, thank you for your help too!
# 9  
Old 11-02-2014
Quote:
Originally Posted by bpmawhin
the only problem with that is that it also changes my folder names to lower case, which makes it a pathname that doesn't exist, I can change the folder name to be lowercase If there is no other option, but is there a way around that?
I used
Code:
find $download_dir -iname "$filename" | perl -lne 'print "mv -v $_ ", lc $_' | sh

That explains why the first command with rename failed for you.
Code:
find $download_dir -iname "$filename" | perl -lne '($f = $_) =~ s/\/(\w|\.)+$/\L$&/ and print "$_ $f"'

Code:
find $download_dir -iname "$filename"| perl -lne '($f = $_) =~ s/\/(\w|\.)+$/\L$&/ and rename $_, $f'

---------- Post updated at 07:38 PM ---------- Previous update was at 07:35 PM ----------

This will not try to rename if the file has not changed:

Code:
find $download_dir -iname "$filename" | while read f; do   
    dir_path=${f%/*}
    filenode=${f##*/}
    lower=$(echo "$filenode" | tr '[[:upper:]]' '[[:lower:]]')

    if [[ $lower != $filenode ]]; then
       #remove the echo and surrounding "" if you see what you want
        echo "mv -v $f ${dir_path}/${lower}"
    fi
done


Last edited by Aia; 11-02-2014 at 11:20 PM.. Reason: sustituing the for loop for a while loop
# 10  
Old 11-02-2014
In cases where your students named their files correctly, the standards allow the command:
Code:
mv -v path path

to successfully do nothing or to issue a diagnostic and exit with a non-zero exit status. So, the code suggested by junior-helper will do what you want, but may report an error for each correctly named file.

You could also try the following:
Code:
find $download_dir -iname "$filename" | awk '
BEGIN {	FS = OFS = "/" }
{	l = tolower($NF)
	if(l != $NF) {
		orig = $0
		$NF = l
		printf("mv -v \"%s\" \"%s\"\n", orig, $0)
	}
}' | sh

Instead of invoking tr and mv for each pathname processed, it invokes mv once for each incorrectly named pathname and awk once.

You haven't said what OS you're using. If you're using a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
# 11  
Old 11-03-2014
Quote:
Originally Posted by bpmawhin
the only problem with that is that it also changes my folder names to lower case, which makes it a pathname that doesn't exist, I can change the folder name to be lowercase If there is no other option, but is there a way around that?
I used
Code:
find $download_dir -iname "$filename" | perl -lne 'print "mv -v $_ ", lc $_' | sh

You can exclude the directories with find option -type f
# 12  
Old 11-03-2014
Quote:
Originally Posted by MadeInGermany
You can exclude the directories with find option -type f
Yes, but the mv commands like:
Code:
mv /a/B/C/Exam.f95 /a/b/c/exam.f95

will still fail. What is wanted for pathnames like this is:
Code:
mv /a/B/C/Exam.f95 /a/B/C/exam.f95

This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 11-03-2014
I see the problem now.
Here is a portable (old style Unix) script:
Code:
find . -type f -name '*.[Ff]95' -name '*[A-Z]*' |
while read f
do
 (
  cd `dirname "$f"` &&
  mv `basename "$f"` `basename "$f" | tr '[A-Z]' '[a-z]'`
 )
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute multiple files in multiple folders and also output on same folder

How to execute multiple files in multiple folders and also output to be generated in the same folder? Hello Team, I have a path like Sanity_test/*/* and it has around 100+ folders inside with files. I would like to run/execute those files and output of execution to be placed on same /... (1 Reply)
Discussion started by: pushpabuzz
1 Replies

2. UNIX for Dummies Questions & Answers

Unzipping a file which has multiple folders and each folder has the files with same name in it

Hi, I have a zipped file a.zip. This has got multiple folders in it say x and y. x contains a.txt and y contains a.txt. Is it possible to unzip this file and have the 2 files extracted and rename them to unique names. Thanks in advance. (1 Reply)
Discussion started by: arunkesi
1 Replies

3. Shell Programming and Scripting

Script to move files in multiple folders

Hello all, I would appreciate any help to write a script. I have folder A which contains over 30 thousands xml files, I would like create multiple folders and move those files (500 in each folders). Thank you (1 Reply)
Discussion started by: mmsiddig
1 Replies

4. Shell Programming and Scripting

Shell Script to delete files within a particular time frame under multiple sub folders

Greetings! I'm looking for starting information for a shell script. Here's my scenario: I have multiple folders(100) for example: /www/test/applications/app1/logs /www/test/applications/app2/logs Within these folders there are log files files that need to be deleted after a month. ... (3 Replies)
Discussion started by: whysolucky
3 Replies

5. Shell Programming and Scripting

Find files of type within folder copy multiple results to different folders

Ok question number two: I'd like to search a directory for multiple file types (rar, txt, deb) and depending on what's found, copy those files to folders named Rar, TextFiles, and Debs. I'm looking for speed here so the faster the script the better. I want it to be a function that I pass 1 argument... (4 Replies)
Discussion started by: DC Slick
4 Replies

6. UNIX for Dummies Questions & Answers

Rename files to indexnumber in multiple folders

I have multiple subfolders with multiple jpg I want the group of files in each subfolder renamed to their index number (3 digits) in the folder, so: folder/a.jpg = folder/001.jpg folder/b.jpg = folder/002.jpg folder/c.jpg = folder/003.jpg folder2/1.jpg = folder2/001.jpg folder2/2.jpg =... (2 Replies)
Discussion started by: cmanniche
2 Replies

7. UNIX for Dummies Questions & Answers

mv folders/files without changing modified date?

Hi all, I'm using Red Hat Linux and want to move some folders and files around but not change the modified date. Is this possible? I know cp has a -p flag which seems to do what I want, but this is a large volume of data so copying and deleting would not be feasible. (13 Replies)
Discussion started by: Annorax
13 Replies

8. UNIX for Dummies Questions & Answers

changing extensions for multiple files at once

I copied some files to another folder, and I want to change them from .doc extensions to .txt extensions. I tried using the cp and mv commands, but it didn't work. Is it possible to change file extensions with these commands, and if so how do I do it? I tried using the * wildcard (say cp *.doc... (1 Reply)
Discussion started by: Straitsfan
1 Replies

9. Shell Programming and Scripting

moving multiple folders/files in subversion using bash script

Hi, I'm new here an dlearning a lot from this forum. i didnt find any solution for this in the forum. I have already checked in folders in subversion named HTT01,... HTT21.. and have files in each folder like below: HTT01/HTT01_00000.hex HTT01/HTT01_00000_fb_result.hex... (2 Replies)
Discussion started by: ravishan21
2 Replies

10. Shell Programming and Scripting

Find and replace files in multiple folders

Hi there, I would like to write a script to automate the copy and renaming of files in multiple dir. I have a generic file named s253e.prb and would like to copy this to multiple dir and rename it. Example: Dir is AL-M1 and the prb file name is AL-M1.prb. I would like to be able to... (6 Replies)
Discussion started by: lodey
6 Replies
Login or Register to Ask a Question