rename file with whitespace embedded


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting rename file with whitespace embedded
# 1  
Old 02-13-2006
a)searching for then b)removing spaces from filenames(from " " to "_")

Say a directory contains files
1) "file name 1.xxx"
2) "file name2.yyy"
3) etc

Using a cshell script, is there a way to (1)search for all files that contain " " in the filename and then (2)rename the files so that you replace the whiitespace " " with a "_".

First problem I run into is when I put "file name 1.xxx" into a list contianing other filenames with spaces each word seperated by a space is treated as a seperate element when used in a foreach loop. Instead of getting "file name 1.xxx" as an element I get 1) "file" 2) "name" 3) "1.xxx".

Example:

set list = `find -type d -name '* *' | tr -d "./"`
foreach i ( $list )
set newfile = `echo $file | sed 's/ /_/' `
mv $i $newfile
end

Second problem, let's assume the $i contained the directory/file name string "file name 1.xxx". If we were to use $i in a mv statement such as

mv $i $newfile

the mv would interpret "file name 1.xxx" as three seperate files 1) "file" 2) "name" 3) "1.xxx" . mv would attempt to mv 3 files into $newfile. Of course this is not what we want. We would like to rename the file named "file name 1.xxx" to a newfile name.

mv "file name 1.xxx" to file_name_1.xxx

It is easy enough at the command line. But it becomes a tedious task when you have many files/directories that you have to rename. It would be nice to do it with a script but I am at a lost. It kills me that I am actually considering doing this by hand. What a waste of time. Spaces in file/directory names are a _itch.

Thanks
O

Last edited by orlando47; 02-14-2006 at 10:58 AM.. Reason: makeing title clearer
# 2  
Old 02-14-2006
Answer for the first part

% find . -name *' '*
./ashish patil
# 3  
Old 02-14-2006
The task can be completed at the command line. The trick is getting it in a script.

Is this an impossible task for a script?

Last edited by orlando47; 02-14-2006 at 11:01 AM..
# 4  
Old 02-14-2006
here is oneliner

bash> for i in `find . -name '* *' | tr -d './' | tr ' ' '~'`; do mv "$(echo $i| tr '~' ' ')" "$(echo $i | tr '~' '_')"; done

assuming there is no "~" in the filenames
# 5  
Old 02-14-2006
Darn.

I guess I will have to use a bash shell script. Yup there are no "~" in the file names.

If ANYBODY has a solution for a CShell script that would be great.

I will look at the solution to see if there is any way I can incorporate that into a cshell script.

FYI: I am using Cygwin under windows running the tcsh shell. Any cshell solution would be great.


Thanks
O
# 6  
Old 02-14-2006
i haven't actively used csh for over a decade, but this MAY be of SOME help... hopefully...


in csh... you maybe able to try this (and this is ASSUMING you're looking in the CURRENT DIRECTORY ):

foreach filename (*)
echo $filename|tr ' ' '_'
end



it's lame... but it MAY be insight on where you may wanna start. now... this will only DISPLAY the changed name... you'll need to insert your own "mv" command where appropriate.

also note that this is very generic and will not traverse down the directory tree. oh... and be a little careful since this will process DIRECTORIES as well as files.
# 7  
Old 02-15-2006
OK, folks here it is...

I GIVE UP. Replacing ' ' with '_' in file/directory names is impossible/very messy using a CSHELL script. The spaces in file/directory names is a killer.

I decided to take the next career change. I will go over to my boss and tell him I quit. I am incapable of creating a cshell script that will find and rename files with spaces in them. I am useless. I have poured over numerous websites, books and interrogated colleagues. I will go to McDonalds and fill out an application. Good Bye!
.
.
.
.
.
.
.

Just kidding, but I was getting to feel this way. This issue is like using a tool like a wrench to turn a phillips head screw. I have played around with Perl but have not really made much use of it. I hear how powerful it is but yet is very cryptic. I have put of learning perl long enough. This issue has opened my eyes. But what a headache. Yes, I was able to create a script that searches for and renames files/directories with ' '.

O
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Putting two perl scripts together... triming whitespace off of recently parsed file

Thanks to people's help, I have composed a single line within a .sh script that Ports a file into a csv: perl -p -i -e... (5 Replies)
Discussion started by: Astrocloud
5 Replies

3. Shell Programming and Scripting

Replace last line of file in memory via embedded command?

Further to my earlier question regarding embedded commands I now want to delete then replace the last line in a file in memory.... FILEHERE=$((echo "$FILEHERE" | sed -e '$d' ) echo "this now added") this is throwing a syntax error but as I'm still getting used to this structure I... (1 Reply)
Discussion started by: Bashingaway
1 Replies

4. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

5. Shell Programming and Scripting

Minimum whitespace separated CSV file generation

Hi, I have a flat text file consisting of rows (each field separated by '|') from a table. e.g; NSW|Gulliver Travels|236||5000|BW This has to be converted to the following format NSW "Gulliver Travels" 236 5000 BW No data field has to be left as a blank character so that we have... (6 Replies)
Discussion started by: vharsha
6 Replies

6. Shell Programming and Scripting

Removing Embedded Newline from Delimited File

Hey there - a bit of background on what I'm trying to accomplish, first off. I am trying to load the data from a pipe delimited file into a database. The loading tool that I use cannot handle embedded newline characters within a field, so I need to scrub them out. Solutions that I have tried... (7 Replies)
Discussion started by: bbetteridge
7 Replies

7. Shell Programming and Scripting

Copy files listed in a text file - whitespace problem.

Hi, Say I have this text file <copy.out> that contains a list of files/directories to be copied out to a different location. $ more copy.out dir1/file1 dir1/file2 dir1/file3 "dir1/white space" dir1/file4 If I do the following: $copy=`more copy.out` $echo $copy dir1/file1... (4 Replies)
Discussion started by: 60doses
4 Replies

8. Shell Programming and Scripting

removing whitespace from middle of file -help

I have a file in which I clean out a bunch of nonsense text as well as path information. What I end up with is something like the following: johnson.........................................................933 Where the periods represent the whitespace The file comes out originally with... (2 Replies)
Discussion started by: roninuta
2 Replies

9. AIX

Unix shell scripting to find latest file having timestamp embedded...

Hi guys, I have a directory in UNIX having files with the below format, i need to pickup the latest file having recent timestamp embedded on it, then need to rename it to a standard file name. Below is the file format: filename_yyyymmdd.csv, i need to pick the latest and move it with the... (2 Replies)
Discussion started by: kaushik25
2 Replies

10. Shell Programming and Scripting

embedded exe file into rtf file

Hi All I want a method in shell script which will embed the exe file into rtf file si that the rtf file can be open in word and so my exe. Actually what is happening in my system at present : The report in my system is a self-extracting Zip file with filetype '.EXE'. Double clicking... (1 Reply)
Discussion started by: rawatds
1 Replies
Login or Register to Ask a Question