Dealing with spaces in file names in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dealing with spaces in file names in a shell script
# 1  
Old 08-11-2008
Dealing with spaces in file names in a shell script

Hi,

What's the best way to find all files under a directory - including ones with space - in order to apply a command to each of them. For instance I want get a list of files under a directory and generate a checksum for each file.

Here's the csh script:

#!/bin/csh

set files = `find $1 -name '*'`
foreach file ($files)
set checksum = `sum $file`

# Ignore directories
if($status == 0) then
echo "$checksum $file"
endif
end

The problem with this is that if there are spaces in files, it doesn't work. Unfortunately csh is the only scripting language I sort of know. Is there a better way to do this?

Thank you in advance,
Sam
# 2  
Old 08-11-2008
find $1 -name "*" -type f -exec sum {} \;

Last edited by sudhamacs; 08-11-2008 at 03:05 PM..
# 3  
Old 08-11-2008
If this is on Linux (i.e. using the GNU utilities) try:

Code:
find $1 -type f -print0 | xargs -0 sum

-print0 tells it to use nulls to separate the filenames instead of line feeds, and the -0 option tells xargs to expect the input in that format.

If you don't have the GNU utilities handy, try this method:

Code:
find $1 -type f ; while read file ; do sum "$file" ; done

On a separate note, generally I would avoid using csh unless you have a good reason to use it.
# 4  
Old 08-12-2008
I do have GNU tools and that worked very well. Thanks.

I was wondering if there's a way to print out just the checksums and not the pathnames. I have a huge number of files - and I want to keep the size of the checksum report to a minimum.

Thanks again,
Sam
# 5  
Old 08-12-2008
Do you not need to know which file each checksum belongs to? You can easily just pull out the checksum column by adding | awk '{print $1}' to the pipeline.

Incidentally I would recommend using cksum or md5sum rather than sum, which has a very simplistic checksum algorithm.
# 6  
Old 08-13-2008
Thanks for those tips. I tried cksum. It much faster which is what I need.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. SCO

Long file names within shell script

I am downloading a zip file that contain files that are very long. I am trying to process them, but cannot. I can move the files from one directory to another at the shell prompt, but not within a shell script, I get a stat error. The files look somewhat like this; ... (5 Replies)
Discussion started by: trolley
5 Replies

2. 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

3. Shell Programming and Scripting

Dealing with filename spaces in Perl

The following command to replace text in place in multiple files in a directory is tripping up on filename spaces (Windows environment). I really don't know Perl. find '\\server\directory' | xargs perl -pi -e 's/textA/textB/g'Mike (2 Replies)
Discussion started by: Michael Stora
2 Replies

4. UNIX for Advanced & Expert Users

Shell script for dealing with XLS file with multiple tabs/worksheets

Hey Guys , Recently working on a requirement , i had to deal with XLS file with multiple tabs and the requirement was as below : 1. Convert one XLS file with multiple tabs to multiple CSV files. -- As i was working on MAC , so it was quite easy through APPLESCRIPT to deal with this.But... (2 Replies)
Discussion started by: himanshu sood
2 Replies

5. Shell Programming and Scripting

Dealing with white spaces in bash scripts

I'm trying to search for all files in directory with particular GID then change the GID to match the UID of each file: #!/bin/sh for i in $(find /dump -gid 200 | sed 's/\ /\\\ /g' | sed 's/\&/\\\&/g'); do chgrp $(ls -ln ${i} | awk '{print $3}') ${i} done I'm using sed to deal with... (7 Replies)
Discussion started by: venmx
7 Replies

6. 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

7. Shell Programming and Scripting

Dealing with files with spaces in the name

Hello, I'm a computer science major and I'm having problems dealing with file names with spaces in them. Particularly I'm saving a file name in a variable and then using the variable in a compare function i.e. a='te xt.txt' b='file2.txt' cmp $a $b If anyone could help me with this particular... (10 Replies)
Discussion started by: jakethegreycat
10 Replies

8. 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

9. Shell Programming and Scripting

want only file names (not whole path) in shell script

hi i wrote following script, #!/usr/bin/sh for index in `ls /tmp/common/*.txt` do echo "$index" done here index is giving full path but in my program i want only file names (not along with whole path) Eg. if in /tmp/common files are a.txt and b.txt den out should be a.txt b.txt ... (6 Replies)
Discussion started by: crackthehit007
6 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