Is it possible to list file names starting with X or containing X?

29,538

Solution 1

"Starting with" is just a specialization of "containing", so you can use the same for both.

ls *X*

Solution 2

To do the "containing X" part, you would do:

ls | grep "X"

ls - Lists all the files in the current directory

| - Pipe, sends all output of the command before it as input to the command after it.

grep "X" - Searches for text in the input given (here, through the pipe).

ls -1 | grep "^X"

ls - Lists files in the current directory, one on each line, essential for the regular expression we will use with grep.

| - Pipe

grep "^X" - This basically translates into: "The beginning of the line, and then X" so it will show files beginning with "X".

Hope this helps!

Share:
29,538

Related videos on Youtube

irl_irl
Author by

irl_irl

Student

Updated on September 17, 2022

Comments

  • irl_irl
    irl_irl almost 2 years

    As the title asks, is it possible to list files starting with X or containing X?

    ls is used to list files. Are there any options I can use so I can list the files beginning with or containing a specific letter?

  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams over 13 years
  • whitequark
    whitequark over 13 years
    And you don't need to pass -1 option when piping ls output, it only outputs compact listing to ttys.
  • jlliagre
    jlliagre over 13 years
    "ls" is more than likely available if bash already is. "ls" has nothing to do with the C-shell. "bash" is absolutely not limited to Linux. I run both of them it on many flavors of Unix, Windows and several more exotic OSes.
  • 75th Trombone
    75th Trombone almost 10 years
    This has the checkmark, but I don't think it answers the question. This shows the contents of each subdirectory of the current folder whose names contain X. It does NOT show a list of all the current folder's contents that contain X.
  • tripleee
    tripleee about 8 years
    @75thTrombone Not sure I understand what you are saying. The wildcard matches file names with X anywhere in them; the listing of the contents of matching subdirectories is a (mis-?)feature of ls, which can be disabled with the -d option.
  • tripleee
    tripleee about 8 years
    This is not a "blog". You get a literal asterisk by escaping it with a backslash. See the formatting help which is displayed in the sidebar while you are editing.
  • phuclv
    phuclv over 5 years
    not only this is a lot slower, it's also a bad idea. see Why not parse ls (and what do to instead)?
  • phuclv
    phuclv over 5 years
    -maxdepth 0 can be done with ls -d. And dot files can be expanded with shopt -s dotglob
  • Moreaki
    Moreaki over 5 years
    While your contribution is much apprieciated, it is unfortunately only partly correct. Try this: touch -- '-X' && ls *X. Under MacOS, you'll get an error and under most Linux distributions you'll get a subset of the actual list of all files. A more complete solution would be to use ls -- *X.
  • Moreaki
    Moreaki over 5 years
    To make this safe and work in all intended cases, one should form a habit of using ls -- *X*.
  • Marcelo Scofano Diniz
    Marcelo Scofano Diniz about 3 years
    Into Powershell Win64 CLI works as a charm...