Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

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

share|improve this question
add comment

4 Answers 4

up vote 2 down vote accepted

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

ls *X*
share|improve this answer
add comment

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|improve this answer
    
    
And you don't need to pass -1 option when piping ls output, it only outputs compact listing to ttys. –  whitequark Nov 21 '10 at 7:52
add comment

Why use ls *X*? When you type this command, bash already interprets the stars and expands them, so echo *X* will do exactly the same.

But it won't list files of which the name begins with a dot. If you really want files, my best suggestion is:

find .  -maxdepth 0 -type f -iname '*X*'

(the -maxdepth 0 prevents scanning subdirectories).

share|improve this answer
add comment

Those are great suggestions - IF YOU ARE USING LINUX! Using the "ls" command is almost the same as MS-DOS's DIR command. However, the ls command only lives in certain command line interpreters like C-shell and NOT in MS-DOS and certainly NOT MS-Windows.

So if you are asking about a Windows solution I might assume you haven't fully explored the Windows explorer (NOT to be confused with "Internet Explorer" either). For example, to view files alphabetically in Windows Explorer just open it up (click on My Computer, for example), make sure you can view by detail, navigate to the folder you want to sort and then click on the column called 'Name' to see all files starting with A followed by files starting with B and so on. Click on it again and you can see all files listed in reverse order where you first see all files starting with Z - click on it a third time and it's back to listing the A's first. But it doesn't stop there. You can then click on the date column (assuming you didn't deselect from the default menu settings and hide it or something) to show an alphabetized list with the most recently dated files listed first. You can even add more columns (through right-clicking) where you can add or delete display options in which you could even specify a heading that could display files according to a search formula or even with check boxes - and that's just for starts!

Then again, if you're old school, you could just use the DOS command line in a DOS box. For example, open up a DOS box (run "cmd.exe"), go to your directory/folder and type in "dir [star]x[star]" (without the quotes) to show all files with x in the name. Of course you can also redirect the output to another file too. The example there would be 'dir [star]x[star]>filename.txt' which would create a text file called 'filename.txt' and contain a list of all files with x in the name. Of course, typing in "dir x[star]" would show only files starting with the letter x. (Notice the subtle difference where x is placed in the command and the asterisk [star] wild card symbol?). And if you really wanted to go nuts, you could even write a batch file! Just don't forget the pause line or redirecting the output as it would be rather pointless otherwise. (Anyone remember DOS batch "programming"?! lol)

Hope it helps.

P.S. I had to substitute the asterisk character using [star] since this blog will not print them - sorry.

P.S.S. I know. I know! I probably can add the asterisk [star] character but I already posted. So please don't get hung up on it. I'll try to remember (how I'm posting) next time.

share|improve this answer
1  
"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. –  jlliagre Nov 21 '10 at 8:33
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.