0

In linux, I have a files contains, amongst other, lots of files and directories containing the literal "scrap". I can see them by typing ls *scrap* without problem. However that returns also directories containing the literal "scrap" and i would like to exclude those.

I tried playing around with ls | grep ".*scrap.*" or ls | grep scrap but this returns only the directories (not the normal files).

Why is this so?

The next step will be (once the rep returns the correct output) to remove the directories from that output. Suggestions on how to do that are also welcomed!

  • 1
    The last paragraph is the actual question here, right ? The rest is a typical example of XY question... – don_crissti Nov 25 '17 at 21:02
  • "but this returns only the directories (not the normal files)" I cannot reproduce that. – Hauke Laging Nov 25 '17 at 21:49
  • The meaning of the first sentance in your question is a little unclear (for me):"In linux, I have a files contains, amongst other, lots of files and directories containing the literal "scrap"." – Robb W. Nov 26 '17 at 00:27
  • Whoops. The meaning of the first sentence in your question is a little unclear (for me): 'In linux, I have a files contains, amongst other, lots of files and directories containing the literal "scrap".' Are you talking about files and/or directories that exist and have the string "scrap" in their names? Or are you describing a file that contains a list of files and/or directory names? If "ls | grep scrap" only returns directories, then there are only directories with scrap in their names. As others mentioned, it might be better to describe just your ultimate goal. – Robb W. Nov 26 '17 at 00:35

4 Answers4

4

Actually, to eventually list all regular files in the current directory (on the 1st level) which have scrap in their names, you don't need ls + grep.

With old good find command:

find . -maxdepth 1 -type f -name "*scrap*" -ls
  • im asking with grep because in the end i want to move the files that are found with grep using mv | grep scrap scraping – jim jarnac Nov 25 '17 at 21:08
  • @jimbasquiat, no problem with that: use find's -exec action as -exec mv ... – RomanPerekhrest Nov 25 '17 at 21:11
  • just tried the suggestion find . -maxdepth 1 -type f -name "*scrap*" -ls but it does not return any result. I tried ls *scrap* again, which works as intended – jim jarnac Nov 25 '17 at 21:14
  • @jimbasquiat, you may omit -ls action – RomanPerekhrest Nov 25 '17 at 21:17
  • I also did that but still no results... find . -maxdepth 1 works fine as well. It is when i tried to add the name flag with find . -maxdepth 1 -name "*scrap*" that it starts returning only the directories... It is supposed to reutn everything but directories, to be clear – jim jarnac Nov 25 '17 at 21:20
  • @jim it you update your question to explain what you want to achieve, rather then seeking a solution to one way of getting there, you might well get some more optimal solutions being offered – Chris Davies Nov 25 '17 at 23:43
1

You run into problems with ls and grep when file names contain spaces or (worse) newlines. But with safe file names you can do this:

ls -l | grep -oP '^-.* \K.*$'
ls -l | sed -n '/^-/s/^.* //p'

But usually you should use the find approach.

Hauke Laging
  • 90,279
0

For the names of the non-directory files in the current directory that contain scrap, in zsh:

print -rC1 -- *scrap*(ND^/)

Note that in ls *scrap* (or the more correct ls -- *scrap*), if any of the *scrap* files are of type directory, then ls will list their contents (with a that-scrap-dir: header unless *scrap* expand into one and only one file that is that directory).

If you want that for files of type directory, ls just prints their name like for other types of files, you'd need to add the -d option:

ls -d -- *scap*(D^/)

But there's hardly any point using ls as all the work of finding files by name and type has been done by the shell by the time ls is invoked, you might as well use the builtin print or printf of your shell.

-2

Seems like this is the answer you're looking for. The post speaks into glob expansion being the reason why only certain things are showing.

qwerty
  • 1