New answers tagged

0

Note that {1..20} is not a wildcard/globbing operator. That's a special form of brace-expansion introduced by zsh and copied with limitations and variations by ksh93 and bash. foo_{0001..0030}.nc doesn't expand to the list of matching files, it expands to foo_0001.nc, foo_0002.nc, ... foo_0030.nc regardless of whether the files exist or not. bash has that ...


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


0

the quotes are needed because of the way zip handles multiple arguments: rm: remove all files in the argument list zip: unzip the file in the first argument. only extract the files in the remaining arguments. $ ls *.zip file1.zip file2.zip file3.zip $ unzip *.zip Archive: file1.zip caution: filename not matched: file2.zip caution: filename not ...


-1

if you want to use an if clause, evaluate the count: if (( `ls *.txt 2> /dev/null|wc -l` ));then...


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


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


20

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


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


47

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


0

The right side of a "Variable Assignment" is considered quoted (no splitting or globing): LESS=+'/A variable may be assigned' man bash A variable may be assigned to by a statement of the form: name=[value] Word splitting is not performed, ... . Pathname expansion is not performed. Therefore: $ var=* $ echo "$var" * To get an asterisk to ...


0

You can use tar over ssh, tar cf - filename | ssh adam@ocelot.cs.edu "cd /data/$ID/EER_DATA/$ID*; tar xf -" Make sure you have setup ssh-agent so it won't prompt you with the password. I don't think you can use wildcard dir with scp, not even working with a regular cp. I've tested this on bash and zsh.


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


0

You can do it with find: find . -regextype posix-extended \ -type f ! -regex '.*/[0-9A-Z]{1,2}_[[:digit:]]{4}_[[:alnum:]_]+?\.dat' -delete Of course you can put it all on one line (removing the \ at the end of the first line). -regextype posix-egrep seems to work exactly as well as -regextype posix-extended. If your version of find doesn't ...


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


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


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.


-1

much simpler would be probably: ls */file1.tex */file2.tex or ls */*.tex | egrep '/(file1|file2).tex' or ls */*.tex | grep -v 'thirdPage/' depending on how much you wish to type...


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


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


0

find . -type f -name "*.gz" -execdir gunzip {} \; the execdir option to find causes 'find' to 'cd' into each directory in turn and run the exec command (gunzip in this case) and then 'cd' back to the cwd


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


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


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

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)


7

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


0

The easiest way in bash to print matching dot files, ignoring . and .. is: $ GLOBIGNORE=/+/ $ printf '%s\n' * To test: $ cd tmp; mkdir empty; cd empty; touch {,.}{a..h} $ GLOBIGNORE=/+/ $ ls * a .a b .b c .c d .d e .e f .f g .g h .h It printed all files with dots or not, with the notable exception of . and ... Your example will ...


29

There are two problems with your example. The primary one is that you're assuming that regular expressions work the same as glob patterns in that * is a wildcard meaning "any sequence of characters." In regular expressions, * means "any number of the previous atom" instead, so fil* means f followed by i followed by zero or more l characters. You need to say ...


1

* is a glob that is expanded by the shell. By default shells don't include files whose name starts with a . (called hidden files or dotfiles) unless the leading . is entered literally. * or [.]* or ?* or *.* or dir/* will not include dotfiles. .* or dir/.* will. So you could do: mv -- * .* /dest/ however some shells including bash (but not zsh, mksh ...



Top 50 recent answers are included