renaming files in the directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers renaming files in the directory
# 1  
Old 05-09-2009
renaming files in the directory

suppose i have a few file like "a b c.txt" , "hello world.c" etc...(files that have space between them in their filenames).

how do i change their filenames to "a_b_c.txt" and "hello_world.c" respectively?

heres what i have tried...but failed
Code:
[c_d@localhost dummy]$ find -name "*" -exec mv "{}" "`echo {} | tr " " "_"`" \; #the command that i m trying
mv: `.' and `./.' are the same file
mv: `./d e f' and `./d e f' are the same file
mv: `./dummy.sh' and `./dummy.sh' are the same file
mv: `./hell owr w' and `./hell owr w' are the same file
mv: `./j' and `./j' are the same file
mv: `./k' and `./k' are the same file
mv: `./file1' and `./file1' are the same file
mv: `./2' and `./2' are the same file
mv: `./a b c' and `./a b c' are the same file
[c_d@localhost dummy]$

this is how i broke it down

find command finds a file. it then executes mv. the mv processes the code `echo <filename> | tr " " "_" ` so output of "a b c" would be transilated to "a_b_c" and this output becomes the "DEST" in "mv SOURCE DEST" command ...where SOURCE is the <filename>

why shouldnt this work?

Last edited by c_d; 05-10-2009 at 01:06 AM..
# 2  
Old 05-10-2009
in bash or ksh93:
Code:
for file in * ; do 
    mv "$file" "${file// /_}"
done

Test first by inserting an echo before the mv to verify the commands it will output, also it will attempt to move all files even if they have no spaces ( but these ones will fail) so you will be able to ignore the messages like:
Code:
mv: `./a b c' and `./a b c' are the same file

# 3  
Old 05-10-2009
i m sorry but i could not understand what you were trying to say...

i have already solved my problem with the help of sed(i m using bash ofcourse)
Code:
find . -type f | while read i;do mv "$i" "$(sed 's/ /_/g' <<< $i)" 2>/dev/null;done

however, i dont understand why my first code does not rename the files...
# 4  
Old 05-10-2009
<delete this>

Last edited by c_d; 05-10-2009 at 10:53 AM..
# 5  
Old 05-11-2009
MySQL

You have two things to be addressed over here.

1. Why does your first code does not works ?

Refer the find manual, it has the answer for your question as

Code:
-exec command {} +
.............Only one instance of '{}' is allowed within the command .....

Note: In your first code, you have used the {} two times. ( one in mv, and another in tr which is wrong ).

2. 2>/dev/null
Redirecting errors is not a good practice.

Actually you should understand the error and try to solve it. Your code is trying to rename the files which has no space in it. But your intension is to rename only the files which has space in its name. So you should try to find only the files which has spaces in it, and rename it.
My suggestion is find the file using the following, which will show only the file names which has the space in it.

Code:
find . -name "* *" -type f

There might be better solutions for finding files with spaces in it is names. If some thing is there, let us know.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files to a directory, renaming it if a file with the same name already exists

Hi All, I need to copy files from one directory to another with the files to be renamed while copying if a file with the same name already exists in the target directory. THanks, Dev (2 Replies)
Discussion started by: dev.devil.1983
2 Replies

2. Shell Programming and Scripting

Copying files from one directory to another, renaming duplicates.

Below is the script i have but i would like simplified but still do the same job. I need a script to copy files not directories or sub-directories into a existing or new directory. The files, if have the same name but different extension; for example 01.doc 01.pdf then only copy the .doc file. ... (1 Reply)
Discussion started by: Gilljambo
1 Replies

3. UNIX for Dummies Questions & Answers

Renaming files with part of their pathname and copying them to new directory

Hi I think this should be relatively simple but I can't figure it out. I have several files with the same name in different folders within a directory (the output of a program that I ran). Something like this: ./myAnalysis/item1/round1/myoutput.txt ./myAnalysis/item1/round2/myoutput.txt... (2 Replies)
Discussion started by: jullee
2 Replies

4. Shell Programming and Scripting

UNIX :renaming the files present in the directory

Hi all, I am looking for a script which renames all the files from the present directory. Eg.: In unix directory contains the below files linux001.txt linux002.txt linux003.txt ...... ....... Now the files should be renamed to unix001.txt unix002.txt unix003.txt Could anyone... (8 Replies)
Discussion started by: scriptscript
8 Replies

5. Shell Programming and Scripting

Moving and renaming multiple files in a directory

Hi. I am trying to automate the movement and renaming of a number of files in a directory. I am using the 'mv' command as I do not have access to 'rename'. I have the following scripted FILES=$(ls /transfer/move/sys/mail/20130123/) if ; then for i in ${FILES} ; do mv... (4 Replies)
Discussion started by: jimbojames
4 Replies

6. Shell Programming and Scripting

Renaming multiple files in a directory

Hello, I would like to rename all available files in a directory from Filename to Filename_Normal. I tried to use below script but it is giving some error: #!/bin/sh for i in `ls` do echo Changing $i mv $i $i_Normal done Error received: Usage: mv src target or: mv ... (10 Replies)
Discussion started by: manishdivs
10 Replies

7. Shell Programming and Scripting

Copying subdirectories of a directory to some other directory and renaming them

Hi, I am a newbie in shell scripting. I have to copy a particular sub-directory (data) from a large no. of directories (all in the same folder) and paste them to another directory ( /home/hubble/data ) and then rename all the subdirectories (data) as the name of its parent directory. please... (8 Replies)
Discussion started by: sholay
8 Replies

8. UNIX for Dummies Questions & Answers

Renaming files after their directory name in multiple sub directories

So I am not sure if this should go in the shell forum or in the beginners. It is my first time posting on these forums. I have a directory, main_dir lets say, with multiple sub directories (one_dir through onehundred_dir for example) and in each sub directory there is a test.txt. How would one... (2 Replies)
Discussion started by: robotsbite
2 Replies

9. Shell Programming and Scripting

Renaming files as per directory

I have many files with duplicate names spread out over several tens of directories. I would like to mv them to the parent directory, but to avoid conflicting filenames I'd like to prefix each filename with the name of the directory it was in. For example, if this is my directory structure:... (2 Replies)
Discussion started by: dotancohen
2 Replies

10. Shell Programming and Scripting

renaming files in a directory to lowercase

can anybody help me in renaming all the file in a directory to lowercase? script will be helpful. (1 Reply)
Discussion started by: vhariprasad
1 Replies
Login or Register to Ask a Question