LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2003, 07:40 PM   #1
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Rep: Reputation: 15
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
 
Old 09-21-2003, 08:44 PM   #2
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Rep: Reputation: 69
ls

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

Cool
 
Old 09-21-2003, 09:05 PM   #3
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
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"
 
Old 09-22-2003, 01:10 AM   #4
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Rep: Reputation: 69
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
 
Old 09-22-2003, 01:40 AM   #5
m0rl0ck
Member
 
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358

Rep: Reputation: 31
What about just:
ls -a|grep -v "/"
 
Old 09-22-2003, 02:29 AM   #6
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
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}'
 
1 members found this post helpful.
Old 09-22-2003, 02:55 AM   #7
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
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
 
Old 09-22-2003, 07:30 AM   #8
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
find . -type f -maxdepth 1 -print
 
Old 09-22-2003, 07:35 AM   #9
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
excellent - thanks alot for all the suggestions - gave me some great insight
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ List Files In Directory bendeco13 Programming 8 11-02-2010 12:08 PM
how to list how many files in a directory? malaka56 Linux - Software 8 09-02-2005 05:37 AM
How to list all the files in a directory Linh Programming 2 05-11-2004 10:09 AM
List Current SFTP users smurf Linux - Networking 0 02-12-2004 02:02 AM
Missing List.* files in URPMI directory Ronw Linux - General 2 10-08-2003 03:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration