My question is about the ls command. If I have too many files in a folder, I might need either:

  1. ls files with a given type only or;

  2. ls files within a range, i.e., files with names a* to b*.

ls *.jpg # files whose name ends with .jpg
ls [ab]* # files whose name starts with a or b
ls [a-c]* # files whose name starts with characters sorted between a and c in the current locale
ls|head -n100 # first 100 files
f=(*);printf %s\\n "${f[@]:0:100}" # first 100 files
ls|sed -n 101,200p # files 101 to 200
find . -name \*aa\* # files whose name contains aa in the directory tree
find . ! -type d # all files except directories in the directory tree
find . -type f -maxdepth 1 -mindepth 1 # regular files in the current directory
shopt -s extglob;printf %s\\n !(*aa*) # files whose name does not contain aa
shopt -s extglob;printf %s\\n @(aa|bb)* # files whose name starts with aa or bb
  • What about I wannt ls files that are NOT in some criteria? – Jun Jan 6 '14 at 6:22
  • I edited the answer. If you use bash, add shopt -s extglob to ~/.bash_profile, and then you can for example use !(prefix*) to list files that don't start with prefix. – user495470 Jan 8 '14 at 14:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.