Tag Info

Hot answers tagged

5

Use bash arrays: pa=("/home/user/folder1" "/home/user/fol der2") find "${pa[@]}" -name '*xy*' ... Discussion Observe that the \ does not do what one hopes: $ pa="/home/user/folder1 /home/user/fol\ der2" $ printf '%s\n' $pa /home/user/folder1 /home/user/fol\ der2 The \ in the definition of pa becomes a literal character in the string, not an escape. ...


5

If your shell supports the ksh ${var/search/replace} form of parameter expansion (ksh93, zsh, mksh, yash, bash): for r1 in *R1*; do r2=${r1/R1/R2} singles=${r1/R1/singles} trimmed1=trimmed$r1 trimmed2=trimmed$r2 sickle pe -f "$r1" \ -r "$r2" \ -o "$trimmed1" \ -p "$trimmed2" \ -s "$singles" done POSIXly, you could do ...


4

It can be done by GNU ls+awk one-liner: ls -vr *.depot | awk -F- '$1 == name{system ("rm \""$0"\"")}{name=$1}' Explanation: the file names are passed as input to the awk script. The options -vr cause the file names to be sorted as version numbers in reverse order, so e.g. foo-1.9.depot comes after foo-1.10.depot. The awk script stores the first part of ...


4

If the output from more is acceptable as it is, just pipe it through cat: more * | cat That will do away with the "more" prompts. Or you can get a bit more control over the display using printf in a one-liner: for fn in *; do printf "::::::\n$fn\n:::::\n"; cat "$fn"; done or as script: for fn in $*; do printf "vvvvvvvvvv\n$fn\n^^^^^^^^^^\n" ...


3

With zsh: for d (subdir*(/)) mixb $d/*.(dat|d01)([1]) The bash equivalent would be something like: shopt -s nullglob extglob for d in subdir*/; do [ -L "${d%/}" ] && continue set -- "$d"*.@(dat|d01) [ "$#" -eq 0 ] || mixb "$1" done


3

As one who has worked in a multitude of ?nix environments, I have had to write in a wide variety of shells. Believe it or not, across platforms, the shells are not the same. So if you maintain your personal library in multiple shells (when necessary) it is very helpful to use extensions to ID the shells. That way when you move to another platform and the ...


2

The . at the beginning of a directory path means that it is a relative path starting at the current working directory. Without the . it starts at the file system root. So you would have to run /usr/bin/foo/bar.sh without a preceding dot. The dot is only needed when you are located in the same directory as the file to distinguish it from an eventually ...


2

Dots matter. Instead of: ./usr/bin/foo/bar.sh Use: /usr/bin/foo/bar.sh Discussion Consider: ./usr/bin/foo/bar.sh This starts in the current directory, ./, and looks for the subdirectory usr. If the current directory contains no such subdirectory, you will see the message: bash: ./usr/bin/foo/bar.sh: No such file or directory By contrast, ...


2

Which Unix? It will be quite difficult to make it so that file creation fails when an "invalid" name is passed to it, but there are some platform-specific ways of doing this. First of all, there is inotify and its relatives, which can at least observe the filesystem and notify you immediately when a file is created. I'm not sure you can actually stop the ...


2

It will work. For example consider a .txt file in a current directory find . -type f -iname "*.txt" -exec basename \{\} .txt \;


2

You can do a few things. head and tail are both spec'd to display the first/last ten lines of a file by default - but if called w/ multiple arguments will do that for all and display the filenames for each. And, of course, for each, you can use the -n[num] argument to specify a number of lines other than the default ten that you would like to display. I ...


2

I would use awk: awk 'FNR==1{print "::::\n"FILENAME"\n::::"}1' *


2

Without ls, since you're just populating its list with shell globs anyway, you can cut out the middle-man like: glob_hsli()(IFS=.;set +f set -f -- '' hsli*.*.h5 for h5 do case ${h5#*.} in (*[!0-9]*.*|.*|'') : ;; (*) set $h5 "${1:-0}"; shift $((3>>($2>$4)));; esac;done printf "0.%d\n" "${1:?No Match Found!}" ...


1

Bash makes it relatively easy to apply a transformation like stripping prefixes and suffixes to elements of an array. shopt -s nullglob # if there are no matches, produce an empty list versions=(hsli*.h5) # list matches versions=("${versions[@]#hsli}") # strip prefix versions=("${versions[@]%.h5}") # strip suffix printf ...


1

You can use -exec to create a new bash shell, then manipulate {} inside the shell by passing it as a parameter (it can be accessed as $0 in the new shell). You can remove the .md filetype ending with parameter expansion: find . -name '*.md' -type f -exec bash -c 'pandoc --filter ./filter1.py -o ${0%md}html' {} \;


1

Try: perl -i.bak -wpe 's/SEARCH_FOR/$ARGV/g' *.txt $ARGV contain the name of current file when reading from <>. Because you using -i switch, you don't have to worry about security implication when running perl with -n or -p switch.


1

A shell script with a "here document" will handle this nicely: #!/bin/bash for NAME in t? do ex -s $NAME << END_EDITS 1s/TestRelation/$NAME/ w! q END_EDITS done Not knowing what your files are named, I chose "t1", "t2", "t3", etc as file names. You will have to generate file names as appropriate. The "for" loop gives shell variable NAME the value ...



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