Globbing means matching files by name patterns containing wildcards.
27
votes
5answers
2k views
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. Is the star in front of ls means how deep it can list the ...
21
votes
7answers
2k views
why does ls -d also list files, and where is it documented?
when specifying ls --directory a* it should list only directories starting with a*
BUT it lists files AND directories starting with a
Questions:
where might I find some documentation on this, ...
18
votes
7answers
11k views
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 ...
17
votes
1answer
649 views
Why doesn't 'find' show this file?
Using find with grep, one can locate files that match a pattern:
# find | grep error
./solr-modifiedSolr4/SolrPhpClient/phpdocs/errors.html
./error_log
./includes/classes/error_log
However, using ...
17
votes
2answers
791 views
What does .??* mean in a shell command?
The following command will tar all "dot" files and folders:
tar -zcvf dotfiles.tar.gz .??*
I am familiar with regular expressions, but I don't understand how to interpret .??*. I executed ls .??* ...
14
votes
4answers
3k views
How to copy some, but not all files?
So, you can use the * as a wild card for all files when using cp within context of a directory. Is there a way to copy all files except x file?
14
votes
3answers
13k views
How do you move all files (including hidden) in a 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 ...
12
votes
8answers
10k views
Converting multiple image files from JPEG to PDF format
I want to convert some files from jpeg to pdf. I am using following command.
$ convert image1.jpg image1.pdf
But I have 100 images. How should I convert all of them to corresponding pdfs?
I ...
12
votes
5answers
3k views
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 need to see all the file that have "abc" as a part of the ...
11
votes
4answers
448 views
How do I choose specific files in a different directory using bash?
I want to list (or delete, or do some other operation) on certain files in a directory, like this:
$ ls /opt/somedir/
aa bb cc aa.txt bb.txt cc.txt
$ ls /opt/somedir/(aa|bb|cc) ## pseudo-bash ...
11
votes
2answers
217 views
Can I make `rm` interactive only when using globbing? (in either bash or zsh or both)
Whenever I rm multiple items at once with shell globbing and there's even the slightest possibility that the pattern expands to more than I expect it to, I always try to remember to add -i, but (of ...
10
votes
5answers
360 views
Pattern matching on path names in bash
I want to act on a list of subdirectories in a directory. Consider:
for x in x86-headers/*/C/populate.sh; do echo $x; done
This gives
x86-headers/elf/C/populate.sh
x86-headers/gl/C/populate.sh
...
10
votes
1answer
377 views
How do ${0##*/} and ${0%/*} work?
I'm quite confused about the following regular expressions I found in a shell script:
${0##*/}
${0%/*}
How do they work?
9
votes
3answers
325 views
what is wild card to select all directories in a directory
i want to set sticky bit for all directories in a directory excluding files. is there any wild card to do this?
#sudo chmod g+s /var/www/<WILD_CARD_FOR_ALL_DIRECTORIES>
8
votes
3answers
2k views
How can I remove all symbolic links with a special target?
With the command:
ls -la *
I can list all my symbolic links.
How can I remove all symbolic links which are linked to a special folder?
For example:
In my directory usr/local/bin I have the ...
8
votes
5answers
234 views
Can I select only one result from a bash glob?
I'm trying to write a script for work to automate some reporting on an output. The Log files are (currently, it's being 'standardise' in the future) stored in this sort of path structure:
...
8
votes
7answers
845 views
How do I delete everything in a directory?
I'm sorry for asking such a basic question:
How do I delete everything in a directory, including hidden files and directories?
Right now, I use the following:
rm -rf *
rm -rf .*
7
votes
7answers
562 views
How do I create a directory in all subdirectories?
Suppose I have a directory structure like this:
$ [~/practice] ls
a/ b/ c/ d/
Now I want to create a directory tmp1 in all sub directories of practice and I do this:
$ [~/practice] mkdir */tmp1
...
7
votes
6answers
4k views
What's the best way to count the number of files in a directory?
If parsing the output of ls is dangerous because it can break on some funky characters (spaces, \n, ... ), what's the best way to know the number of files in a directory?
I usualy rely on find to ...
7
votes
2answers
205 views
“grep -r foo *” vs “grep -r foo .”
When using grep -r you could search in all files with either * or .
and it seems to return the same thing but is it really the same?
Let's say I search for "foo", then I could write
grep -r foo *
...
7
votes
2answers
4k views
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 configs do SCP need to allow "*" in the path?
UPDATE: the problem is not on server side, pscp is ...
7
votes
1answer
134 views
What is the significance of the dot in bash commands and how is it different from an asterisk?
I'm trying to understand the significance of the dot in bash and how it differs from an asterisk. Can someone please elaborate? For example, what's the difference between cp -ar /foo/. /foo2/ and cp ...
7
votes
1answer
3k views
How to download specific files from some url path with wget
If I don't want to have to download the files found in a specific url path manually, what options do I have? Using wildcards fail:
$ wget 'http://www.shinken-monitoring.org/pub/debian/*deb'
Warning: ...
7
votes
1answer
360 views
Why is bash extended-globbing variable substitution acting at the byte level?
I thought that bash variable substitution and globbing worked at character resolution, so I was rather surprised to see it acting at the byte level.
Everything in my locale is en_AU.UTF-8
When ...
6
votes
1answer
6k views
Bash: assign ls | grep to a variable and echo it with a string
I want to assign the result of an expression to a variable and concatenate it with a string, then echo it. Here's what I've got:
#!/bin/bash
cd ~/Desktop;
thefile= ls -t -U | grep -m 1 "Screen Shot";
...
6
votes
2answers
221 views
What is the setting in bash for globbing, to control whether * matches dot files
I was surprised recently when I did something like
mv ./* ../somedirectory
and found that files like .gitignore were not moved.
I do most of my work in zsh on OS X, and this surprise bit me in bash ...
6
votes
3answers
3k views
How do I reverse a for loop?
How do I properly do a for loop in reverse order?
for f in /var/logs/foo*.log; do
bar "$f"
done
I need a solution that doesn't break for funky characters in the file names.
6
votes
1answer
123 views
Wildcards inside quotes
This will be an easy one, but in my memories, when shell scripting, using double quotes would allow expanding globbing and variables.
But in the following code:
#!/bin/sh
echo *.sh
echo "*.sh"
echo ...
6
votes
1answer
249 views
Can I have my shell history record how wildcards expanded?
If I run:
$ ls *
foo bar buzz
$ history | tail -1
ls *
You can see that in my shell history it remembers that I ran ls * rather than ls foo bar buzz. Ideally, I'd like to record both in separate ...
5
votes
5answers
337 views
What is the difference between $* and $@?
Consider the following code:
foo () {
echo $*
}
bar () {
echo $@
}
foo 1 2 3 4
bar 1 2 3 4
It outputs:
1 2 3 4
1 2 3 4
I am using Ksh88, but I am interested in other common ...
5
votes
2answers
612 views
Why doesn't “rm *” work when there are files that begin with a hyphen?
from: http://seclists.org/fulldisclosure/2011/Sep/190
[USER@MACHINE ~] mkdir ejha
[USER@MACHINE ~] cd ejha/
[USER@MACHINE ~/ejha] touch ize
[USER@MACHINE ~/ejha] touch -- -f -i
[USER@MACHINE ...
5
votes
5answers
2k views
How to copy all html files from a directory tree to a single directory?
I want to copy all the .html files in myDir and its subdirectories to ~/otherDir. Here's what I tried (it doesn't work):
$ find myDir -name *.html -print | xargs -0 cp ~/otherDir
usage: cp [-R [-H | ...
5
votes
3answers
209 views
How do I remove all sub-directories from within a directory?
This question is kind of a phase II to the first question I posted at here
I have a directory that contains a bunch of sub-directories, .zip files, and other random files not contained within a ...
5
votes
3answers
264 views
“mkdir foo; svn mv * foo”
I often want to do some variant of this idiom:
$ mkdir 2010
$ svn mv * 2010
Of course, I get an error because the glob matches 2010 as well:
svn: Cannot copy path '2010' into its own child ...
5
votes
4answers
1k views
Find a string only in a specific file inside subdirectories
Let's say I need to find the function GetTypes() in all C# source file (.cs) the directories/subdirectories.
I used grep -rn GetTypes *.cs, but I got an error with grep: *.cs: No such file or ...
5
votes
2answers
147 views
How to put the specific files from a directory in an array in bash?
Suppose I have a directory under which there are 3 files named: file1.txt,file2.txt and file3.txt.
Now how can I fill an array with those file names(I just know that all the files have certain ...
5
votes
1answer
2k views
What is the .gitignore pattern equivalent of the regex (Big|Small)(State|City)-[0-9]*\.csv
I have a regex I stuck in my .gitignore similar to:
(Big|Small)(State|City)-[0-9]*\.csv
It didn't work so I tested it against RegexLab.NET.
I then found the gitignore man page which led me to ...
5
votes
2answers
120 views
Remove all Vim undo files in all but one directory
I just realized that I have tons of Vim undo (.un~) files sprinkled around my file system. I'd like to delete all of these files except in one directory—~/.tmp. My first problem is that I can't seem ...
5
votes
1answer
66 views
Bash globbing: how to express “something-*.txt except *-foobar.txt” as glob?
On the command line, can I express easily in terms of shell globs that I would like to catch all files something-*.txt, except for something-*-foobar.txt? If I can, how?
NB: I know how to achieve the ...
5
votes
1answer
1k views
Listing with `ls` and regular expression
How can I list files with a filename ending with last character and with .txt extension ?
I have tried ls *+([[:digit:]]).txt but this is true for abc12.txt and abc2.txt.
But I need to get only ...
5
votes
3answers
336 views
Glob with Numerical Order
I have this list of pdf files in a directory:
c0.pdf c12.pdf c15.pdf c18.pdf c20.pdf c4.pdf c7.pdf
c10.pdf c13.pdf c16.pdf c19.pdf c2.pdf c5.pdf c8.pdf
c11.pdf c14.pdf c17.pdf c1.pdf ...
5
votes
3answers
289 views
Bash globbing of multi-part argument
Below are some examples of a find command I'm trying to run. I'm looking for different ways that I might be able to use globbing to generate (as an example) a find command with predicates joined ...
5
votes
2answers
500 views
ls, regexp and environment variable
I wanted to declare an environment variable that stocks all the extensions of video files so I can use it when using the shell.
I tried several things but never got it to work:
If in my .bash_profile ...
4
votes
4answers
243 views
Avoiding errors due to unexpanded asterisk
In bash I often use for-loops such as the following
for file in *.type; do
sommecommand "$file";
done;
to perform an operation for all files matching *.type. If no file with this ending is ...
4
votes
3answers
138 views
Create symbolic links to files using wildcards
I want to create symlinks to multiple files:
ln -s dev-*.php 's/dev-(.*\.php)/$1/'
Results hoped for:
site.php links to dev-site.php
file.php links to dev-file.php
What's the most ...
4
votes
2answers
168 views
Why 2 linux machine behave differently with command of the same syntax?
I am using 2 machine , which is both Red Hat Enterprise Linux AS release 3 (Taroon Update 2)
( I check it in /etc/*-release ).
I checked they are using the same default shell by ps -p $$, which is ...
4
votes
3answers
941 views
Why does tar --exclude=“.*” create an empty archive?
Everything I read says that to exclude .svn and .htaccess and other hidden files when creating a tar archive, use the --exclude=".*" pattern.
When I try, I get an empty archive. When I leave out the ...
4
votes
3answers
228 views
How to pipe to multiple places with vertical bar?
I have a folder full of files with a .dot extension like this:
a.dot
b.dot
c.dot
I want to go through all the files in this folder and run a command on them such as this:
dot -Tpdf a.dot -o a.pdf
...
4
votes
2answers
487 views
mv on a glob pattern does not work without sudo
I am using MySQL database on Ubuntu machine.
My MySQL data directory is /var/lib/mysql/ , since I have a database named "db_test" , so, I have a directory named db_test/ under /var/lib/mysql/ . And ...
4
votes
2answers
481 views
ZSH: Recursive globbing with .directories
I thought the glob pattern:
**/(*|.*)
would represent every folder and file starting with dot (.) or not, but it skips directories in the current directory that start with ..
What is the glob ...

