Glam Prestige Journal

Bright entertainment trends with youth appeal.

I want to list all of my files in a directory in FreeBSD 9.2, but I don't want to include a specific directory. I checked both -I and --ignore parameters for ls, but I only ignores superuser mode and --ignore is not supported.

Does anyone have an idea how to use ls or do that using another command (like grep)?

2 Answers

If you want to list all the files except those within a certain directory, say, notHere you can use the find command to do it like

find . -path ./notHere -prune -o -type f -print

Explanation:

  • .: the directory to start the find in
  • -path ./notHere: start a rule that will match the path ./notHere
  • -prune: prevent find from descending into the directory with the current match
  • -o: add an or to specify what we want to happen if the first rule did not match
  • -type f: match only files, not, say, directories (remove this if you want to see the directory entries as well)
  • -print: often not needed as it's the default action, but some versions of find still want you to expressly say that you want the result to be printed out

You mention wanting other output (specifically ls -ll) than just the list of file names. find often has a builtin to help with that, though not those specific options to ls. You could replace the -print flag above with -ls and see if that set of ls options is close enough for you:

find . -path ./notHere -prune -o -type f -ls

or if that isn't good enough, you could have find run the command you want instead of trying to process the output separately like:

find . -path ./notHere -prune -o -type f -exec ls -ll {} +
3

I use this script to filter out all folders with names starting with '_'. It probably won't work with folders that have spaces but you could amend it.

#!/bin/sh
A=`ls /j/ | xargs | sed 's/_[a-z0-9]*//g' | awk '{$1=$1;print}'`
W=`echo $A | wc -w | tr -d '[[:space:]]'`
echo $W

In general the part that's interesting is this:

ls some_folder | xargs | sed 's/_[a-z0-9]*//g'

The regex _[a-z0-9]* contains the pattern to match against the name that should be excluded.

This is if you want list folders without the -a option (just names). If you want to use -a then the simplest is:

ls -la | grep -v skip_folder

where skip_folder is the name that you want to exclude. Use quotation marks if the folder contains spaces, e.g.:

ls -la | grep -v "skip folder"
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy