LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   list only files in current directory (https://www.linuxquestions.org/questions/programming-9/list-only-files-in-current-directory-95295/)

xscousr 09-21-2003 07:40 PM

list only files in current directory
 
i need to figure out how to list only the files in a current dir

e.g.

say i'm in /tmp

in /tmp there is /sub1 /sub2 file1 file2 file3

i need to know how to use either find /tmp -type f or ls to list only the files in /tmp - not the sub-dir's or their contents on the output just file1 file2 file3

how do i do this?

tia

MasterC 09-21-2003 08:44 PM

ls

This is the command to list files in any given directory.

Cool

xscousr 09-21-2003 09:05 PM

yeah, but it also lists the directories. I need a switch or a way to list the files, and only the files in a given directory. I need this to use the output as a variable in a script.

files=`find ${DIR} -type f `
for files in ${files}
do

" i need to know how to use either find /tmp -type f or ls to list only the files in /tmp - not the sub-dir's or their contents"

MasterC 09-22-2003 01:10 AM

I see ;) I don't know how easy that'll be as Linux sees both Files and Directories all as Files. I'll move this to Programming as it's not related to Redhat, and you'll likely get a better response there.

Moving to Programming

Cool

m0rl0ck 09-22-2003 01:40 AM

What about just:
ls -a|grep -v "/"

SaTaN 09-22-2003 02:29 AM

Quote:

Originally posted by m0rl0ck
What about just:
ls -a|grep -v "/"

But "." and ".." will also be displayed which are directories....

I suppose this would do it fo you

ls -l | grep -v "^d" | awk '{print $9}'

or if you want to eliminate even links n other stuff and want only
regular files....
then this should do it for you

ls -l | grep "^-" | awk '{print $9}'

megaspaz 09-22-2003 02:55 AM

how about a little bash script?

Code:

#!/bin/bash

if [ -n "$1" ]
then
        for files in `ls -a $1`
        do
                if [ -f "$files" ]
                then
                        echo "$files"
                fi
        done
else
        echo "You didn't specify a directory"
fi

exit 0


/bin/bash 09-22-2003 07:30 AM

find . -type f -maxdepth 1 -print

xscousr 09-22-2003 07:35 AM

excellent - thanks alot for all the suggestions - gave me some great insight


All times are GMT -5. The time now is 09:01 AM.