Tag Info

Hot answers tagged

33

Note that when using range expressions like [a-z], letters of the other case may be included, depending on the setting of LC_COLLATE. LC_COLLATE is a variable which determines the collation order used when sorting the results of pathname expansion, and determines the behavior of range expressions, equivalence classes, and collating sequences within ...


17

[A-Z] in bash matches all characters that sort after A and sort before Z. In your locale, c probably sorts in-between B and C. $ printf '%s\n' A a á b B c C Ç z Z Ẑ | sort a A á b B c C Ç z Z Ẑ So c or z would be matched by [A-Z], but not Ẑ or a. $ printf '%s\n' A a á b B c C Ç z Z Ẑ | pipe> bash -c 'while IFS= read -r x; do case $x in [A-Z]) echo ...


9

Function They mean different things. The asterisk matches zero to infinity characters. The question mark matches exactly one character. From the references above: The * character serves as a "wild card" for filename expansion in globbing. The ? character serves as a single-character "wild card" for filename expansion in globbing… Performance ...


6

You installed cygwin, so you can just use its shell for maximum command support : "C:\Program Files\Cygwin\cygwin.bat this will give you a bash shell Then you can change directoty to go to the images location. Suppose your image location is "D:\Your Name\Images", to go there type cd "/cygdrive/d/Your Name/Images" and then call your command using the bash ...


6

It's intended and documented in bash documentation, pattern matching section. The range expression [X-Y] will be included any characters between X and Y using the current locale’s collating sequence and character set: LC_ALL=en_US.utf8 bash -c 'case b in [A-Z]) echo yes; esac' yes You can see, b sorted between A and Z in en_US.utf8 locale. You have some ...


3

You could do this, to supply tar with a list of all files inside protTests except those which are symlinks: find protTests -maxdepth 1 -mindepth 1 -not -type l -print0 | tar --null --files-from - -cvf protTests.tar By the way, your existing command: tar -cvf protTests.tar protTests/* will not archive all files in protTests, it will only archive ...


3

Locale can change what characters are matched by [A-Z]. Use (LC_ALL=C; rm [A-Z]*) to eliminate the influence. (I used a subshell to localize the change).


3

You don't need .* with find and the space between . and * is often a mistake, since the * will expand to every entry in the current directory, and tell find to use that as a path to search. That's also why your other find sometimes shows files twice. If the j$(pwd) actually matches a file it will also be matched by *. So your delete one will probably do ...


2

With ksh93: printf '%s\n' {12}(?) for (non-hidden) files whose name are made of 12 characters. Or if you prefer regular expressions: printf '%s\n' ~(E)^.{12}$


2

With zsh you could use a glob like ?(#cN) (here the c flag requires the previous ? to match exactly N times): setopt extendedglob print -rl -- ?(#c12) if you prefer ls: ls -d -- ?(#c12) You can also add qualifiers, e.g. search recursively for regular files with fixed name length: print -rl -- **/?(#c12)(.)


2

With zsh: files=(*.RAW(DN)) if (($#files)); then echo Yes else echo No fi If you're only interested in regular files (or symlinks to regular files) as your -f implies, that would be: file=(*.RAW(ND-.)) instead. POSIXly: has_regfiles_by_extension() { for ext do for file in .*."$ext" ."$ext" *."$ext"; do [ -f "$file" ] && return ...


2

Consider this more of an expansion/enhancement to @Slyx's answer. Cygwin has a useful utility cygpath to convert Windows path to *nix-style understood within Cygwin's bash shell: $ cygpath "D:\Path\To\Images" /cygdrive/d/Path/To/Images Instead of the explicit for-loop suggested, you can also consider using find that has better support for filtering ...


2

I observed this behavior on a new Amazon EC2 instance. Since the OP didn't offer an MCVE, I'll post one: $ cd $(mktemp -d) $ touch foo $ echo [A-Z]* # prepare for a surprise! foo $ echo $BASH_VERSION 4.1.2(1)-release $ uname -a Linux spinup-tmp12 3.14.27-25.47.amzn1.x86_64 #1 SMP Wed Dec 17 18:36:15 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux $ env | ...


1

test -f won't work for multiple files expanded from wildcards. Instead you might well use a shell function with null-redirected ls. present() { ls "$@" >/dev/null 2>&1 } if [ $# -lt 1 ]; then echo "Please enter the path" exit fi path=$1 if ! present $path/cc*.csv && ! present $path/cc*.rpt && ! present ...


1

If I understand the question correctly, you want to rsync all the files in the range A0000000 through A0000095.  Well, then, don’t say A*; use a list of positive wildcards (a.k.a. globs or filename expansion patterns) that generate the file names you want, rather than identifying the ones you want to exclude.  Do it by decomposing the range into subranges: ...


1

My tar implementation is the best method star -cv -f out.tar -find protTests ! -type l


1

By using \0-delimited strings, this can handle spaces and \n in file names. cd "${PROJECT_DIR%/*}" outdir="output"; mkdir -p "$outdir" find "$PROJECT_DIR" -type f -name '*.log' -printf "%p\0${outdir}/%P\0" | awk 'BEGIN{FS="/";RS=ORS="\0"} NR%2||NF==2 {print; next} {gsub("/","#"); sub("#","/#"); print}' | xargs -0 -n2 cp -T mkdir -p ...


1

#!/bin/bash newdir=/absolute/path/output olddir=/absolute/path/project find $olddir -name '*log' | while read line ; do if [ "$olddir" == "$( basename "$line" )" ] ; then #just move the file if there are no subdirectories mv "$line" "$newdir" else #1) replace old project dir with nothing #2) replace all slashes with hashes #3) set ...


1

To check what shell you are really using with /bin/sh, call: /bin/sh whatshell.sh and fetch the whatshell script from this page: http://www.in-ulm.de/~mascheck/various/whatshell/ call e.g. wget http://www.in-ulm.de/~mascheck/various/whatshell/whatshell.sh Typical output: $ sh whatshell.sh SVR4 Bourne shell (SunOS 5 variant) $ bosh whatshell.sh SVR4 ...


1

As stated in user3188445's answer this is not POSIX-compliant. However, if you want to do it anyway, you have to use another tool like find: find dir/ -maxdepth 1 -type f -regex ".*.pdf\|.*.png" -delete find searches the dir dir/, not recursively (-maxdepth 1), only file are found (-type f). The regular expression matches .pdf and .png files and -delete ...


1

No, that won't work in a strictly POSIX-compliant shell. Here's the normative reference: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13_03 As you can see, definitely no regex, and not even the {a,b} brace notation.


1

The ?? are more specific, in the event there are or could be other, longer files the * glob would match. % touch 10001 100dalmations % ls 100?? 10001 % ls 100* 10001 100dalmations %


1

use quotes to stop bash from globbing. var="A*" echo "$var"



Only top voted, non community-wiki answers of a minimum length are eligible