Hot answers tagged

5

In theory yes. In practice usually also yes. If you're calling a shell script or alias that does something weird, then maybe no. You could use echo to see what a particular command would be expanded to by the shell: $ echo rm -R ~ rm -R /home/frostschutz $ echo rm -R "~" rm -R ~ Note that echo removes the "" so you should not copy-paste what it prints. ...


5

As Kalvin Lee mentioned, you can cd to the directory and remove its contents, then use rmdir to remove the directory. I recommend this over the rm -R approach because you're less likely to fat-finger the command and blow away your home directory. Generally, you can put things that you don't want the shell to expand in single quotes. This will remove an ...


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

You can either escape the $ sign: rm -r .\$EXTEND or use single quotes: rm -r '.$EXTEND'


3

In addition to frostschutz's double quotes method, and Andy's simple quote one, there are also the shorter: rm -r \~ and the relative path one: rm -rf ./~


3

Make sure you have a backup of your files before starting to change them all You can run something like the following: for i in *.fasta; do nr="${i%.fasta}"; sed -i 's/^>OTU\(.*\)$/>OTU\1_'$nr'/' "$i"; done if you have a version of sed that supports -i (in place editing). The nr="${i%.fasta}" part gets you the number from the filename. The \(.*\) ...


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

dirname of file is missing in first part, try grep -w 'sucessfully completed.' "/var/log/folder/$(ls -1rt /var/log/folder | tail -n1)" do not try ... unles there is no dir in /var/log/folder/ grep -w 'sucessfully completed.' "$(ls -1rt /var/log/folder/* | tail -n1)"


2

To differentiate a variable from a string bash uses $. When you do : rm -R .$EXTEND variable $EXTEND is expanded and the result is substituted. Inorder to remove .$EXTEND file you need to tell bash to interpret $ without its special meaning. You can do it two ways : rm -R .\$EXTEND #\ strips the special meaning of $ or rm -R '.$EXTEND' # Use single ...


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

With sed you can get the first 30 characters: sed 's|\(.\{30\}\).*|\1|' and use that instead of cut in your for loop. The breakdown of that sed substitution is that what is matched between \( \) you substitute with \1. the {30} (escaped with \) counts 30 single characters (.).


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


1

I know this is an old question but i have been searching all night for a similar solution. I found a few helpful tips but they did not do exactly what i needed, so I had to mix and match a few to get the correct outcome I was looking for to simply remove special characters and replace them with a (.) dot for f in *.txt; do mv "$f" `echo $f | sed ...



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