Questions tagged [wildcards]

Globbing means matching files by name patterns containing wildcards.

You can specify a set of files matching a certain glob pattern using wildcard characters, e.g. cp *.txt /directory. This is handled by the shell, so it is available on the command line no matter what program you are starting. Conversely, if you need to pass one of the wildcard characters to a command, it must be quoted, e.g. rsync -a --exclude='*.bak' /source /destination.

Globbing is known by various names accross shell documentations: filename generation, pathname expansion, pattern matching, wildcard matching… Advanced shells such as , and implement additions to the basic glob patterns. These additions make wildcards as expressive as regular-expressions , but with a different syntax. There are other programs, such as , that use a similar wildcards syntax to match files by name.

For more complex tasks such as matching files in subdirectories recursively or matching files by metadata such as date or size, try .

References

Further reading

1083 questions
731
votes
5 answers

Zip all files in directory?

Is there a way to zip all files in a given directory with the zip command? I've heard of using *.*, but I want it to work for extensionless files, too.
tkbx
  • 10,597
  • 13
  • 35
  • 41
231
votes
5 answers

How to use wildcards (*) when copying with scp?

Why can't I copy with scp when I'm using * characters in the path? scp SERVERNAME:/DIR/* . What configuration does SCP need in order to allow * in the path? UPDATE: the problem is not on server side; pscp is trying to use SCPv1, and that's why the…
LanceBaynes
  • 39,295
  • 97
  • 250
  • 349
219
votes
12 answers

How do you move all files (including hidden) from one directory to another?

How do I move all files in a directory (including the hidden ones) to another directory? For example, if I have a folder "Foo" with the files ".hidden" and "notHidden" inside, how do I move both files to a directory named "Bar"? The following does…
Cory Klein
  • 18,391
  • 26
  • 81
  • 93
205
votes
11 answers

Rsync filter: copying one pattern only

I am trying to create a directory that will house all and only my PDFs compiled from LaTeX. I like keeping each project in a separate folder, all housed in a big folder called LaTeX. So I tried running: rsync -avn *.pdf ~/LaTeX/ ~/Output/ which…
Seamus
  • 3,553
  • 7
  • 25
  • 25
164
votes
9 answers

List only regular files (but not directories) in current directory

I can use ls -ld */ to list all the directory entries in the current directory. Is there a similarly easy way to just list all the regular files in the current directory? I know I can use find find . -maxdepth 1 -type f or stat stat -c "%F %n" * |…
daniel kullmann
  • 9,427
  • 11
  • 38
  • 45
152
votes
14 answers

rm -rf all files and all hidden files without . & .. error

rm -rf /some/path/* deletes all non-hidden files in that dir (and subdirs). rm -rf /some/path/.* deletes all hidden files in that dir (but not subdirs) and also gives the following error/warning: rm: cannot remove directory: `/some/dir/.' rm: cannot…
Jake Wilson
  • 1,623
  • 2
  • 11
  • 6
146
votes
3 answers

Recursive glob?

I'd like to write something like this: $ ls **.py in order to get all .py filenames, recursively walking a directory hierarchy. Even if there are .py files to find, the shell (bash) gives this output: ls: cannot access **.py: No such file or…
Paolo
  • 16,955
  • 11
  • 31
  • 40
137
votes
9 answers

In linux, how to delete all files EXCEPT the pattern *.txt?

I know how to delete all txt file under current directory by rm *.txt. Does anyone know how to delete all files in current directory EXCEPT txt file?
Firegun
  • 1,829
  • 3
  • 15
  • 9
121
votes
5 answers

Exclude one pattern from glob match

I have several files with the same base filename. I'd like to remove all but one foo.org #keep foo.tex #delete foo.fls #delete foo.bib #delete etc If I didn't need to keep one, I know I could use rm foo.*. TLDP demonstrates ^ to negate a match.…
jake
  • 1,407
  • 2
  • 11
  • 11
111
votes
1 answer

Why does my regular expression work in X but not in Y?

I wrote a regular expression which works well in a certain program (grep, sed, awk, perl, python, ruby, ksh, bash, zsh, find, emacs, vi, vim, gedit, …). But when I use it in a different program (or on a different unix variant), it stops matching.…
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
97
votes
5 answers

The result of ls * , ls ** and ls ***

I know using the command ls will list all the directories. But what does the ls * command do ? I used it and it just lists the directories. Does the star in front of ls mean how deep it will list the directories?
Andy M
  • 1,083
  • 1
  • 9
  • 7
96
votes
1 answer

Why is nullglob not default?

In most shells nullglob isn't the default. That means, for example, if you run this command ls * in an empty directory, it will expand the * glob to a literal *, instead to an empty list of arguments. There are ways to change that behaviour, so…
Dakkaron
  • 1,997
  • 2
  • 16
  • 25
92
votes
5 answers

How do I recursively delete directories with wildcard?

I am working through SSH on a WD My Book World Edition. Basically I would like to start at a particular directory level, and recursively remove all sub-directories matching .Apple*. How would I go about that? I tried rm -rf .Apple* and rm -fR…
codedog
  • 1,023
  • 1
  • 7
  • 5
90
votes
7 answers

How to match case insensitive patterns with ls?

I would like to list all files matching a certain pattern while ignoring the case. For example, I run the following commands: ls *abc* I want to see all the files that have "abc" as a part of the file name, ignoring the case, like -rw-r--r-- 1 mtk…
mtk
  • 26,802
  • 35
  • 91
  • 130
86
votes
4 answers

Using OR patterns in shell wildcards

Contents of my dir are $ ls -lrt total 0 -rw-r--r-- 1 user1 admin 19 Oct 8 12:31 night.txt -rw-r--r-- 1 user1 admin 19 Oct 8 12:31 noon.txt -rw-r--r-- 1 user1 admin 38 Oct 8 12:31 day.txt I would like to list out details of files that have a…
mtk
  • 26,802
  • 35
  • 91
  • 130
1
2 3
72 73