Moving filenames containing spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving filenames containing spaces
# 1  
Old 01-23-2009
Moving filenames containing spaces

I want to ftp all the sh files in the directory. Also if any of the file name
contains spaces in them, it should be converted to underscores before it is ftped. I wrote the following code below:

FILESSH=$(ls /mysh/*.sh)

--- FILESH being used here for some other task ---

echo "$FILESSH" | while read FILE
do

echo "$FILE" | grep " " >/dev/null && {

mv "$FILE" `echo "$FILE" | sed 's/ /_/g'`; echo "Name changed";
FILE=`echo "$FILE" | sed 's/ /_/g'`;

}

-- I will FTP the $FILE here ---

done

Can the line in red be avoided without using another variable in the code. I dont know why I am asking but just want to know if it can done. Is there any other nice method of doing the above task. (The dir may contain thousands of files so dont want to perform mv on all).... Thanks.......
# 2  
Old 01-23-2009
Quote:
Originally Posted by amicon007
I want to ftp all the sh files in the directory. Also if any of the file name
contains spaces in them, it should be converted to underscores before it is ftped. I wrote the following code below:

FILESSH=$(ls /mysh/*.sh)

--- FILESH being used here for some other task ---

echo "$FILESSH" | while read FILE

That will break your script if any filenames contain leading or trailing spaces. Use wildcards directly:

Code:
for FILE in /mysh/*.sh

Or set IFS to a null string:

Code:
echo "$FILESSH" | while IFS= read -r FILE

Quote:
do

echo "$FILE" | grep " " >/dev/null && {

There's no need for an external command:

Code:
case $FILE in
     *\ *) : do whatever ;;
esac

Quote:

mv "$FILE" `echo "$FILE" | sed 's/ /_/g'`; echo "Name changed";
FILE=`echo "$FILE" | sed 's/ /_/g'`;

}

-- I will FTP the $FILE here ---

done

Can the line in red be avoided without using another variable in the code. I dont know why I am asking but just want to know if it can done.


Code:
NEWFILE=`echo "$FILE" | sed 's/ /_/g'`
## in bash or ksh93:
## NEWFILE=${FILE// /_}

mv "$FILE" "$NEWFILE" && echo "Name changed"

# 3  
Old 01-23-2009
Code:
FILESSH=$(ls /mysh/*.sh)
--- FILESH being used here for some other task ---
echo "$FILESSH" | while read FILE

This does not break for leading or traing whitespaces for me...

Code:
 for FILE in $(echo $FILESSH)

This breaks for any space in the name

Code:
for FILE in /mysh/*.sh

Code:
echo "$FILESSH" | while IFS= read -r FILE


Code 1,3,4 breaks if no sh file is found in the directory (they goes in loop once)....Code 2 does not but it breaks for any space in filename Any ideas?

Last edited by amicon007; 01-23-2009 at 04:08 PM..
# 4  
Old 01-24-2009
No duplicate or cross-posting, please read the rules.

Continue here:

https://www.unix.com/shell-programmin...ng-spaces.html

Thread closed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Handling filenames with spaces

I'm trying to handle some files with spaces in their name using "" or \ . Like "file 1" or file\ 1. My current confusion can be expressed by the following shell script: #!/bin/bash touch "file 1" "file 2" echo -n "ls: " ; ls echo --- for file in "file 1" "file 2" ; do echo $file... (9 Replies)
Discussion started by: Ralph
9 Replies

2. Shell Programming and Scripting

Bash: Picking up filenames then moving to next directory

I have a collection of directories, for example as below I want to create a loop that goes in the first directory and picks up the *hhz*.sac.pzs filename in a variable, and the other files matching *hhz*.sac in another variable (however I do not want to pick the *hhz*.sac.pzs). This is because... (4 Replies)
Discussion started by: kristinu
4 Replies

3. Shell Programming and Scripting

removing spaces in filenames

I have a problem mounting images because of the spaces in the filenames. Does anyone know how to rename files by removing the spaces with the find command? find Desktop/$dir -name "*.dmg" -print -exec ??? (4 Replies)
Discussion started by: ianebaj
4 Replies

4. Shell Programming and Scripting

awk and spaces in filenames

Hey there, this is my first post and I'll try to explain my situation as best I can.Here is a sample of the input file: ADO Sample.h,v ADO Sample 2010-05-21 lyonsb /repository/patents/TSCommon/OpenSource/Dundass/ug6mfc/DataSources/Ado/ADO Sample ADO SampleDoc.h,v ADO SampleDoc 2010-05-21... (3 Replies)
Discussion started by: rodan90
3 Replies

5. Shell Programming and Scripting

Looping through filenames with spaces

I need to loop through the files in a directory and process the files. But some of the filenames contain spaces. Here is a little test script I've been using to experiment. (I'm not really going to call 'echo', I'm doing some other processing.) Everything I try fails. How can I do this??... (7 Replies)
Discussion started by: KenJackson
7 Replies

6. Shell Programming and Scripting

spaces in filenames

Hi I hope someone will be able to resolve this little teaser! I am running a script for file in `ls directory` do echo "$file" ...other code here.... done this works fine unless we receive a file with a name which has a space in it ie "filena me" (I know its not good... (8 Replies)
Discussion started by: Bab00shka
8 Replies

7. Shell Programming and Scripting

spaces in filenames, for do

Hi All, I see similar problems in past threads but so far no answers have worked for me. I am trying to write a script which parses a txt file that contains one filename per line, then finds those files on the local disk and copies them to a specified directory. What I have: ... (4 Replies)
Discussion started by: naviztirf
4 Replies

8. Shell Programming and Scripting

Unix filenames and spaces

I have files on my unix boxes that users have created with spaces. Example: /tmp/project plan ls -l "/tmp/project plan" works fine. $/tmp>ls -l "/tmp/project plan" -rw-r--r-- 1 root other 0 Jan 31 12:32 /tmp/project plan I created a file called test and put just the... (2 Replies)
Discussion started by: x96riley3
2 Replies

9. Shell Programming and Scripting

how to handle spaces in filenames

I'm trying to do something like that: for $filename in `ls -1` do some_command $filename done but it doesn't work properly for file names with spaces, for...in splits at spaces. Anyway around? (4 Replies)
Discussion started by: rayne
4 Replies

10. Shell Programming and Scripting

spaces in filenames

I have a problem with the script below #!/bin/sh for vo in `find -maxdepth 1 -type f -regex "^\./*$"` do ls -l "$vo" some other commands done It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed Any solutions? (4 Replies)
Discussion started by: Hitori
4 Replies
Login or Register to Ask a Question