Tag Info

New answers tagged

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

It should work if you remove the outer square brackets in the second if condition


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: ...


0

Since the numbers are padded with zeros to the same width, the numerical order is identical to the lexicographic order. Therefore your problem is equivalent to removing the files starting with a given file in the lexical order. You can do this by building a string containing the file names separated by newlines, and using string substitution to remove the ...


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 ...


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 ...


0

(cd "$PROJECT_DIR" && find . -name "*.log") | tar -cf - -T - | (cd $OUTPUT_DIR && tar -xf -) cd to project directory find all of the log files tar's list of log files to stdout cd to output directory untar stdin


0

Thanks for all the help, although I haven't been able to get some of these to work. I did, however, successfully implement code I found in an answer to another question! (http://unix.stackexchange.com/a/87476) export DIR=./folder if ls ${DIR}/*.RAW &>/dev/null then echo "Yes!" else echo "No!" fi


-1

Two Bourne Shell Solutions With the Bourne shell (which doesn't support arrays, or shell options like nullglob or failglob) you have to work around the fact that a non-zero exit status or the glob itself will be returned if a glob isn't found. For example: if [ -n "`ls *.RAW 2> /dev/null`" ]; then echo "At least one RAW file found!" else echo ...


0

Your example answer is almost correct. First, you need to separate the testing string ".RAW" from the closing ]: if [ ! -f "*.RAW" ]; You can do shell globbing in this context by taking the asterisk outside of the quotes. So the finished command would be: if [ ! -f *".RAW" ]; However, this approach isn't so useful if you want to use the .RAW files. ...


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 ...


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 %


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 ...


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 ...


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 ...


-1

ls ?.* should give you desired results.


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)(.)


1

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


1

I had a situation similar where I was surrounding the -name value in quotes, but still wasn't getting all of the find hits that I was hoping for. I conjectured that it was because of symlinks and sure enough that was the case. If you want to force find to search through symlinks you can modify the command to the following: find -L . -name '*.java'


0

In bash, certain patterns are not enabled by default, but can be turned on by setting an option with shopt. The patterns !(NOT-THIS), @(THIS|THAT), *(ZERO-OR-MORE), +(ONE-OR-MORE) and ?(OPTIONAL), which didn't exist in early Unix shells, were later added in ksh, and aren't standardized by POSIX, are not recognized by default, you have to run shopt -s extglob ...


1

Shell Script Approach By default, globs don't work in a BASH script (although you can turn them on with shopt). If the shell script ever gets run by a non-BASH interpreter, globs might not work at all. You can get the same effect using the find command, which is how I'd recommend doing it (because of how much more control you can have once your ...


4

You need to turn extglob on: shopt -s extglob


-2

Here is one: ls | cut -d '_' -f1,2


2

The equivalent of the * regexp operator in zsh -o extendedglob is # (## for +). And you can also use ksh-style globbing with the kshglob option. So either: setopt extendedglob # best in ~/.zshrc ls -d filename.mainsuff_[[:alnum:]]# Or: setopt kshglob ls -d filename.mainsuff_*([[:alnum:]])(-) (The (-) part is to prevent ([[:alnum:]]) from being taken as ...



Top 50 recent answers are included