Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to create a list of all the files in a directory, without listing any of the subdirectories that reside in that same directory, and print that list to a new file. ls -d * > filelist will create a list of all the files in the current directory, but it also lists the subdirectories in the current directory. I tried the find command using the -maxdepth 1 option - however, the output format is a problem as find also prints out the path along with the file names. If anyone can please tell me perhaps another command or options to use that will produce an output list of just the files in a directory and not the names of the subdirectories or their contents, I would appreciate it. thank you, Janet

share|improve this question
    
I am confused ls -d * only list the files and folders in the current folder excluding hidden files/folders here; –  GM-Script-Writer-62850 May 2 '13 at 0:51
    
yes, but i don't want the folders in that directory to be listed, only want the files in that directory to be listed –  janet May 2 '13 at 2:27
add comment

4 Answers 4

I suggest to use find and just remove the directory name from the output if necessary

find . -type f -maxdepth 1 | sed s,^./,,

share|improve this answer
1  
-maxdepth 1 should come before any filters, and you can achieve this result without using sed with the -printf option, i.e.: find . -maxdepth 1 -type f -printf '%f\n' (this will be more efficient than using an extra program). –  evilsoup Oct 2 '13 at 7:11
add comment

Find-based solution:

find . -maxdepth 1 -type f -printf '%f\n'

Bash-based solution:

for f in *; do [[ -d "$f" ]] || echo "$f"; done
##  or, if you want coloured output:
for f in *; do [[ -d "$f" ]] || ls -- "$f"; done

The bash-based solution will get you everything that isn't a directory; it will include things like named pipes (you probably want this). If you specifically want just files, either use the find command or one of these:

for f in *; do [[ -f "$f" ]] && echo "$f"; done
##  or, if you want coloured output:
for f in *; do [[ -f "$f" ]] && ls -- "$f"; done

If you're going to be using this regularly, you can of course put this into an alias somewhere in your ~/.bashrc:

alias lsfiles='for f in *; do [[ -f "$f" ]] && ls -- "$f"; done'

Since you noted in the comments that you're actually on OSX rather than Ubuntu, I would suggest that next time you direct questions to the Apple or more general Unix & Linux Stack Exchange sites.

share|improve this answer
add comment

Yet another solution, a naively short one that worked for me:

ls -la | grep -E '^[^d]' > files
share|improve this answer
    
perfect for me, ls -alR |grep -E '^-' –  here Jan 7 at 9:21
add comment
ls -1 --file-type | grep -v '/' | sed s/@$// > filelist

Another possible option is

ls -F | grep -v '/' | sed /[@*]$// > filelist

The --file-type puts a / at the end of the folders (but also a @ at the end of symbolic links. The grep -v '/' removes the subdirectories (because they now end with a '/'). The sed s/@$// removes that @. The -1 prints one file per line so that the grep -v will work correctly.

share|improve this answer
    
thank you for your response. unfortunately, it's not working for me, perhaps i am executing it incorrectly. if i use the exact command you gave me, i get the following error: ls: illegal option -- - usage: ls [-ABCFGHLPRSTWabcdefghiklmnopqrstuwx1] [file ...] i tried using the * wildcard character in place of as well as in conjunction with --file-type. if you could clarify i would appreciate it! –  janet May 2 '13 at 2:16
    
The command works for me (running 13.04), what version of ubuntu are you using?; -1 does not matter to grep you don't need it; if you want to includes hidden files use the -a option on ls –  GM-Script-Writer-62850 May 2 '13 at 2:48
    
actually, i am working on a mac. perhaps the command is slightly different on mac os x or unix? –  janet May 2 '13 at 2:50
    
You could also try ls -F | grep -v '/' | sed s/[@*]$// > filelist –  Dr.Tower May 2 '13 at 3:07
    
thank you! that did the trick! –  janet May 2 '13 at 3:17
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.