Hot answers tagged

54

You've explained the situation very well. The final piece to the puzzle is that unzip can handle wildcards itself: http://www.info-zip.org/mans/unzip.html ARGUMENTS file[.zip] ... Wildcard expressions are similar to those supported in commonly used Unix shells (sh, ksh, csh) and may contain: * matches a sequence of 0 or more ...


21

The difference between those two commands is the quoted * character. If you call a command in a shell and use the * character for an argument, the shell itself will evaluate the argument. See this example: $ ls file1.zip file2.zip file3.zip file4.txt Now with a *: $ ls *.zip file1.zip file2.zip file3.zip The shell evaluates the wildcard and builds ...


7

The ~ is part of the filename: ls *.py~ Thus, to delete all such files: rm *~


6

The difference is in the first case the shell itself expands the glob: % cd / % echo * Applications Library Network System Users Volumes bin cores ... % while in the second case the application itself Does Something™ with that literal character: % cd / % perl -E 'chdir "/tmp" or die; say for ...


5

Using extended globs: shopt -s extglob printf '%s\n' !([[:digit:][:upper:]]?([[:digit:][:upper:]])_[[:digit:]][[:digit:]][[:digit:]][[:digit:]]_+([[:alnum:]]).dat) this will print all file/directory names that do not (!) match [[:digit:][:upper:]] followed by zero or one [[:digit:][:upper:]] followed by 4 [[:digit:]] in between _s and then one or more ...


4

This uses gunzip to unzip all files in a folder and have the ending .out.gz gunzip */*.out.gz This will "loop"* through all folders that have a zipped file in them. Let me add an example: A a.out.gz B b.out.gz C c.out.gz D d.out.gz E e.out Using the command above, a.out.gz b.out.gz c.out.gz d.out.gz will all get unzipped, but it won't touch e.out ...


3

? is a special character in pattern matching, which match any single character. So the command means find all files and directories in /foo/path and its subdirectories, whose names are exactly one character long. The \? is used to prevent your shell from performing filename generation. You can use other quoting mechanisms: find /foo/path -name '?' or: ...


3

There are many ways of doing this. You could use a scripting language that understands regular expressions. For example, in Perl: perl -le 'unlink(grep(!/[0-9A-Z]{1,2}_\d{4}_\w+?.dat/,@ARGV))' * That will look for all files (not subdirectories) in the current directory, collect those that don't match the regex and delete them. You could also do a ...


3

Problem : * is not getting expanded ; there really is no such file named * , so grep reports that. Solution : remove the last * ; it will work with -r , making grep look into all the files in that Directory.


3

Each line you write must have a command, usually the first word. To get something printed, a common command is echo. If the pwd (present working directory) has files a, aa, bb, and ccc. Then, this command will print all files in the directory: $ echo * a aa bb ccc And this command will print all files in the pwd that have one character: $ echo ? a ...


3

With zsh: printf '%s\n' d/*.txt(:t) :t like for csh history modifiers but here in a glob qualifier, gets you the tail of the file name. Also: files=(d/*.txt) printf '%s\n' $files:t In other Bourne-like shells, you could always do: (cd d && printf '%s\n' *.txt) Note that it doesn't fork a new shell, it creates a subshell environment. In most ...


2

Unless you need to act on the each directory as a whole, you can make a single loop to enumerate all the files. Activate the ksh-style extended patterns to get or-patterns. shopt -s extglob for file in @(lebenslauf|titlePage)/*.tex; do echo "$file" done N.B. Always use double quotes around variable substitutions.


2

*.tex is a pattern, not a regular expression. To match against a pattern, don't use the =~ operator. Also, your loop iterates over the directories, not the files inside them. d=( lebenslauf titlePage ) for dir in "${d[@]}" ; do for file in "$dir"/*.tex ; do echo $file done done


2

I believe that the simplest way to do what you ask is: $ ( cd d; ls *.txt ) 1.txt 2.txt 3.txt that is happening inside a sub-shell ( ... ) so the directory change is not permanent, is valid only for the execution of the two commands. A more robust version is: $ ( cd d && ls -d -- *.txt ) 1.txt 2.txt 3.txt In which ls is not executed unless ...


2

You should put quotes around your variables: while IFS= read -r line; do out=$(echo "$line" | awk '{ print $3 }') echo "$out" done < file_with_asterisks In your case the echo $line expands (I am assuming a space follows the initial asterisks on each line)


2

A command will receive the arguments after they have been processed by the shell. On first processing, an unquoted * will be expanded by the shell (to the list of files in the present directory (pwd) that match the pattern): echo *.zip Will list all .zip files. But echo "*".zip" will not. On first processing, a quoted "*" will not be expanded, it will ...


2

The ? is part of a mechanism called "pathname expansion" in the shell. Colloquially, the shell mechanism is called "globing". The basic glob makes use just of three characters: * ? and [ that build "patterns". An asterisk * means: Any character in any quantity (any string). A question mark (?) means: Any character one time. The square braces ...


1

Brace expansion happens before variable expansion, so there's no way to use variables in it. You can use seq instead: seq -f foo_%03.0f.nc $ns $ne


1

There are several options, Use NFS or FUSE-over-SSH or something to expose the remote filesystem locally, then let bash apply the default FOO=/*/passwd glob on that exported filesystem path. (ZSH has a ${~spec} glob substitution parameter expansion, otherwise see your shell's manual.) SSH over to the remote system, and do the glob there. ssh host 'echo ...


1

One example: $ echo jeff > jeff $ echo not > not $ echo 's/jeff/not/' | sed -i -f - * $ cat jeff not For more information on what standard shells should do with filename/pathname expansion (globbing), see: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_06 where it says: After field splitting, if set -f is not ...


1

If all the files in question have the same prefix (i.e., the text before the number; c in this case), you can use gs …args… c?.pdf c??.pdf c?.pdf expands to c0.pdf c1.pdf … c9.pdf.  c??.pdf expands to c10.pdf c11.pdf … c20.pdf (and up to c99.pdf, as applicable).  While each command-line word containing pathname expansion character(s) is expanded to a ...



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