17

I discovered that

ls -AF /var/ |grep \/$

helps me to find all directories from a directories without more information. Now i need exactly the opposite - showing all files without any further information just the file name in each line

file1
file2
file3

and filtering the directories because those - i don't need

1
  • @piokuc And how would the manpage for ls help in this case?
    – Shawn Chin
    Commented Oct 25, 2012 at 8:59

5 Answers 5

56

Simplest way: its not L its digit - 1

$ ls -1
3
  • 1
    this also includes directories
    – yellowsir
    Commented Sep 29, 2019 at 13:40
  • @Shaunak-Sontakke you saved my sanity. Confirmed this works as described on bash on MacOS Monterey.
    – Adam
    Commented Oct 8, 2022 at 19:55
  • 1
    @yellowsir if you pass a "*.*" at the end you will get rid of the directories. Commented May 20, 2023 at 0:25
15

For finding files matching a certain expression there exists find. Its man-page is quite good and includes also some interesting examples. For getting only the files of a directory you can use:

find /var -maxdepth 1 -type f -printf "%f\n"
0
4

Just use the -v switch for grep to invert the match:

ls -AF /var/ |grep -v /$ 
3

ls -1Ap /var/ | grep -v /

Similar to @choroba's answer, but this version will list clean file names on each line without appending additional classification indicators, if you don't want them.

-p flag appends / to directories

-1 flag is always handy to list one file per line

-1

Or use find :

find -maxdepth 1 -type f /var/
4
  • 1
    Several issues with that command. The path is missing, -maxdepth has to come before -type, and it does not give what the OP wants (show only filenames without the path)
    – Shawn Chin
    Commented Oct 25, 2012 at 9:01
  • path is not mandatory, but you need -maxdepth before -type Commented Oct 25, 2012 at 9:01
  • path is required if you the target directory is not pwd, which appears to be the case in this question.
    – Shawn Chin
    Commented Oct 25, 2012 at 9:03
  • 1
    Sorry, didn't have a terminal available to test. Lothar Krause's answer is better anyway, didn't think the trailing / and beginning ./ were going to be a problem for the OP.
    – pistache
    Commented Oct 25, 2012 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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