Test File for Existence with Whitespaces in Path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Test File for Existence with Whitespaces in Path
# 1  
Old 12-16-2009
Question Test File for Existence with Whitespaces in Path

Hi Everyone!

I'm quite new to shell scripting so this might be trivial, though 3 days of struggle and search didn't help to solve the problem:

I want to look for files called '*HUN*' in a huge amount of directories most of their names contain whitespaces and print the path of the directory if it contains such a file.

example:
/home/usr/test/f 3/

so I try to use:
Code:
[ -e  "/home/usr/test/f 3/*HUN*" ] && echo y || echo n

it returns "n", though there is a file called "f_HUN.avi" in the directory.
the funny thing is, the directory is recognised with the -d option, i.e.
Code:
[ -d  "/home/usr/test/f 3" ] && echo y || echo n

returns "y".
Is there a way to overcome this issue with the space for the -e or -f options?

Any help would be much appreciated!

s

Last edited by pludi; 12-17-2009 at 03:25 AM.. Reason: code tags, please...
# 2  
Old 12-16-2009
hello
Code:
find /dir -type f -iname '*HUN*'

# 3  
Old 12-16-2009
@OP: By using quotes you are preventing glob expansion so you are testing for a file called *HUN* . And without the quotes it would only work if the asterisks expand to exactly one file. If there are more files, then the test expression will become invalid.
# 4  
Old 12-17-2009
Thanks for the quick replies!

@gaurav1086: it works in deed, but I thought there must be a way to get it working with "test"
Actually I want to have a list with all the directories containing movies without Hungarian subtitles in them. So I have a list of directories and want to check them.
The problem with find is that it will give me a lot of double records, as there are many files in the directories.

Here's the little script I want to use (it seems working now, I must have been changing something):

Code:
#!/bin/bash
 
find $(pwd)/* -type d | while read dir ; do
[ -f "$dir"/*HUN* ] && echo "found in: $dir" || echo "not found in: $dir"
done



---------- Post updated at 09:22 AM ---------- Previous update was at 09:21 AM ----------

@Scrutinizer: thanks for your comment, it explains why it was not working and why the script above seemed to be working but actually giving error messages if found more than 1 file in the dir. Is it possible to fix this with regex? I mean something like this:

Code:
[ -f "$dir"/.*HUN.* ]

with or without quotes, donno...

Last edited by sumi76; 12-17-2009 at 03:49 AM.. Reason: code tags, please...
# 5  
Old 12-17-2009
You could e.g. use:
Code:
find $(pwd)/* -type d | while read dir
do
  for file in "$dir"/*HUN*
  do 
    if [ -f "$file" ] 
    then
      echo "found in: $dir" 
      break
    else
      echo "not found in: $dir" 
    fi
  done
done


Last edited by Scrutinizer; 12-17-2009 at 04:19 AM..
# 6  
Old 12-17-2009
Thanks Scrutinizer!

This works. So basically break exits the loop after the first file matching the criteria is found. Is it right? I just want to be sure I understand correctly what the script's doing.

Last edited by sumi76; 12-17-2009 at 09:23 AM..
# 7  
Old 12-17-2009
Hi, it breaks the inner loop, correct.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

test for existence of files with same extension

Hi, I am checking for existence of files with the same extensions #! /usr/bin/ksh txtfiles = '*.txt' if then cp ${dirpath}/${txtfiles} ${dir2path} fi I am getting the following error line 5: [: too many arguments for the if check condition (4 Replies)
Discussion started by: chen.sara
4 Replies

2. Solaris

How to test the existence of trailer record

SunOS 5.10 Generic_142900-15 sun4v sparc SUNW,T5240 I have a script that needs to test a file for the existence of a trailer record. Is there a command and is a header and trailer differect record type? Thanks in advance (1 Reply)
Discussion started by: Harleyrci
1 Replies

3. Shell Programming and Scripting

Test for existence of files

Hello, Can you please help me to see if log files exist in a directory? I need to scan logs in different directories, so I am using an array to change dynamically. I need help in the if test statement dir=/logs/MSD dir=/logs/UPD countA=1 while (( countA <= ${#dir } )) do cd ${dir}... (1 Reply)
Discussion started by: drbiloukos
1 Replies

4. Shell Programming and Scripting

Test for a file existence

deleted (1 Reply)
Discussion started by: ust
1 Replies

5. Shell Programming and Scripting

replace nulls with whitespaces in a file

Hi, i have a file which contains data in fixed length. each row contains data of 10 characters fixed length. The data in the file appears like 4567782882 some times i may recieve dat less than fixed length of 10. in such a case i find nulls appended at the trailing spaces when i do a... (2 Replies)
Discussion started by: meeragiri
2 Replies

6. UNIX for Dummies Questions & Answers

trimming whitespaces in a file

Hello, Im trying write a bash sript to search a file and identify any file names that contain a space at the end of them. I was trying to use the grep command but I can find how I would identify the character I'm looking for. Any help would be grately appreciated. (2 Replies)
Discussion started by: gintreach
2 Replies

7. Shell Programming and Scripting

check existence of the path

Hi How can I check if the path exist or not? echo "Enter path:"; read my_path; ##I should check whether my_path exists or not.... (5 Replies)
Discussion started by: tjay83
5 Replies

8. Programming

C function to test existence of a login

Hi everybody, I need to check in C program whether a given login is known on the system. Is there any system function that could do this ? So far, all I could find is getpwnam(), which answers my problem by parsing the local password database. But won't work if a user is authenticated by... (2 Replies)
Discussion started by: xavier054
2 Replies

9. Shell Programming and Scripting

Test whether absolute path in variable

I'm using the following line in bash to test whether an argument supplied is an absolute path or not: if echo $1 | grep '^/' > /dev/null then absolute=1 else absoute=0 fi The line appears to work but seems somewhat unprofessional looking to me. Is it an acceptable... (2 Replies)
Discussion started by: dkieran
2 Replies

10. Shell Programming and Scripting

how to test for file existence using file size?

ok im doing this in a normal shell. i need to check the file in the script. how should i write the if else statment? if the filesize contains 0kb then it will echo out file contains nothing else if the script contains more than 0kb then it will echo out file exist. any one care to help?... (3 Replies)
Discussion started by: forevercalz
3 Replies
Login or Register to Ask a Question