Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listing files using the ls command 1

Status
Not open for further replies.

kupham

MIS
Sep 14, 2001
6
US
I am trying to list just the files in a directory (not the subdirectories or files under the subdirectories) into a temporary file. I have defined the variable VAR_DIRECTORY as the directory I would like the files listed from.
When I use the following command:

ls $VAR_DIRECTORY/*.* >> temp.txt

some directories contain too many files and I receive an error "The parameter list is too long" I don't want to cd to the directory because I want the directory and file name to both be written to temp.txt. So it would look like $VAR_DIRECTORY/filename.

Is there another command I should use? Does ls have any options that would fix this problem?
 
You can use find for this. The '-maxdepth' argument controls how far find descends into directories.

find /$VAR_DIRECTORY/*.* -maxdepth 0 -print >> temp.txt
 
While I'm a big fan of find, I think that this is overkill when ls can do exactly what you desire (with less keystokes). Use the -d flag:

ls -d $VAR_DIRECTORY/*.* >> temp.txt

This will cause ls to list directory names instead of the contents of directories.

Now, if you want to list just files and not directory entries, on the otherhand, find has its place.

- Steve StevieW85@go.com
 
Another option is to use the 'type' argument of find.

To find only regular files:
find DIR -type f -print
To find only directories:
find DIR -type d -print
 
If you are in the directory of a file, is there a way to list the entire path of that file with the ls command?

 
kupham,

The easiest is to give ls the current directory:
ls `pwd`
or
ls $CWD

Alternatively, if you're trying to list only files that match the "*.*", as in your original question, use
ls -d `pwd`/*.*
(Again, use the -d to limit the list to entries in the current directory rather than getting directory listings for the *.*/ directories.)

- Steve StevieW85@go.com
 
I still receive the error that the parameter list is too lonb because of the *.*
 
I think the problem is the *.* you're including. To list all contents of the specifed directory, you don't even need to use wildcards. This will list all files in the specifed directory (it excludes all subdirectories & their files) ...

ls -l $VAR_DIRECTORY|grep -v ^d|tr -s " "|cut -d" " -f9

Greg.
 
The command Greg listed works but it only lists the file name. How do you list the entire path and the file name using the command he gave me (see below)?

ls -l $VAR_DIRECTORY|grep -v ^d|tr -s " "|cut -d" " -f9


Also, can someone explain the command above to me? Thanks!
 
I think the best solution is to use the find command but unfortunately the -maxdepth option is not available on my system. Is there any other way to limit the find command to just the files in the current directory without using *.*?
 
Why do you need to do it this particular way?

Anyway, this will print the directory name in front of the filename

ls -l $VAR_DIRECTORY|grep -v ^d|tr -s " "|awk '{print lead"/"$9}' lead=$VAR_DIRECTORY

If I split it up to explain what it does it says ...

ls -l $VAR_DIRECTORY - list all files in the directory

grep -v ^d - then show me all lines which don't start with d

tr -s " " - remove extra spaces in each line so there is only 1 space between each word

awk '{print lead"/"$9}' lead=$VAR_DIRECTORY - Take the filenames, and shove the directory name in front.

Regards,

Greg.
 
One way to limit the find command:
Code:
cd /some/dir
find . \( -type d ! -name . -prune \) -o \( -type f -print \)
The first brackets stops recursion into any directory except the current one.

This find command does not suffer the parameter list too long problem that ls does, as shell interpolation is not involved.

The find command can be piped on further to other commands.

Example 1. Copy files to backup_dir:
Code:
find . \( -type d ! -name . -prune \) -o \( -type f -print \) | xargs -i cp {} backup_dir

Example 2. Compress, UUencode, Mail:
Code:
find . \( -type d ! -name . -prune \) -o \( -type f -print \) | while read file
do
    compress -f $file
    uuencode $file.Z $file.Z > $file.uu
    cat $file.uu | mail fred
done

Happy coding. Cheers NEIL ;-)


 
Other way to list only files
without the file path output

%find $DIR -type f -print -type f -printf "%f\n"

i use printf to format the output to list
only filenames(%f) and the '\n' to print
the LineFeed Character.


Have Fun.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top