4

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

4 Answers 4

5

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)

4
  • 1
    May I quote my documentation: "Direct invocation as fgrep is deprecated" Commented Apr 28, 2013 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
    Commented Apr 28, 2013 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. Commented May 6, 2013 at 20:20
  • @HaukeLaging @Anthon: I [sadly] must add that egrep is also deprecated and grep -E is now the standard.
    – erch
    Commented May 6, 2013 at 20:36
3

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.

2

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

ls -d -- !(A B C)
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .