Tag Info

New answers tagged

6

As mentioned in my comment, that is a common error people see when trying to use extended globs(shopt -s extglob) before enabling the option. This includes function definitions.


2

The syntax you use in echo **/*(AIE) it for zsh; It does not work for bash, for example. The characters in the () are glob qualifiers; Multiple qualifiers are combined by logical AND - that is, they all ned to apply. So the command above shows filenames that are readable, writable and executable for the group. They have the permissions that are set by ...


3

This looks for files in the data directory that contain a dot followed by f or u but not ending in f or u: ls -lL datafiles/*.*[fu]*[^fu] Answer to version 1 of this question You wrote "f or u". To write that in a glob, use [fu]. To also insist that the f or u be preceded by a dot, try: ls -lL .*[fu]* *.*[fu]* The first glob above, .*[fu]*, expands ...


3

On Unix-like systems, *.sh is a glob expression that is expanded by the shell and the results are passed as arguments to the program being invoked. If and only if there are no matching files will the glob expression be passed as-is. You should get in the habit of quoting wildcards if you want them to be passed to the program you're running. As an example, ...


1

This is mostly a duplicate of this question, but the reason you get no output is a bit different. With -path, the argument must match the entire path (including the command line argument). When foo.sh exists in the current directory, you're executing find . -path foo.sh Because foo.sh does not match ./foo.sh, you get no output. The solution is the same ...


9

* in shell patterns matches 0 or more characters. It's not to be confused with the * regular expression operator that means 0 or more of the preceding atom. There is no equivalent of regexp * in basic shell patterns. However, various shells have extensions for that. ksh has *(something): ls a_*([a-z])_data you can have the same in bash with shopt -s ...


18

So the problem is: why does a_[a-z]*_data match a_clean_0db_data? This can be broken down into four parts: a_ matches the beginning of a_clean_0db_data, leaving clean_0db_data to be matched [a-z] matches any character in the range a-z (e.g. c), leaving lean_0db_data to be matched * matches any number of characters, e.g. lean_0db _data matches the trailing ...


26

The [a-z] part isn't what matches the number; it's the *. You may be confusing shell globbing and regular expressions. Tools like grep accept various flavours of regexes (basic by default, -E for extended, -P for Perl regex) E.g. (-v inverts the match) $ ls a_[a-z]*_data | grep -v "[0-9]" a_clean_data If you want to use a bash regex, here is an example ...


0

Following Kyle Jones advice to use available rsync options to do the job (instead of coding one) I've found rsync --include-from=file_to_exclude --recursive \ --delete-excluded \ /var/somedir/ /var/somedir/ to work just fine. I've also tried using --ignore-existing --ignore-non-existing --delete on multiple scenarios but the result was the same as without ...


0

Don't do this. It is a very bad idea to try to replicate what rsync include/exclude patterns do without using rsync. The fact that some of the patterns can be complex is even more reason not to attempt it. Use rsync itself to guarantee consistent behavior and minimize surprises. From the rsync manual page: --existing, --ignore-non-existing ...


9

If you want to loop over all the arguments to your script, in any Bourne like shell, it's: for i do something with "$i" done You could also do: for i in "$@"; do something with "$i" done but it's longer and not as portable (though is for modern shells). Note that: for i; do something with "$i" done is neither Bourne nor POSIX so should be ...


0

#!/bin/bash n=1 echo "$0 got $# args..." while [ $# -gt 0 ] ;do echo "$n: $1" shift n=$(( $n + 1 )) done Alternatively, look up 'Listing arguments with $* and $@' in http://www.tldp.org/LDP/abs/html/abs-guide.html which will elaborate on many aspects of these.


1

What does mv * do? Here's a shorter answer: The shell expands the wildcard * to a list of directory contents. Then the shell passes that full list to the command. The command never sees *. The command mv file1 file2 ... filen directory will move file1 ... filen into directory. Example Here I make a test directory containing three files $ mkdir t $ ...


0

To check what exactly mv command did, you may check it by adding echo before the command, so shell will expand all wildcards and print the result command, e.g.: $ echo mv /tmp/folder/* /* $ echo mv /tmp/* /* mv /tmp/launch-4TgsLB /tmp/skl /bin /dev /etc /home /lost+found /mnt /net /opt /private /sbin /tmp /usr /var So basically it'll move your files to ...


27

First I'll make a test base - 5 files and one folder: touch file1 file2 file3 file4 file5 mkdir folder Next I'll run a test command. The -v option specifies that I want every command the shell executes to be printed to stderr. The -x option specifies that I want the same printed to stderr - but I want it done after the command is evaluated but before the ...


3

Unquoted variables and command substitutions like $i or $(git …) apply the split+glob operator to the string result. That is: Build a string containing the value of the variable or the output of the command (minus final newlines in the latter case). Split the string into separate fields according to the value of IFS. Interpret each field as a wildcard ...


2

The wildcard pattern is expanded by the shell before the command is invoked. See G-Man's answer for a full explanation. Most shells require some form of intermediate command in order to apply a text transformation to the matches. Zsh offers a way to transform matches on the fly with its glob qualifiers: e or + to execute code for each match, which can ...


2

The point that you may be missing – that many people have trouble with, especially if they have experience with other operating systems before they come to *nix – is that, in many other OSs, wildcards on the command line are normally passed to the command to process as it sees fit.  For example, in Windows Command Prompt, rename *.jpeg *.jpg Whereas, in ...



Top 50 recent answers are included