1

I've used DOS for 22 years but I'm new to Linux. So in Linux, when I execute

ls -R *.zip

Why do I receive:

ls: *.zip: No such file or directory

When I'm at the top of a huge folder and file tree? Alternatively, how do I do

Dir /s *.zip

In Linux?

1

2 Answers 2

3

Try ls -R|grep .zip or for full paths:

find .  -name '*.zip'

This will find any files ending in .zip in current directory and any subdirectories.

It seems a little longer but is much more powerful than the dir /s *.zip way of windows.

2
  • Okay - I getcha. I sometimes use dir /s/a/b|find /i "blah" which is conceptually the same. Nov 27, 2012 at 12:42
  • Exactly; grep matches a string, or if you use "grep -E 'blah'" then you can use regular expressions in it
    – Justin
    Nov 28, 2012 at 0:09
4

To expand a bit further on the above answer, on Linux, the wildcards are expanded by the shell, which means that the shell will call the command with a list of matching file names as arguments. And the shell only looks in the current directory for matching files (or in whatever directory you specify prior to the wildcard). For example, a command such as:

ls *.zip

will actually translate into:

ls file1.zip file2.zip file3.zip

And, if there are no .zip files in the current directory, the literal file name *.zip gets passed to the command, in which case it doesn't fine a file called *.zip. (A * is a legal character in a Unix/Linux file name, btw). Now if you don't want the shell to expand the wildcards, but instead if you want the wildcard passed directly to the command you are calling, you will have to enclose it in quotes. This has an interesting effect, in that a command such as:

find . -name *.zip

will give the expected results if you have no .zip files in your current directory (the find command sees *.zip). But if you do have one or more .zip files in the current directory, what gets executed is:

find . -name file1.zip file2.zip

which won't find any matches in any subdirectories (unless they happen to be named exactly file1.zip or file2.zip, etc). So the moral of the story is, when using find, make sure to use quotes around any file name with wildcards.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .