How to deal files with space in names?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to deal files with space in names?
# 1  
Old 07-15-2011
How to deal files with space in names?

I have a bash file as following:
Code:
#!/bin/sh
   deal_file(){
       printf $1
       printf "\t is a file"
       echo
   }
   
   main(){
       for file in `find "$1" `
      do
          deal_file $file
      done
  }
  
  main $1

then I run
Code:
./t.sh  .

,get the following results:
Code:
./     is a file
./b     is a file
./e     is a file
f     is a file
./t.sh     is a file
./e     is a file
f     is a file
c     is a file
./e     is a file
f     is a file
c/b     is a file
./e     is a file
f     is a file
c/c     is a file
./e     is a file
f     is a file
c/a     is a file
./a     is a file
b     is a file
./c     is a file
./a.txt     is a file
./a     is a file

However the file of the current directory is
Code:
[river@localhost f]$ find ./
./
./b
./e f
./t.sh
./e f c
./e f c/b
./e f c/c
./e f c/a
./a b
./c
./a.txt
./a

so the for loop spilt the file names if containing spaces, how can I fix the bug?
Thanks!
# 2  
Old 07-15-2011
try while loop

Code:
 
#!/bin/sh
   deal_file(){
       printf $1
       printf "\t is a file"
       echo
   }
 
   main(){
      find "$1" | while read line;
      do
        deal_file "$line"
      done
  }
 
main $1


Last edited by itkamaraj; 07-15-2011 at 06:47 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

2. Shell Programming and Scripting

Remove trailing space from file and folder names

I have a folder that contains many sub folders and files. This tree has to be backed up to an archive system. According to the tech support, one of the archives is failing to back up due to the possibility of trailing spaces on file and folder names. Therefore, I would like to have a script... (16 Replies)
Discussion started by: vipertech
16 Replies

3. Shell Programming and Scripting

Deal with a range of file names

Red Hat enterprise Lines 5.8 I have files like below. All the below files are created in the same minute. Using mv command, I want to move 33 files starting from pg424_fcds_1_718519105_34304.dat to pg424_fcds_1_718519105_34336.datto another direcory called /data/fcds_logs/ ls... (6 Replies)
Discussion started by: omega3
6 Replies

4. Shell Programming and Scripting

How to deal with replaced files?

My task is to copy all files from many directories in one. The big problem i encounter is that some files in different directory have the same names. Is they are way to copy the files that have same names in a sub directory ( need to preserve the name of the files unchanged ) I have list with... (6 Replies)
Discussion started by: gogok_bg
6 Replies

5. UNIX for Dummies Questions & Answers

How to deal with incomplete image files

Sorry for the odd title, but I couldn't think of an easy way to describe my issue. Background I have a home security system that continually sends (via FTP) 4 different still images to a directory on my personal website - cam0.jpg, cam1.jpg, etc. I've written an extremely basic html script to... (4 Replies)
Discussion started by: CinciJeff
4 Replies

6. Shell Programming and Scripting

how to copy files followed by list of names of all the files in /etc?

....... (2 Replies)
Discussion started by: pcbuilder
2 Replies

7. Shell Programming and Scripting

How to deal with .tar.gz files in Linux?x

Dear all, there are more than 10 files with .tar.gz extension in my folder i didnt want to extract them i just want to run the query to fetch my necessary data from all the files. but when i run the command it untar the files in that folder. gunzip -c abc.tar.gz | tar -xf - | grep REC |... (0 Replies)
Discussion started by: jojo123
0 Replies

8. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

9. Shell Programming and Scripting

sftp how to deal with space in path to dir?

OK I am trying to use something similar to this: #!/bin/sh echo "OK, starting now..." sftp -b /dev/fd/0 user@remotehost <<EOF cd pub ascii put filename.txt bye EOF only difference is the dir I need to cd to has a space in it like this /Import/Server/Prospect File ,... (3 Replies)
Discussion started by: NewSolarisAdmin
3 Replies

10. UNIX for Advanced & Expert Users

what commands are used to deal with core files on linux redhat???

how do you cause a running pocess to dump a core file on linux systems?? i tried sleep 100 & kill -SEGV PID but nothing is created also, what commands can be used to analyze them? (extract useful info from them) (2 Replies)
Discussion started by: TRUEST
2 Replies
Login or Register to Ask a Question