-5

I am currently taking a Controlled Assessment in a Computing GCSE and this is the task.

At the command prompt, type ls -a. Demonstrate and explain what happens. Modify this command so that it only shows hidden files beginning with the letter ‘b’.

I have already done ls -a and I tried ls -b, but it didn't work.

  • ls -a | grep .b* should do I think – ddnomad Feb 23 '17 at 9:24
  • that's good. Please set my answer below as approved then :) – ddnomad Feb 23 '17 at 9:29
  • 3
    @ddnomad greping an ls output is unnecessarily complex, when you can specify the pattern with ls -a -d .b* - note that you need -d for not listing contents of the results. Also that grep will show files like 123.bWRONG. – Fiximan Feb 23 '17 at 9:35
  • 4
    All the information you need is in man ls. I suggest you read it. – terdon Feb 23 '17 at 9:54
4

The names of hidden files start with . (dot), there are none that start with b. To list names starting with .b use

$ ls .b*

This will also list the contents of any directory whose name starts with .b. Note that -a is not needed here as we explicitly give a filename pattern for the shell to match. Since the pattern starts with . (dot), it will match hidden files. The expanded pattern will be given to ls for processing.

To avoid listing the contents of directories, use

$ ls -d .b*

The above solutions will give an error if no name matches the pattern.

To only list the names of regular files whose names start with .b, use

$ for name in .b*; do test -f "$name" && printf '%s\n' "$name"; done

To get the output in a similar way as with plain ls (note that column is a non-standard utility):

$ for name in .b*; do test -f "$name" && printf '%s\n' "$name"; done | column

The last couple of solutions will not print anything of no name matches the pattern.

  • When worrying about file type regular, a find command would be much simpler: find -maxdepth 1 -type f -name '.b*'. Also faster than a for loop. However, I assume ls-usage was intended. – Fiximan Feb 23 '17 at 10:02
  • @Fiximan If your find is able to do it that way, of course. Standard find needs a search path, and doesn't understand -maxdepth. This will also generate output with ./ prepended to the name (with GNU find). – Kusalananda Feb 23 '17 at 10:06
  • I was aware of the implied . serach path being a GNU find variation, but not that even -maxdepth is, since BSD find had it (see man find )- good to know! – Fiximan Feb 23 '17 at 10:17
4

ls is the command, and -a is the flag for the command. In order to see the files staring from b one can specify a pattern - .b* Where . means that the files are hidden and the * means everything else...

and in general when you don't know what a command does, read the manual - man ls

For avoiding listing of the contents of the listed items, use the -d option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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