Hot answers tagged

12

Your shell code has two issues: The echo should not be there. The variable $i is mistyped as $1 in the destination file name. To make a copy of a file in the same directory as the file itself, use cp thefile thecopy If you insert anything else in there, e.g. cp thefile theotherthing thecopy then it is assumed that you'd like to copy thefile and ...


9

You can throw away error reporting from find with 2>/dev/null, or you can avoid running the command at all: test -d /my-directory && find /my-directory -type f -mtime +14 -print0 | xargs -r0 rm As a slight optimisation and clearer code, some versions of find - including yours - can perform the rm for you directly: test -d /my/directory &&...


9

for i in {1..100}; do cp test.ogg "test_$i.ogg" ; done


7

Short and precise < test.ogg tee test{1..100}.ogg or even better do tee test{1..100}.ogg < test.ogg >/dev/null see tee command usage for more help. Update as suggested by @Gilles, using tee has the defect of not preserving any file metadata. To overcome that issue, you might have to run below command after that: cp --attributes-only --...


6

No. By the time a shebang comes into play, you have already lost. A shebang is applied when a process is exec()'d and typically that happens after forking, so you're already in a separate process. It's not the shell that reads the shebang, it's the kernel.


5

$$ is the process ID of the current shell instance. So in your case the number, 23019, is the PID of that instance of bash. The following should give you a better idea: ps -p $$


5

There must not be any spaces between a variable name and the equation mark. When there are spaces, the variable name is interpreted as a command, in this case the command host is run with parameters = and the host name.


4

The expressions in the apostrophes are not evaluated (nor subshell nor variables). You need to use normal quotes: $ pos=2; $ printf "Masi \nwas \nhere" > /tmp/1 $ sed -i "`echo $pos`i huhu" /tmp/1 $ cat /tmp/1 Masi huhu was here which is equivalent of $ sed -i "${pos}i huhu" /tmp/1 (without the subshell)


4

The sed script consists of three substitute commands. Substitute commands are of the form s/old/new/ which looks for something in the text that matches regular expression old and replaces it with new. If a g is put after the command, then this substitution is done repetitively ("globally"). The first one removes periods. The second one makes the text ...


4

You can use eval: $ set -a $ eval "$(command_that_generate_output)" $ set +a $ sh -c 'printf "%s\n" "$DATABASE_URL"' someurl


4

Two changes to your current script: don't parse ls; instead rely on the shell's globbing because the files are in a subdirectory, either cd there first and run the loop, or use basename and dirname to pull out the directory and filename portions of the file before adding the prefix. (Note: I also changed your "/Path" to "./Path" as I didn't want to ...


3

The wc (word-count) utility is able to count lines in a file: $ wc -l num.txt ... or rather, it counts the number of newlines in the file, which most of the time is the same thing (actually, on a Unix system, that is defined as the same thing). The manual (on Mac OS X) states: "Characters beyond the final <newline> character will not be ...


3

Historically the original /bin/sh Bourne shell would use $ as the normal prompt and # for the root user prompt (and csh would use %). This made it pretty easy to tell if you were running as superuser or not. # is also the comment character, so anyone blindly re-entering data wouldn't run any real commands. More modern shells (eg ksh, bash) continue this ...


2

You have to parse it: All shells and programs that use exec*p library calls next is the verb: handle searching for executables in directories named in PATH The exec*p refers to a subset of the system "exec" functions, whose name ends with p, as a clue to the fact that they (as the rest of the sentence says) use the environment variable PATH as a ...


2

print0 and xargs -r 0 are useless here, find has that capability builtin: [ -d /my-directory ] && find /my-directory -type f -mtime +14 -exec rm {} + or, as you are using GNU find, this variant suggested by @terdon: [ -d /my-directory ] && find /my-directory -type f -mtime +14 -delete


2

Well, if the date strings are in the file names and all the files are in the same directory, you could do: mv 201601*.txt 2016/January Doing this 12 times manually would be a pain, so I would create a list with the number and corresponding month name: $ paste <(printf '%s\n' {01..12}) <(cal 2016 | grep -Po '\s+\K[A-Z]\w{2,}') 01 January 02 ...


2

You can use, find /my-directory -type f -mtime +14 -print0 2>/dev/null | xargs -r0 rm Explaination: 2> /dev/null means redirects stderr to /dev/null. /dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.


2

Use eval on your final line, and make sure the parent shell doesn't eat the $: #!/bin/bash export FOO=bar export BAR=baz eval "$@" run like so: wouter@gangtai:~$ ./foo.sh echo '$BAR' baz For more information, see help eval.


2

Assuming the perl rename command: You're quite close with the last command. rename 's/(0.) /$1 - /' *.mp3 would work. There's no need to escape the space, they have no special meaning in regular expressions (they do in file names, but that doesn't matter here), and you need parentheses around the part you want to reuse.


2

As user @muru says, it's not possible to do because you have already left the shell session behind when you get to the #!-line. However, depending on what your shell files do, there might be another solution. I'm guessing that they set environment variables that you use for some project. Let's call a project subtool (because that's a project I have). Then ...


2

awk '!x[$0]++' <<< "$list" | while read -r line; do array[count++]=$line done The array (italic) in this case is a part of the subshell (bold). The $line and $array has a value whilst the subshell is alive, so to speak. Once the subshell finishes, aka dies, the parent (spawner) environment is restored. This includes obliteration of any ...


2

You can use a parallel shell such as clustershell or pdsh. This way, assuming you already set up a passwordless SSH authentication from a central machine, you can run a command on each of the 100 servers at the same time. You can also go further and do various groups in order to organize them logically. Lets assume your machines are named aws0, aws1, aws2, ...


2

Tradition. The Single Unix Specification says (my emphasis): PS1 This variable is used for interactive prompts. Historically, the "superuser" has had a prompt of '#'. Since privileges are not required to be monolithic, it is difficult to define which privileges should cause the alternate prompt. However, a sufficiently powerful user should ...


1

From Advanced Bash-Scripting Guide: $$ is the process ID (PID) of the script itself. $BASHPID is the process ID of the current instance of Bash. This is not the same as the $$ variable, but it often gives the same result.


1

$ rename 's/^(\d\d)\s*/$1 - /' *.mp3 This will rename all MP3 files that has a double digit at the start of their file names, inserting space-dash-space after the digits. So 01 Track name.mp3 will become 01 - Track name.mp3 Judging from your own attempts, all filenames start with the digit zero, and you appear to want to insert a dash directly after the ...


1

find . -name '*.bedgraph' -delete should work. Be careful not to delete anything inadvertently.


1

Some solutions to your problem without the loop # use bash's mapfile with process substitution mapfile -t arr < <( awk '!x[$0]++' <<<"$list" ) # use array assignment syntax (at least bash, ksh, zsh) # of a command-substituted value split at newline only # and (if the data can contain globs) globbing disabled set -f; IFS='\n' arr=( $( awk '!...


1

Does it have to use the rename command? $ ls 01 Track name.mp3 02 Track name.mp3 03 Track name.mp3 $ for a in *.mp3 > do > mv -i "$a" "${a%% *} - ${a#* }" > done $ ls 01 - Track name.mp3 02 - Track name.mp3 03 - Track name.mp3


1

That's the Perl rename, I suppose. Perhaps something like this would work: rename 's/^(\d+) ([^-])/$1 - $2/' [0-9]*.mp3 Match anything starting with numbers, then a space, then something other than a dash. Replace with the numbers, a dash, and the next character. (The rest of the name is not touched.) Explicitly checking for the dash here so repeated ...


1

You could get your production_env.sh to test for no args (or some special single arg like -i) and then read a single line (with -e to allow input editing) and execute it. Eg change its last line to: if [ $# = 0 -a -t 0 ] then read -p 'prod> ' -e cmd bash -c "$cmd" else "$@" fi



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