Take the 2-minute tour ×
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.

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;

    and

  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
add comment

6 Answers

up vote 16 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
add comment

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
2  
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
    
@Caleb, in this case, find with maxdepth produced just what I needed on the Mac (at least, as far as I can tell - there are several thousand files, so I can't go through it line by line.) –  EmmyS Jul 1 '11 at 19:58
    
@EmmyS: You're quite right, BSD find on OSX has -maxdepth, I just checked one here. @Gilles: To which versions of find is this not portable? –  Caleb Jul 2 '11 at 6:16
show 5 more comments
ls -l /folder | grep ^- | awk '{print $9}'
share|improve this answer
    
What is $9 in this statement? –  EmmyS Oct 17 '12 at 17:05
2  
@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
add comment

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
add comment

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
add comment

I found these short ways:

ls -p uploads | grep -Ev '/'

or

ls -pIt_* uploads | grep -v /
share|improve this answer
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.