Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Sometimes a file name is created from a couple of names and space between each name as the following:

   $ touch  "A B C"
   $ ls
   A B C      <-- one file but has three words !                        
   file1
   file2
   file3

How can I ignore (filter) the file that actually created from couple of names as "A B C"?

Expected output

     $ ls <filter>
     file1
     file2
     file3
share|improve this question
add comment

4 Answers

up vote 5 down vote accepted

If you do the following:

ls | grep -F -v ' '

You will not see any file with spaces in the names

( I used to have fgrep instead of grep -F in the example, but as Hauke Laging pointed out that is deprecated)

share|improve this answer
1  
May I quote my documentation: "Direct invocation as fgrep is deprecated" –  Hauke Laging Apr 28 '13 at 15:16
    
@HaukeLaging Of course, thanks for pointing out. After 29 years of using fgrep I need to change my habits, that will take a bit –  Anthon Apr 28 '13 at 15:52
    
..though you may see parts of a filename that contains spaces, one or more newline characters, and at least one line not containing spaces. Remember grep works on lines and a filename can be made of several lines. –  Stephane Chazelas May 6 '13 at 20:20
    
@HaukeLaging @Anthon: I [sadly] must add that egrep is also deprecated and grep -E is now the standard. –  Chirp. Not Luke. May 6 '13 at 20:36
add comment

With GNU ls, i.e. under non-embedded Linux:

ls -I '* *'

Not that there's much of a reason to skip files whose name contains a space. If you're doing that because you have something like file_names=$(ls) or for f in $(ls) in a script, don't parse the output of ls, and your problem will probably go away.

share|improve this answer
add comment

Using KSH's extended globs, you can filter out filenames in a glob expansion:

ls -d -- !(A B C)
share|improve this answer
add comment

Maybe this is also helpful:

ls -l --quoting-style=literal:
-rw-r--r-- 1 hl hauke 0 28. Apr 16:55 A B C

ls -l --quoting-style=locale:
-rw-r--r-- 1 hl hauke 0 28. Apr 16:55 „A B C“

ls -l --quoting-style=shell:
-rw-r--r-- 1 hl hauke 0 28. Apr 16:55 'A B C'

ls -l --quoting-style=shell-always:
-rw-r--r-- 1 hl hauke 0 28. Apr 16:55 'A B C'

ls -l --quoting-style=c:
-rw-r--r-- 1 hl hauke 0 28. Apr 16:55 "A B C"

ls -l --quoting-style=escape:
-rw-r--r-- 1 hl hauke 0 28. Apr 16:55 A\ B\ C
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.