Is it possible to list file names starting with X or containing X?
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!
Related videos on Youtube
Comments
-
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 over 13 years
-
whitequark over 13 yearsAnd you don't need to pass
-1
option when pipingls
output, it only outputs compact listing to ttys. -
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 almost 10 yearsThis 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 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 about 8 yearsThis 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 over 5 yearsnot only this is a lot slower, it's also a bad idea. see Why not parse
ls
(and what do to instead)? -
phuclv over 5 years
-maxdepth 0
can be done withls -d
. And dot files can be expanded withshopt -s dotglob
-
Moreaki over 5 yearsWhile 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 usels -- *X
. -
Moreaki over 5 yearsTo make this safe and work in all intended cases, one should form a habit of using
ls -- *X*
. -
Marcelo Scofano Diniz about 3 yearsInto Powershell Win64 CLI works as a charm...