Directory names that contain spaces and other suggestions?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Directory names that contain spaces and other suggestions?
# 1  
Old 01-07-2002
Directory names that contain spaces and other suggestions?

The script below was written to select files and convert a particular string to something other and replace that file. However, I came across some issues with filenames that contain spaces, any suggestions to get around this? Any other suggestions that may apply to this code would also be appreciated.



user=`whoami | tr "[A-Z]" "[a-z]"`
rtime=`date +'%H.%M.%S %d-%m-%y'`
ENVIRON=`pwd`
find $ENVIRON -name "*.pde" -type f -exec egrep -ily "DMRG[ ]*3,1" {} \;| while read pde
   do
        directory=`dirname $dir`
        if [ -w $directory ] ; then        
            cp $pde ${pde}. pdb
            cat $pde | se d 's/\(DMRG[ ,]*3,\)1/\10/g' > ${pde}.tmp
            if [ -s ${pde}.tmp ] ; then
                mv&n bsp;${pde}.tmp $pde
                echo  "PDE Conversion Success Log Generated at $rtime" \
                >&nb sp;/syd/devel/code/training/steves/pdeSCS.log
                echo  "$pde File has been converted Backup file is ${pde}.pd b"  \
                >>&n bsp;/syd/devel/code/training/steves/pdeSCS.log    
            else
                echo  "PDE Conversion Error Log Generated at $rtime" > pdeER R.log
                echo  "Failure writing ${pde}.tmp Backup file is ${pde}.pdb"
                echo  "Failure writing ${pde}.tmp Backup file is ${pde}.pdb" >>&n bsp;pdeERR.log
            fi
        else
            echo "$directory i s not writeable" 
        fi
   done
Shakey21
# 2  
Old 01-08-2002
hi!!,
i am not clear whether the filename has spaces or the directory name has spaces.... U have written diferent things in the main body of post and the Title..

anyway.. i assume that the directory name has space..

try replacing the line..

if [ -w $directory ] ; then

with

if [ -w "$directory" ] ; then

it may help you..
Smilie
# 3  
Old 01-08-2002
sorry for being unclear

its actually the directory name that contains the space.

The error first occurs with the dirname command. I got around that before by quoting specific things, however the find command dies if you are for example under a directory that contains a name with spaces.

ie /dir/spec/test/new\ test\ directory/script.sh

but the find command wont recognise the path, i tried doing something like this unsuccessfully ENVIRON=`pwd | sed s/\ /\\\\\ /g

Any other suggestions?
Shakey21
# 4  
Old 01-09-2002
shaik,
replace the line

find $ENVIRON -name "*.pde" -type f -exec egrep -ily "DMRG

with

find "$ENVIRON" -name "*.pde" -type f -exec egrep -ily "DMRG

I have used "find" bY CREATING A DIRECTORY WITH NAME CONTAINING SPACE. AND IT WORKS FINE ON SOLARIS-5.6.

which OS are u using??
# 5  
Old 01-09-2002
i have tested by creating 3-4 levels of directory names with spaces inside the directories having spaces in names. and find works fine in all those levels just by puttting $ENVIRON in quotes.
# 6  
Old 01-09-2002
thankyou

Actually, I tried that before, but it didn't work running from the directory with the space in the directory name. But I just tried it again from the command line in a test directory and it worked.

Anyway, I tried it again adapting it to the script and it worked. I think the last time I tried I must have missed something else, being too complex or something else was at play.

Thanks again for the help!

Anyway this is the next version of the script:

#!/usr/bin/sh

user=`whoami | tr "[A-Z]" "[a-z]"`
rtime=`date +'%H.%M.%S %d-%m-%y'`
ENVIRON=`pwd`
find "$ENVIRON" -name "*.pde" -type f -exec egrep -ily "DMRG[ ]*3,1" {} \;| while read pde
   do
        directory=`dirname "$pde"`
echo $directory
        if [ -w "$directory" ] ; then        
            cp "$pde" "${pde}. pdb"
            cat "$pde" |  sed 's/\(DMRG[ ,]*3,\)1/\10/g' > ${pde}.tmp
            if [ -s "${pde}.tmp" ] ; then
                mv&n bsp;"${pde}.tmp" "$pde"
                echo  "PDE Conversion Success Log Generated at $rtime" \
                >&nb sp;/syd/devel/code/training/steves/pdeSCS.log
                echo  "$pde File has been converted Backup file is ${pde}.pd b"  \
                >>&n bsp;/syd/devel/code/training/steves/pdeSCS.log    
            else
                echo  "PDE Conversion Error Log Generated at $rtime" > pdeER R.log
                echo  "Failure writing ${pde}.tmp Backup file is ${pde}.pdb"
                echo  "Failure writing ${pde}.tmp Backup file is ${pde}.pdb" >>&n bsp;pdeERR.log
            fi
        else
            echo "$directory i s not writeable" 
        fi
   done 
Shakey21
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Problem with shell script while spaces encountered in directory names

Hi, I am having issues with the jar -tf command when I put in the shell script. The command runs fine from the command line as shown below. # jar -tf "./VirtualBox Dropped Files/2016-04-17T20:58:49.129139000Z/hive-exec-0.8.1.jar" But when I put in a shell script(shown below) and the... (10 Replies)
Discussion started by: vinoo128
10 Replies

2. Shell Programming and Scripting

Spaces in names between quotes

command i'm running: echo "services": "contactgroups": , | awk -F"]" '{print $1}' | cut -d' which provides the following output: check_win_semsrv_process, checking logs, check_win_semwebsrv_process, check_win_semlaunchsrv_process As you can see here, it outputs everything just... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. OS X (Apple)

Remove leading spaces from file names and folders

Hi All, I have a vexing issue with leading spaces in file names. Basically, we're moving tons of data from our ancient afp file share to Box.com and Box forbids leading spaces in files or folders. The HFS file system seems to be perfectly fine with this, but almost all other Unix file systems... (1 Reply)
Discussion started by: prometheon123
1 Replies

4. Shell Programming and Scripting

Remove spaces from start of file names

Hi, I have a directory with the following file names 01 - abc hyn 02-def 03-ghi.dir 04 - jhu.dir abc1 kil def bil The last two file names abc1 starts with one space and def starts with double space. I want these files in my directory to be renamed as ABC HYN DEF GHI.dir... (6 Replies)
Discussion started by: jacobs.smith
6 Replies

5. Shell Programming and Scripting

making find/sed to include directory names with spaces

how can i make find/sed to include directory names with spaces the command is like this for i in `find wp-content/themes -type f -print0 | xargs -0 grep -l -iE 'e'`;do sed -i -e 's/word1/word2/gI' "$i";done but it skips one directory names with spaces sed: can't read ./Nova: No such... (5 Replies)
Discussion started by: vanessafan99
5 Replies

6. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

7. Shell Programming and Scripting

How to strip the spaces in file names?

please somebody tell me what is wrong with this, while the thumbnail grabbing works and encoding works, but what is not working is, mv $i.jpg /var/www/thumbs/ and mv $i.mp4 /var/www/uploads/ #!/bin/bash # MINT 9 - FFMPEG - QT-FASTSTART - X264 - MP4 DIR=/var/www/tmp for i in... (9 Replies)
Discussion started by: mysoogal
9 Replies

8. UNIX for Dummies Questions & Answers

How to display only Owner and directory/sub directory names under particular root

hai, I am new to Unix, I have a requirement to display owner name , directory or sub directory name, who's owner name is not equal to "oasitqtc". (here "oasitqtc" is the owner of the directory or sub directory.) i have a command (below) which will display all folders and sub folders, but i... (6 Replies)
Discussion started by: gagan4599
6 Replies

9. Shell Programming and Scripting

Remove spaces between file names

Hi All, I have spaces in between file names. "Material Header.txt" "Customer Header.txt" "Vendor Header.txt" And how can I remove spaces between file names like below MaterialHeader.txt CustomerHeader.txt VendorHeader.txt Thanks Srimitta (10 Replies)
Discussion started by: srimitta
10 Replies

10. Shell Programming and Scripting

File names with spaces? Is it possible?

Gurus - I got one simple TXT file with long file name with blank spaces in between the words. I am trying to display that full file name, but it breaks while displaying. Could somebody shed some light here? Script ------ for i in `cat ~\temp\employee.txt` do echo $i done (5 Replies)
Discussion started by: Eric_2005
5 Replies
Login or Register to Ask a Question