Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory called uploads. It contains a bunch of files, plus a few subdirectories which in turn contain files.

Is there a way I can (in one step) do the following:

  1. List ONLY the files in the root uploads directory -- I do not want to see the subfolder names or their contents;


  2. Do NOT list any files that start with t_

I know about the -d flag, but it doesn't get me quite what I want.

share|improve this question
up vote 32 down vote accepted

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_*'
share|improve this answer
Thanks! I'm pretty new to CLI and had never used find before. I'll definitely look further into it. – EmmyS Jul 1 '11 at 17:50
@EmmyS: You might find one more little trick useful here. @Gilles mentioned using -exec ls -lG -- {} + in his answer to get the output using extra options to ls. You can also add just an -ls to this find to get a quick and dirty approximation of ls's detailed view. – Caleb Jul 2 '11 at 9:12

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
share|improve this answer
Do you have a dayjob? ;-) I'm constantly amazed at the length and level of details of your answers, even to the simplest looking questions here at Unix SE!.. Keep that up anyway! :) – alex Jul 1 '11 at 19:01
Thanks! Great detail. I don't know if I have "advanced tools" or not right now; I'm on a Mac at work (although Linux at home, so I'm sure this will come in handy at some point.) – EmmyS Jul 1 '11 at 19:30
@EmmyS: For your linux box my use of -maxdepth should work, but the "portability" problem Gilles refers to is that the BSD find (also found on OSX I think) is a bit different animal, so you might need his answer too. @Gilles: +1 for the zsh trick! – Caleb Jul 1 '11 at 19:54
@Gilles: Some not so juicy comments about prunes come to mind, but I'll let it go by simply saying that when I'm mucking around on my own systems the number of times the thought "would this command work if I was on AIX, Solaris or MINIX3" is approximately ... standby ... I'm having a hard time getting a meter reading ... the display is frozen with 0.00. – Caleb Jul 2 '11 at 9:17
Doesn't Gnu ls have a --use-telepathy option? That's the quickest way to display just the files you want. It might not be portable though. – dubiousjim Oct 17 '12 at 18:20
ls -l /folder | grep ^- | awk '{print $9}'
share|improve this answer
What is $9 in this statement? – EmmyS Oct 17 '12 at 17:05
@EmmyS It means the 9th field / column of the output. This answer is trying to parse ls output. – jw013 Oct 17 '12 at 17:36

Let me give it a shot using ls and pipes:

ls  -l --hide='t_*'  uploads/ | grep -v ^d | tr -s ' ' | cut -d ' ' -f 9

say last column is the 9th one.

share|improve this answer
What if there are files with spaces? – jordanm Oct 17 '12 at 17:52

If find's -maxdepth portability issue is a concern, you could use:

ls -l uploads/ | awk '/^-/ && $9 !~/^t_/ {print $9}'

I'm not an expert, but I think this should work. I suppose it looks a little cryptic, but if you speak awk, it's really not. $NF could be used instead of $9. You could also do:

ls -l uploads/ | awk '/^-/' | awk '!/^t_/ {print $9}'

which does the same thing (but perhaps less efficiently than the former ???).

Actually, after thinking about the other posts, it looks like

ls -l --hide=t_* uploads/ | awk '/^-/ {print $9}'

is an even shorter and clearer way.

Someone please correct me if I'm wrong. :-)

share|improve this answer
Parsing ls is generally not a good idea. Your awk tricks will fail with filenames with whitespace in them for instance. – Mat Jun 23 '13 at 18:53
@mat -- Thank you for the correction. After reading the article that you (and jw013) pointed out, I see that ls cannot be trusted to output correct file names. Even if I were to rewrite the awk code to print everything from $9 to the end of the line (to include whitespaces), this ls output may still be erroneous. This is disappointing and should be corrected IMO. Thanks again. – kkaszub Jun 24 '13 at 3:37

I found these short ways:

ls -p uploads | grep -Ev '/'


ls -pIt_* uploads | grep -v /
share|improve this answer
ls -l | grep -v ^d | grep name$
share|improve this answer
What would make this solution (which requires multiple commands and pipes) preferable to find? – HalosGhost Oct 3 '14 at 16:49

Your Answer


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.