How to list files without directories, and filter by name (ls options)

This sounds like a job for find.

  • Use -maxdepth to only return the current directory, not recursivly search inside subfolders
  • Use -type f to only return files and not directories or device nodes or whatever else
  • Use a combination if -not and -name to avoid the files with names you don't want

It might come together like this:

find /path/to/uploads -maxdepth 1 -type f -not -name 't_*'

GNU ls (i.e. the ls command on non-embedded Linux systems and Cygwin, also available on some other unices) has an option to hide some files, based on their names. There's no way to ignore directories though.

ls --hide='t_*' uploads

Another approach is to make your shell do the matching. Bash, ksh and zsh have a negation pattern !(t_*) to match all files except those matching t*; in bash this feature needs to be turned on with shopt -s extglob, and in zsh it needs to be turned on with setopt ksh_glob. Zsh also has the equivalent syntax ^t_* which needs to be turned on with setopt extended_glob. This still doesn't ignore directories. Zsh has an extra feature that allows to match files not only by name but also by metadata and more: glob qualifiers. Add (.) at the end of a match to restrict to regular files. The negation ^ is part of the name matching syntax, so ^t_*(.) means “all regular files not matching t_*” and not “all files that aren't regular files matching t_*”.

setopt extended_glob  # put this in your ~/.zshrc
ls uploads/^t_*(.)

If you find yourself without advanced tools, you can do this on any unix with find. It's not the kind of thing you'd typically type on the command line, but it's powerful and precise. Caleb has already shown how to do this with GNU find. The -maxdepth option isn't portable; you can use -prune instead, to portably stop find from recursing.

find uploads/* -type d -prune -o \! -type f -name 't_*' -print

Replace -print by -exec ls -lG -- {} + to execute ls with your favorite options on the files.

All the commands above hide dot files (i.e. files whose name begins with a .). If you want to display them, pass -A to ls, or add the D glob qualifier in zsh (ls uploads/^t_*(.D)). With find, you can use a different approach of making it recurse one level only (find doesn't treat dot files specially). This only fully works if you run find in the current directory.

cd uploads && find . -name . -o -type d -prune -o \! -type f -name 't_*' -print

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

Tags:

Filenames

Ls