Add extention to files recursively.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add extention to files recursively.
# 1  
Old 10-16-2009
Add extention to files recursively.

I want to add a .txt extension to all files in a directory and it's sub directories.

So far I have managed to come up with this for the current directory but how do I do this recursively?
Code:
for e in *; do mv "$e" "`echo $e | sed -e 's/\ /_/g'`.txt"; done

Thanks

Last edited by pludi; 10-16-2009 at 03:05 AM.. Reason: code tags please...
# 2  
Old 10-16-2009
Code:
find . -type f -exec echo mv {} {}.txt \;

# 3  
Old 10-16-2009
thanks, and to think there I was breaking my head with sed hehe
# 4  
Old 10-19-2009
Code:
function fun
{
for i in $1/*;do
  if [ -d $i ];then
  fun $i
  else
    mv $i ${i}.bak
  fi
done
}
fun .

# 5  
Old 10-19-2009
Quote:
Originally Posted by summer_cherry
Code:
function fun
{
for i in $1/*;do
  if [ -d $i ];then
  fun $i
  else
    mv $i ${i}.bak
  fi
done
}
fun .


That will fail if any names contain spaces, and that function definition syntax will only work in bash and ksh.

Code:
fun()
 for i in "$1"/*;do
    if [ -d "$i" ];then
      fun "$i"
    else
      mv "$i" "$i.bak"
    fi
 done

fun .


Last edited by cfajohnson; 10-19-2009 at 08:44 AM.. Reason: Fixed (cosmetic) indentation
# 6  
Old 10-19-2009
That's really nice, I love a drop of recursion in my code Smilie

The lack of indentation on fun "$i", is that a carry over of a typo or some convention regarding recursive calls I am unaware of?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find -ctime -1 cannot find files without extention

The problem is this one. I tar and gzip files on remote server find . -ctime -1 | tar -cvf transfer_dmz_start_daily.tar *${Today}*.*; Command find . -ctime -1 Doesn't find files without extension .csv .txt I have to collect all files for current day, when the program... (1 Reply)
Discussion started by: digioleg54
1 Replies

2. Shell Programming and Scripting

find -ctime -1 cannot find files without extention

The problem is this one. I tar and gzip files on remote server Code: find . -ctime -1 | tar -cvf transfer_dmz_start_daily.tar *${Today}*.*; Command Code: find . -ctime -1 Doesn't find files without extension Code: .csv .txt I have to collect all files for current... (1 Reply)
Discussion started by: digioleg54
1 Replies

3. Shell Programming and Scripting

add a extention to all files

I'm working on implementing a mail indexer and it currently saves every file with a series of numbers with no extension. ex. 021545545544 I was wondering if someone could point me in the correct direction for a script that would check all the files and give it a .eml extension if it doesn't... (1 Reply)
Discussion started by: binary-ninja
1 Replies

4. Shell Programming and Scripting

Recursively rename some files

Hello, I have one directory with 3 level sub-directories, and about houndard files under those directories. I need a shell script to rename all patern mateched directories and files. For example: the patern is AA in the directory or file name. Orignal directory:... (2 Replies)
Discussion started by: mail4mz
2 Replies

5. Shell Programming and Scripting

Recursively move directories along with files/specific files

I would like to transfer all files ending with .log from /tmp and to /tmp/archive (using find ) The directory structure looks like :- /tmp a.log b.log c.log /abcd d.log e.log When I tried the following command , it movies all the log files... (8 Replies)
Discussion started by: frintocf
8 Replies

6. Shell Programming and Scripting

Rename files recursively

hi I have files named 123_234_aaa.jpg 234_231_345.jpg and i wish to rename these files to aaa.jpg and 345.jpg. i.e inital number,_,next number should be removed form the file name. Please let me know how can i do it. (2 Replies)
Discussion started by: vasuarjula
2 Replies

7. Shell Programming and Scripting

Listing files in a given directory with given extention

for some reason my code does not give the right number of files. can omeone help me please? (2 Replies)
Discussion started by: andrew1400
2 Replies

8. Shell Programming and Scripting

Copy only files recursively

Hi, find . | xargs -s 47518 can list all the files and directories recursively , is there any possibility to copy only files from directories and subdirectoreis once it is listed. Please help Thans & Regards Uma (3 Replies)
Discussion started by: umapearl
3 Replies

9. UNIX for Advanced & Expert Users

Search files with specfic extention and later search content

Hi, I would appriciate if somebody can help me figure out how to search for all the *.xml file under a specific directory and subdirectroies (/home/username) and later search of content "<start>" inside the xml file returned by search. -Lovin.V (2 Replies)
Discussion started by: lovi_v
2 Replies

10. UNIX for Dummies Questions & Answers

List Files Recursively

Hi! I'd like to list my files recursively BUT: I want them in this format, so that I can use them as options for commands like ftp->put or del ./directory1/file1.tar ./directory1/file2.tar ./directory1/file3.tar ./directory2/file1.tar ./directory2/file2.tar ./directory2/file3.tar... (9 Replies)
Discussion started by: roberthawke
9 Replies
Login or Register to Ask a Question