listing files in a directory without listing subdirectories and their contents in that directory
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
findcommand using the
-maxdepth 1option - 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.
I am confused `ls -d *` only list the files and folders in the current folder excluding hidden files/folders here;
yes, but i don't want the folders in that directory to be listed, only want the files in that directory to be listed
Similar: List only regular files (but not directories) in current directory at Unix SE
find . -maxdepth 1 -type f -printf '%f\n'
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
findcommand 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
alias lsfiles='for f in *; do [[ -f "$f" ]] && ls -- "$f"; done'
List filenames only:
1. ls -p | grep -v / (without hidden files) 2. ls -l | grep ^- | tr -s ' ' | cut -d ' ' -f 9 (without hidden files) a) ls -pa | grep -v / (with hidden files) b) ls -la | grep ^- | tr -s ' ' | cut -d ' ' -f (with hidden files)
List directories only:
1. ls -p | grep / (without hidden) 2. ls -l | grep ^d | tr -s ' ' | cut -d ' ' -f 9 (without hidden) a) ls -pa | grep / (with hidden) b) ls -l | grep ^d | tr -s ' ' | cut -d ' ' -f 9 (with hidden)
grep -v -e ^$ is to remove blank lines from the result.
ls -p flag is to put '/' at the end of the directory name, -R flag is for recursive search, -l for listing with info, -a for listing all(including hidden files) with info, grep -v flag is for result invertion and -e flag for regex matching.
To list regular files only:
ls -al | grep ^-
With symbolic links included:
ls -al | grep ^[-l]
Where the first character of the list describes the type of file, so
-means that it's a regular file, for symbolic link is
Print the names of the all matching files (including links):
run-parts --list --regex . .
With absolute paths:
run-parts --list --regex . $PWD
Print the names of all files in
/etcthat start with
pand end with
run-parts --list --regex '^p.*d$' /etc
I would suggest to use find and just remove the directory name from the output if necessary:
find . -type f -maxdepth 1 | sed s,^./,,
ls -1 --file-type | grep -v '/' | sed s/@$// > filelist
Another possible option is
ls -F | grep -v '/' | sed /[@*]$// > filelist
--file-typeputs 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.
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!
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`
actually, i am working on a mac. perhaps the command is slightly different on mac os x or unix?