QuestionId
stringlengths
3
8
QuestionTitle
stringlengths
15
149
QuestionScore
int64
1
27.2k
QuestionCreatedUtc
stringdate
2008-08-01 18:33:08
2025-06-16 04:59:48
QuestionBodyHtml
stringlengths
40
28.2k
QuestionViewCount
int64
78
16.5M
QuestionOwnerUserId
float64
4
25.2M
QuestionLastActivityUtc
stringdate
2008-09-08 19:26:01
2026-01-24 22:40:25
AcceptedAnswerId
int64
266
79.7M
AnswerScore
int64
2
30.1k
AnswerCreatedUtc
stringdate
2008-08-01 23:40:28
2025-06-16 05:23:39
AnswerOwnerUserId
float64
1
29.5M
AnswerLastActivityUtc
stringdate
2008-08-01 23:40:28
2026-01-23 17:46:09
QuestionLink
stringlengths
31
36
AnswerLink
stringlengths
31
36
AnswerBodyHtml
stringlengths
35
33k
AnswerBodyPreview
stringlengths
35
400
AllTagIdsCsv
stringlengths
2
37
AllTagNamesCsv
stringlengths
2
106
MergedQuestion
stringlengths
74
28.4k
quality_filter
stringclasses
10 values
unique_id
int64
0
29k
tag_major_lang
stringclasses
10 values
MergedQuestion_md
stringlengths
60
28.1k
AnswerBodyHtml_md
stringlengths
14
29.2k
356100
How to wait in bash for several subprocesses to finish, and return exit code !=0 when any subprocess ends with code !=0?
782
2008-12-10 13:54:26
<p>How to wait in a bash script for several subprocesses spawned from that script to finish, and then return exit code <code>!=0</code> when any of the subprocesses ends with code <code>!=0</code>?</p> <p>Simple script:</p> <pre><code>#!/bin/bash for i in `seq 0 9`; do calculations $i &amp; done wait </code></pre> <p...
822,624
42,201
2024-06-21 16:46:40
356,154
759
2008-12-10 14:07:51
42,448
2024-06-21 16:46:40
https://stackoverflow.com/q/356100
https://stackoverflow.com/a/356154
<p><a href="https://www.gnu.org/software/bash/manual/html_node/Job-Control-Builtins.html#index-wait" rel="noreferrer"><code>wait</code></a> also (optionally) takes the <code>PID</code> of the process to wait for, and with <code>$!</code> you get the <code>PID</code> of the last command launched in the background. Modif...
<p><a href="https://www.gnu.org/software/bash/manual/html_node/Job-Control-Builtins.html#index-wait" rel="noreferrer"><code>wait</code></a> also (optionally) takes the <code>PID</code> of the process to wait for, and with <code>$!</code> you get the <code>PID</code> of the last command launched in the background. Modif...
387, 5910, 21289
bash, process, wait
<h1>How to wait in bash for several subprocesses to finish, and return exit code !=0 when any subprocess ends with code !=0?</h1> <p>How to wait in a bash script for several subprocesses spawned from that script to finish, and then return exit code <code>!=0</code> when any of the subprocesses ends with code <code>!=0<...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
216
bash
# How to wait in bash for several subprocesses to finish, and return exit code !=0 when any subprocess ends with code !=0? How to wait in a bash script for several subprocesses spawned from that script to finish, and then return exit code `!=0` when any of the subprocesses ends with code `!=0`? Simple script: ``` #!...
[`wait`](https://www.gnu.org/software/bash/manual/html_node/Job-Control-Builtins.html#index-wait) also (optionally) takes the `PID` of the process to wait for, and with `$!` you get the `PID` of the last command launched in the background. Modify the loop to store the `PID` of each spawned sub-process into an array, an...
11287861
How to check if a file contains a specific string using Bash
442
2012-07-02 04:40:11
<p>I want to check if a file contains a specific string or not in bash. I used this script, but it doesn't work:</p> <pre><code> if [[ 'grep 'SomeString' $File' ]];then # Some Actions fi </code></pre> <p>What's wrong in my code?</p>
737,467
1,265,960
2019-07-23 06:51:10
11,287,896
759
2012-07-02 04:47:14
1,458,569
2018-01-19 20:02:03
https://stackoverflow.com/q/11287861
https://stackoverflow.com/a/11287896
<pre><code>if grep -q SomeString "$File"; then Some Actions # SomeString was found fi </code></pre> <p>You don't need <code>[[ ]]</code> here. Just run the command directly. Add <code>-q</code> option when you don't need the string displayed when it was found.</p> <p>The <code>grep</code> command returns 0 or 1 in ...
<pre><code>if grep -q SomeString "$File"; then Some Actions # SomeString was found fi </code></pre> <p>You don't need <code>[[ ]]</code> here. Just run the command directly. Add <code>-q</code> option when you don't need the string displayed when it was found.</p> <p>The <code>grep</code> command returns 0 or 1 in ...
387, 390, 1271
bash, grep, shell
<h1>How to check if a file contains a specific string using Bash</h1> <p>I want to check if a file contains a specific string or not in bash. I used this script, but it doesn't work:</p> <pre><code> if [[ 'grep 'SomeString' $File' ]];then # Some Actions fi </code></pre> <p>What's wrong in my code?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
217
bash
# How to check if a file contains a specific string using Bash I want to check if a file contains a specific string or not in bash. I used this script, but it doesn't work: ``` if [[ 'grep 'SomeString' $File' ]];then # Some Actions fi ``` What's wrong in my code?
``` if grep -q SomeString "$File"; then Some Actions # SomeString was found fi ``` You don't need `[[ ]]` here. Just run the command directly. Add `-q` option when you don't need the string displayed when it was found. The `grep` command returns 0 or 1 in the exit code depending on the result of search. 0 if someth...
18096670
What does -z mean in Bash?
571
2013-08-07 07:00:26
<p>I'm looking at the following code: </p> <pre><code>if [ -z $2 ]; then echo "usage: ... </code></pre> <p>(The 3 dots are irrelevant usage details.)<br> Maybe I'm googling it wrong, but I couldn't find an explanation for the <code>-z</code> option.</p>
500,963
427,653
2024-06-26 11:13:27
18,096,739
754
2013-08-07 07:04:01
1,009,479
2022-11-17 12:11:41
https://stackoverflow.com/q/18096670
https://stackoverflow.com/a/18096739
<p><code>-z string</code>: True if the string is null (an empty string)</p> <p>See <a href="https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions" rel="noreferrer">https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions</a></p>
<p><code>-z string</code>: True if the string is null (an empty string)</p> <p>See <a href="https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions" rel="noreferrer">https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions</a></p>
387
bash
<h1>What does -z mean in Bash?</h1> <p>I'm looking at the following code: </p> <pre><code>if [ -z $2 ]; then echo "usage: ... </code></pre> <p>(The 3 dots are irrelevant usage details.)<br> Maybe I'm googling it wrong, but I couldn't find an explanation for the <code>-z</code> option.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
218
bash
# What does -z mean in Bash? I'm looking at the following code: ``` if [ -z $2 ]; then echo "usage: ... ``` (The 3 dots are irrelevant usage details.) Maybe I'm googling it wrong, but I couldn't find an explanation for the `-z` option.
`-z string`: True if the string is null (an empty string) See <https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions>
8153750
How to search a string in multiple files and return the names of files in Powershell?
477
2011-11-16 15:00:29
<p>I have started learning powershell a couple of days ago, and I couldn't find anything on google that does what I need so please bear with my question.</p> <p>I have been asked to replace some text strings into multiple files. I do not necessarily know the extension of the possible target files and I don't know thei...
761,527
1,049,905
2023-11-30 20:27:05
8,153,857
754
2011-11-16 15:08:33
1,021,945
2020-01-30 15:42:06
https://stackoverflow.com/q/8153750
https://stackoverflow.com/a/8153857
<p>This should give the location of the files that contain your pattern:</p> <pre><code>Get-ChildItem -Recurse | Select-String "dummy" -List | Select Path </code></pre>
<p>This should give the location of the files that contain your pattern:</p> <pre><code>Get-ChildItem -Recurse | Select-String "dummy" -List | Select Path </code></pre>
139, 526, 578, 816, 1469
powershell, recursion, search, string, text
<h1>How to search a string in multiple files and return the names of files in Powershell?</h1> <p>I have started learning powershell a couple of days ago, and I couldn't find anything on google that does what I need so please bear with my question.</p> <p>I have been asked to replace some text strings into multiple fi...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
219
bash
# How to search a string in multiple files and return the names of files in Powershell? I have started learning powershell a couple of days ago, and I couldn't find anything on google that does what I need so please bear with my question. I have been asked to replace some text strings into multiple files. I do not ne...
This should give the location of the files that contain your pattern: ``` Get-ChildItem -Recurse | Select-String "dummy" -List | Select Path ```
242538
Unix shell script find out which directory the script file resides?
700
2008-10-28 08:19:24
<p>Basically I need to run the script with paths related to the shell script file location, how can I change the current directory to the same directory as where the script file resides?</p>
752,456
16,371
2023-06-05 09:11:53
242,550
753
2008-10-28 08:26:03
31,099
2016-02-28 03:37:38
https://stackoverflow.com/q/242538
https://stackoverflow.com/a/242550
<p>In Bash, you should get what you need like this:</p> <pre><code>#!/usr/bin/env bash BASEDIR=$(dirname "$0") echo "$BASEDIR" </code></pre>
<p>In Bash, you should get what you need like this:</p> <pre><code>#!/usr/bin/env bash BASEDIR=$(dirname "$0") echo "$BASEDIR" </code></pre>
34, 390
shell, unix
<h1>Unix shell script find out which directory the script file resides?</h1> <p>Basically I need to run the script with paths related to the shell script file location, how can I change the current directory to the same directory as where the script file resides?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
220
bash
# Unix shell script find out which directory the script file resides? Basically I need to run the script with paths related to the shell script file location, how can I change the current directory to the same directory as where the script file resides?
In Bash, you should get what you need like this: ``` #!/usr/bin/env bash BASEDIR=$(dirname "$0") echo "$BASEDIR" ```
3112687
How to iterate over associative arrays in Bash
482
2010-06-24 18:07:29
<p>Based on an associative array in a Bash script, I need to iterate over it to get the key and value.</p> <pre><code>#!/bin/bash declare -A array array[foo]=bar array[bar]=foo </code></pre> <p>I actually don't understand how to get the key while using a for-in loop.</p>
298,371
231,150
2021-01-02 05:18:41
3,113,285
753
2010-06-24 19:31:41
26,428
2010-06-24 19:31:41
https://stackoverflow.com/q/3112687
https://stackoverflow.com/a/3113285
<p>The keys are accessed using an exclamation point: <code>${!array[@]}</code>, the <strong><em>values</em></strong> are accessed using <code>${array[@]}</code>.</p> <p>You can iterate over the key/value pairs like this:</p> <pre><code>for i in "${!array[@]}" do echo "key : $i" echo "value: ${array[$i]}" done </...
<p>The keys are accessed using an exclamation point: <code>${!array[@]}</code>, the <strong><em>values</em></strong> are accessed using <code>${array[@]}</code>.</p> <p>You can iterate over the key/value pairs like this:</p> <pre><code>for i in "${!array[@]}" do echo "key : $i" echo "value: ${array[$i]}" done </...
387, 2575, 13003, 29051
associative-array, bash, bash4, key-value
<h1>How to iterate over associative arrays in Bash</h1> <p>Based on an associative array in a Bash script, I need to iterate over it to get the key and value.</p> <pre><code>#!/bin/bash declare -A array array[foo]=bar array[bar]=foo </code></pre> <p>I actually don't understand how to get the key while using a for-in...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
221
bash
# How to iterate over associative arrays in Bash Based on an associative array in a Bash script, I need to iterate over it to get the key and value. ``` #!/bin/bash declare -A array array[foo]=bar array[bar]=foo ``` I actually don't understand how to get the key while using a for-in loop.
The keys are accessed using an exclamation point: `${!array[@]}`, the ***values*** are accessed using `${array[@]}`. You can iterate over the key/value pairs like this: ``` for i in "${!array[@]}" do echo "key : $i" echo "value: ${array[$i]}" done ``` Note the use of quotes around the variable in the `for` stat...
878600
How to create a cron job using Bash automatically without the interactive editor?
560
2009-05-18 16:37:47
<p>Does <code>crontab</code> have an argument for creating cron jobs without using the editor (<code>crontab -e</code>)? If so, what would be the code to create a cron job from a Bash script?</p>
518,466
53,797
2025-03-03 16:30:14
878,647
751
2009-05-18 16:47:43
7,412
2016-07-09 15:20:04
https://stackoverflow.com/q/878600
https://stackoverflow.com/a/878647
<p>You can add to the crontab as follows:</p> <pre><code>#write out current crontab crontab -l &gt; mycron #echo new cron into cron file echo "00 09 * * 1-5 echo hello" &gt;&gt; mycron #install new cron file crontab mycron rm mycron </code></pre> <hr> <h2>Cron line explaination</h2> <pre><code>* * * * * "command to...
<p>You can add to the crontab as follows:</p> <pre><code>#write out current crontab crontab -l &gt; mycron #echo new cron into cron file echo "00 09 * * 1-5 echo hello" &gt;&gt; mycron #install new cron file crontab mycron rm mycron </code></pre> <hr> <h2>Cron line explaination</h2> <pre><code>* * * * * "command to...
387, 390, 601
bash, cron, shell
<h1>How to create a cron job using Bash automatically without the interactive editor?</h1> <p>Does <code>crontab</code> have an argument for creating cron jobs without using the editor (<code>crontab -e</code>)? If so, what would be the code to create a cron job from a Bash script?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
222
bash
# How to create a cron job using Bash automatically without the interactive editor? Does `crontab` have an argument for creating cron jobs without using the editor (`crontab -e`)? If so, what would be the code to create a cron job from a Bash script?
You can add to the crontab as follows: ``` #write out current crontab crontab -l > mycron #echo new cron into cron file echo "00 09 * * 1-5 echo hello" >> mycron #install new cron file crontab mycron rm mycron ``` --- ## Cron line explaination ``` * * * * * "command to be executed" - - - - - | | | | | | | | | -----...
246007
How to determine whether a given Linux is 32 bit or 64 bit?
479
2008-10-29 06:59:40
<p>When I type <code>uname -a</code>, it gives the following output.</p> <pre><code>Linux mars 2.6.9-67.0.15.ELsmp #1 SMP Tue Apr 22 13:50:33 EDT 2008 i686 i686 i386 GNU/Linux </code></pre> <p>How can I know from this that the given OS is 32 or 64 bit?</p> <p>This is useful when writing <code>configure</code> script...
526,782
11,602
2018-01-05 23:20:29
246,014
751
2008-10-29 07:06:42
6,309
2017-02-28 03:42:15
https://stackoverflow.com/q/246007
https://stackoverflow.com/a/246014
<p>Try <a href="http://linuxmanpages.net/manpages/fedora16/man1/uname.1.html" rel="noreferrer"><code>uname -m</code></a>. Which is short of <code>uname --machine</code> and it outputs: </p> <pre><code>x86_64 ==&gt; 64-bit kernel i686 ==&gt; 32-bit kernel </code></pre> <hr> <p>Otherwise, <strong>not for the Linux k...
<p>Try <a href="http://linuxmanpages.net/manpages/fedora16/man1/uname.1.html" rel="noreferrer"><code>uname -m</code></a>. Which is short of <code>uname --machine</code> and it outputs: </p> <pre><code>x86_64 ==&gt; 64-bit kernel i686 ==&gt; 32-bit kernel </code></pre> <hr> <p>Otherwise, <strong>not for the Linux k...
58, 390, 5368, 32643
32bit-64bit, linux, processor, shell
<h1>How to determine whether a given Linux is 32 bit or 64 bit?</h1> <p>When I type <code>uname -a</code>, it gives the following output.</p> <pre><code>Linux mars 2.6.9-67.0.15.ELsmp #1 SMP Tue Apr 22 13:50:33 EDT 2008 i686 i686 i386 GNU/Linux </code></pre> <p>How can I know from this that the given OS is 32 or 64 b...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
223
bash
# How to determine whether a given Linux is 32 bit or 64 bit? When I type `uname -a`, it gives the following output. ``` Linux mars 2.6.9-67.0.15.ELsmp #1 SMP Tue Apr 22 13:50:33 EDT 2008 i686 i686 i386 GNU/Linux ``` How can I know from this that the given OS is 32 or 64 bit? This is useful when writing `configure`...
Try [`uname -m`](http://linuxmanpages.net/manpages/fedora16/man1/uname.1.html). Which is short of `uname --machine` and it outputs: ``` x86_64 ==> 64-bit kernel i686 ==> 32-bit kernel ``` --- Otherwise, **not for the Linux kernel, but for the CPU**, you type: ``` cat /proc/cpuinfo ``` or: ``` grep flags /proc/c...
21334348
How to count items in JSON object using command line?
471
2014-01-24 13:47:19
<p>I'm getting this kind of <code>JSON</code> reply from a <code>curl</code> command:</p> <pre><code>[ { "cid": 49, "pyn": "yi4", "hans": "亿", "hant": "億", "tid": 68, "l10n": "cent million", "pid": 1, "pos": "num", "pos_txt": "" }, { "cid": 50, "pyn": "yi4", "hans"...
463,774
802,365
2023-08-16 22:00:28
21,355,442
747
2014-01-25 19:50:26
816,157
2018-09-25 19:59:10
https://stackoverflow.com/q/21334348
https://stackoverflow.com/a/21355442
<p>Just throwing another solution in the mix...</p> <p>Try <a href="http://stedolan.github.io/jq/" rel="noreferrer"><code>jq</code></a>, a lightweight and flexible command-line JSON processor:</p> <pre><code>jq length /tmp/test.json </code></pre> <p>Prints the length of the array of objects.</p>
<p>Just throwing another solution in the mix...</p> <p>Try <a href="http://stedolan.github.io/jq/" rel="noreferrer"><code>jq</code></a>, a lightweight and flexible command-line JSON processor:</p> <pre><code>jq length /tmp/test.json </code></pre> <p>Prints the length of the array of objects.</p>
387, 1508, 1554, 105170
bash, curl, jq, json
<h1>How to count items in JSON object using command line?</h1> <p>I'm getting this kind of <code>JSON</code> reply from a <code>curl</code> command:</p> <pre><code>[ { "cid": 49, "pyn": "yi4", "hans": "亿", "hant": "億", "tid": 68, "l10n": "cent million", "pid": 1, "pos": "num", "po...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
224
bash
# How to count items in JSON object using command line? I'm getting this kind of `JSON` reply from a `curl` command: ``` [ { "cid": 49, "pyn": "yi4", "hans": "亿", "hant": "億", "tid": 68, "l10n": "cent million", "pid": 1, "pos": "num", "pos_txt": "" }, { "cid": 50, "py...
Just throwing another solution in the mix... Try [`jq`](http://stedolan.github.io/jq/), a lightweight and flexible command-line JSON processor: ``` jq length /tmp/test.json ``` Prints the length of the array of objects.
1167746
How can I assign a heredoc value to a variable in Bash?
570
2009-07-22 19:43:31
<p>I have this multi-line string (quotes included):</p> <pre><code>abc'asdf&quot; $(dont-execute-this) foo&quot;bar&quot;'' </code></pre> <p>How would I assign it to a variable using a heredoc in Bash?</p> <p>I need to preserve newlines.</p> <p>I don't want to escape the characters in the string. That would be annoying...
327,860
14,193
2025-04-28 19:46:18
1,655,389
745
2009-10-31 19:41:21
26,428
2024-11-14 14:58:37
https://stackoverflow.com/q/1167746
https://stackoverflow.com/a/1655389
<p>You can avoid a useless use of <code>cat</code> and handle mismatched quotes better with this:</p> <pre><code>$ read -r -d '' VAR &lt;&lt;'EOF' abc'asdf&quot; $(dont-execute-this) foo&quot;bar&quot;'' EOF </code></pre> <p>If you don't quote the variable when you echo it, newlines are lost. Quoting it preserves them:...
<p>You can avoid a useless use of <code>cat</code> and handle mismatched quotes better with this:</p> <pre><code>$ read -r -d '' VAR &lt;&lt;'EOF' abc'asdf&quot; $(dont-execute-this) foo&quot;bar&quot;'' EOF </code></pre> <p>If you don't quote the variable when you echo it, newlines are lost. Quoting it preserves them:...
387, 7449
bash, heredoc
<h1>How can I assign a heredoc value to a variable in Bash?</h1> <p>I have this multi-line string (quotes included):</p> <pre><code>abc'asdf&quot; $(dont-execute-this) foo&quot;bar&quot;'' </code></pre> <p>How would I assign it to a variable using a heredoc in Bash?</p> <p>I need to preserve newlines.</p> <p>I don't wa...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
225
bash
# How can I assign a heredoc value to a variable in Bash? I have this multi-line string (quotes included): ``` abc'asdf" $(dont-execute-this) foo"bar"'' ``` How would I assign it to a variable using a heredoc in Bash? I need to preserve newlines. I don't want to escape the characters in the string. That would be a...
You can avoid a useless use of `cat` and handle mismatched quotes better with this: ``` $ read -r -d '' VAR <<'EOF' abc'asdf" $(dont-execute-this) foo"bar"'' EOF ``` If you don't quote the variable when you echo it, newlines are lost. Quoting it preserves them: ``` $ echo "$VAR" abc'asdf" $(dont-execute-this) foo"ba...
20381128
Installing Homebrew on macOS
367
2013-12-04 16:47:16
<p>According to the Homebrew site, to install it, I need to type:</p> <pre><code>brew install wget </code></pre> <p>I get an error message:</p> <pre><code>-bash: brew: command not found </code></pre> <p>Found <a href="https://stackoverflow.com/questions/14288682/error-installing-homebrew-brew-command-not-found">this an...
807,374
1,472,743
2025-03-10 23:17:31
20,381,183
745
2013-12-04 16:49:12
263,104
2021-05-24 14:12:05
https://stackoverflow.com/q/20381128
https://stackoverflow.com/a/20381183
<p>It's on the top of the <a href="http://brew.sh/" rel="noreferrer">Homebrew homepage</a>.</p> <p>From a Terminal prompt:</p> <pre><code>/bin/bash -c &quot;$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)&quot; </code></pre> <p>The command <code>brew install wget</code> is an example ...
<p>It's on the top of the <a href="http://brew.sh/" rel="noreferrer">Homebrew homepage</a>.</p> <p>From a Terminal prompt:</p> <pre><code>/bin/bash -c &quot;$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)&quot; </code></pre> <p>The command <code>brew install wget</code> is an example ...
369, 387, 529, 1067
bash, homebrew, installation, macos
<h1>Installing Homebrew on macOS</h1> <p>According to the Homebrew site, to install it, I need to type:</p> <pre><code>brew install wget </code></pre> <p>I get an error message:</p> <pre><code>-bash: brew: command not found </code></pre> <p>Found <a href="https://stackoverflow.com/questions/14288682/error-installing-ho...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
226
bash
# Installing Homebrew on macOS According to the Homebrew site, to install it, I need to type: ``` brew install wget ``` I get an error message: ``` -bash: brew: command not found ``` Found [this answer](https://stackoverflow.com/questions/14288682/error-installing-homebrew-brew-command-not-found). The problem, how...
It's on the top of the [Homebrew homepage](http://brew.sh/). From a Terminal prompt: ``` /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" ``` The command `brew install wget` is an example of how to use Homebrew to install another application (in this case, `wget`) aft...
18599711
How can I split a shell command over multiple lines when using an IF statement?
520
2013-09-03 19:10:19
<p>How can I split a command over multiple lines in the shell, when the command is part of an <code>if</code> statement?</p> <p>This works: </p> <pre><code>if ! fab --fabfile=.deploy/fabfile.py --forward-agent --disable-known-hosts deploy:$target; then rc=1 ...
577,656
741,970
2022-01-16 15:41:15
18,599,906
741
2013-09-03 19:22:45
797,049
2022-01-16 15:41:15
https://stackoverflow.com/q/18599711
https://stackoverflow.com/a/18599906
<p>The line-continuation will fail if you have whitespace (spaces or tab characters¹) after the backslash and before the newline. With no such whitespace, your example works fine for me:</p> <pre><code>$ cat test.sh if ! fab --fabfile=.deploy/fabfile.py \ --forward-agent \ --disable-known-hosts deploy:$target; th...
<p>The line-continuation will fail if you have whitespace (spaces or tab characters¹) after the backslash and before the newline. With no such whitespace, your example works fine for me:</p> <pre><code>$ cat test.sh if ! fab --fabfile=.deploy/fabfile.py \ --forward-agent \ --disable-known-hosts deploy:$target; th...
367, 10327
sh, syntax
<h1>How can I split a shell command over multiple lines when using an IF statement?</h1> <p>How can I split a command over multiple lines in the shell, when the command is part of an <code>if</code> statement?</p> <p>This works: </p> <pre><code>if ! fab --fabfile=.deploy/fabfile.py --forward-agent --disable-known-hos...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
227
bash
# How can I split a shell command over multiple lines when using an IF statement? How can I split a command over multiple lines in the shell, when the command is part of an `if` statement? This works: ``` if ! fab --fabfile=.deploy/fabfile.py --forward-agent --disable-known-hosts deploy:$target; then rc=1 ...
The line-continuation will fail if you have whitespace (spaces or tab characters¹) after the backslash and before the newline. With no such whitespace, your example works fine for me: ``` $ cat test.sh if ! fab --fabfile=.deploy/fabfile.py \ --forward-agent \ --disable-known-hosts deploy:$target; then echo ...
40944479
How to use bash with an Alpine based docker image?
481
2016-12-03 05:14:18
<p>I created a docker image from <code>openjdk:8-jdk-alpine</code> and I want to use <code>bash</code>, rather than <code>sh</code> as my shell, however when I try to execute simple commands I get the following errors:</p> <pre><code>RUN bash /bin/sh: bash: not found RUN ./gradlew build env: can't execute 'bash': No s...
492,917
4,830,460
2025-08-16 17:59:24
40,944,512
741
2016-12-03 05:18:06
548,225
2022-09-04 21:34:15
https://stackoverflow.com/q/40944479
https://stackoverflow.com/a/40944512
<p>Alpine docker image doesn't have bash installed by default. You will need to add the following commands to get <code>bash</code>:</p> <pre><code>RUN apk update &amp;&amp; apk add bash </code></pre> <p>If you're using <code>Alpine 3.3+</code> then you can just do:</p> <pre><code>RUN apk add --no-cache bash </code></p...
<p>Alpine docker image doesn't have bash installed by default. You will need to add the following commands to get <code>bash</code>:</p> <pre><code>RUN apk update &amp;&amp; apk add bash </code></pre> <p>If you're using <code>Alpine 3.3+</code> then you can just do:</p> <pre><code>RUN apk add --no-cache bash </code></p...
387, 390, 90304, 115689
alpine-linux, bash, docker, shell
<h1>How to use bash with an Alpine based docker image?</h1> <p>I created a docker image from <code>openjdk:8-jdk-alpine</code> and I want to use <code>bash</code>, rather than <code>sh</code> as my shell, however when I try to execute simple commands I get the following errors:</p> <pre><code>RUN bash /bin/sh: bash: no...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
228
bash
# How to use bash with an Alpine based docker image? I created a docker image from `openjdk:8-jdk-alpine` and I want to use `bash`, rather than `sh` as my shell, however when I try to execute simple commands I get the following errors: ``` RUN bash /bin/sh: bash: not found RUN ./gradlew build env: can't execute 'bas...
Alpine docker image doesn't have bash installed by default. You will need to add the following commands to get `bash`: ``` RUN apk update && apk add bash ``` If you're using `Alpine 3.3+` then you can just do: ``` RUN apk add --no-cache bash ``` To keep the docker image size small. (Thanks to comment from @sprkysnr...
3983406
vim: how to delete a newline/linefeed character(s)?
358
2010-10-21 00:32:15
<p>Is there a way to delete the newline at the end of a line in Vim, so that the next line is appended to the current line?</p> <p>For example:</p> <pre><code>Evaluator&lt;T&gt;(): _bestPos(){ } </code></pre> <p>I'd like to put this all on one line without copying lines and pasting them into the previous one. I...
286,963
247,763
2024-02-19 03:50:01
3,983,437
739
2010-10-21 00:39:04
476,895
2016-09-07 14:48:06
https://stackoverflow.com/q/3983406
https://stackoverflow.com/a/3983437
<p>If you are on the first line, pressing (upper case) <kbd>J</kbd> will join that line and the next line together, removing the newline. You can also combine this with a count, so pressing <code>3J</code> will combine all 3 lines together.</p>
<p>If you are on the first line, pressing (upper case) <kbd>J</kbd> will join that line and the next line together, removing the newline. You can also combine this with a count, so pressing <code>3J</code> will combine all 3 lines together.</p>
34, 293, 370, 386, 390
shell, ssh, unix, vi, vim
<h1>vim: how to delete a newline/linefeed character(s)?</h1> <p>Is there a way to delete the newline at the end of a line in Vim, so that the next line is appended to the current line?</p> <p>For example:</p> <pre><code>Evaluator&lt;T&gt;(): _bestPos(){ } </code></pre> <p>I'd like to put this all on one line wit...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
229
bash
# vim: how to delete a newline/linefeed character(s)? Is there a way to delete the newline at the end of a line in Vim, so that the next line is appended to the current line? For example: ``` Evaluator<T>(): _bestPos(){ } ``` I'd like to put this all on one line without copying lines and pasting them into the p...
If you are on the first line, pressing (upper case) `J` will join that line and the next line together, removing the newline. You can also combine this with a count, so pressing `3J` will combine all 3 lines together.
2961635
Using awk to print all columns from the nth to the last
484
2010-06-02 21:13:03
<p>This line worked until I had whitespace in the second field:</p> <pre><code>svn status | grep '\!' | gawk '{print $2;}' &gt; removedProjs </code></pre> <p>Is there a way to have <code>awk</code> print everything in <code>$2</code> or greater? (<code>$3</code>, <code>$4</code>.. until we don't have any more columns?...
703,125
275,354
2025-09-05 20:16:03
2,961,994
725
2010-06-02 22:10:36
286,595
2021-03-13 05:20:46
https://stackoverflow.com/q/2961635
https://stackoverflow.com/a/2961994
<p>Print all columns:</p> <pre><code>awk '{print $0}' somefile </code></pre> <p>Print all but the first column:</p> <pre><code>awk '{$1=&quot;&quot;; print $0}' somefile </code></pre> <p>Print all but the first two columns:</p> <pre><code>awk '{$1=$2=&quot;&quot;; print $0}' somefile </code></pre>
<p>Print all columns:</p> <pre><code>awk '{print $0}' somefile </code></pre> <p>Print all but the first column:</p> <pre><code>awk '{$1=&quot;&quot;; print $0}' somefile </code></pre> <p>Print all but the first two columns:</p> <pre><code>awk '{$1=$2=&quot;&quot;; print $0}' somefile </code></pre>
58, 390, 990, 1731
awk, cygwin, linux, shell
<h1>Using awk to print all columns from the nth to the last</h1> <p>This line worked until I had whitespace in the second field:</p> <pre><code>svn status | grep '\!' | gawk '{print $2;}' &gt; removedProjs </code></pre> <p>Is there a way to have <code>awk</code> print everything in <code>$2</code> or greater? (<code>$...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
230
bash
# Using awk to print all columns from the nth to the last This line worked until I had whitespace in the second field: ``` svn status | grep '\!' | gawk '{print $2;}' > removedProjs ``` Is there a way to have `awk` print everything in `$2` or greater? (`$3`, `$4`.. until we don't have any more columns?) I'm doing t...
Print all columns: ``` awk '{print $0}' somefile ``` Print all but the first column: ``` awk '{$1=""; print $0}' somefile ``` Print all but the first two columns: ``` awk '{$1=$2=""; print $0}' somefile ```
2188199
How to use double or single brackets, parentheses, curly braces
804
2010-02-02 22:22:54
<p>I am confused by the usage of brackets, parentheses, curly braces in Bash, as well as the difference between their double or single forms. Is there a clear explanation?</p>
492,885
156,458
2024-10-29 22:41:30
2,188,369
723
2010-02-02 22:48:42
26,428
2023-08-01 18:01:36
https://stackoverflow.com/q/2188199
https://stackoverflow.com/a/2188369
<p>In Bash, <code>test</code> and <code>[</code> are shell builtins.</p> <p>The <a href="http://mywiki.wooledge.org/BashFAQ/031" rel="noreferrer">double bracket</a> <code>[[</code>, which is a shell keyword, enables additional functionality. For example, you can use <code>&amp;&amp;</code> and <code>||</code> instead o...
<p>In Bash, <code>test</code> and <code>[</code> are shell builtins.</p> <p>The <a href="http://mywiki.wooledge.org/BashFAQ/031" rel="noreferrer">double bracket</a> <code>[[</code>, which is a shell keyword, enables additional functionality. For example, you can use <code>&amp;&amp;</code> and <code>||</code> instead o...
367, 387
bash, syntax
<h1>How to use double or single brackets, parentheses, curly braces</h1> <p>I am confused by the usage of brackets, parentheses, curly braces in Bash, as well as the difference between their double or single forms. Is there a clear explanation?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
231
bash
# How to use double or single brackets, parentheses, curly braces I am confused by the usage of brackets, parentheses, curly braces in Bash, as well as the difference between their double or single forms. Is there a clear explanation?
In Bash, `test` and `[` are shell builtins. The [double bracket](http://mywiki.wooledge.org/BashFAQ/031) `[[`, which is a shell keyword, enables additional functionality. For example, you can use `&&` and `||` instead of `-a` and `-o` and there's a regular expression matching operator `=~`. Also, in a simple test, do...
1891797
Capturing Groups From a Grep RegEx
541
2009-12-12 00:55:14
<p>I've got this script in <code>sh</code> (macOS 10.6) to look through an array of files:</p> <pre><code>files=&quot;*.jpg&quot; for f in $files do echo $f | grep -oEi '[0-9]+_([a-z]+)_[0-9a-z]*' name=$? echo $name done </code></pre> <p>So far <code>$name</code> merely holds 0, 1 or 2, ...
699,902
142,240
2024-07-11 21:54:01
1,892,107
717
2009-12-12 02:59:03
26,428
2024-01-09 16:26:03
https://stackoverflow.com/q/1891797
https://stackoverflow.com/a/1892107
<p>If you're using Bash, you don't even have to use <code>grep</code>:</p> <pre><code>files=&quot;*.jpg&quot; regex=&quot;[0-9]+_([a-z]+)_[0-9a-z]*&quot; # put the regex in a variable because some patterns won't work if included literally for f in $files # unquoted in order to allow the glob to expand do if [[ $...
<p>If you're using Bash, you don't even have to use <code>grep</code>:</p> <pre><code>files=&quot;*.jpg&quot; regex=&quot;[0-9]+_([a-z]+)_[0-9a-z]*&quot; # put the regex in a variable because some patterns won't work if included literally for f in $files # unquoted in order to allow the glob to expand do if [[ $...
387, 390, 1271
bash, grep, shell
<h1>Capturing Groups From a Grep RegEx</h1> <p>I've got this script in <code>sh</code> (macOS 10.6) to look through an array of files:</p> <pre><code>files=&quot;*.jpg&quot; for f in $files do echo $f | grep -oEi '[0-9]+_([a-z]+)_[0-9a-z]*' name=$? echo $name done </code></pre> <p>So far...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
232
bash
# Capturing Groups From a Grep RegEx I've got this script in `sh` (macOS 10.6) to look through an array of files: ``` files="*.jpg" for f in $files do echo $f | grep -oEi '[0-9]+_([a-z]+)_[0-9a-z]*' name=$? echo $name done ``` So far `$name` merely holds 0, 1 or 2, depending on if `gr...
If you're using Bash, you don't even have to use `grep`: ``` files="*.jpg" regex="[0-9]+_([a-z]+)_[0-9a-z]*" # put the regex in a variable because some patterns won't work if included literally for f in $files # unquoted in order to allow the glob to expand do if [[ $f =~ $regex ]] then name="${BASH...
13738634
How can I check if a string is null or empty in PowerShell?
646
2012-12-06 07:09:28
<p>Is there a built-in <code>IsNullOrEmpty</code>-like function in order to check if a string is null or empty, in PowerShell?</p> <p>I could not find it so far and if there is a built-in way, I do not want to write a function for this.</p>
1,027,825
136,141
2023-11-21 06:04:15
13,739,113
716
2012-12-06 07:48:30
9,833
2014-03-06 08:00:36
https://stackoverflow.com/q/13738634
https://stackoverflow.com/a/13739113
<p>You can use the <code>IsNullOrEmpty</code> static method:</p> <pre><code>[string]::IsNullOrEmpty(...) </code></pre>
<p>You can use the <code>IsNullOrEmpty</code> static method:</p> <pre><code>[string]::IsNullOrEmpty(...) </code></pre>
1, 139, 526, 694
.net, null, powershell, string
<h1>How can I check if a string is null or empty in PowerShell?</h1> <p>Is there a built-in <code>IsNullOrEmpty</code>-like function in order to check if a string is null or empty, in PowerShell?</p> <p>I could not find it so far and if there is a built-in way, I do not want to write a function for this.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
233
bash
# How can I check if a string is null or empty in PowerShell? Is there a built-in `IsNullOrEmpty`-like function in order to check if a string is null or empty, in PowerShell? I could not find it so far and if there is a built-in way, I do not want to write a function for this.
You can use the `IsNullOrEmpty` static method: ``` [string]::IsNullOrEmpty(...) ```
8095638
How do I negate a condition in PowerShell?
404
2011-11-11 14:50:10
<p>How do I negate a conditional test in PowerShell?</p> <p>For example, if I want to check for the directory C:\Code, I can run:</p> <pre><code>if (Test-Path C:\Code){ write "it exists!" } </code></pre> <p>Is there a way to negate that condition, e.g. (non-working):</p> <pre><code>if (Not (Test-Path C:\Code)){ ...
478,837
166,258
2025-12-29 19:57:40
8,095,680
716
2011-11-11 14:54:03
291,709
2025-10-08 18:07:41
https://stackoverflow.com/q/8095638
https://stackoverflow.com/a/8095680
<p>You almost had it with <code>Not</code>. It should be:</p> <pre><code>if (-Not (Test-Path C:\Code)) { write &quot;it doesn't exist!&quot; } </code></pre> <p>You can also use <code>!</code>:</p> <pre><code>if (!(Test-Path C:\Code)){}` </code></pre> <p>Note that <code>!</code> above <strong>requires</strong> an a...
<p>You almost had it with <code>Not</code>. It should be:</p> <pre><code>if (-Not (Test-Path C:\Code)) { write &quot;it doesn't exist!&quot; } </code></pre> <p>You can also use <code>!</code>:</p> <pre><code>if (!(Test-Path C:\Code)){}` </code></pre> <p>Note that <code>!</code> above <strong>requires</strong> an a...
64, 526
powershell, windows
<h1>How do I negate a condition in PowerShell?</h1> <p>How do I negate a conditional test in PowerShell?</p> <p>For example, if I want to check for the directory C:\Code, I can run:</p> <pre><code>if (Test-Path C:\Code){ write "it exists!" } </code></pre> <p>Is there a way to negate that condition, e.g. (non-worki...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
234
bash
# How do I negate a condition in PowerShell? How do I negate a conditional test in PowerShell? For example, if I want to check for the directory C:\Code, I can run: ``` if (Test-Path C:\Code){ write "it exists!" } ``` Is there a way to negate that condition, e.g. (non-working): ``` if (Not (Test-Path C:\Code)){ ...
You almost had it with `Not`. It should be: ``` if (-Not (Test-Path C:\Code)) { write "it doesn't exist!" } ``` You can also use `!`: ``` if (!(Test-Path C:\Code)){}` ``` Note that `!` above **requires** an additional set of parentheses around the negated conditional statement; `if !(Test-path C:\Code) {}` will...
3213748
Get line number while using grep
478
2010-07-09 14:49:26
<p>I am using grep recursive to search files for a string, and all the matched files and the lines containing that string are printed on the terminal. But is it possible to get the line numbers of those lines too?</p> <p>Example: presently, I get <code>/var/www/file.php: $options = &quot;this.target&quot;</code>, but I...
553,763
320,755
2024-01-28 05:29:56
3,213,773
715
2010-07-09 14:52:26
369,692
2020-05-10 14:10:37
https://stackoverflow.com/q/3213748
https://stackoverflow.com/a/3213773
<pre><code>grep -n SEARCHTERM file1 file2 ... </code></pre>
<pre><code>grep -n SEARCHTERM file1 file2 ... </code></pre>
58, 390, 1271, 9325
grep, line, linux, shell
<h1>Get line number while using grep</h1> <p>I am using grep recursive to search files for a string, and all the matched files and the lines containing that string are printed on the terminal. But is it possible to get the line numbers of those lines too?</p> <p>Example: presently, I get <code>/var/www/file.php: $optio...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
235
bash
# Get line number while using grep I am using grep recursive to search files for a string, and all the matched files and the lines containing that string are printed on the terminal. But is it possible to get the line numbers of those lines too? Example: presently, I get `/var/www/file.php: $options = "this.target"`,...
``` grep -n SEARCHTERM file1 file2 ... ```
8903239
How can I calculate time elapsed in a Bash script?
335
2012-01-17 23:38:54
<p>I print the start and end time using <code>date +&quot;%T&quot;</code>, which results in something like:</p> <pre><code>10:33:56 10:36:10 </code></pre> <p>How could I calculate and print the difference between these two?</p> <p>I would like to get something like:</p> <pre><code>2m 14s </code></pre>
436,105
247,243
2025-03-13 16:14:00
8,903,280
715
2012-01-17 23:44:07
1,155,000
2024-03-13 01:39:16
https://stackoverflow.com/q/8903239
https://stackoverflow.com/a/8903280
<p>Bash has a handy <code>SECONDS</code> builtin variable that tracks the number of seconds that have passed since the shell was started. This variable retains its properties when assigned to, and the value returned after the assignment is the number of seconds since the assignment plus the assigned value.</p> <p>Thus,...
<p>Bash has a handy <code>SECONDS</code> builtin variable that tracks the number of seconds that have passed since the shell was started. This variable retains its properties when assigned to, and the value returned after the assignment is the number of seconds since the assignment plus the assigned value.</p> <p>Thus,...
387, 5002
bash, date
<h1>How can I calculate time elapsed in a Bash script?</h1> <p>I print the start and end time using <code>date +&quot;%T&quot;</code>, which results in something like:</p> <pre><code>10:33:56 10:36:10 </code></pre> <p>How could I calculate and print the difference between these two?</p> <p>I would like to get something...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
236
bash
# How can I calculate time elapsed in a Bash script? I print the start and end time using `date +"%T"`, which results in something like: ``` 10:33:56 10:36:10 ``` How could I calculate and print the difference between these two? I would like to get something like: ``` 2m 14s ```
Bash has a handy `SECONDS` builtin variable that tracks the number of seconds that have passed since the shell was started. This variable retains its properties when assigned to, and the value returned after the assignment is the number of seconds since the assignment plus the assigned value. Thus, you can just set `S...
36273665
What does `set -x` do?
498
2016-03-29 00:58:08
<p>I have a shell script with the following line in it:</p> <pre><code>[ "$DEBUG" == 'true' ] &amp;&amp; set -x </code></pre>
416,474
1,684,269
2024-07-28 14:42:43
36,273,740
710
2016-03-29 01:07:25
4,323
2024-07-28 14:42:43
https://stackoverflow.com/q/36273665
https://stackoverflow.com/a/36273740
<p><code>set -x</code> enables a shell mode where all executed commands are printed to the terminal.</p> <p>In your case it's used for debugging, which is a typical use case for <code>set -x</code>: printing every command as it is executed may help you visualize the script's control flow if it is not functioning as exp...
<p><code>set -x</code> enables a shell mode where all executed commands are printed to the terminal.</p> <p>In your case it's used for debugging, which is a typical use case for <code>set -x</code>: printing every command as it is executed may help you visualize the script's control flow if it is not functioning as exp...
34, 390, 531
scripting, shell, unix
<h1>What does `set -x` do?</h1> <p>I have a shell script with the following line in it:</p> <pre><code>[ "$DEBUG" == 'true' ] &amp;&amp; set -x </code></pre>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
237
bash
# What does `set -x` do? I have a shell script with the following line in it: ``` [ "$DEBUG" == 'true' ] && set -x ```
`set -x` enables a shell mode where all executed commands are printed to the terminal. In your case it's used for debugging, which is a typical use case for `set -x`: printing every command as it is executed may help you visualize the script's control flow if it is not functioning as expected. `set +x` disables it.
8586648
Going to a specific line number using Less in Unix
513
2011-12-21 08:04:01
<p>I have a file that has around million lines. I need to go to line number 320123 to check the data. How do I do that?</p>
394,521
386,946
2023-10-05 10:43:48
8,586,690
708
2011-12-21 08:08:03
275,431
2014-09-15 15:20:19
https://stackoverflow.com/q/8586648
https://stackoverflow.com/a/8586690
<p>With <code>n</code> being the line number:</p> <ul> <li><code>ng</code>: Jump to line number n. Default is the start of the file.</li> <li><code>nG</code>: Jump to line number n. Default is the end of the file.</li> </ul> <p>So to go to line number 320123, you would type <code>320123g</code>.</p> <p>Copy-pasted s...
<p>With <code>n</code> being the line number:</p> <ul> <li><code>ng</code>: Jump to line number n. Default is the start of the file.</li> <li><code>nG</code>: Jump to line number n. Default is the end of the file.</li> </ul> <p>So to go to line number 320123, you would type <code>320123g</code>.</p> <p>Copy-pasted s...
34, 390, 816, 126205
less-unix, search, shell, unix
<h1>Going to a specific line number using Less in Unix</h1> <p>I have a file that has around million lines. I need to go to line number 320123 to check the data. How do I do that?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
238
bash
# Going to a specific line number using Less in Unix I have a file that has around million lines. I need to go to line number 320123 to check the data. How do I do that?
With `n` being the line number: - `ng`: Jump to line number n. Default is the start of the file. - `nG`: Jump to line number n. Default is the end of the file. So to go to line number 320123, you would type `320123g`. Copy-pasted straight from [Wikipedia](http://en.wikipedia.org/wiki/Less_(Unix)).
255414
Why can't I change directories using "cd" in a script?
840
2008-11-01 02:07:08
<p>I'm trying to write a small script to change the current directory to my project directory:</p> <pre><code>#!/bin/bash cd /home/tree/projects/java </code></pre> <p>I saved this file as proj, added execute permission with <code>chmod</code>, and copied it to <code>/usr/bin</code>. When I call it by: <code>proj</cod...
779,356
33,203
2024-07-18 02:27:41
255,415
707
2008-11-01 02:09:06
893
2014-05-08 13:18:46
https://stackoverflow.com/q/255414
https://stackoverflow.com/a/255415
<p>Shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is. The <code>cd</code> succeeds, but as soon as the subshell exits, you're back in the interactive shell and nothing ever changed there.</p> <p>One way to get around this is to use an alias instead:</p> <p...
<p>Shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is. The <code>cd</code> succeeds, but as soon as the subshell exits, you're back in the interactive shell and nothing ever changed there.</p> <p>One way to get around this is to use an alias instead:</p> <p...
58, 387, 390
bash, linux, shell
<h1>Why can't I change directories using "cd" in a script?</h1> <p>I'm trying to write a small script to change the current directory to my project directory:</p> <pre><code>#!/bin/bash cd /home/tree/projects/java </code></pre> <p>I saved this file as proj, added execute permission with <code>chmod</code>, and copied...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
239
bash
# Why can't I change directories using "cd" in a script? I'm trying to write a small script to change the current directory to my project directory: ``` #!/bin/bash cd /home/tree/projects/java ``` I saved this file as proj, added execute permission with `chmod`, and copied it to `/usr/bin`. When I call it by: `proj`...
Shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is. The `cd` succeeds, but as soon as the subshell exits, you're back in the interactive shell and nothing ever changed there. One way to get around this is to use an alias instead: ``` alias proj="cd /home/tr...
1765311
How to view files in binary from bash?
410
2009-11-19 18:04:11
<p>I would like to view the contents of a file in the current directory, but in binary from the command line. How can I achieve this?</p> <p>For example, something like this:</p> <pre class="lang-none prettyprint-override"><code>$ cat test Hello, world! $ binarycat test 01001000 01100101 01101100 01101100 01101111 001...
627,783
211,176
2025-12-02 16:22:44
20,305,782
707
2013-11-30 21:20:58
1,409,834
2020-04-10 08:47:10
https://stackoverflow.com/q/1765311
https://stackoverflow.com/a/20305782
<p><a href="https://linux.die.net/man/1/xxd" rel="noreferrer"><code>xxd</code></a> does both binary and hexadecimal.</p> <p>bin:</p> <pre><code>xxd -b file </code></pre> <p>hex:</p> <pre><code>xxd file </code></pre>
<p><a href="https://linux.die.net/man/1/xxd" rel="noreferrer"><code>xxd</code></a> does both binary and hexadecimal.</p> <p>bin:</p> <pre><code>xxd -b file </code></pre> <p>hex:</p> <pre><code>xxd file </code></pre>
368, 390
binary, shell
<h1>How to view files in binary from bash?</h1> <p>I would like to view the contents of a file in the current directory, but in binary from the command line. How can I achieve this?</p> <p>For example, something like this:</p> <pre class="lang-none prettyprint-override"><code>$ cat test Hello, world! $ binarycat test ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
240
bash
# How to view files in binary from bash? I would like to view the contents of a file in the current directory, but in binary from the command line. How can I achieve this? For example, something like this: ``` $ cat test Hello, world! $ binarycat test 01001000 01100101 01101100 01101100 01101111 00101100 00100000 0...
[`xxd`](https://linux.die.net/man/1/xxd) does both binary and hexadecimal. bin: ``` xxd -b file ``` hex: ``` xxd file ```
29244351
How to sort a file in-place?
350
2015-03-24 22:49:08
<p>When we use the <code>sort file</code> command, the file shows its contents in a sorted way. What if I don't want to get any output on stdout, but in the input file instead?</p>
218,702
4,705,190
2023-04-25 02:41:34
29,244,408
706
2015-03-24 22:53:57
4,698,204
2022-06-06 09:44:06
https://stackoverflow.com/q/29244351
https://stackoverflow.com/a/29244408
<p>You can use the <code>-o</code>, <code>--output=FILE</code> option of sort to indicate the same input and output file:</p> <pre><code>sort -o file file </code></pre> <p>Without repeating the filename (with <a href="https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html" rel="noreferrer">bash brace e...
<p>You can use the <code>-o</code>, <code>--output=FILE</code> option of sort to indicate the same input and output file:</p> <pre><code>sort -o file file </code></pre> <p>Without repeating the filename (with <a href="https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html" rel="noreferrer">bash brace e...
34, 58, 387, 390
bash, linux, shell, unix
<h1>How to sort a file in-place?</h1> <p>When we use the <code>sort file</code> command, the file shows its contents in a sorted way. What if I don't want to get any output on stdout, but in the input file instead?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
241
bash
# How to sort a file in-place? When we use the `sort file` command, the file shows its contents in a sorted way. What if I don't want to get any output on stdout, but in the input file instead?
You can use the `-o`, `--output=FILE` option of sort to indicate the same input and output file: ``` sort -o file file ``` Without repeating the filename (with [bash brace expansion](https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html)) ``` sort -o file{,} ``` ⚠️ **Important note:** a common mis...
45142855
/bin/sh: apt-get: not found
283
2017-07-17 11:18:39
<p>I am trying to change a dockerFile to work with aspell. I have a bash script that I want to wrap in a dock</p> <blockquote> <p>Step 4: Wrap the script in a Docker container.</p> <p>The sample SDK we downloaded earlier contains an example of an action wrapped in a Docker container. In particular, the sample SDK inclu...
455,702
7,877,578
2024-10-05 08:27:39
45,143,116
706
2017-07-17 11:29:43
3,411,861
2017-07-17 11:33:41
https://stackoverflow.com/q/45142855
https://stackoverflow.com/a/45143116
<p>The <a href="https://hub.docker.com/r/openwhisk/dockerskeleton/~/dockerfile/" rel="noreferrer">image you're using</a> is <a href="https://alpinelinux.org" rel="noreferrer">Alpine based</a>, so you can't use <code>apt-get</code> because it's Ubuntu's package manager.</p> <p>To fix this just use: </p> <p><code>apk u...
<p>The <a href="https://hub.docker.com/r/openwhisk/dockerskeleton/~/dockerfile/" rel="noreferrer">image you're using</a> is <a href="https://alpinelinux.org" rel="noreferrer">Alpine based</a>, so you can't use <code>apt-get</code> because it's Ubuntu's package manager.</p> <p>To fix this just use: </p> <p><code>apk u...
387, 4905, 90304, 108477
aspell, bash, docker, dockerfile
<h1>/bin/sh: apt-get: not found</h1> <p>I am trying to change a dockerFile to work with aspell. I have a bash script that I want to wrap in a dock</p> <blockquote> <p>Step 4: Wrap the script in a Docker container.</p> <p>The sample SDK we downloaded earlier contains an example of an action wrapped in a Docker container...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
242
bash
# /bin/sh: apt-get: not found I am trying to change a dockerFile to work with aspell. I have a bash script that I want to wrap in a dock > Step 4: Wrap the script in a Docker container. > > The sample SDK we downloaded earlier contains an example of an action wrapped in a Docker container. In particular, the sample S...
The [image you're using](https://hub.docker.com/r/openwhisk/dockerskeleton/~/dockerfile/) is [Alpine based](https://alpinelinux.org), so you can't use `apt-get` because it's Ubuntu's package manager. To fix this just use: `apk update` and `apk add`
296536
How to urlencode data for curl command?
570
2008-11-17 19:09:59
<p>I am trying to write a bash script for testing that takes a parameter and sends it through curl to web site. I need to url encode the value to make sure that special characters are processed properly. What is the best way to do this? </p> <p>Here is my basic script so far:</p> <pre><code>#!/bin/bash host=${1:?'...
823,331
19,130
2024-08-07 11:38:35
2,027,690
700
2010-01-08 13:05:40
114,168
2022-04-19 08:20:09
https://stackoverflow.com/q/296536
https://stackoverflow.com/a/2027690
<p>Use <code>curl --data-urlencode</code>; from <code>man curl</code>:</p> <blockquote> <p>This posts data, similar to the other <code>--data</code> options with the exception that this performs URL-encoding. To be CGI-compliant, the <code>&lt;data&gt;</code> part should begin with a name followed by a separator and a ...
<p>Use <code>curl --data-urlencode</code>; from <code>man curl</code>:</p> <blockquote> <p>This posts data, similar to the other <code>--data</code> options with the exception that this performs URL-encoding. To be CGI-compliant, the <code>&lt;data&gt;</code> part should begin with a name followed by a separator and a ...
387, 390, 531, 1554, 8023
bash, curl, scripting, shell, urlencode
<h1>How to urlencode data for curl command?</h1> <p>I am trying to write a bash script for testing that takes a parameter and sends it through curl to web site. I need to url encode the value to make sure that special characters are processed properly. What is the best way to do this? </p> <p>Here is my basic scrip...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
243
bash
# How to urlencode data for curl command? I am trying to write a bash script for testing that takes a parameter and sends it through curl to web site. I need to url encode the value to make sure that special characters are processed properly. What is the best way to do this? Here is my basic script so far: ``` #!/bi...
Use `curl --data-urlencode`; from `man curl`: > This posts data, similar to the other `--data` options with the exception that this performs URL-encoding. To be CGI-compliant, the `<data>` part should begin with a name followed by a separator and a content specification. Example usage: ``` curl \ --data-urlencod...
50861082
How to print environment variables to the console in PowerShell?
481
2018-06-14 15:29:32
<p>I'm starting to use PowerShell and am trying to figure out how to <code>echo</code> a system environment variable to the console to read it.</p> <p>Neither of the below are working. The first just prints <code>%PATH%</code>, and the second prints nothing.</p> <pre><code>echo %PATH% echo $PATH </code></pre>
550,616
1,103,595
2024-07-28 11:03:17
50,861,113
698
2018-06-14 15:31:25
712,649
2024-07-28 11:03:17
https://stackoverflow.com/q/50861082
https://stackoverflow.com/a/50861113
<p>Prefix the variable name with <code>env</code>:</p> <pre><code>$env:path </code></pre> <p>For example, if you want to print the value of environment value <code>MINISHIFT_USERNAME</code>, then command will be:</p> <pre><code>$env:MINISHIFT_USERNAME </code></pre> <p>In case the environment variable label contains cha...
<p>Prefix the variable name with <code>env</code>:</p> <pre><code>$env:path </code></pre> <p>For example, if you want to print the value of environment value <code>MINISHIFT_USERNAME</code>, then command will be:</p> <pre><code>$env:MINISHIFT_USERNAME </code></pre> <p>In case the environment variable label contains cha...
526
powershell
<h1>How to print environment variables to the console in PowerShell?</h1> <p>I'm starting to use PowerShell and am trying to figure out how to <code>echo</code> a system environment variable to the console to read it.</p> <p>Neither of the below are working. The first just prints <code>%PATH%</code>, and the second pr...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
244
bash
# How to print environment variables to the console in PowerShell? I'm starting to use PowerShell and am trying to figure out how to `echo` a system environment variable to the console to read it. Neither of the below are working. The first just prints `%PATH%`, and the second prints nothing. ``` echo %PATH% echo $P...
Prefix the variable name with `env`: ``` $env:path ``` For example, if you want to print the value of environment value `MINISHIFT_USERNAME`, then command will be: ``` $env:MINISHIFT_USERNAME ``` In case the environment variable label contains characters otherwise interpreted as bareword token terminators (like `.`...
3968103
How can I format my grep output to show line numbers at the end of the line, and also the hit count?
445
2010-10-19 12:06:02
<p>I'm using grep to match string in a file. Here is an example file:</p> <pre><code>example one, example two null, example three, example four null, </code></pre> <p><code>grep -i null myfile.txt</code> returns </p> <pre><code>example two null, example four null, </code></pre> <p>How can I return matched lines tog...
444,052
282,383
2019-07-03 14:28:46
3,968,142
695
2010-10-19 12:10:17
7,412
2016-08-08 18:20:38
https://stackoverflow.com/q/3968103
https://stackoverflow.com/a/3968142
<p><code>-n</code> returns line number.</p> <p><code>-i</code> is for ignore-case. Only to be used if case matching is not necessary</p> <pre><code>$ grep -in null myfile.txt 2:example two null, 4:example four null, </code></pre> <p>Combine with <code>awk</code> to print out the line number after the match:</p> <p...
<p><code>-n</code> returns line number.</p> <p><code>-i</code> is for ignore-case. Only to be used if case matching is not necessary</p> <pre><code>$ grep -in null myfile.txt 2:example two null, 4:example four null, </code></pre> <p>Combine with <code>awk</code> to print out the line number after the match:</p> <p...
34, 58, 387, 1271
bash, grep, linux, unix
<h1>How can I format my grep output to show line numbers at the end of the line, and also the hit count?</h1> <p>I'm using grep to match string in a file. Here is an example file:</p> <pre><code>example one, example two null, example three, example four null, </code></pre> <p><code>grep -i null myfile.txt</code> retu...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
245
bash
# How can I format my grep output to show line numbers at the end of the line, and also the hit count? I'm using grep to match string in a file. Here is an example file: ``` example one, example two null, example three, example four null, ``` `grep -i null myfile.txt` returns ``` example two null, example four null...
`-n` returns line number. `-i` is for ignore-case. Only to be used if case matching is not necessary ``` $ grep -in null myfile.txt 2:example two null, 4:example four null, ``` Combine with `awk` to print out the line number after the match: ``` $ grep -in null myfile.txt | awk -F: '{print $2" - Line number : "$1}...
12457457
count number of lines in terminal output
393
2012-09-17 10:31:50
<p>couldn't find this on SO. I ran the following command in the terminal: </p> <pre><code>&gt;&gt; grep -Rl "curl" ./ </code></pre> <p>and this displays the list of files where the keyword curl occurs. I want to count the number of files. First way I can think of, is to count the number of lines in the output that ca...
420,134
799,068
2024-08-22 14:27:05
12,457,470
695
2012-09-17 10:32:36
140,816
2012-09-17 10:32:36
https://stackoverflow.com/q/12457457
https://stackoverflow.com/a/12457470
<p>Pipe the result to <a href="http://en.wikipedia.org/wiki/Wc_%28Unix%29" rel="noreferrer"><code>wc</code></a> using the <code>-l</code> (<em>line count</em>) switch:</p> <pre><code>grep -Rl "curl" ./ | wc -l </code></pre>
<p>Pipe the result to <a href="http://en.wikipedia.org/wiki/Wc_%28Unix%29" rel="noreferrer"><code>wc</code></a> using the <code>-l</code> (<em>line count</em>) switch:</p> <pre><code>grep -Rl "curl" ./ | wc -l </code></pre>
387, 391
bash, terminal
<h1>count number of lines in terminal output</h1> <p>couldn't find this on SO. I ran the following command in the terminal: </p> <pre><code>&gt;&gt; grep -Rl "curl" ./ </code></pre> <p>and this displays the list of files where the keyword curl occurs. I want to count the number of files. First way I can think of, is ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
246
bash
# count number of lines in terminal output couldn't find this on SO. I ran the following command in the terminal: ``` >> grep -Rl "curl" ./ ``` and this displays the list of files where the keyword curl occurs. I want to count the number of files. First way I can think of, is to count the number of lines in the outp...
Pipe the result to [`wc`](http://en.wikipedia.org/wiki/Wc_%28Unix%29) using the `-l` (*line count*) switch: ``` grep -Rl "curl" ./ | wc -l ```
9381463
How to create a file in Linux from terminal window?
553
2012-02-21 16:42:47
<p>What's the easiest way to create a file in Linux terminal?</p>
2,216,433
791,406
2019-04-15 21:16:46
9,381,492
694
2012-02-21 16:44:48
1,100,552
2013-07-30 11:49:39
https://stackoverflow.com/q/9381463
https://stackoverflow.com/a/9381492
<p>Depending on what you want the file to contain:</p> <ul> <li><code>touch /path/to/file</code> for an empty file</li> <li><p><code>somecommand &gt; /path/to/file</code> for a file containing the output of some command.</p> <pre><code> eg: grep --help &gt; randomtext.txt echo "This is some text" &gt; randomte...
<p>Depending on what you want the file to contain:</p> <ul> <li><code>touch /path/to/file</code> for an empty file</li> <li><p><code>somecommand &gt; /path/to/file</code> for a file containing the output of some command.</p> <pre><code> eg: grep --help &gt; randomtext.txt echo "This is some text" &gt; randomte...
58, 387, 5310, 11225
bash, command-line-interface, file, linux
<h1>How to create a file in Linux from terminal window?</h1> <p>What's the easiest way to create a file in Linux terminal?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
247
bash
# How to create a file in Linux from terminal window? What's the easiest way to create a file in Linux terminal?
Depending on what you want the file to contain: - `touch /path/to/file` for an empty file - `somecommand > /path/to/file` for a file containing the output of some command. ``` eg: grep --help > randomtext.txt echo "This is some text" > randomtext.txt ``` - `nano /path/to/file` or `vi /path/to/file` (o...
617182
How can I suppress all output from a command using Bash?
417
2009-03-05 23:38:25
<p>I have a Bash script that runs a program with parameters. That program outputs some status (doing this, doing that...). There isn't any option for this program to be quiet. How can I prevent the script from displaying anything?</p> <p>I am looking for something like Windows' <a href="https://ss64.com/nt/echo.html"...
510,578
74,480
2024-08-13 12:57:53
617,184
690
2009-03-05 23:39:17
46,354
2020-06-04 13:35:47
https://stackoverflow.com/q/617182
https://stackoverflow.com/a/617184
<p>The following sends standard output to the null device (bit bucket).</p> <pre><code>scriptname &gt;/dev/null </code></pre> <p>And if you also want error messages to be sent there, use one of (the first may not work in all shells):</p> <pre><code>scriptname &amp;&gt;/dev/null scriptname &gt;/dev/null 2&gt;&amp;1 s...
<p>The following sends standard output to the null device (bit bucket).</p> <pre><code>scriptname &gt;/dev/null </code></pre> <p>And if you also want error messages to be sent there, use one of (the first may not work in all shells):</p> <pre><code>scriptname &amp;&gt;/dev/null scriptname &gt;/dev/null 2&gt;&amp;1 s...
387, 390, 531, 13824
bash, echo, scripting, shell
<h1>How can I suppress all output from a command using Bash?</h1> <p>I have a Bash script that runs a program with parameters. That program outputs some status (doing this, doing that...). There isn't any option for this program to be quiet. How can I prevent the script from displaying anything?</p> <p>I am looking f...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
248
bash
# How can I suppress all output from a command using Bash? I have a Bash script that runs a program with parameters. That program outputs some status (doing this, doing that...). There isn't any option for this program to be quiet. How can I prevent the script from displaying anything? I am looking for something like...
The following sends standard output to the null device (bit bucket). ``` scriptname >/dev/null ``` And if you also want error messages to be sent there, use one of (the first may not work in all shells): ``` scriptname &>/dev/null scriptname >/dev/null 2>&1 scriptname >/dev/null 2>/dev/null ``` And, if you want to ...
7114990
Pseudo-terminal will not be allocated because stdin is not a terminal
512
2011-08-18 22:38:40
<p>I am trying to write a shell script that creates some directories on a remote server and then uses scp to copy files from my local machine onto the remote. Here's what I have so far:</p> <pre><code>ssh -t user@server&lt;&lt;EOT DEP_ROOT='/home/matthewr/releases' datestamp=$(date +%Y%m%d%H%M%S) REL_DIR=$DEP_ROOT"/"$...
779,453
818,091
2022-11-27 20:30:14
7,122,115
689
2011-08-19 13:14:23
902,495
2017-12-02 14:30:41
https://stackoverflow.com/q/7114990
https://stackoverflow.com/a/7122115
<p>Try <code>ssh -t -t</code>(or <code>ssh -tt</code> for short) to force pseudo-tty allocation even if stdin isn't a terminal.</p> <p>See also: <a href="https://stackoverflow.com/questions/7085429/terminating-ssh-session-executed-by-bash-script">Terminating SSH session executed by bash script</a></p> <p>From ssh man...
<p>Try <code>ssh -t -t</code>(or <code>ssh -tt</code> for short) to force pseudo-tty allocation even if stdin isn't a terminal.</p> <p>See also: <a href="https://stackoverflow.com/questions/7085429/terminating-ssh-session-executed-by-bash-script">Terminating SSH session executed by bash script</a></p> <p>From ssh man...
58, 386, 387, 390
bash, linux, shell, ssh
<h1>Pseudo-terminal will not be allocated because stdin is not a terminal</h1> <p>I am trying to write a shell script that creates some directories on a remote server and then uses scp to copy files from my local machine onto the remote. Here's what I have so far:</p> <pre><code>ssh -t user@server&lt;&lt;EOT DEP_ROOT=...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
249
bash
# Pseudo-terminal will not be allocated because stdin is not a terminal I am trying to write a shell script that creates some directories on a remote server and then uses scp to copy files from my local machine onto the remote. Here's what I have so far: ``` ssh -t user@server<<EOT DEP_ROOT='/home/matthewr/releases' ...
Try `ssh -t -t`(or `ssh -tt` for short) to force pseudo-tty allocation even if stdin isn't a terminal. See also: [Terminating SSH session executed by bash script](https://stackoverflow.com/questions/7085429/terminating-ssh-session-executed-by-bash-script) From ssh manpage: ``` -T Disable pseudo-tty allocation. ...
10341271
Switching from zsh to bash on OS X, and back again?
359
2012-04-26 20:52:23
<p>I'm learning to develop in Rails, and have discovered the power of <code>zsh</code>. However, for some of my other tasks, I wish to use normal <code>bash</code>.</p> <p>Although they are the same, I just feel comfortable with the layout of <code>bash</code> in some situations.</p> <p>How do I switch back and forth, ...
509,519
1,359,708
2023-09-09 17:05:35
10,341,338
687
2012-04-26 20:58:41
147,356
2012-04-26 20:58:41
https://stackoverflow.com/q/10341271
https://stackoverflow.com/a/10341338
<p>You can just use <code>exec</code> to replace your current shell with a new shell:</p> <p>Switch to <code>bash</code>:</p> <pre><code>exec bash </code></pre> <p>Switch to <code>zsh</code>:</p> <pre><code>exec zsh </code></pre> <p>This won't affect new terminal windows or anything, but it's convenient.</p>
<p>You can just use <code>exec</code> to replace your current shell with a new shell:</p> <p>Switch to <code>bash</code>:</p> <pre><code>exec bash </code></pre> <p>Switch to <code>zsh</code>:</p> <pre><code>exec zsh </code></pre> <p>This won't affect new terminal windows or anything, but it's convenient.</p>
387, 391, 3791
bash, terminal, zsh
<h1>Switching from zsh to bash on OS X, and back again?</h1> <p>I'm learning to develop in Rails, and have discovered the power of <code>zsh</code>. However, for some of my other tasks, I wish to use normal <code>bash</code>.</p> <p>Although they are the same, I just feel comfortable with the layout of <code>bash</code...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
250
bash
# Switching from zsh to bash on OS X, and back again? I'm learning to develop in Rails, and have discovered the power of `zsh`. However, for some of my other tasks, I wish to use normal `bash`. Although they are the same, I just feel comfortable with the layout of `bash` in some situations. How do I switch back and ...
You can just use `exec` to replace your current shell with a new shell: Switch to `bash`: ``` exec bash ``` Switch to `zsh`: ``` exec zsh ``` This won't affect new terminal windows or anything, but it's convenient.
19075671
How do I use shell variables in an awk script?
416
2013-09-29 07:45:03
<p>I found some ways to pass external shell variables to an <code>awk</code> script, but I'm confused about <code>'</code> and <code>"</code>.</p> <p>First, I tried with a shell script:</p> <pre><code>$ v=123test $ echo $v 123test $ echo "$v" 123test </code></pre> <p>Then tried awk:</p> <pre><code>$ awk 'BEGIN{prin...
746,867
2,266,338
2024-06-07 07:37:51
19,075,707
685
2013-09-29 07:51:52
2,341,847
2024-06-07 07:37:51
https://stackoverflow.com/q/19075671
https://stackoverflow.com/a/19075707
<blockquote> <p>#Getting shell variables into <code>awk</code> may be done in several ways. Some are better than others. This should cover most of them. If you have a comment, please leave below.                                                                                    v1.5</p> </blockquote> <hr /> <h2>Using ...
<blockquote> <p>#Getting shell variables into <code>awk</code> may be done in several ways. Some are better than others. This should cover most of them. If you have a comment, please leave below.                                                                                    v1.5</p> </blockquote> <hr /> <h2>Using ...
387, 390, 990
awk, bash, shell
<h1>How do I use shell variables in an awk script?</h1> <p>I found some ways to pass external shell variables to an <code>awk</code> script, but I'm confused about <code>'</code> and <code>"</code>.</p> <p>First, I tried with a shell script:</p> <pre><code>$ v=123test $ echo $v 123test $ echo "$v" 123test </code></pr...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
251
bash
# How do I use shell variables in an awk script? I found some ways to pass external shell variables to an `awk` script, but I'm confused about `'` and `"`. First, I tried with a shell script: ``` $ v=123test $ echo $v 123test $ echo "$v" 123test ``` Then tried awk: ``` $ awk 'BEGIN{print "'$v'"}' $ 123test $ awk '...
> #Getting shell variables into `awk` > may be done in several ways. Some are better than others. This should cover most of them. If you have a comment, please leave below.                                                                                    v1.5 --- ## Using `-v` (The best way, most portable) Use the ...
786376
How do I run a program with a different working directory from current, from Linux shell?
433
2009-04-24 15:36:23
<p>Using a <strong>Linux shell</strong>, how do I start a program with a different working directory from the current working directory?</p> <p>For example, I have a binary file <code>helloworld</code> that creates the file <code>hello-world.txt</code> in the <strong>current directory</strong>. <br><br> This file is ...
344,355
18,775
2024-09-04 17:11:45
786,419
682
2009-04-24 15:46:04
4,918
2015-05-02 23:31:27
https://stackoverflow.com/q/786376
https://stackoverflow.com/a/786419
<p>Call the program like this:</p> <pre><code>(cd /c; /a/helloworld) </code></pre> <p>The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to <code>/c</code>, then executes <code>helloworld</code> from <code>/a</code>. After the program exits, the sub-shell terminates, re...
<p>Call the program like this:</p> <pre><code>(cd /c; /a/helloworld) </code></pre> <p>The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to <code>/c</code>, then executes <code>helloworld</code> from <code>/a</code>. After the program exits, the sub-shell terminates, re...
58, 390, 421
environment, linux, shell
<h1>How do I run a program with a different working directory from current, from Linux shell?</h1> <p>Using a <strong>Linux shell</strong>, how do I start a program with a different working directory from the current working directory?</p> <p>For example, I have a binary file <code>helloworld</code> that creates the f...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
252
bash
# How do I run a program with a different working directory from current, from Linux shell? Using a **Linux shell**, how do I start a program with a different working directory from the current working directory? For example, I have a binary file `helloworld` that creates the file `hello-world.txt` in the **current d...
Call the program like this: ``` (cd /c; /a/helloworld) ``` The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to `/c`, then executes `helloworld` from `/a`. After the program exits, the sub-shell terminates, returning you to your prompt of the parent shell, in the direc...
11193466
"echo -n" prints "-n"
638
2012-06-25 16:38:14
<p>I have a problem with <code>echo</code> in my script:</p> <pre><code>echo -n "Some string..." </code></pre> <p>prints</p> <pre class="lang-none prettyprint-override"><code>-n Some string... </code></pre> <p>and moves to the next line. In the console it's working correcly without newline:</p> <pre class="lang-no...
708,612
1,202,769
2024-03-13 02:08:19
11,193,533
679
2012-06-25 16:42:33
827,263
2024-03-13 02:08:19
https://stackoverflow.com/q/11193466
https://stackoverflow.com/a/11193533
<p>There are multiple versions of the <code>echo</code> command, with different behaviors. It's often built into the shell. If it isn't, <code>echo</code> will typically invoke <code>/bin/echo</code> or <code>/usr/bin/echo</code>, which itself can be from any of a number of implementations.</p> <p>Apparently the shell ...
<p>There are multiple versions of the <code>echo</code> command, with different behaviors. It's often built into the shell. If it isn't, <code>echo</code> will typically invoke <code>/bin/echo</code> or <code>/usr/bin/echo</code>, which itself can be from any of a number of implementations.</p> <p>Apparently the shell ...
10327
sh
<h1>"echo -n" prints "-n"</h1> <p>I have a problem with <code>echo</code> in my script:</p> <pre><code>echo -n "Some string..." </code></pre> <p>prints</p> <pre class="lang-none prettyprint-override"><code>-n Some string... </code></pre> <p>and moves to the next line. In the console it's working correcly without ne...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
253
bash
# "echo -n" prints "-n" I have a problem with `echo` in my script: ``` echo -n "Some string..." ``` prints ``` -n Some string... ``` and moves to the next line. In the console it's working correcly without newline: ``` Some string... ```
There are multiple versions of the `echo` command, with different behaviors. It's often built into the shell. If it isn't, `echo` will typically invoke `/bin/echo` or `/usr/bin/echo`, which itself can be from any of a number of implementations. Apparently the shell used for your script uses a version that doesn't reco...
15691942
Print array elements on separate lines in Bash?
371
2013-03-28 20:53:47
<p>How do I print the array element of a Bash array on separate lines? This one works, but surely there is a better way:</p> <pre><code>$ my_array=(one two three) $ for i in ${my_array[@]}; do echo $i; done one two three </code></pre> <p>Tried this one but it did not work:</p> <pre><code>$ IFS=$'\n' echo ${my_array[...
544,889
155,425
2023-08-02 05:51:29
15,692,004
679
2013-03-28 20:57:30
465,183
2023-02-20 17:32:28
https://stackoverflow.com/q/15691942
https://stackoverflow.com/a/15692004
<p>Try doing this :</p> <pre><code>$ printf '%s\n' &quot;${my_array[@]}&quot; </code></pre> <p>The difference between <code>$@</code> and <code>$*</code>:</p> <ul> <li><p>Unquoted, the results are unspecified. In Bash, both expand to separate args and then wordsplit and globbed.</p> </li> <li><p>Quoted, <code>&quot;$@&...
<p>Try doing this :</p> <pre><code>$ printf '%s\n' &quot;${my_array[@]}&quot; </code></pre> <p>The difference between <code>$@</code> and <code>$*</code>:</p> <ul> <li><p>Unquoted, the results are unspecified. In Bash, both expand to separate args and then wordsplit and globbed.</p> </li> <li><p>Quoted, <code>&quot;$@&...
114, 387
arrays, bash
<h1>Print array elements on separate lines in Bash?</h1> <p>How do I print the array element of a Bash array on separate lines? This one works, but surely there is a better way:</p> <pre><code>$ my_array=(one two three) $ for i in ${my_array[@]}; do echo $i; done one two three </code></pre> <p>Tried this one but it d...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
254
bash
# Print array elements on separate lines in Bash? How do I print the array element of a Bash array on separate lines? This one works, but surely there is a better way: ``` $ my_array=(one two three) $ for i in ${my_array[@]}; do echo $i; done one two three ``` Tried this one but it did not work: ``` $ IFS=$'\n' ech...
Try doing this : ``` $ printf '%s\n' "${my_array[@]}" ``` The difference between `$@` and `$*`: - Unquoted, the results are unspecified. In Bash, both expand to separate args and then wordsplit and globbed. - Quoted, `"$@"` expands each element as a separate argument, while `"$*"` expands to the args merged into...
10552711
How to make "if not true condition"?
489
2012-05-11 13:51:37
<p>I would like to have the <code>echo</code> command executed when <code>cat /etc/passwd | grep "sysa"</code> is not true.</p> <p>What am I doing wrong?</p> <pre><code>if ! [ $(cat /etc/passwd | grep "sysa") ]; then echo "ERROR - The user sysa could not be looked up" exit 2 fi </code></pre>
949,653
256,439
2025-08-22 04:55:33
10,552,775
675
2012-05-11 13:54:59
620,097
2025-08-22 04:55:33
https://stackoverflow.com/q/10552711
https://stackoverflow.com/a/10552775
<p>Try</p> <pre><code>if ! grep -q sysa /etc/passwd ; then </code></pre> <p><code>grep</code> returns <code>true</code> if it finds the search target, and <code>false</code> if it doesn't.</p> <p>So NOT false (<code>! false</code>) == <code>true</code>.</p> <p><code>if</code> evaluation in shells are designed to be ver...
<p>Try</p> <pre><code>if ! grep -q sysa /etc/passwd ; then </code></pre> <p><code>grep</code> returns <code>true</code> if it finds the search target, and <code>false</code> if it doesn't.</p> <p>So NOT false (<code>! false</code>) == <code>true</code>.</p> <p><code>if</code> evaluation in shells are designed to be ver...
367, 387, 2773, 15128
bash, boolean-expression, if-statement, syntax
<h1>How to make "if not true condition"?</h1> <p>I would like to have the <code>echo</code> command executed when <code>cat /etc/passwd | grep "sysa"</code> is not true.</p> <p>What am I doing wrong?</p> <pre><code>if ! [ $(cat /etc/passwd | grep "sysa") ]; then echo "ERROR - The user sysa could not be looked...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
255
bash
# How to make "if not true condition"? I would like to have the `echo` command executed when `cat /etc/passwd | grep "sysa"` is not true. What am I doing wrong? ``` if ! [ $(cat /etc/passwd | grep "sysa") ]; then echo "ERROR - The user sysa could not be looked up" exit 2 fi ```
Try ``` if ! grep -q sysa /etc/passwd ; then ``` `grep` returns `true` if it finds the search target, and `false` if it doesn't. So NOT false (`! false`) == `true`. `if` evaluation in shells are designed to be very flexible, and many times doesn't require chains of commands (as you have written). Also, looking at ...
11245144
Replace whole line containing a string using Sed
470
2012-06-28 12:55:26
<p>I have a text file which has a particular line something like</p> <pre><code>sometext sometext sometext TEXT_TO_BE_REPLACED sometext sometext sometext </code></pre> <p>I need to replace the whole line above with </p> <pre><code>This line is removed by the admin. </code></pre> <p>The search keyword is <code>TEXT_...
804,847
1,254,009
2025-06-04 13:22:54
11,245,501
674
2012-06-28 13:15:02
1,301,972
2018-11-30 00:25:02
https://stackoverflow.com/q/11245144
https://stackoverflow.com/a/11245501
<p>You can use the <em>change</em> command to replace the entire line, and the <code>-i</code> flag to make the changes in-place. For example, using GNU sed:</p> <pre><code>sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo </code></pre>
<p>You can use the <em>change</em> command to replace the entire line, and the <code>-i</code> flag to make the changes in-place. For example, using GNU sed:</p> <pre><code>sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo </code></pre>
139, 390, 5282
sed, shell, string
<h1>Replace whole line containing a string using Sed</h1> <p>I have a text file which has a particular line something like</p> <pre><code>sometext sometext sometext TEXT_TO_BE_REPLACED sometext sometext sometext </code></pre> <p>I need to replace the whole line above with </p> <pre><code>This line is removed by the ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
256
bash
# Replace whole line containing a string using Sed I have a text file which has a particular line something like ``` sometext sometext sometext TEXT_TO_BE_REPLACED sometext sometext sometext ``` I need to replace the whole line above with ``` This line is removed by the admin. ``` The search keyword is `TEXT_TO_BE...
You can use the *change* command to replace the entire line, and the `-i` flag to make the changes in-place. For example, using GNU sed: ``` sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo ```
7875540
How to write multiple line string using Bash with variables?
348
2011-10-24 12:23:51
<p>How can I write multi-lines in a file called <code>myconfig.conf</code> using BASH?</p> <pre><code>#!/bin/bash kernel="2.6.39"; distro="xyz"; echo &lt;&lt;&lt; EOL line 1, ${kernel} line 2, line 3, ${distro} line 4 line ... EOL &gt;&gt; /etc/myconfig.conf; cat /etc/myconfig.conf; </code></pre>
371,266
null
2023-07-10 14:08:00
7,875,614
674
2011-10-24 12:29:40
944,391
2019-09-08 11:13:47
https://stackoverflow.com/q/7875540
https://stackoverflow.com/a/7875614
<p>The syntax (<code>&lt;&lt;&lt;</code>) and the command used (<code>echo</code>) is wrong.</p> <p>Correct would be:</p> <pre><code>#!/bin/bash kernel="2.6.39" distro="xyz" cat &gt;/etc/myconfig.conf &lt;&lt;EOL line 1, ${kernel} line 2, line 3, ${distro} line 4 line ... EOL cat /etc/myconfig.conf </code></pre> ...
<p>The syntax (<code>&lt;&lt;&lt;</code>) and the command used (<code>echo</code>) is wrong.</p> <p>Correct would be:</p> <pre><code>#!/bin/bash kernel="2.6.39" distro="xyz" cat &gt;/etc/myconfig.conf &lt;&lt;EOL line 1, ${kernel} line 2, line 3, ${distro} line 4 line ... EOL cat /etc/myconfig.conf </code></pre> ...
58, 387, 29051
bash, bash4, linux
<h1>How to write multiple line string using Bash with variables?</h1> <p>How can I write multi-lines in a file called <code>myconfig.conf</code> using BASH?</p> <pre><code>#!/bin/bash kernel="2.6.39"; distro="xyz"; echo &lt;&lt;&lt; EOL line 1, ${kernel} line 2, line 3, ${distro} line 4 line ... EOL &gt;&gt; /etc/myc...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
257
bash
# How to write multiple line string using Bash with variables? How can I write multi-lines in a file called `myconfig.conf` using BASH? ``` #!/bin/bash kernel="2.6.39"; distro="xyz"; echo <<< EOL line 1, ${kernel} line 2, line 3, ${distro} line 4 line ... EOL >> /etc/myconfig.conf; cat /etc/myconfig.conf; ```
The syntax (`<<<`) and the command used (`echo`) is wrong. Correct would be: ``` #!/bin/bash kernel="2.6.39" distro="xyz" cat >/etc/myconfig.conf <<EOL line 1, ${kernel} line 2, line 3, ${distro} line 4 line ... EOL cat /etc/myconfig.conf ``` This construction is referred to as a [Here Document](https://en.wikip...
4426442
Unix tail equivalent command in Windows Powershell
507
2010-12-13 06:55:28
<p>I have to look at the last few lines of a large file (typical size is 500MB-2GB). I am looking for an equivalent of the Unix command <code>tail</code> for Windows Powershell. A few alternatives available are,</p> <p><a href="http://tailforwin32.sourceforge.net/" rel="noreferrer">http://tailforwin32.sourceforge.net/<...
545,213
520,382
2024-08-23 01:35:47
4,427,285
673
2010-12-13 09:15:34
236,131
2017-01-05 15:05:55
https://stackoverflow.com/q/4426442
https://stackoverflow.com/a/4427285
<p>Use the <code>-wait</code> parameter with Get-Content, which displays lines as they are added to the file. This feature was present in PowerShell v1, but for some reason not documented well in v2.</p> <p>Here is an example</p> <pre><code>Get-Content -Path "C:\scripts\test.txt" -Wait </code></pre> <p>Once you run ...
<p>Use the <code>-wait</code> parameter with Get-Content, which displays lines as they are added to the file. This feature was present in PowerShell v1, but for some reason not documented well in v2.</p> <p>Here is an example</p> <pre><code>Get-Content -Path "C:\scripts\test.txt" -Wait </code></pre> <p>Once you run ...
34, 64, 526, 7896
powershell, tail, unix, windows
<h1>Unix tail equivalent command in Windows Powershell</h1> <p>I have to look at the last few lines of a large file (typical size is 500MB-2GB). I am looking for an equivalent of the Unix command <code>tail</code> for Windows Powershell. A few alternatives available are,</p> <p><a href="http://tailforwin32.sourceforge....
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
258
bash
# Unix tail equivalent command in Windows Powershell I have to look at the last few lines of a large file (typical size is 500MB-2GB). I am looking for an equivalent of the Unix command `tail` for Windows Powershell. A few alternatives available are, <http://tailforwin32.sourceforge.net/> and ``` Get-Content [filen...
Use the `-wait` parameter with Get-Content, which displays lines as they are added to the file. This feature was present in PowerShell v1, but for some reason not documented well in v2. Here is an example ``` Get-Content -Path "C:\scripts\test.txt" -Wait ``` Once you run this, update and save the file and you will s...
1529946
Linux: copy and create destination dir if it does not exist
661
2009-10-07 06:55:27
<p>I want a command (or probably an option to cp) that creates the destination directory if it does not exist.</p> <p>Example:</p> <pre><code>cp -? file /path/to/copy/file/to/is/very/deep/there </code></pre>
693,773
63,051
2025-01-23 13:34:12
1,529,965
666
2009-10-07 06:58:54
95,382
2018-09-10 09:33:58
https://stackoverflow.com/q/1529946
https://stackoverflow.com/a/1529965
<pre><code>mkdir -p "$d" &amp;&amp; cp file "$d" </code></pre> <p>(there's no such option for <code>cp</code>).</p>
<pre><code>mkdir -p "$d" &amp;&amp; cp file "$d" </code></pre> <p>(there's no such option for <code>cp</code>).</p>
34, 58, 387, 390, 25299
bash, cp, linux, shell, unix
<h1>Linux: copy and create destination dir if it does not exist</h1> <p>I want a command (or probably an option to cp) that creates the destination directory if it does not exist.</p> <p>Example:</p> <pre><code>cp -? file /path/to/copy/file/to/is/very/deep/there </code></pre>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
259
bash
# Linux: copy and create destination dir if it does not exist I want a command (or probably an option to cp) that creates the destination directory if it does not exist. Example: ``` cp -? file /path/to/copy/file/to/is/very/deep/there ```
``` mkdir -p "$d" && cp file "$d" ``` (there's no such option for `cp`).
16085958
Find the files that have been changed in last 24 hours
372
2013-04-18 14:38:46
<p>E.g., a MySQL server is running on my Ubuntu machine. Some data has been changed during the last 24 hours.</p> <p>What (Linux) scripts can find the files that have been changed during the last 24 hours?</p> <p>Please list the file names, file sizes, and modified time.</p>
581,231
1,258,409
2024-06-20 22:56:16
16,086,041
665
2013-04-18 14:42:08
970,616
2016-12-08 18:27:03
https://stackoverflow.com/q/16085958
https://stackoverflow.com/a/16086041
<p>To find all files modified in the last 24 hours (last full day) in a particular specific directory and its sub-directories:</p> <pre><code>find /directory_path -mtime -1 -ls </code></pre> <p>Should be to your liking</p> <p>The <code>-</code> before <code>1</code> is important - it means anything changed one day o...
<p>To find all files modified in the last 24 hours (last full day) in a particular specific directory and its sub-directories:</p> <pre><code>find /directory_path -mtime -1 -ls </code></pre> <p>Should be to your liking</p> <p>The <code>-</code> before <code>1</code> is important - it means anything changed one day o...
58, 387, 10193
bash, find, linux
<h1>Find the files that have been changed in last 24 hours</h1> <p>E.g., a MySQL server is running on my Ubuntu machine. Some data has been changed during the last 24 hours.</p> <p>What (Linux) scripts can find the files that have been changed during the last 24 hours?</p> <p>Please list the file names, file sizes, a...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
260
bash
# Find the files that have been changed in last 24 hours E.g., a MySQL server is running on my Ubuntu machine. Some data has been changed during the last 24 hours. What (Linux) scripts can find the files that have been changed during the last 24 hours? Please list the file names, file sizes, and modified time.
To find all files modified in the last 24 hours (last full day) in a particular specific directory and its sub-directories: ``` find /directory_path -mtime -1 -ls ``` Should be to your liking The `-` before `1` is important - it means anything changed one day or less ago. A `+` before `1` would instead mean anything...
37458287
How to run a cron job inside a docker container?
570
2016-05-26 10:32:53
<p>I am trying to run a cronjob inside a docker container that invokes a shell script.</p> <p>How can I do this?</p>
844,130
6,268,839
2026-01-02 18:30:59
37,458,519
660
2016-05-26 10:42:39
6,309
2026-01-02 18:30:59
https://stackoverflow.com/q/37458287
https://stackoverflow.com/a/37458519
<p>You can copy your <code>crontab</code> into an image, in order for the container launched from said image to run the job.</p> <hr /> <p><strong>Important</strong>: as noted in <a href="https://github.com/Ekito/docker-cron/issues/3" rel="nofollow noreferrer">docker-cron issue 3</a>: use <a href="https://stackoverflow...
<p>You can copy your <code>crontab</code> into an image, in order for the container launched from said image to run the job.</p> <hr /> <p><strong>Important</strong>: as noted in <a href="https://github.com/Ekito/docker-cron/issues/3" rel="nofollow noreferrer">docker-cron issue 3</a>: use <a href="https://stackoverflow...
601, 1364, 10327, 90304
containers, cron, docker, sh
<h1>How to run a cron job inside a docker container?</h1> <p>I am trying to run a cronjob inside a docker container that invokes a shell script.</p> <p>How can I do this?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
261
bash
# How to run a cron job inside a docker container? I am trying to run a cronjob inside a docker container that invokes a shell script. How can I do this?
You can copy your `crontab` into an image, in order for the container launched from said image to run the job. --- **Important**: as noted in [docker-cron issue 3](https://github.com/Ekito/docker-cron/issues/3): use [LF, not CRLF](https://stackoverflow.com/q/1552749/6309) for your `cron` file. --- See "[Run a cron ...
3886295
How do I list one filename per output line in Linux?
362
2010-10-07 22:21:11
<p>I'm using <code>ls -a</code> command to get the file names in a directory, but the output is in a single line. </p> <p>Like this:</p> <pre><code>. .. .bash_history .ssh updater_error_log.txt </code></pre> <p>I need a built-in alternative to get filenames, each on a new line, like this:</p> <pre><code>. .. ...
258,336
170,005
2022-07-01 18:21:03
3,886,328
660
2010-10-07 22:25:25
11,296
2018-12-29 02:57:02
https://stackoverflow.com/q/3886295
https://stackoverflow.com/a/3886328
<p>Use the <code>-1</code> option (note this is a "one" digit, not a lowercase letter "L"), like this: </p> <pre class="lang-none prettyprint-override"><code>ls -1a </code></pre> <hr> <p>First, though, make sure your <code>ls</code> supports <code>-1</code>. GNU coreutils (installed on standard Linux systems) and ...
<p>Use the <code>-1</code> option (note this is a "one" digit, not a lowercase letter "L"), like this: </p> <pre class="lang-none prettyprint-override"><code>ls -1a </code></pre> <hr> <p>First, though, make sure your <code>ls</code> supports <code>-1</code>. GNU coreutils (installed on standard Linux systems) and ...
58, 390, 3589
linux, ls, shell
<h1>How do I list one filename per output line in Linux?</h1> <p>I'm using <code>ls -a</code> command to get the file names in a directory, but the output is in a single line. </p> <p>Like this:</p> <pre><code>. .. .bash_history .ssh updater_error_log.txt </code></pre> <p>I need a built-in alternative to get fil...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
262
bash
# How do I list one filename per output line in Linux? I'm using `ls -a` command to get the file names in a directory, but the output is in a single line. Like this: ``` . .. .bash_history .ssh updater_error_log.txt ``` I need a built-in alternative to get filenames, each on a new line, like this: ``` . .. ...
Use the `-1` option (note this is a "one" digit, not a lowercase letter "L"), like this: ``` ls -1a ``` --- First, though, make sure your `ls` supports `-1`. GNU coreutils (installed on standard Linux systems) and Solaris do; but if in doubt, use `man ls` or `ls --help` or check the documentation. E.g.: ``` $ man l...
26598738
How to create User/Database in script for Docker Postgres
399
2014-10-27 23:47:58
<p>I have been trying to set up a container for a development postgres instance by creating a custom user &amp; database. I am using the <a href="https://registry.hub.docker.com/_/postgres/" rel="noreferrer">official postgres docker image</a>. In the documentation it instructs you to insert a bash script inside of the ...
601,418
2,308,178
2023-11-15 16:54:12
26,599,273
654
2014-10-28 00:58:38
107,049
2018-06-26 14:22:30
https://stackoverflow.com/q/26598738
https://stackoverflow.com/a/26599273
<h2>EDIT - since Jul 23, 2015</h2> <p>The <a href="https://hub.docker.com/_/postgres/" rel="noreferrer">official postgres docker image</a> will run <code>.sql</code> scripts found in the <code>/docker-entrypoint-initdb.d/</code> folder. </p> <p>So all you need is to create the following sql script: </p> <p><em>init....
<h2>EDIT - since Jul 23, 2015</h2> <p>The <a href="https://hub.docker.com/_/postgres/" rel="noreferrer">official postgres docker image</a> will run <code>.sql</code> scripts found in the <code>/docker-entrypoint-initdb.d/</code> folder. </p> <p>So all you need is to create the following sql script: </p> <p><em>init....
256, 387, 90304, 106339
bash, docker, dockerhub, postgresql
<h1>How to create User/Database in script for Docker Postgres</h1> <p>I have been trying to set up a container for a development postgres instance by creating a custom user &amp; database. I am using the <a href="https://registry.hub.docker.com/_/postgres/" rel="noreferrer">official postgres docker image</a>. In the do...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
263
bash
# How to create User/Database in script for Docker Postgres I have been trying to set up a container for a development postgres instance by creating a custom user & database. I am using the [official postgres docker image](https://registry.hub.docker.com/_/postgres/). In the documentation it instructs you to insert a ...
## EDIT - since Jul 23, 2015 The [official postgres docker image](https://hub.docker.com/_/postgres/) will run `.sql` scripts found in the `/docker-entrypoint-initdb.d/` folder. So all you need is to create the following sql script: *init.sql* ``` CREATE USER docker; CREATE DATABASE docker; GRANT ALL PRIVILEGES ON ...
1133698
Using find to locate files that match one of multiple patterns
459
2009-07-15 20:12:21
<p>I was trying to get a list of all python and html files in a directory with the command <code>find Documents -name "*.{py,html}"</code>.</p> <p>Then along came the man page:</p> <blockquote> <p>Braces within the pattern (‘{}’) are not considered to be special (that is, find . -name 'foo{1,2}' matches a file name...
535,661
120,999
2020-12-30 11:42:34
1,133,720
652
2009-07-15 20:16:00
21,886
2020-04-23 15:52:07
https://stackoverflow.com/q/1133698
https://stackoverflow.com/a/1133720
<p>Use <code>-o</code>, which means "or":</p> <pre><code>find Documents \( -name "*.py" -o -name "*.html" \) </code></pre> <hr> <p>You'd need to build that command line programmatically, which isn't that easy.</p> <p>Are you using bash (or Cygwin on Windows)? If you are, you should be able to do this:</p> <pre><c...
<p>Use <code>-o</code>, which means "or":</p> <pre><code>find Documents \( -name "*.py" -o -name "*.html" \) </code></pre> <hr> <p>You'd need to build that command line programmatically, which isn't that easy.</p> <p>Are you using bash (or Cygwin on Windows)? If you are, you should be able to do this:</p> <pre><c...
390, 10193
find, shell
<h1>Using find to locate files that match one of multiple patterns</h1> <p>I was trying to get a list of all python and html files in a directory with the command <code>find Documents -name "*.{py,html}"</code>.</p> <p>Then along came the man page:</p> <blockquote> <p>Braces within the pattern (‘{}’) are not consid...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
264
bash
# Using find to locate files that match one of multiple patterns I was trying to get a list of all python and html files in a directory with the command `find Documents -name "*.{py,html}"`. Then along came the man page: > Braces within the pattern (‘{}’) are not considered to be special (that is, find . -name 'foo{...
Use `-o`, which means "or": ``` find Documents \( -name "*.py" -o -name "*.html" \) ``` --- You'd need to build that command line programmatically, which isn't that easy. Are you using bash (or Cygwin on Windows)? If you are, you should be able to do this: ``` ls **/*.py **/*.html ``` which might be easier to bui...
4676459
Write to file, but overwrite it if it exists
421
2011-01-13 03:15:37
<pre><code>echo "text" &gt;&gt; 'Users/Name/Desktop/TheAccount.txt' </code></pre> <p>How do I make it so it creates the file if it doesn't exist, but overwrites it if it already exists. Right now this script just appends.</p>
535,540
240,569
2025-04-23 18:46:20
4,676,473
652
2011-01-13 03:18:15
247,824
2020-07-02 17:34:56
https://stackoverflow.com/q/4676459
https://stackoverflow.com/a/4676473
<p>The <code>&gt;&gt;</code> redirection operator will append lines to the end of the specified file, where-as the single greater than <code>&gt;</code> will empty and overwrite the file.</p> <pre><code>echo &quot;text&quot; &gt; 'Users/Name/Desktop/TheAccount.txt' </code></pre>
<p>The <code>&gt;&gt;</code> redirection operator will append lines to the end of the specified file, where-as the single greater than <code>&gt;</code> will empty and overwrite the file.</p> <pre><code>echo &quot;text&quot; &gt; 'Users/Name/Desktop/TheAccount.txt' </code></pre>
34, 387
bash, unix
<h1>Write to file, but overwrite it if it exists</h1> <pre><code>echo "text" &gt;&gt; 'Users/Name/Desktop/TheAccount.txt' </code></pre> <p>How do I make it so it creates the file if it doesn't exist, but overwrites it if it already exists. Right now this script just appends.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
265
bash
# Write to file, but overwrite it if it exists ``` echo "text" >> 'Users/Name/Desktop/TheAccount.txt' ``` How do I make it so it creates the file if it doesn't exist, but overwrites it if it already exists. Right now this script just appends.
The `>>` redirection operator will append lines to the end of the specified file, where-as the single greater than `>` will empty and overwrite the file. ``` echo "text" > 'Users/Name/Desktop/TheAccount.txt' ```
6114119
How do I read the first line of a file using cat?
360
2011-05-24 17:04:42
<p>How do I read the first line of a file using <code>cat</code>?</p>
415,757
601,095
2020-11-03 23:35:45
6,114,132
647
2011-05-24 17:06:12
116,908
2019-09-27 15:34:04
https://stackoverflow.com/q/6114119
https://stackoverflow.com/a/6114132
<p>You don't need <code>cat</code>. </p> <pre><code>head -1 file </code></pre> <p>will work fine.</p>
<p>You don't need <code>cat</code>. </p> <pre><code>head -1 file </code></pre> <p>will work fine.</p>
387, 724, 22820
bash, cat, file-io
<h1>How do I read the first line of a file using cat?</h1> <p>How do I read the first line of a file using <code>cat</code>?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
266
bash
# How do I read the first line of a file using cat? How do I read the first line of a file using `cat`?
You don't need `cat`. ``` head -1 file ``` will work fine.
10523415
Execute command on all files in a directory
467
2012-05-09 20:12:42
<p>Could somebody please provide the code to do the following: Assume there is a directory of files, all of which need to be run through a program. The program outputs the results to standard out. I need a script that will go into a directory, execute the command on each file, and concat the output into one big output ...
619,964
370,639
2024-08-30 16:17:01
10,523,501
646
2012-05-09 20:18:24
966,590
2015-11-24 13:28:34
https://stackoverflow.com/q/10523415
https://stackoverflow.com/a/10523501
<p>The following bash code will pass $file to command where $file will represent every file in /dir</p> <pre><code>for file in /dir/* do cmd [option] "$file" &gt;&gt; results.out done </code></pre> <p>Example</p> <pre><code>el@defiant ~/foo $ touch foo.txt bar.txt baz.txt el@defiant ~/foo $ for i in *.txt; do echo...
<p>The following bash code will pass $file to command where $file will represent every file in /dir</p> <pre><code>for file in /dir/* do cmd [option] "$file" &gt;&gt; results.out done </code></pre> <p>Example</p> <pre><code>el@defiant ~/foo $ touch foo.txt bar.txt baz.txt el@defiant ~/foo $ for i in *.txt; do echo...
387, 531
bash, scripting
<h1>Execute command on all files in a directory</h1> <p>Could somebody please provide the code to do the following: Assume there is a directory of files, all of which need to be run through a program. The program outputs the results to standard out. I need a script that will go into a directory, execute the command on ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
267
bash
# Execute command on all files in a directory Could somebody please provide the code to do the following: Assume there is a directory of files, all of which need to be run through a program. The program outputs the results to standard out. I need a script that will go into a directory, execute the command on each file...
The following bash code will pass $file to command where $file will represent every file in /dir ``` for file in /dir/* do cmd [option] "$file" >> results.out done ``` Example ``` el@defiant ~/foo $ touch foo.txt bar.txt baz.txt el@defiant ~/foo $ for i in *.txt; do echo "hello $i"; done hello bar.txt hello baz.tx...
1221833
Pipe output and capture exit status in Bash
548
2009-08-03 11:31:52
<p>I want to execute a long running command in Bash, and both capture its exit status, and <a href="http://en.wikipedia.org/wiki/Tee_(command)" rel="noreferrer">tee</a> its output.</p> <p>So I do this:</p> <pre><code>command | tee out.txt ST=$? </code></pre> <p>The problem is that the variable ST captures the exit s...
224,275
63,051
2024-04-19 23:41:30
1,221,870
644
2009-08-03 11:40:20
429,467
2016-12-21 17:17:14
https://stackoverflow.com/q/1221833
https://stackoverflow.com/a/1221870
<p>There is an internal Bash variable called <code>$PIPESTATUS</code>; it’s an array that holds the exit status of each command in your last foreground pipeline of commands.</p> <pre><code>&lt;command&gt; | tee out.txt ; test ${PIPESTATUS[0]} -eq 0 </code></pre> <p>Or another alternative which also works with other s...
<p>There is an internal Bash variable called <code>$PIPESTATUS</code>; it’s an array that holds the exit status of each command in your last foreground pipeline of commands.</p> <pre><code>&lt;command&gt; | tee out.txt ; test ${PIPESTATUS[0]} -eq 0 </code></pre> <p>Or another alternative which also works with other s...
379, 387, 390, 5813
bash, error-handling, pipe, shell
<h1>Pipe output and capture exit status in Bash</h1> <p>I want to execute a long running command in Bash, and both capture its exit status, and <a href="http://en.wikipedia.org/wiki/Tee_(command)" rel="noreferrer">tee</a> its output.</p> <p>So I do this:</p> <pre><code>command | tee out.txt ST=$? </code></pre> <p>Th...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
268
bash
# Pipe output and capture exit status in Bash I want to execute a long running command in Bash, and both capture its exit status, and [tee](http://en.wikipedia.org/wiki/Tee_(command)) its output. So I do this: ``` command | tee out.txt ST=$? ``` The problem is that the variable ST captures the exit status of `tee` ...
There is an internal Bash variable called `$PIPESTATUS`; it’s an array that holds the exit status of each command in your last foreground pipeline of commands. ``` <command> | tee out.txt ; test ${PIPESTATUS[0]} -eq 0 ``` Or another alternative which also works with other shells (like zsh) would be to enable pipefail...
6363441
Check if a file exists with a wildcard in a shell script
419
2011-06-15 19:50:26
<p>I'm trying to check if a file exists, but with a wildcard. Here is my example:</p> <pre><code>if [ -f &quot;xorg-x11-fonts*&quot; ]; then printf &quot;BLAH&quot; fi </code></pre> <p>I have also tried it without the double quotes.</p>
506,748
695,727
2021-12-06 15:59:40
6,364,244
643
2011-06-15 20:56:02
262,683
2021-10-07 10:26:34
https://stackoverflow.com/q/6363441
https://stackoverflow.com/a/6364244
<p>For Bash scripts, the most direct and performant approach is:</p> <pre class="lang-sh prettyprint-override"><code>if compgen -G &quot;${PROJECT_DIR}/*.png&quot; &gt; /dev/null; then echo &quot;pattern exists!&quot; fi </code></pre> <p>This will work very speedily even in directories with millions of files and do...
<p>For Bash scripts, the most direct and performant approach is:</p> <pre class="lang-sh prettyprint-override"><code>if compgen -G &quot;${PROJECT_DIR}/*.png&quot; &gt; /dev/null; then echo &quot;pattern exists!&quot; fi </code></pre> <p>This will work very speedily even in directories with millions of files and do...
390, 5997, 10327
sh, shell, wildcard
<h1>Check if a file exists with a wildcard in a shell script</h1> <p>I'm trying to check if a file exists, but with a wildcard. Here is my example:</p> <pre><code>if [ -f &quot;xorg-x11-fonts*&quot; ]; then printf &quot;BLAH&quot; fi </code></pre> <p>I have also tried it without the double quotes.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
269
bash
# Check if a file exists with a wildcard in a shell script I'm trying to check if a file exists, but with a wildcard. Here is my example: ``` if [ -f "xorg-x11-fonts*" ]; then printf "BLAH" fi ``` I have also tried it without the double quotes.
For Bash scripts, the most direct and performant approach is: ``` if compgen -G "${PROJECT_DIR}/*.png" > /dev/null; then echo "pattern exists!" fi ``` This will work very speedily even in directories with millions of files and does not involve a new subshell. [Source](https://stackoverflow.com/a/34195247/430062)...
18681595
Tar a directory, but don't store full absolute paths in the archive
512
2013-09-08 07:43:34
<p>I have the following command in the part of a backup shell script:</p> <pre><code>tar -cjf site1.bz2 /var/www/site1/ </code></pre> <p>When I list the contents of the archive, I get:</p> <pre><code>tar -tf site1.bz2 var/www/site1/style.css var/www/site1/index.html var/www/site1/page2.html var/www/site1/page3.html ...
388,516
1,971,216
2024-08-09 10:58:20
18,681,628
638
2013-09-08 07:49:53
1,427,529
2015-06-11 09:50:34
https://stackoverflow.com/q/18681595
https://stackoverflow.com/a/18681628
<pre><code>tar -cjf site1.tar.bz2 -C /var/www/site1 . </code></pre> <p>In the above example, tar will change to directory <code>/var/www/site1</code> before doing its thing because the option <code>-C /var/www/site1</code> was given.</p> <p>From <code>man tar</code>:</p> <pre><code>OTHER OPTIONS -C, --directory D...
<pre><code>tar -cjf site1.tar.bz2 -C /var/www/site1 . </code></pre> <p>In the above example, tar will change to directory <code>/var/www/site1</code> before doing its thing because the option <code>-C /var/www/site1</code> was given.</p> <p>From <code>man tar</code>:</p> <pre><code>OTHER OPTIONS -C, --directory D...
58, 387, 493, 8773
backup, bash, linux, tar
<h1>Tar a directory, but don't store full absolute paths in the archive</h1> <p>I have the following command in the part of a backup shell script:</p> <pre><code>tar -cjf site1.bz2 /var/www/site1/ </code></pre> <p>When I list the contents of the archive, I get:</p> <pre><code>tar -tf site1.bz2 var/www/site1/style.cs...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
270
bash
# Tar a directory, but don't store full absolute paths in the archive I have the following command in the part of a backup shell script: ``` tar -cjf site1.bz2 /var/www/site1/ ``` When I list the contents of the archive, I get: ``` tar -tf site1.bz2 var/www/site1/style.css var/www/site1/index.html var/www/site1/pag...
``` tar -cjf site1.tar.bz2 -C /var/www/site1 . ``` In the above example, tar will change to directory `/var/www/site1` before doing its thing because the option `-C /var/www/site1` was given. From `man tar`: ``` OTHER OPTIONS -C, --directory DIR change to directory DIR ```
2019857
Diff files present in two different directories
455
2010-01-07 11:25:54
<p>I have two directories with the same list of files. I need to compare all the files present in both the directories using the <code>diff</code> command. Is there a simple command line option to do it, or do I have to write a shell script to get the file listing and then iterate through them?</p>
449,636
24,949
2024-11-08 10:30:23
2,019,897
634
2010-01-07 11:31:33
121,278
2017-01-19 16:14:27
https://stackoverflow.com/q/2019857
https://stackoverflow.com/a/2019897
<p>You can use the <code>diff</code> command for that:</p> <pre><code>diff -bur folder1/ folder2/ </code></pre> <p>This will output a recursive diff that ignore spaces, with a unified context:</p> <ul> <li><strong>b</strong> flag means ignoring whitespace</li> <li><strong>u</strong> flag means a unified context (3 l...
<p>You can use the <code>diff</code> command for that:</p> <pre><code>diff -bur folder1/ folder2/ </code></pre> <p>This will output a recursive diff that ignore spaces, with a unified context:</p> <ul> <li><strong>b</strong> flag means ignoring whitespace</li> <li><strong>u</strong> flag means a unified context (3 l...
34, 390, 606
diff, shell, unix
<h1>Diff files present in two different directories</h1> <p>I have two directories with the same list of files. I need to compare all the files present in both the directories using the <code>diff</code> command. Is there a simple command line option to do it, or do I have to write a shell script to get the file listin...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
271
bash
# Diff files present in two different directories I have two directories with the same list of files. I need to compare all the files present in both the directories using the `diff` command. Is there a simple command line option to do it, or do I have to write a shell script to get the file listing and then iterate t...
You can use the `diff` command for that: ``` diff -bur folder1/ folder2/ ``` This will output a recursive diff that ignore spaces, with a unified context: - **b** flag means ignoring whitespace - **u** flag means a unified context (3 lines before and after) - **r** flag means recursive
4509624
How to limit depth for recursive file list?
453
2010-12-22 13:28:02
<p>Is there a way to limit the depth of a recursive file listing in linux?</p> <p>The command I'm using at the moment is:</p> <pre><code>ls -laR &gt; dirlist.txt </code></pre> <p>But I've got about 200 directories and each of them have 10's of directories. So it's just going to take far too long and hog too many sys...
458,808
177,132
2019-10-28 06:19:12
4,509,648
632
2010-12-22 13:31:29
57,095
2010-12-22 13:39:15
https://stackoverflow.com/q/4509624
https://stackoverflow.com/a/4509648
<p>Checkout the <code>-maxdepth</code> flag of <code>find</code></p> <pre><code>find . -maxdepth 1 -type d -exec ls -ld "{}" \; </code></pre> <p>Here I used 1 as max level depth, <code>-type d</code> means find only directories, which then <code>ls -ld</code> lists contents of, in long format.</p>
<p>Checkout the <code>-maxdepth</code> flag of <code>find</code></p> <pre><code>find . -maxdepth 1 -type d -exec ls -ld "{}" \; </code></pre> <p>Here I used 1 as max level depth, <code>-type d</code> means find only directories, which then <code>ls -ld</code> lists contents of, in long format.</p>
58, 387
bash, linux
<h1>How to limit depth for recursive file list?</h1> <p>Is there a way to limit the depth of a recursive file listing in linux?</p> <p>The command I'm using at the moment is:</p> <pre><code>ls -laR &gt; dirlist.txt </code></pre> <p>But I've got about 200 directories and each of them have 10's of directories. So it's...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
272
bash
# How to limit depth for recursive file list? Is there a way to limit the depth of a recursive file listing in linux? The command I'm using at the moment is: ``` ls -laR > dirlist.txt ``` But I've got about 200 directories and each of them have 10's of directories. So it's just going to take far too long and hog to...
Checkout the `-maxdepth` flag of `find` ``` find . -maxdepth 1 -type d -exec ls -ld "{}" \; ``` Here I used 1 as max level depth, `-type d` means find only directories, which then `ls -ld` lists contents of, in long format.
18428374
in mac always getting zsh: command not found:
370
2013-08-25 11:12:35
<p>I am using the z Shell (<code>zsh</code>) instead of the default bash, and something wrong happen so that all commands that used to work are no longer recognized:</p> <pre class="lang-bash prettyprint-override"><code> ls zsh: command not found: ls open -e .zshrc zsh: correct 'open' to '_open' [nyae]?...
1,218,738
602,257
2023-04-20 16:47:28
18,428,774
628
2013-08-25 12:04:49
2,235,132
2023-04-20 16:47:28
https://stackoverflow.com/q/18428374
https://stackoverflow.com/a/18428774
<p>It's evident that you've managed to mess up your <code>PATH</code> variable. (Your current <code>PATH</code> doesn't contain any location where common utilities are located.)</p> <p>Try:</p> <pre><code>PATH=/bin:/usr/bin:/usr/local/bin:/sbin:${PATH} export PATH </code></pre> <p>Alternatively, for &quot;resetting&qu...
<p>It's evident that you've managed to mess up your <code>PATH</code> variable. (Your current <code>PATH</code> doesn't contain any location where common utilities are located.)</p> <p>Try:</p> <pre><code>PATH=/bin:/usr/bin:/usr/local/bin:/sbin:${PATH} export PATH </code></pre> <p>Alternatively, for &quot;resetting&qu...
390, 3791, 71219
osx-lion, shell, zsh
<h1>in mac always getting zsh: command not found:</h1> <p>I am using the z Shell (<code>zsh</code>) instead of the default bash, and something wrong happen so that all commands that used to work are no longer recognized:</p> <pre class="lang-bash prettyprint-override"><code> ls zsh: command not found: ls op...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
273
bash
# in mac always getting zsh: command not found: I am using the z Shell (`zsh`) instead of the default bash, and something wrong happen so that all commands that used to work are no longer recognized: ``` ls zsh: command not found: ls open -e .zshrc zsh: correct 'open' to '_open' [nyae]? ``` I don't ...
It's evident that you've managed to mess up your `PATH` variable. (Your current `PATH` doesn't contain any location where common utilities are located.) Try: ``` PATH=/bin:/usr/bin:/usr/local/bin:/sbin:${PATH} export PATH ``` Alternatively, for "resetting" zsh, specify the complete path to the shell: ``` exec /bin/...
4247068
sed command with -i option failing on Mac, but works on Linux
477
2010-11-22 15:33:49
<p>I've successfully used the following <code>sed</code> command to search/replace text in Linux:</p> <pre><code>sed -i 's/old_link/new_link/g' * </code></pre> <p>However, when I try it on my Mac OS X, I get:</p> <blockquote> <p>"command c expects \ followed by text"</p> </blockquote> <p>I thought my Mac runs a n...
396,489
165,673
2025-01-09 00:33:41
4,247,319
626
2010-11-22 15:53:28
515,275
2025-01-09 00:33:41
https://stackoverflow.com/q/4247068
https://stackoverflow.com/a/4247319
<blockquote> <p><strong>Portable solution</strong> below</p> </blockquote> <h3>Why you get the error</h3> <p>The <code>-i</code> option (alternatively, <code>--in-place</code>) means that you want files edited in-place, rather than streaming the change to a new place.</p> <p>Modifying a file in-place suggests a need fo...
<blockquote> <p><strong>Portable solution</strong> below</p> </blockquote> <h3>Why you get the error</h3> <p>The <code>-i</code> option (alternatively, <code>--in-place</code>) means that you want files edited in-place, rather than streaming the change to a new place.</p> <p>Modifying a file in-place suggests a need fo...
58, 369, 387, 5282
bash, linux, macos, sed
<h1>sed command with -i option failing on Mac, but works on Linux</h1> <p>I've successfully used the following <code>sed</code> command to search/replace text in Linux:</p> <pre><code>sed -i 's/old_link/new_link/g' * </code></pre> <p>However, when I try it on my Mac OS X, I get:</p> <blockquote> <p>"command c expe...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
274
bash
# sed command with -i option failing on Mac, but works on Linux I've successfully used the following `sed` command to search/replace text in Linux: ``` sed -i 's/old_link/new_link/g' * ``` However, when I try it on my Mac OS X, I get: > "command c expects \ followed by text" I thought my Mac runs a normal BASH she...
> **Portable solution** below ### Why you get the error The `-i` option (alternatively, `--in-place`) means that you want files edited in-place, rather than streaming the change to a new place. Modifying a file in-place suggests a need for a backup file - and so a user-specified extension is expected after `-i`, but...
21112707
Check if a string matches a regex in Bash script
403
2014-01-14 11:43:09
<p>One of the arguments that my script receives is a date in the following format: <code>yyyymmdd</code>.</p> <p>I want to check if I get a valid date as an input.</p> <p>How can I do this? I am trying to use a regex like: <code>[0-9]\{\8}</code></p>
651,553
3,145,943
2022-08-22 22:11:17
21,112,809
626
2014-01-14 11:47:58
1,983,854
2022-03-29 17:48:44
https://stackoverflow.com/q/21112707
https://stackoverflow.com/a/21112809
<p>You can use the test construct, <code>[[ ]]</code>, along with the regular expression match operator, <code>=~</code>, to check if a string matches a regex pattern (<a href="https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#index-_005b_005b" rel="noreferrer">documentation</a>).</p> <p>Fo...
<p>You can use the test construct, <code>[[ ]]</code>, along with the regular expression match operator, <code>=~</code>, to check if a string matches a regex pattern (<a href="https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#index-_005b_005b" rel="noreferrer">documentation</a>).</p> <p>Fo...
18, 387, 390, 531
bash, regex, scripting, shell
<h1>Check if a string matches a regex in Bash script</h1> <p>One of the arguments that my script receives is a date in the following format: <code>yyyymmdd</code>.</p> <p>I want to check if I get a valid date as an input.</p> <p>How can I do this? I am trying to use a regex like: <code>[0-9]\{\8}</code></p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
275
bash
# Check if a string matches a regex in Bash script One of the arguments that my script receives is a date in the following format: `yyyymmdd`. I want to check if I get a valid date as an input. How can I do this? I am trying to use a regex like: `[0-9]\{\8}`
You can use the test construct, `[[ ]]`, along with the regular expression match operator, `=~`, to check if a string matches a regex pattern ([documentation](https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#index-_005b_005b)). For your specific case, you can write: ``` [[ "$date" =~ ^[0...
3358420
Generating a SHA-256 hash from the Linux command line
401
2010-07-28 23:41:05
<p>I know the string "foobar" generates the SHA-256 hash <code>c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2</code> using <a href="http://hash.online-convert.com/sha256-generator" rel="noreferrer">http://hash.online-convert.com/sha256-generator</a></p> <p>However the command line shell:</p> <pre><c...
617,785
4,534
2024-05-17 12:42:20
3,358,428
622
2010-07-28 23:43:12
371,793
2015-06-03 02:46:20
https://stackoverflow.com/q/3358420
https://stackoverflow.com/a/3358428
<p><code>echo</code> will normally output a newline, which is suppressed with <code>-n</code>. Try this:</p> <pre><code>echo -n foobar | sha256sum </code></pre>
<p><code>echo</code> will normally output a newline, which is suppressed with <code>-n</code>. Try this:</p> <pre><code>echo -n foobar | sha256sum </code></pre>
58, 390, 13892
linux, sha256, shell
<h1>Generating a SHA-256 hash from the Linux command line</h1> <p>I know the string "foobar" generates the SHA-256 hash <code>c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2</code> using <a href="http://hash.online-convert.com/sha256-generator" rel="noreferrer">http://hash.online-convert.com/sha256-gen...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
276
bash
# Generating a SHA-256 hash from the Linux command line I know the string "foobar" generates the SHA-256 hash `c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2` using <http://hash.online-convert.com/sha256-generator> However the command line shell: ``` hendry@x201 ~$ echo foobar | sha256sum aec070645...
`echo` will normally output a newline, which is suppressed with `-n`. Try this: ``` echo -n foobar | sha256sum ```
2609552
How can I use ":" as an AWK field separator?
402
2010-04-09 17:30:51
<p>Given the following command,</p> <pre><code>echo &quot;1: &quot; | awk '/1/ -F &quot;:&quot; {print $1}' </code></pre> <p>why does AWK output:</p> <pre><code>1: </code></pre> <p>?</p>
914,335
173,446
2025-01-13 10:12:21
2,609,565
621
2010-04-09 17:33:25
308,679
2022-10-14 16:39:25
https://stackoverflow.com/q/2609552
https://stackoverflow.com/a/2609565
<p><code>-F</code> is a command line argument, not AWK syntax. Try:</p> <pre class="lang-bash prettyprint-override"><code>echo '1: ' | awk -F ':' '/1/ {print $1}' </code></pre>
<p><code>-F</code> is a command line argument, not AWK syntax. Try:</p> <pre class="lang-bash prettyprint-override"><code>echo '1: ' | awk -F ':' '/1/ {print $1}' </code></pre>
387, 990
awk, bash
<h1>How can I use ":" as an AWK field separator?</h1> <p>Given the following command,</p> <pre><code>echo &quot;1: &quot; | awk '/1/ -F &quot;:&quot; {print $1}' </code></pre> <p>why does AWK output:</p> <pre><code>1: </code></pre> <p>?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
277
bash
# How can I use ":" as an AWK field separator? Given the following command, ``` echo "1: " | awk '/1/ -F ":" {print $1}' ``` why does AWK output: ``` 1: ``` ?
`-F` is a command line argument, not AWK syntax. Try: ``` echo '1: ' | awk -F ':' '/1/ {print $1}' ```
8961334
How to change folder with git bash?
353
2012-01-22 13:15:33
<p>My default <code>git</code> folder is <code>C:\Users\username\.git</code>.</p> <p>What command should I use to go into <code>C:/project</code>?</p>
1,186,972
1,124,803
2022-04-18 19:16:22
8,961,349
621
2012-01-22 13:17:39
19,165
2019-10-15 08:01:04
https://stackoverflow.com/q/8961334
https://stackoverflow.com/a/8961349
<p>The command is:</p> <pre><code>cd /c/project/ </code></pre> <p><strong>Tip:</strong><br> Use the <code>pwd</code> command to see which path you are currently in, handy when you did a right-click "Git Bash here..."</p>
<p>The command is:</p> <pre><code>cd /c/project/ </code></pre> <p><strong>Tip:</strong><br> Use the <code>pwd</code> command to see which path you are currently in, handy when you did a right-click "Git Bash here..."</p>
119, 61874
git, git-bash
<h1>How to change folder with git bash?</h1> <p>My default <code>git</code> folder is <code>C:\Users\username\.git</code>.</p> <p>What command should I use to go into <code>C:/project</code>?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
278
bash
# How to change folder with git bash? My default `git` folder is `C:\Users\username\.git`. What command should I use to go into `C:/project`?
The command is: ``` cd /c/project/ ``` **Tip:** Use the `pwd` command to see which path you are currently in, handy when you did a right-click "Git Bash here..."
7909167
How to quietly remove a directory with content in PowerShell
422
2011-10-26 21:29:17
<p>Using PowerShell, is it possible to remove some directory that contains files without prompting to confirm action?</p>
642,517
223,386
2023-08-05 12:43:26
7,909,195
620
2011-10-26 21:32:09
985,032
2022-09-16 13:30:08
https://stackoverflow.com/q/7909167
https://stackoverflow.com/a/7909195
<pre><code>Remove-Item -LiteralPath &quot;foldertodelete&quot; -Force -Recurse </code></pre> <p>or, with shorter version</p> <pre><code>rm /path -r -force </code></pre>
<pre><code>Remove-Item -LiteralPath &quot;foldertodelete&quot; -Force -Recurse </code></pre> <p>or, with shorter version</p> <pre><code>rm /path -r -force </code></pre>
218, 526
directory, powershell
<h1>How to quietly remove a directory with content in PowerShell</h1> <p>Using PowerShell, is it possible to remove some directory that contains files without prompting to confirm action?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
279
bash
# How to quietly remove a directory with content in PowerShell Using PowerShell, is it possible to remove some directory that contains files without prompting to confirm action?
``` Remove-Item -LiteralPath "foldertodelete" -Force -Recurse ``` or, with shorter version ``` rm /path -r -force ```
4412238
What is the cleanest way to ssh and run multiple commands in Bash?
485
2010-12-10 18:50:48
<p>I already have an ssh agent set up, and I can run commands on an external server in Bash script doing stuff like:</p> <pre><code>ssh blah_server &quot;ls; pwd;&quot; </code></pre> <p>Now, what I'd really like to do is run a lot of long commands on an external server. Enclosing all of these in between quotation marks...
595,242
392,975
2025-01-14 16:13:38
4,412,338
618
2010-12-10 19:03:15
3,333
2017-11-15 02:14:17
https://stackoverflow.com/q/4412238
https://stackoverflow.com/a/4412338
<p>How about a Bash <a href="http://www.tldp.org/LDP/abs/html/here-docs.html" rel="noreferrer">Here Document</a>:</p> <pre><code>ssh otherhost &lt;&lt; EOF ls some_folder; ./someaction.sh 'some params' pwd ./some_other_action 'other params' EOF </code></pre> <p>To avoid the problems mentioned by @Globalz in ...
<p>How about a Bash <a href="http://www.tldp.org/LDP/abs/html/here-docs.html" rel="noreferrer">Here Document</a>:</p> <pre><code>ssh otherhost &lt;&lt; EOF ls some_folder; ./someaction.sh 'some params' pwd ./some_other_action 'other params' EOF </code></pre> <p>To avoid the problems mentioned by @Globalz in ...
34, 386, 387
bash, ssh, unix
<h1>What is the cleanest way to ssh and run multiple commands in Bash?</h1> <p>I already have an ssh agent set up, and I can run commands on an external server in Bash script doing stuff like:</p> <pre><code>ssh blah_server &quot;ls; pwd;&quot; </code></pre> <p>Now, what I'd really like to do is run a lot of long comma...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
280
bash
# What is the cleanest way to ssh and run multiple commands in Bash? I already have an ssh agent set up, and I can run commands on an external server in Bash script doing stuff like: ``` ssh blah_server "ls; pwd;" ``` Now, what I'd really like to do is run a lot of long commands on an external server. Enclosing all ...
How about a Bash [Here Document](http://www.tldp.org/LDP/abs/html/here-docs.html): ``` ssh otherhost << EOF ls some_folder; ./someaction.sh 'some params' pwd ./some_other_action 'other params' EOF ``` To avoid the problems mentioned by @Globalz in the comments, you may be able to (depending what you're doing...
13322485
How to get the primary IP address of the local machine on Linux and OS X?
477
2012-11-10 13:32:11
<p>I am looking for a command line solution that would return me the primary (first) IP address of the localhost, other than 127.0.0.1</p> <p>The solution should work at least for Linux (Debian and RedHat) and OS X 10.7+</p> <p>I am aware that <code>ifconfig</code> is available on both but its output is not so consis...
983,879
99,834
2024-02-19 07:14:15
13,322,549
618
2012-11-10 13:39:13
1,066,031
2018-01-12 22:14:17
https://stackoverflow.com/q/13322485
https://stackoverflow.com/a/13322549
<p>Use <code>grep</code> to filter IP address from <code>ifconfig</code>: </p> <p><code>ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'</code></p> <p>Or with <code>sed</code>:</p> <p><code>ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[...
<p>Use <code>grep</code> to filter IP address from <code>ifconfig</code>: </p> <p><code>ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'</code></p> <p>Or with <code>sed</code>:</p> <p><code>ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[...
34, 294, 387, 30175
bash, ifconfig, ip, unix
<h1>How to get the primary IP address of the local machine on Linux and OS X?</h1> <p>I am looking for a command line solution that would return me the primary (first) IP address of the localhost, other than 127.0.0.1</p> <p>The solution should work at least for Linux (Debian and RedHat) and OS X 10.7+</p> <p>I am aw...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
281
bash
# How to get the primary IP address of the local machine on Linux and OS X? I am looking for a command line solution that would return me the primary (first) IP address of the localhost, other than 127.0.0.1 The solution should work at least for Linux (Debian and RedHat) and OS X 10.7+ I am aware that `ifconfig` is ...
Use `grep` to filter IP address from `ifconfig`: `ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'` Or with `sed`: `ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'` If you are only interested in certain interfaces, wlan0, ...
307503
What's a concise way to check that environment variables are set in a Unix shell script?
526
2008-11-21 01:11:27
<p>I've got a few Unix shell scripts where I need to check that certain environment variables are set before I start doing stuff, so I do this sort of thing:</p> <pre><code>if [ -z "$STATE" ]; then echo "Need to set STATE" exit 1 fi if [ -z "$DEST" ]; then echo "Need to set DEST" exit 1 fi </code></...
404,586
2,994
2024-05-13 15:28:16
307,735
616
2008-11-21 03:25:31
15,168
2015-10-06 19:36:18
https://stackoverflow.com/q/307503
https://stackoverflow.com/a/307735
<h3>Parameter Expansion</h3> <p>The obvious answer is to use one of the special forms of parameter expansion:</p> <pre><code>: ${STATE?&quot;Need to set STATE&quot;} : ${DEST:?&quot;Need to set DEST non-empty&quot;} </code></pre> <p>Or, better (see section on 'Position of double quotes' below):</p> <pre><code>: &quot;$...
<h3>Parameter Expansion</h3> <p>The obvious answer is to use one of the special forms of parameter expansion:</p> <pre><code>: ${STATE?&quot;Need to set STATE&quot;} : ${DEST:?&quot;Need to set DEST non-empty&quot;} </code></pre> <p>Or, better (see section on 'Position of double quotes' below):</p> <pre><code>: &quot;$...
34, 387, 390
bash, shell, unix
<h1>What's a concise way to check that environment variables are set in a Unix shell script?</h1> <p>I've got a few Unix shell scripts where I need to check that certain environment variables are set before I start doing stuff, so I do this sort of thing:</p> <pre><code>if [ -z "$STATE" ]; then echo "Need to set S...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
282
bash
# What's a concise way to check that environment variables are set in a Unix shell script? I've got a few Unix shell scripts where I need to check that certain environment variables are set before I start doing stuff, so I do this sort of thing: ``` if [ -z "$STATE" ]; then echo "Need to set STATE" exit 1 fi ...
### Parameter Expansion The obvious answer is to use one of the special forms of parameter expansion: ``` : ${STATE?"Need to set STATE"} : ${DEST:?"Need to set DEST non-empty"} ``` Or, better (see section on 'Position of double quotes' below): ``` : "${STATE?Need to set STATE}" : "${DEST:?Need to set DEST non-empty...
2924697
How does one output bold text in Bash?
371
2010-05-27 20:36:09
<p>I'm writing a Bash script that prints some text to the screen:</p> <pre><code>echo "Some Text" </code></pre> <p>Can I format the text? I would like to make it bold.</p>
317,735
335,937
2025-07-17 05:30:21
2,924,755
614
2010-05-27 20:42:16
333,698
2024-03-01 17:05:03
https://stackoverflow.com/q/2924697
https://stackoverflow.com/a/2924755
<p>The most compatible way of doing this is using <code>tput</code> to discover the right sequences to send to the terminal:</p> <pre><code>bold=$(tput bold) normal=$(tput sgr0) </code></pre> <p>then you can use the variables <code>$bold</code> and <code>$normal</code> to format things:</p> <pre><code>echo &quot;this i...
<p>The most compatible way of doing this is using <code>tput</code> to discover the right sequences to send to the terminal:</p> <pre><code>bold=$(tput bold) normal=$(tput sgr0) </code></pre> <p>then you can use the variables <code>$bold</code> and <code>$normal</code> to format things:</p> <pre><code>echo &quot;this i...
387, 488, 555, 13824
bash, console, echo, formatting
<h1>How does one output bold text in Bash?</h1> <p>I'm writing a Bash script that prints some text to the screen:</p> <pre><code>echo "Some Text" </code></pre> <p>Can I format the text? I would like to make it bold.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
283
bash
# How does one output bold text in Bash? I'm writing a Bash script that prints some text to the screen: ``` echo "Some Text" ``` Can I format the text? I would like to make it bold.
The most compatible way of doing this is using `tput` to discover the right sequences to send to the terminal: ``` bold=$(tput bold) normal=$(tput sgr0) ``` then you can use the variables `$bold` and `$normal` to format things: ``` echo "this is ${bold}bold${normal} but this isn't" ``` gives > this is **bold** but...
40082346
How to check if a file exists in a shell script
338
2016-10-17 09:05:51
<p>I'd like to write a shell script which checks if a certain file, <code>archived_sensor_data.json</code>, exists, and if so, deletes it. Following <a href="http://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-expressions.html" rel="noreferrer">http://www.cyberciti.biz/tips/find-out-if-file-exists-wi...
771,328
995,862
2024-02-08 19:28:10
40,082,454
613
2016-10-17 09:11:22
307,297
2018-11-01 20:54:26
https://stackoverflow.com/q/40082346
https://stackoverflow.com/a/40082454
<p>You're missing a required space between the bracket and <code>-e</code>:</p> <pre><code>#!/bin/bash if [ -e x.txt ] then echo "ok" else echo "nok" fi </code></pre>
<p>You're missing a required space between the bracket and <code>-e</code>:</p> <pre><code>#!/bin/bash if [ -e x.txt ] then echo "ok" else echo "nok" fi </code></pre>
390
shell
<h1>How to check if a file exists in a shell script</h1> <p>I'd like to write a shell script which checks if a certain file, <code>archived_sensor_data.json</code>, exists, and if so, deletes it. Following <a href="http://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-expressions.html" rel="noreferrer"...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
284
bash
# How to check if a file exists in a shell script I'd like to write a shell script which checks if a certain file, `archived_sensor_data.json`, exists, and if so, deletes it. Following <http://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-expressions.html>, I've tried the following: ``` [-e archived...
You're missing a required space between the bracket and `-e`: ``` #!/bin/bash if [ -e x.txt ] then echo "ok" else echo "nok" fi ```
3162385
How to split a string in shell and get the last field
415
2010-07-01 23:29:38
<p>Suppose I have the string <code>1:2:3:4:5</code> and I want to get its last field (<code>5</code> in this case). How do I do that using Bash? I tried <code>cut</code>, but I don't know how to specify the last field with <code>-f</code>.</p>
549,788
38,333
2025-01-21 17:39:32
3,162,500
609
2010-07-02 00:05:40
303,180
2010-07-02 00:05:40
https://stackoverflow.com/q/3162385
https://stackoverflow.com/a/3162500
<p>You can use <a href="http://www.linuxjournal.com/article/8919" rel="noreferrer">string operators</a>:</p> <pre><code>$ foo=1:2:3:4:5 $ echo ${foo##*:} 5 </code></pre> <p>This trims everything from the front until a ':', greedily.</p> <pre><code>${foo &lt;-- from variable foo ## &lt;-- greedy front trim * ...
<p>You can use <a href="http://www.linuxjournal.com/article/8919" rel="noreferrer">string operators</a>:</p> <pre><code>$ foo=1:2:3:4:5 $ echo ${foo##*:} 5 </code></pre> <p>This trims everything from the front until a ':', greedily.</p> <pre><code>${foo &lt;-- from variable foo ## &lt;-- greedy front trim * ...
387, 2193, 4257, 15047
bash, cut, split, tokenize
<h1>How to split a string in shell and get the last field</h1> <p>Suppose I have the string <code>1:2:3:4:5</code> and I want to get its last field (<code>5</code> in this case). How do I do that using Bash? I tried <code>cut</code>, but I don't know how to specify the last field with <code>-f</code>.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
285
bash
# How to split a string in shell and get the last field Suppose I have the string `1:2:3:4:5` and I want to get its last field (`5` in this case). How do I do that using Bash? I tried `cut`, but I don't know how to specify the last field with `-f`.
You can use [string operators](http://www.linuxjournal.com/article/8919): ``` $ foo=1:2:3:4:5 $ echo ${foo##*:} 5 ``` This trims everything from the front until a ':', greedily. ``` ${foo <-- from variable foo ## <-- greedy front trim * <-- matches anything : <-- until the last ':' } ```
3569997
How to find out line-endings in a text file?
501
2010-08-25 20:36:06
<p>I'm trying to use something in bash to show me the line endings in a file printed rather than interpreted. The file is a dump from SSIS/SQL Server being read in by a Linux machine for processing.</p> <ul> <li><p>Are there any switches within <code>vi</code>, <code>less</code>, <code>more</code>, etc?</p></li> <li><...
587,701
196,832
2025-03-18 09:56:12
3,570,574
607
2010-08-25 22:00:50
26,428
2010-08-25 22:00:50
https://stackoverflow.com/q/3569997
https://stackoverflow.com/a/3570574
<p>You can use the <code>file</code> utility to give you an indication of the type of line endings.</p> <p>Unix:</p> <pre><code>$ file testfile1.txt testfile.txt: ASCII text </code></pre> <p>"DOS":</p> <pre><code>$ file testfile2.txt testfile2.txt: ASCII text, with CRLF line terminators </code></pre> <p>To convert...
<p>You can use the <code>file</code> utility to give you an indication of the type of line endings.</p> <p>Unix:</p> <pre><code>$ file testfile1.txt testfile.txt: ASCII text </code></pre> <p>"DOS":</p> <pre><code>$ file testfile2.txt testfile2.txt: ASCII text, with CRLF line terminators </code></pre> <p>To convert...
58, 387, 1231, 3705, 8329
bash, command-line, line-endings, linux, newline
<h1>How to find out line-endings in a text file?</h1> <p>I'm trying to use something in bash to show me the line endings in a file printed rather than interpreted. The file is a dump from SSIS/SQL Server being read in by a Linux machine for processing.</p> <ul> <li><p>Are there any switches within <code>vi</code>, <co...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
286
bash
# How to find out line-endings in a text file? I'm trying to use something in bash to show me the line endings in a file printed rather than interpreted. The file is a dump from SSIS/SQL Server being read in by a Linux machine for processing. - Are there any switches within `vi`, `less`, `more`, etc? - In addition to...
You can use the `file` utility to give you an indication of the type of line endings. Unix: ``` $ file testfile1.txt testfile.txt: ASCII text ``` "DOS": ``` $ file testfile2.txt testfile2.txt: ASCII text, with CRLF line terminators ``` To convert from "DOS" to Unix: ``` $ dos2unix testfile2.txt ``` To convert fr...
4881930
Remove the last line from a file in Bash
483
2011-02-03 01:45:20
<p>I have a file, <code>foo.txt</code>, containing the following lines:</p> <pre><code>a b c </code></pre> <p>I want a simple command that results in the contents of <code>foo.txt</code> being:</p> <pre><code>a b </code></pre>
609,679
121,112
2024-10-08 04:55:29
4,881,990
607
2011-02-03 01:59:52
507,519
2018-12-10 23:53:36
https://stackoverflow.com/q/4881930
https://stackoverflow.com/a/4881990
<p>Using <a href="http://www.gnu.org/software/sed/" rel="noreferrer"><code>GNU sed</code></a>:</p> <pre><code>sed -i '$ d' foo.txt </code></pre> <p>The <code>-i</code> option does not exist in <code>GNU sed</code> versions older than 3.95, so you have to use it as a filter with a temporary file:</p> <pre><code>cp fo...
<p>Using <a href="http://www.gnu.org/software/sed/" rel="noreferrer"><code>GNU sed</code></a>:</p> <pre><code>sed -i '$ d' foo.txt </code></pre> <p>The <code>-i</code> option does not exist in <code>GNU sed</code> versions older than 3.95, so you have to use it as a filter with a temporary file:</p> <pre><code>cp fo...
387, 531, 1231, 3601
bash, command-line, scripting, truncate
<h1>Remove the last line from a file in Bash</h1> <p>I have a file, <code>foo.txt</code>, containing the following lines:</p> <pre><code>a b c </code></pre> <p>I want a simple command that results in the contents of <code>foo.txt</code> being:</p> <pre><code>a b </code></pre>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
287
bash
# Remove the last line from a file in Bash I have a file, `foo.txt`, containing the following lines: ``` a b c ``` I want a simple command that results in the contents of `foo.txt` being: ``` a b ```
Using [`GNU sed`](http://www.gnu.org/software/sed/): ``` sed -i '$ d' foo.txt ``` The `-i` option does not exist in `GNU sed` versions older than 3.95, so you have to use it as a filter with a temporary file: ``` cp foo.txt foo.txt.tmp sed '$ d' foo.txt.tmp > foo.txt rm -f foo.txt.tmp ``` Of course, in that case yo...
13593771
Repeat command automatically in Linux
352
2012-11-27 21:50:24
<p>Is it possible in Linux command line to have a command repeat every <em>n</em> seconds?</p> <p>Say, I have an import running, and I am doing</p> <pre><code>ls -l </code></pre> <p>to check if the file size is increasing. I would like to have a command to have this repeat automatically.</p>
417,832
1,189,880
2024-11-02 18:49:01
13,593,790
607
2012-11-27 21:51:47
449,958
2012-11-27 22:05:33
https://stackoverflow.com/q/13593771
https://stackoverflow.com/a/13593790
<p>Watch every 5 seconds ...</p> <p><code>watch -n 5 ls -l</code></p> <p>If you wish to have visual confirmation of changes, append <code>--differences</code> prior to the <code>ls</code> command.</p> <p>According to the OSX man page, there's also</p> <blockquote> <p>The --cumulative option makes highlighting "st...
<p>Watch every 5 seconds ...</p> <p><code>watch -n 5 ls -l</code></p> <p>If you wish to have visual confirmation of changes, append <code>--differences</code> prior to the <code>ls</code> command.</p> <p>According to the OSX man page, there's also</p> <blockquote> <p>The --cumulative option makes highlighting "st...
58, 387, 549
bash, linux, ubuntu
<h1>Repeat command automatically in Linux</h1> <p>Is it possible in Linux command line to have a command repeat every <em>n</em> seconds?</p> <p>Say, I have an import running, and I am doing</p> <pre><code>ls -l </code></pre> <p>to check if the file size is increasing. I would like to have a command to have this rep...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
288
bash
# Repeat command automatically in Linux Is it possible in Linux command line to have a command repeat every *n* seconds? Say, I have an import running, and I am doing ``` ls -l ``` to check if the file size is increasing. I would like to have a command to have this repeat automatically.
Watch every 5 seconds ... `watch -n 5 ls -l` If you wish to have visual confirmation of changes, append `--differences` prior to the `ls` command. According to the OSX man page, there's also > The --cumulative option makes highlighting "sticky", presenting a > running display of all positions that have ever changed...
2085744
How do I get the current username in Windows PowerShell?
521
2010-01-18 11:50:32
<p>How do I get the current username in Windows PowerShell?</p>
1,069,114
15,985
2025-06-30 00:44:33
2,085,755
605
2010-01-18 11:52:52
15,985
2023-05-15 19:47:58
https://stackoverflow.com/q/2085744
https://stackoverflow.com/a/2085755
<p>I found it:</p> <pre><code>[Environment]::UserName $Env:UserName </code></pre> <p>There is also:</p> <pre><code>$Env:UserDomain $Env:ComputerName </code></pre>
<p>I found it:</p> <pre><code>[Environment]::UserName $Env:UserName </code></pre> <p>There is also:</p> <pre><code>$Env:UserDomain $Env:ComputerName </code></pre>
64, 526, 531
powershell, scripting, windows
<h1>How do I get the current username in Windows PowerShell?</h1> <p>How do I get the current username in Windows PowerShell?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
289
bash
# How do I get the current username in Windows PowerShell? How do I get the current username in Windows PowerShell?
I found it: ``` [Environment]::UserName $Env:UserName ``` There is also: ``` $Env:UserDomain $Env:ComputerName ```
7526971
How to redirect both stdout and stderr to a file
387
2011-09-23 09:35:45
<p>I am running a bash script that creates a log file for the execution of the command</p> <p>I use the following</p> <pre><code>Command1 &gt;&gt; log_file Command2 &gt;&gt; log_file </code></pre> <p>This only sends the standard output and not the standard error which appears on the terminal.</p>
321,596
525,376
2021-12-15 12:48:19
7,526,988
605
2011-09-23 09:37:31
635,608
2011-09-23 09:37:31
https://stackoverflow.com/q/7526971
https://stackoverflow.com/a/7526988
<p>If you want to log to the same file:</p> <pre><code>command1 &gt;&gt; log_file 2&gt;&amp;1 </code></pre> <p>If you want different files:</p> <pre><code>command1 &gt;&gt; log_file 2&gt;&gt; err_file </code></pre>
<p>If you want to log to the same file:</p> <pre><code>command1 &gt;&gt; log_file 2&gt;&amp;1 </code></pre> <p>If you want different files:</p> <pre><code>command1 &gt;&gt; log_file 2&gt;&gt; err_file </code></pre>
387, 4867, 19156, 26698
bash, io-redirection, stderr, stdout
<h1>How to redirect both stdout and stderr to a file</h1> <p>I am running a bash script that creates a log file for the execution of the command</p> <p>I use the following</p> <pre><code>Command1 &gt;&gt; log_file Command2 &gt;&gt; log_file </code></pre> <p>This only sends the standard output and not the standard error...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
290
bash
# How to redirect both stdout and stderr to a file I am running a bash script that creates a log file for the execution of the command I use the following ``` Command1 >> log_file Command2 >> log_file ``` This only sends the standard output and not the standard error which appears on the terminal.
If you want to log to the same file: ``` command1 >> log_file 2>&1 ``` If you want different files: ``` command1 >> log_file 2>> err_file ```
4143252
How to make the 'cut' command treat same sequental delimiters as one?
345
2010-11-10 10:04:15
<p>I'm trying to extract a certain (the fourth) field from the column-based, 'space'-adjusted text stream. I'm trying to use the <code>cut</code> command in the following manner: </p> <p><code>cat text.txt | cut -d " " -f 4</code></p> <p>Unfortunately, <code>cut</code> doesn't treat several spaces as one delimiter. I...
178,967
411,928
2024-11-21 10:30:22
4,483,833
602
2010-12-19 16:22:01
348,785
2018-04-23 23:03:23
https://stackoverflow.com/q/4143252
https://stackoverflow.com/a/4483833
<p>Try:</p> <pre><code>tr -s ' ' &lt;text.txt | cut -d ' ' -f4 </code></pre> <p>From the <code>tr</code> man page:</p> <pre> -s, --squeeze-repeats replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character </...
<p>Try:</p> <pre><code>tr -s ' ' &lt;text.txt | cut -d ' ' -f4 </code></pre> <p>From the <code>tr</code> man page:</p> <pre> -s, --squeeze-repeats replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character </...
34, 387, 4257, 16168
bash, cut, delimiter, unix
<h1>How to make the 'cut' command treat same sequental delimiters as one?</h1> <p>I'm trying to extract a certain (the fourth) field from the column-based, 'space'-adjusted text stream. I'm trying to use the <code>cut</code> command in the following manner: </p> <p><code>cat text.txt | cut -d " " -f 4</code></p> <p>U...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
291
bash
# How to make the 'cut' command treat same sequental delimiters as one? I'm trying to extract a certain (the fourth) field from the column-based, 'space'-adjusted text stream. I'm trying to use the `cut` command in the following manner: `cat text.txt | cut -d " " -f 4` Unfortunately, `cut` doesn't treat several spac...
Try: ``` tr -s ' ' <text.txt | cut -d ' ' -f4 ``` From the `tr` man page: ``` -s, --squeeze-repeats replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character ```
5731234
How can I get the start time of a long-running Linux process?
403
2011-04-20 13:38:27
<p>Is it possible to get the start time of an old running process? It seems that <code>ps</code> will report the date (not the time) if it wasn't started today, and only the year if it wasn't started this year. Is the precision lost forever for old processes?</p>
368,238
512,652
2024-12-19 07:30:42
5,731,337
600
2011-04-20 13:43:47
297,696
2018-05-21 16:40:32
https://stackoverflow.com/q/5731234
https://stackoverflow.com/a/5731337
<p>You can specify a formatter and use <code>lstart</code>, like this command:</p> <pre><code>ps -eo pid,lstart,cmd </code></pre> <p>The above command will output all processes, with formatters to get PID, command run, and date+time started.</p> <p>Example (from Debian/Jessie command line)</p> <pre><code>$ ps -eo p...
<p>You can specify a formatter and use <code>lstart</code>, like this command:</p> <pre><code>ps -eo pid,lstart,cmd </code></pre> <p>The above command will output all processes, with formatters to get PID, command run, and date+time started.</p> <p>Example (from Debian/Jessie command line)</p> <pre><code>$ ps -eo p...
58, 387, 5910
bash, linux, process
<h1>How can I get the start time of a long-running Linux process?</h1> <p>Is it possible to get the start time of an old running process? It seems that <code>ps</code> will report the date (not the time) if it wasn't started today, and only the year if it wasn't started this year. Is the precision lost forever for old ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
292
bash
# How can I get the start time of a long-running Linux process? Is it possible to get the start time of an old running process? It seems that `ps` will report the date (not the time) if it wasn't started today, and only the year if it wasn't started this year. Is the precision lost forever for old processes?
You can specify a formatter and use `lstart`, like this command: ``` ps -eo pid,lstart,cmd ``` The above command will output all processes, with formatters to get PID, command run, and date+time started. Example (from Debian/Jessie command line) ``` $ ps -eo pid,lstart,cmd PID CMD ...
2439579
How to get the first line of a file in a bash script?
392
2010-03-13 18:56:13
<p>I have to put in a bash variable the first line of a file. I guess it is with the grep command, but it is any way to restrict the number of lines?</p>
586,502
288,247
2024-06-17 08:18:19
2,439,587
600
2010-03-13 18:58:06
56,338
2010-03-13 18:58:06
https://stackoverflow.com/q/2439579
https://stackoverflow.com/a/2439587
<p><code>head</code> takes the first lines from a file, and the <code>-n</code> parameter can be used to specify how many lines should be extracted:</p> <pre><code>line=$(head -n 1 filename) </code></pre>
<p><code>head</code> takes the first lines from a file, and the <code>-n</code> parameter can be used to specify how many lines should be extracted:</p> <pre><code>line=$(head -n 1 filename) </code></pre>
387
bash
<h1>How to get the first line of a file in a bash script?</h1> <p>I have to put in a bash variable the first line of a file. I guess it is with the grep command, but it is any way to restrict the number of lines?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
293
bash
# How to get the first line of a file in a bash script? I have to put in a bash variable the first line of a file. I guess it is with the grep command, but it is any way to restrict the number of lines?
`head` takes the first lines from a file, and the `-n` parameter can be used to specify how many lines should be extracted: ``` line=$(head -n 1 filename) ```
11162406
Open and write data to text file using Bash?
375
2012-06-22 18:52:22
<p>How can I write data to a text file automatically by shell scripting in Linux?</p> <p>I was able to open the file. However, I don't know how to write data to it.</p>
1,056,480
1,055,090
2023-11-07 06:59:49
11,162,510
600
2012-06-22 18:59:35
428,916
2022-12-02 01:43:25
https://stackoverflow.com/q/11162406
https://stackoverflow.com/a/11162510
<p>The short answer:</p> <pre class="lang-bash prettyprint-override"><code>echo &quot;some data for the file&quot; &gt;&gt; fileName </code></pre> <p>However, <a href="https://unix.stackexchange.com/a/58312/364698"><code>echo</code> doesn't deal with end of line characters (EOFs) in an ideal way</a>. So, if you're goin...
<p>The short answer:</p> <pre class="lang-bash prettyprint-override"><code>echo &quot;some data for the file&quot; &gt;&gt; fileName </code></pre> <p>However, <a href="https://unix.stackexchange.com/a/58312/364698"><code>echo</code> doesn't deal with end of line characters (EOFs) in an ideal way</a>. So, if you're goin...
387, 390
bash, shell
<h1>Open and write data to text file using Bash?</h1> <p>How can I write data to a text file automatically by shell scripting in Linux?</p> <p>I was able to open the file. However, I don't know how to write data to it.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
294
bash
# Open and write data to text file using Bash? How can I write data to a text file automatically by shell scripting in Linux? I was able to open the file. However, I don't know how to write data to it.
The short answer: ``` echo "some data for the file" >> fileName ``` However, [`echo` doesn't deal with end of line characters (EOFs) in an ideal way](https://unix.stackexchange.com/a/58312/364698). So, if you're going to append more than one line, do it with `printf`: ``` printf "some data for the file\nAnd a new li...
183901
How do I start PowerShell from Windows Explorer?
312
2008-10-08 17:30:16
<p>Is there a way to start PowerShell in a specific folder from Windows Explorer, e.g. to right-click in a folder and have an option like "Open PowerShell in this Folder"?</p> <p>It's really annoying to have to change directories to my project folder the first time I run MSBuild every day.</p>
185,220
549
2024-11-20 00:16:38
6,599,296
600
2011-07-06 15:47:40
1,630
2017-05-28 23:21:34
https://stackoverflow.com/q/183901
https://stackoverflow.com/a/6599296
<p>In Windows Explorer, just go to the Address Bar at the top (keyboard shortcuts: <kbd>Alt</kbd>+<kbd>D</kbd> or <kbd>Ctrl</kbd>+<kbd>L</kbd>) and type <code>powershell</code> or <code>powershell_ise</code> and press <kbd>Enter</kbd>. A PowerShell command window opens with the current directory.</p>
<p>In Windows Explorer, just go to the Address Bar at the top (keyboard shortcuts: <kbd>Alt</kbd>+<kbd>D</kbd> or <kbd>Ctrl</kbd>+<kbd>L</kbd>) and type <code>powershell</code> or <code>powershell_ise</code> and press <kbd>Enter</kbd>. A PowerShell command window opens with the current directory.</p>
64, 526
powershell, windows
<h1>How do I start PowerShell from Windows Explorer?</h1> <p>Is there a way to start PowerShell in a specific folder from Windows Explorer, e.g. to right-click in a folder and have an option like "Open PowerShell in this Folder"?</p> <p>It's really annoying to have to change directories to my project folder the first ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
295
bash
# How do I start PowerShell from Windows Explorer? Is there a way to start PowerShell in a specific folder from Windows Explorer, e.g. to right-click in a folder and have an option like "Open PowerShell in this Folder"? It's really annoying to have to change directories to my project folder the first time I run MSBui...
In Windows Explorer, just go to the Address Bar at the top (keyboard shortcuts: `Alt`+`D` or `Ctrl`+`L`) and type `powershell` or `powershell_ise` and press `Enter`. A PowerShell command window opens with the current directory.
626533
How can I ssh directly to a particular directory?
365
2009-03-09 14:50:21
<p>I often have to login to one of several servers and go to one of several directories on those machines. Currently I do something of this sort:</p> <pre> localhost ~]$ ssh somehost Welcome to somehost! somehost ~]$ cd /some/directory/somewhere/named/Foo somehost Foo]$ </pre> <p>I have scripts that can determine...
339,613
7,476
2024-01-10 16:15:14
626,574
599
2009-03-09 14:59:05
28,388
2024-01-10 16:15:14
https://stackoverflow.com/q/626533
https://stackoverflow.com/a/626574
<p>You can do the following:</p> <pre><code>ssh -t xxx.xxx.xxx.xxx &quot;cd /directory_wanted ; bash --login&quot; </code></pre> <p>This way, you will get a login shell right on the directory_wanted.</p> <hr /> <p>Explanation</p> <blockquote> <p><code>-t</code> Force pseudo-terminal allocation. This can be used to exe...
<p>You can do the following:</p> <pre><code>ssh -t xxx.xxx.xxx.xxx &quot;cd /directory_wanted ; bash --login&quot; </code></pre> <p>This way, you will get a login shell right on the directory_wanted.</p> <hr /> <p>Explanation</p> <blockquote> <p><code>-t</code> Force pseudo-terminal allocation. This can be used to exe...
386, 387, 390, 531
bash, scripting, shell, ssh
<h1>How can I ssh directly to a particular directory?</h1> <p>I often have to login to one of several servers and go to one of several directories on those machines. Currently I do something of this sort:</p> <pre> localhost ~]$ ssh somehost Welcome to somehost! somehost ~]$ cd /some/directory/somewhere/named/Foo s...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
296
bash
# How can I ssh directly to a particular directory? I often have to login to one of several servers and go to one of several directories on those machines. Currently I do something of this sort: ``` localhost ~]$ ssh somehost Welcome to somehost! somehost ~]$ cd /some/directory/somewhere/named/Foo somehost Foo]$ ``...
You can do the following: ``` ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted ; bash --login" ``` This way, you will get a login shell right on the directory_wanted. --- Explanation > `-t` Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be v...
25373188
How to place the ~/.composer/vendor/bin directory in your PATH?
276
2014-08-18 22:44:43
<p>I'm on Ubuntu 14.04 and I've been trying all possible methods to install Laravel to no avail. Error messages everything I try. I'm now trying the first method in the quickstart documentation, that is, via Laravel Installer, but it says to "Make sure to place the <code>~/.composer/vendor/bin</code> directory in your ...
331,160
2,827,463
2022-08-29 11:55:46
25,373,254
599
2014-08-18 22:51:30
3,906,760
2018-09-03 07:25:26
https://stackoverflow.com/q/25373188
https://stackoverflow.com/a/25373254
<p>To put this folder on the PATH environment variable type</p> <pre><code>export PATH="$PATH:$HOME/.composer/vendor/bin" </code></pre> <p>This appends the folder to your existing PATH, however, it is only active for your current terminal session.</p> <p>If you want it to be automatically set, it depends on the shel...
<p>To put this folder on the PATH environment variable type</p> <pre><code>export PATH="$PATH:$HOME/.composer/vendor/bin" </code></pre> <p>This appends the folder to your existing PATH, however, it is only active for your current terminal session.</p> <p>If you want it to be automatically set, it depends on the shel...
218, 387, 549, 6268, 75151
bash, directory, laravel, path, ubuntu
<h1>How to place the ~/.composer/vendor/bin directory in your PATH?</h1> <p>I'm on Ubuntu 14.04 and I've been trying all possible methods to install Laravel to no avail. Error messages everything I try. I'm now trying the first method in the quickstart documentation, that is, via Laravel Installer, but it says to "Make...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
297
bash
# How to place the ~/.composer/vendor/bin directory in your PATH? I'm on Ubuntu 14.04 and I've been trying all possible methods to install Laravel to no avail. Error messages everything I try. I'm now trying the first method in the quickstart documentation, that is, via Laravel Installer, but it says to "Make sure to ...
To put this folder on the PATH environment variable type ``` export PATH="$PATH:$HOME/.composer/vendor/bin" ``` This appends the folder to your existing PATH, however, it is only active for your current terminal session. If you want it to be automatically set, it depends on the shell you are using. For bash, you can...
805418
How can I find encoding of a file via a script on Linux?
447
2009-04-30 05:13:48
<p>I need to find the encoding of all files that are placed in a directory. <br /> Is there a way to find the encoding used?</p> <p>The <a href="https://linux.die.net/man/1/file" rel="noreferrer"><code>file</code></a> command is not able to do this.</p> <p>The encoding of interest to me is <a href="https://en.wikipedia...
740,005
64,833
2024-11-08 01:59:42
805,474
594
2009-04-30 05:41:58
54,491
2021-05-28 23:34:59
https://stackoverflow.com/q/805418
https://stackoverflow.com/a/805474
<p>It sounds like you're looking for <code>enca</code>. It can guess and even convert between encodings. Just look at the <a href="http://linux.die.net/man/1/enconv" rel="noreferrer">man page</a>.</p> <p>Or, failing that, use <code>file -i</code> (Linux) or <code>file -I</code> (OS X). That will output MIME-type inform...
<p>It sounds like you're looking for <code>enca</code>. It can guess and even convert between encodings. Just look at the <a href="http://linux.die.net/man/1/enconv" rel="noreferrer">man page</a>.</p> <p>Or, failing that, use <code>file -i</code> (Linux) or <code>file -I</code> (OS X). That will output MIME-type inform...
34, 390, 1124, 5310
encoding, file, shell, unix
<h1>How can I find encoding of a file via a script on Linux?</h1> <p>I need to find the encoding of all files that are placed in a directory. <br /> Is there a way to find the encoding used?</p> <p>The <a href="https://linux.die.net/man/1/file" rel="noreferrer"><code>file</code></a> command is not able to do this.</p> ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
298
bash
# How can I find encoding of a file via a script on Linux? I need to find the encoding of all files that are placed in a directory. Is there a way to find the encoding used? The [`file`](https://linux.die.net/man/1/file) command is not able to do this. The encoding of interest to me is [ISO 8859-1](https://en.wik...
It sounds like you're looking for `enca`. It can guess and even convert between encodings. Just look at the [man page](http://linux.die.net/man/1/enconv). Or, failing that, use `file -i` (Linux) or `file -I` (OS X). That will output MIME-type information for the file, which will also include the character-set encoding...
5349718
How can I repeat a character in Bash?
378
2011-03-18 08:45:42
<p>How could I do this with <code>echo</code>?</p> <pre class="lang-sh prettyprint-override"><code>perl -E 'say "=" x 100' </code></pre>
276,214
198,183
2025-10-31 17:30:42
5,349,842
593
2011-03-18 08:58:47
7,412
2024-09-16 11:12:31
https://stackoverflow.com/q/5349718
https://stackoverflow.com/a/5349842
<p>You can use:</p> <pre class="lang-bash prettyprint-override"><code>printf '=%.0s' {1..100} </code></pre> <p>How this works:</p> <p>Bash expands <code>{1..100}</code> so the command becomes:</p> <pre class="lang-bash prettyprint-override"><code>printf '=%.0s' 1 2 3 4 ... 100 </code></pre> <p>I've set printf's format ...
<p>You can use:</p> <pre class="lang-bash prettyprint-override"><code>printf '=%.0s' {1..100} </code></pre> <p>How this works:</p> <p>Bash expands <code>{1..100}</code> so the command becomes:</p> <pre class="lang-bash prettyprint-override"><code>printf '=%.0s' 1 2 3 4 ... 100 </code></pre> <p>I've set printf's format ...
387, 390, 13824
bash, echo, shell
<h1>How can I repeat a character in Bash?</h1> <p>How could I do this with <code>echo</code>?</p> <pre class="lang-sh prettyprint-override"><code>perl -E 'say "=" x 100' </code></pre>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
299
bash
# How can I repeat a character in Bash? How could I do this with `echo`? ``` perl -E 'say "=" x 100' ```
You can use: ``` printf '=%.0s' {1..100} ``` How this works: Bash expands `{1..100}` so the command becomes: ``` printf '=%.0s' 1 2 3 4 ... 100 ``` I've set printf's format to `=%.0s` which means that it will always print a single `=` no matter what argument it is given. Therefore it prints 100 `=`s. NB: To print...
9727695
if arguments is equal to this string, define a variable like this string
345
2012-03-15 20:31:29
<p>I am doing some bash script and now I got one variable call <code>source</code> and one array called <code>samples</code>, like this:</p> <pre><code>source='country' samples=(US Canada Mexico...) </code></pre> <p>as I want to expand the number of sources (and each source has its own samples) I tried to add some ar...
649,777
804,184
2022-12-24 20:26:35
9,727,942
593
2012-03-15 20:48:58
1,268,429
2012-03-15 20:48:58
https://stackoverflow.com/q/9727695
https://stackoverflow.com/a/9727942
<p>Don't forget about spaces:</p> <pre><code>source="" samples=("") if [ $1 = "country" ]; then source="country" samples="US Canada Mexico..." else echo "try again" fi </code></pre>
<p>Don't forget about spaces:</p> <pre><code>source="" samples=("") if [ $1 = "country" ]; then source="country" samples="US Canada Mexico..." else echo "try again" fi </code></pre>
387, 531, 2313
arguments, bash, scripting
<h1>if arguments is equal to this string, define a variable like this string</h1> <p>I am doing some bash script and now I got one variable call <code>source</code> and one array called <code>samples</code>, like this:</p> <pre><code>source='country' samples=(US Canada Mexico...) </code></pre> <p>as I want to expand ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
300
bash
# if arguments is equal to this string, define a variable like this string I am doing some bash script and now I got one variable call `source` and one array called `samples`, like this: ``` source='country' samples=(US Canada Mexico...) ``` as I want to expand the number of sources (and each source has its own samp...
Don't forget about spaces: ``` source="" samples=("") if [ $1 = "country" ]; then source="country" samples="US Canada Mexico..." else echo "try again" fi ```
1489277
How to use '-prune' option of 'find' in sh?
293
2009-09-28 20:47:00
<p>I don't quite understand the example given from the <code>man find</code>, can anyone give me some examples and explanations? Can I combine regular expression in it? </p> <hr> <p>The more detailed question is like this: </p> <p>Write a shell script, <code>changeall</code>, which has an interface like <code>change...
167,190
115,622
2021-12-30 14:50:54
1,489,405
593
2009-09-28 21:12:49
90,848
2020-09-13 12:55:15
https://stackoverflow.com/q/1489277
https://stackoverflow.com/a/1489405
<p>The thing I'd found confusing about <code>-prune</code> is that it's an action (like <code>-print</code>), not a test (like <code>-name</code>). It alters the &quot;to-do&quot; list, <em>but always returns true</em>.</p> <p>The general pattern for using <code>-prune</code> is this:</p> <pre><code>find [path] [condit...
<p>The thing I'd found confusing about <code>-prune</code> is that it's an action (like <code>-print</code>), not a test (like <code>-name</code>). It alters the &quot;to-do&quot; list, <em>but always returns true</em>.</p> <p>The general pattern for using <code>-prune</code> is this:</p> <pre><code>find [path] [condit...
18, 390, 10193, 10720
find, manual, regex, shell
<h1>How to use '-prune' option of 'find' in sh?</h1> <p>I don't quite understand the example given from the <code>man find</code>, can anyone give me some examples and explanations? Can I combine regular expression in it? </p> <hr> <p>The more detailed question is like this: </p> <p>Write a shell script, <code>chang...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
301
bash
# How to use '-prune' option of 'find' in sh? I don't quite understand the example given from the `man find`, can anyone give me some examples and explanations? Can I combine regular expression in it? --- The more detailed question is like this: Write a shell script, `changeall`, which has an interface like `change...
The thing I'd found confusing about `-prune` is that it's an action (like `-print`), not a test (like `-name`). It alters the "to-do" list, *but always returns true*. The general pattern for using `-prune` is this: ``` find [path] [conditions to prune] -prune -o \ [your usual conditions] [actions to perfo...
385408
Get program execution time in the shell
465
2008-12-22 02:27:41
<p>I want to execute something in a linux shell under a few different conditions, and be able to output the execution time of each execution.</p> <p>I know I could write a perl or python script that would do this, but is there a way I can do it in the shell? (which happens to be bash)</p>
521,018
41,613
2022-12-18 21:36:46
385,418
592
2008-12-22 02:31:17
25,222
2016-10-30 03:52:23
https://stackoverflow.com/q/385408
https://stackoverflow.com/a/385418
<p>Use the built-in <code>time</code> keyword:</p> <pre> $ help time time: time [-p] PIPELINE Execute PIPELINE and print a summary of the real time, user CPU time, and system CPU time spent executing PIPELINE when it terminates. The return status is the return status of PIPELINE. The `-p' option prin...
<p>Use the built-in <code>time</code> keyword:</p> <pre> $ help time time: time [-p] PIPELINE Execute PIPELINE and print a summary of the real time, user CPU time, and system CPU time spent executing PIPELINE when it terminates. The return status is the return status of PIPELINE. The `-p' option prin...
58, 387, 390
bash, linux, shell
<h1>Get program execution time in the shell</h1> <p>I want to execute something in a linux shell under a few different conditions, and be able to output the execution time of each execution.</p> <p>I know I could write a perl or python script that would do this, but is there a way I can do it in the shell? (which happ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
302
bash
# Get program execution time in the shell I want to execute something in a linux shell under a few different conditions, and be able to output the execution time of each execution. I know I could write a perl or python script that would do this, but is there a way I can do it in the shell? (which happens to be bash)
Use the built-in `time` keyword: ``` $ help time time: time [-p] PIPELINE Execute PIPELINE and print a summary of the real time, user CPU time, and system CPU time spent executing PIPELINE when it terminates. The return status is the return status of PIPELINE. The `-p' option prints the timing summar...
16034749
if, elif, else statement issues in Bash
463
2013-04-16 10:30:37
<p>I can't seem to work out what the issue with the following <code>if</code> statement is in regards to the <code>elif</code> and <code>then</code>. Keep in mind the <code>printf</code> is still under development I just haven't been able to test it yet in the statement so is more than likely wrong.</p> <p>The error I...
958,636
507,313
2021-10-21 03:34:38
16,034,814
589
2013-04-16 10:34:25
1,983,854
2021-10-21 03:34:38
https://stackoverflow.com/q/16034749
https://stackoverflow.com/a/16034814
<p>There is a space missing between <code>elif</code> and <code>[</code>:</p> <pre><code>elif[ &quot;$seconds&quot; -gt 0 ] </code></pre> <p>should be</p> <pre><code>elif [ &quot;$seconds&quot; -gt 0 ] </code></pre> <p>All together, the syntax to follow is:</p> <pre><code>if [ conditions ]; then # Things elif [ othe...
<p>There is a space missing between <code>elif</code> and <code>[</code>:</p> <pre><code>elif[ &quot;$seconds&quot; -gt 0 ] </code></pre> <p>should be</p> <pre><code>elif [ &quot;$seconds&quot; -gt 0 ] </code></pre> <p>All together, the syntax to follow is:</p> <pre><code>if [ conditions ]; then # Things elif [ othe...
387, 2773
bash, if-statement
<h1>if, elif, else statement issues in Bash</h1> <p>I can't seem to work out what the issue with the following <code>if</code> statement is in regards to the <code>elif</code> and <code>then</code>. Keep in mind the <code>printf</code> is still under development I just haven't been able to test it yet in the statement ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
303
bash
# if, elif, else statement issues in Bash I can't seem to work out what the issue with the following `if` statement is in regards to the `elif` and `then`. Keep in mind the `printf` is still under development I just haven't been able to test it yet in the statement so is more than likely wrong. The error I'm getting ...
There is a space missing between `elif` and `[`: ``` elif[ "$seconds" -gt 0 ] ``` should be ``` elif [ "$seconds" -gt 0 ] ``` All together, the syntax to follow is: ``` if [ conditions ]; then # Things elif [ other_conditions ]; then # Other things else # In case none of the above occurs fi ``` --- As I...
17144355
How can I replace every occurrence of a String in a file with PowerShell?
402
2013-06-17 09:29:35
<p>Using PowerShell, I want to replace all exact occurrences of <code>[MYID]</code> in a given file with <code>MyValue</code>. What is the easiest way to do so?</p>
635,295
373,674
2025-03-28 18:43:28
17,144,445
589
2013-06-17 09:35:15
381,149
2023-10-23 12:43:50
https://stackoverflow.com/q/17144355
https://stackoverflow.com/a/17144445
<p>Use (V3 version):</p> <pre><code>(Get-Content c:\temp\test.txt).Replace('[MYID]', 'MyValue') | Set-Content c:\temp\test.txt </code></pre> <p>Or for V2:</p> <pre><code>(Get-Content c:\temp\test.txt) -replace '\[MYID\]', 'MyValue' | Set-Content c:\temp\test.txt </code></pre>
<p>Use (V3 version):</p> <pre><code>(Get-Content c:\temp\test.txt).Replace('[MYID]', 'MyValue') | Set-Content c:\temp\test.txt </code></pre> <p>Or for V2:</p> <pre><code>(Get-Content c:\temp\test.txt) -replace '\[MYID\]', 'MyValue' | Set-Content c:\temp\test.txt </code></pre>
526
powershell
<h1>How can I replace every occurrence of a String in a file with PowerShell?</h1> <p>Using PowerShell, I want to replace all exact occurrences of <code>[MYID]</code> in a given file with <code>MyValue</code>. What is the easiest way to do so?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
304
bash
# How can I replace every occurrence of a String in a file with PowerShell? Using PowerShell, I want to replace all exact occurrences of `[MYID]` in a given file with `MyValue`. What is the easiest way to do so?
Use (V3 version): ``` (Get-Content c:\temp\test.txt).Replace('[MYID]', 'MyValue') | Set-Content c:\temp\test.txt ``` Or for V2: ``` (Get-Content c:\temp\test.txt) -replace '\[MYID\]', 'MyValue' | Set-Content c:\temp\test.txt ```
6958689
Running multiple commands with xargs
464
2011-08-05 15:22:14
<pre><code>cat a.txt | xargs -I % echo % </code></pre> <p>In the example above, <code>xargs</code> takes <code>echo %</code> as the command argument. But in some cases, I need multiple commands to process the argument instead of one. For example:</p> <pre><code>cat a.txt | xargs -I % {command1; command2; ... } </code><...
304,228
404,264
2025-08-11 03:11:38
6,958,957
586
2011-08-05 15:41:33
827,263
2019-10-01 01:50:32
https://stackoverflow.com/q/6958689
https://stackoverflow.com/a/6958957
<pre><code>cat a.txt | xargs -d $'\n' sh -c 'for arg do command1 "$arg"; command2 "$arg"; ...; done' _ </code></pre> <p>...or, without a <a href="http://www.catb.org/jargon/html/U/UUOC.html" rel="noreferrer">Useless Use Of cat</a>:</p> <pre><code>&lt;a.txt xargs -d $'\n' sh -c 'for arg do command1 "$arg"; command2 "$...
<pre><code>cat a.txt | xargs -d $'\n' sh -c 'for arg do command1 "$arg"; command2 "$arg"; ...; done' _ </code></pre> <p>...or, without a <a href="http://www.catb.org/jargon/html/U/UUOC.html" rel="noreferrer">Useless Use Of cat</a>:</p> <pre><code>&lt;a.txt xargs -d $'\n' sh -c 'for arg do command1 "$arg"; command2 "$...
387, 10326
bash, xargs
<h1>Running multiple commands with xargs</h1> <pre><code>cat a.txt | xargs -I % echo % </code></pre> <p>In the example above, <code>xargs</code> takes <code>echo %</code> as the command argument. But in some cases, I need multiple commands to process the argument instead of one. For example:</p> <pre><code>cat a.txt | ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
305
bash
# Running multiple commands with xargs ``` cat a.txt | xargs -I % echo % ``` In the example above, `xargs` takes `echo %` as the command argument. But in some cases, I need multiple commands to process the argument instead of one. For example: ``` cat a.txt | xargs -I % {command1; command2; ... } ``` But `xargs` do...
``` cat a.txt | xargs -d $'\n' sh -c 'for arg do command1 "$arg"; command2 "$arg"; ...; done' _ ``` ...or, without a [Useless Use Of cat](http://www.catb.org/jargon/html/U/UUOC.html): ``` <a.txt xargs -d $'\n' sh -c 'for arg do command1 "$arg"; command2 "$arg"; ...; done' _ ``` --- To explain some of the finer poin...
5274294
How can you run a command in bash over and over until success?
372
2011-03-11 14:41:26
<p>I have a script and want to ask the user for some information, but the script cannot continue until the user fills in this information. The following is my attempt at putting a command into a loop to achieve this but it doesn't work for some reason:</p> <pre class="lang-sh prettyprint-override"><code>echo &quot;Plea...
246,498
506,962
2024-11-15 03:06:11
5,274,386
583
2011-03-11 14:48:26
616,700
2020-05-11 16:19:47
https://stackoverflow.com/q/5274294
https://stackoverflow.com/a/5274386
<pre><code>until passwd do echo "Try again" done </code></pre> <p>or</p> <pre><code>while ! passwd do echo "Try again" done </code></pre>
<pre><code>until passwd do echo "Try again" done </code></pre> <p>or</p> <pre><code>while ! passwd do echo "Try again" done </code></pre>
387, 1796, 17310
bash, command, while-loop
<h1>How can you run a command in bash over and over until success?</h1> <p>I have a script and want to ask the user for some information, but the script cannot continue until the user fills in this information. The following is my attempt at putting a command into a loop to achieve this but it doesn't work for some rea...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
306
bash
# How can you run a command in bash over and over until success? I have a script and want to ask the user for some information, but the script cannot continue until the user fills in this information. The following is my attempt at putting a command into a loop to achieve this but it doesn't work for some reason: ```...
``` until passwd do echo "Try again" done ``` or ``` while ! passwd do echo "Try again" done ```
618378
Select unique or distinct values from a list in UNIX shell script
333
2009-03-06 10:33:38
<p>I have a ksh script that returns a long list of values, newline separated, and I want to see only the unique/distinct values. It is possible to do this?</p> <p>For example, say my output is file suffixes in a directory:</p> <blockquote> <pre><code>tar gz java gz java tar class class </code></pre> </blockquote> <p...
443,681
2,362
2021-08-28 06:16:02
618,381
581
2009-03-06 10:34:51
15,537
2009-03-06 10:48:23
https://stackoverflow.com/q/618378
https://stackoverflow.com/a/618381
<p>You might want to look at the <code>uniq</code> and <code>sort</code> applications.</p> <pre> ./yourscript.ksh | sort | uniq </pre> <p>(FYI, yes, the sort is necessary in this command line, <code>uniq</code> only strips duplicate lines that are immediately after each other)</p> <p><strong>EDIT:</strong></p> <p>C...
<p>You might want to look at the <code>uniq</code> and <code>sort</code> applications.</p> <pre> ./yourscript.ksh | sort | uniq </pre> <p>(FYI, yes, the sort is necessary in this command line, <code>uniq</code> only strips duplicate lines that are immediately after each other)</p> <p><strong>EDIT:</strong></p> <p>C...
387, 1964, 6205, 9525, 10327
bash, distinct, ksh, sh, unique
<h1>Select unique or distinct values from a list in UNIX shell script</h1> <p>I have a ksh script that returns a long list of values, newline separated, and I want to see only the unique/distinct values. It is possible to do this?</p> <p>For example, say my output is file suffixes in a directory:</p> <blockquote> <pr...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
307
bash
# Select unique or distinct values from a list in UNIX shell script I have a ksh script that returns a long list of values, newline separated, and I want to see only the unique/distinct values. It is possible to do this? For example, say my output is file suffixes in a directory: > ``` > tar > gz > java > gz > java ...
You might want to look at the `uniq` and `sort` applications. ``` ./yourscript.ksh | sort | uniq ``` (FYI, yes, the sort is necessary in this command line, `uniq` only strips duplicate lines that are immediately after each other) **EDIT:** Contrary to what has been posted by [Aaron Digulla](https://stackoverflow.co...
8967902
Why do you need to put #!/bin/bash at the beginning of a script file?
707
2012-01-23 06:24:31
<p>I have made <a href="http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29" rel="noreferrer">Bash</a> scripts before and they all ran fine without <code>#!/bin/bash</code> at the beginning.</p> <p>What's the point of putting it in? Would things be any different?</p> <p>Also, how do you pronounce <code>#</code>? I kno...
961,878
404,020
2024-04-03 14:57:59
8,967,916
578
2012-01-23 06:26:04
421,195
2017-09-26 17:00:30
https://stackoverflow.com/q/8967902
https://stackoverflow.com/a/8967916
<p>It's a convention so the *nix shell knows what kind of interpreter to run.</p> <p>For example, older flavors of ATT defaulted to <em>sh</em> (the Bourne shell), while older versions of BSD defaulted to <em>csh</em> (the C shell).</p> <p>Even today (where most systems run bash, the <em>"Bourne Again Shell"</em>), s...
<p>It's a convention so the *nix shell knows what kind of interpreter to run.</p> <p>For example, older flavors of ATT defaulted to <em>sh</em> (the Bourne shell), while older versions of BSD defaulted to <em>csh</em> (the C shell).</p> <p>Even today (where most systems run bash, the <em>"Bourne Again Shell"</em>), s...
58, 387, 531, 8780
bash, linux, scripting, shebang
<h1>Why do you need to put #!/bin/bash at the beginning of a script file?</h1> <p>I have made <a href="http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29" rel="noreferrer">Bash</a> scripts before and they all ran fine without <code>#!/bin/bash</code> at the beginning.</p> <p>What's the point of putting it in? Would th...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
308
bash
# Why do you need to put #!/bin/bash at the beginning of a script file? I have made [Bash](http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) scripts before and they all ran fine without `#!/bin/bash` at the beginning. What's the point of putting it in? Would things be any different? Also, how do you pronounce `#`?...
It's a convention so the *nix shell knows what kind of interpreter to run. For example, older flavors of ATT defaulted to *sh* (the Bourne shell), while older versions of BSD defaulted to *csh* (the C shell). Even today (where most systems run bash, the *"Bourne Again Shell"*), scripts can be in bash, python, perl, r...
4837673
How to execute mongo commands through shell scripts?
505
2011-01-29 15:26:01
<p>I want to execute <code>mongo</code> commands in shell script, e.g. in a script <code>test.sh</code>:</p> <pre><code>#!/bin/sh mongo myDbName db.mycollection.findOne() show collections </code></pre> <p>When I execute this script via <code>./test.sh</code>, then the connection to MongoDB is established, but the fol...
541,000
297,907
2023-10-19 06:12:27
6,000,360
578
2011-05-14 07:12:33
174,936
2019-05-24 10:52:42
https://stackoverflow.com/q/4837673
https://stackoverflow.com/a/6000360
<p>You can also evaluate a command using the <code>--eval</code> flag, if it is just a single command.</p> <pre><code>mongo --eval "printjson(db.serverStatus())" </code></pre> <p><strong>Please note:</strong> if you are using Mongo operators, starting with a $ sign, you'll want to surround the eval argument in single...
<p>You can also evaluate a command using the <code>--eval</code> flag, if it is just a single command.</p> <pre><code>mongo --eval "printjson(db.serverStatus())" </code></pre> <p><strong>Please note:</strong> if you are using Mongo operators, starting with a $ sign, you'll want to surround the eval argument in single...
387, 390, 10327, 30073
bash, mongodb, sh, shell
<h1>How to execute mongo commands through shell scripts?</h1> <p>I want to execute <code>mongo</code> commands in shell script, e.g. in a script <code>test.sh</code>:</p> <pre><code>#!/bin/sh mongo myDbName db.mycollection.findOne() show collections </code></pre> <p>When I execute this script via <code>./test.sh</cod...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
309
bash
# How to execute mongo commands through shell scripts? I want to execute `mongo` commands in shell script, e.g. in a script `test.sh`: ``` #!/bin/sh mongo myDbName db.mycollection.findOne() show collections ``` When I execute this script via `./test.sh`, then the connection to MongoDB is established, but the followi...
You can also evaluate a command using the `--eval` flag, if it is just a single command. ``` mongo --eval "printjson(db.serverStatus())" ``` **Please note:** if you are using Mongo operators, starting with a $ sign, you'll want to surround the eval argument in single quotes to keep the shell from evaluating the opera...
22101778
How to preserve line breaks when storing command output to a variable?
307
2014-02-28 17:22:16
<p>I’m using bash shell on Linux. I have this simple script …</p> <pre><code>#!/bin/bash TEMP=`sed -n '/'"Starting deployment of"'/,/'"Failed to start context"'/p' "/usr/java/jboss/standalone/log/server.log" | tac | awk '/'"Starting deployment of"'/ {print;exit} 1' | tac` echo $TEMP </code></pre> <p>However, when I...
162,335
1,235,929
2023-08-09 21:17:55
22,101,842
575
2014-02-28 17:25:31
970,195
2023-08-09 21:17:55
https://stackoverflow.com/q/22101778
https://stackoverflow.com/a/22101842
<p>With shell scripting, one needs to always <strong>quote</strong> variables, especially when working with strings.</p> <p>Here is an example of the problem:</p> <p><em>Example variable:</em></p> <pre><code>$ f=&quot;fafafda &gt; adffd &gt; adfadf &gt; adfafd &gt; afd&quot; </code></pre> <p><strong>Output without quot...
<p>With shell scripting, one needs to always <strong>quote</strong> variables, especially when working with strings.</p> <p>Here is an example of the problem:</p> <p><em>Example variable:</em></p> <pre><code>$ f=&quot;fafafda &gt; adffd &gt; adfadf &gt; adfafd &gt; afd&quot; </code></pre> <p><strong>Output without quot...
58, 387, 390, 9313
bash, line-breaks, linux, shell
<h1>How to preserve line breaks when storing command output to a variable?</h1> <p>I’m using bash shell on Linux. I have this simple script …</p> <pre><code>#!/bin/bash TEMP=`sed -n '/'"Starting deployment of"'/,/'"Failed to start context"'/p' "/usr/java/jboss/standalone/log/server.log" | tac | awk '/'"Starting depl...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
310
bash
# How to preserve line breaks when storing command output to a variable? I’m using bash shell on Linux. I have this simple script … ``` #!/bin/bash TEMP=`sed -n '/'"Starting deployment of"'/,/'"Failed to start context"'/p' "/usr/java/jboss/standalone/log/server.log" | tac | awk '/'"Starting deployment of"'/ {print;e...
With shell scripting, one needs to always **quote** variables, especially when working with strings. Here is an example of the problem: *Example variable:* ``` $ f="fafafda > adffd > adfadf > adfafd > afd" ``` **Output without quoting the variable:** ``` $ echo $f fafafda adffd adfadf adfafd afd ``` **Output WITH...
3224878
What is the purpose of the : (colon) GNU Bash builtin?
488
2010-07-11 22:30:25
<p>What is the purpose of a command that does nothing, being little more than a comment leader, but is actually a shell builtin in and of itself?</p> <p>It's slower than inserting a comment into your scripts by about 40% per call, which probably varies greatly depending on the size of the comment. The only possible rea...
210,397
237,955
2025-03-18 07:04:41
3,224,910
574
2010-07-11 22:42:16
135,724
2015-06-04 23:52:28
https://stackoverflow.com/q/3224878
https://stackoverflow.com/a/3224910
<p><strong>Historically</strong>, Bourne shells didn't have <code>true</code> and <code>false</code> as built-in commands. <code>true</code> was instead simply aliased to <code>:</code>, and <code>false</code> to something like <code>let 0</code>.</p> <p><code>:</code> is slightly better than <code>true</code> for por...
<p><strong>Historically</strong>, Bourne shells didn't have <code>true</code> and <code>false</code> as built-in commands. <code>true</code> was instead simply aliased to <code>:</code>, and <code>false</code> to something like <code>let 0</code>.</p> <p><code>:</code> is slightly better than <code>true</code> for por...
387, 390, 14334
bash, built-in, shell
<h1>What is the purpose of the : (colon) GNU Bash builtin?</h1> <p>What is the purpose of a command that does nothing, being little more than a comment leader, but is actually a shell builtin in and of itself?</p> <p>It's slower than inserting a comment into your scripts by about 40% per call, which probably varies gre...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
311
bash
# What is the purpose of the : (colon) GNU Bash builtin? What is the purpose of a command that does nothing, being little more than a comment leader, but is actually a shell builtin in and of itself? It's slower than inserting a comment into your scripts by about 40% per call, which probably varies greatly depending ...
**Historically**, Bourne shells didn't have `true` and `false` as built-in commands. `true` was instead simply aliased to `:`, and `false` to something like `let 0`. `:` is slightly better than `true` for portability to ancient Bourne-derived shells. As a simple example, consider having neither the `!` pipeline operat...
2871181
Replacing some characters in a string with another character
498
2010-05-20 05:18:39
<p>I have a string like <code>AxxBCyyyDEFzzLMN</code> and I want to replace all the occurrences of <code>x</code>, <code>y</code>, and <code>z</code> with <code>_</code>.</p> <p>How can I achieve this?</p> <p>I know that <code>echo &quot;$string&quot; | tr 'x' '_' | tr 'y' '_'</code> would work, but I want to do that i...
944,193
302,351
2022-09-01 15:46:29
2,871,217
573
2010-05-20 05:27:28
329,467
2014-08-23 23:29:48
https://stackoverflow.com/q/2871181
https://stackoverflow.com/a/2871217
<pre><code>echo "$string" | tr xyz _ </code></pre> <p>would replace each occurrence of <code>x</code>, <code>y</code>, or <code>z</code> with <code>_</code>, giving <code>A__BC___DEF__LMN</code> in your example.</p> <pre><code>echo "$string" | sed -r 's/[xyz]+/_/g' </code></pre> <p>would replace repeating occurrence...
<pre><code>echo "$string" | tr xyz _ </code></pre> <p>would replace each occurrence of <code>x</code>, <code>y</code>, or <code>z</code> with <code>_</code>, giving <code>A__BC___DEF__LMN</code> in your example.</p> <pre><code>echo "$string" | sed -r 's/[xyz]+/_/g' </code></pre> <p>would replace repeating occurrence...
139, 387
bash, string
<h1>Replacing some characters in a string with another character</h1> <p>I have a string like <code>AxxBCyyyDEFzzLMN</code> and I want to replace all the occurrences of <code>x</code>, <code>y</code>, and <code>z</code> with <code>_</code>.</p> <p>How can I achieve this?</p> <p>I know that <code>echo &quot;$string&quot...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
312
bash
# Replacing some characters in a string with another character I have a string like `AxxBCyyyDEFzzLMN` and I want to replace all the occurrences of `x`, `y`, and `z` with `_`. How can I achieve this? I know that `echo "$string" | tr 'x' '_' | tr 'y' '_'` would work, but I want to do that in one go, without using pip...
``` echo "$string" | tr xyz _ ``` would replace each occurrence of `x`, `y`, or `z` with `_`, giving `A__BC___DEF__LMN` in your example. ``` echo "$string" | sed -r 's/[xyz]+/_/g' ``` would replace repeating occurrences of `x`, `y`, or `z` with a single `_`, giving `A_BC_DEF_LMN` in your example.
3822621
How to exit if a command failed?
315
2010-09-29 14:32:37
<p>I am a noob in shell-scripting. I want to print a message and exit my script if a command fails. I've tried:</p> <pre><code>my_command &amp;&amp; (echo 'my_command failed; exit) </code></pre> <p>but it does not work. It keeps executing the instructions following this line in the script. I'm using Ubuntu and bash.<...
260,904
459,246
2022-07-10 11:07:14
3,822,649
573
2010-09-29 14:35:17
227,665
2013-01-30 17:16:31
https://stackoverflow.com/q/3822621
https://stackoverflow.com/a/3822649
<p>Try:</p> <pre><code>my_command || { echo 'my_command failed' ; exit 1; } </code></pre> <p>Four changes:</p> <ul> <li>Change <code>&amp;&amp;</code> to <code>||</code></li> <li>Use <code>{ }</code> in place of <code>( )</code></li> <li>Introduce <code>;</code> after <code>exit</code> and</li> <li>spaces after <cod...
<p>Try:</p> <pre><code>my_command || { echo 'my_command failed' ; exit 1; } </code></pre> <p>Four changes:</p> <ul> <li>Change <code>&amp;&amp;</code> to <code>||</code></li> <li>Use <code>{ }</code> in place of <code>( )</code></li> <li>Introduce <code>;</code> after <code>exit</code> and</li> <li>spaces after <cod...
58, 387, 10871, 54576
bash, exit, exitstatus, linux
<h1>How to exit if a command failed?</h1> <p>I am a noob in shell-scripting. I want to print a message and exit my script if a command fails. I've tried:</p> <pre><code>my_command &amp;&amp; (echo 'my_command failed; exit) </code></pre> <p>but it does not work. It keeps executing the instructions following this line ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
313
bash
# How to exit if a command failed? I am a noob in shell-scripting. I want to print a message and exit my script if a command fails. I've tried: ``` my_command && (echo 'my_command failed; exit) ``` but it does not work. It keeps executing the instructions following this line in the script. I'm using Ubuntu and bash.
Try: ``` my_command || { echo 'my_command failed' ; exit 1; } ``` Four changes: - Change `&&` to `||` - Use `{ }` in place of `( )` - Introduce `;` after `exit` and - spaces after `{` and before `}` Since you want to print the message and exit only when the command fails ( exits with non-zero value) you need a `||`...
13046192
How to change the default shell in Linux?
413
2012-10-24 09:15:37
<p>How is it possible to change the default shell? The <code>env</code> command currently says:</p> <pre><code>SHELL=/bin/tcsh </code></pre> <p>and I want to change that to Bash.</p>
808,901
859,227
2024-05-05 14:13:11
13,046,283
572
2012-10-24 09:21:12
258,355
2018-03-09 18:42:17
https://stackoverflow.com/q/13046192
https://stackoverflow.com/a/13046283
<p>Try linux command <code>chsh</code>.</p> <p>The detailed command is <code>chsh -s /bin/bash</code>. It will prompt you to enter your password. Your default login shell is <code>/bin/bash</code> now. <strong>You must log out and log back in to see this change.</strong></p> <p>The following is quoted from man page:<...
<p>Try linux command <code>chsh</code>.</p> <p>The detailed command is <code>chsh -s /bin/bash</code>. It will prompt you to enter your password. Your default login shell is <code>/bin/bash</code> now. <strong>You must log out and log back in to see this change.</strong></p> <p>The following is quoted from man page:<...
58, 387, 390, 421, 8599
bash, environment, linux, shell, tcsh
<h1>How to change the default shell in Linux?</h1> <p>How is it possible to change the default shell? The <code>env</code> command currently says:</p> <pre><code>SHELL=/bin/tcsh </code></pre> <p>and I want to change that to Bash.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
314
bash
# How to change the default shell in Linux? How is it possible to change the default shell? The `env` command currently says: ``` SHELL=/bin/tcsh ``` and I want to change that to Bash.
Try linux command `chsh`. The detailed command is `chsh -s /bin/bash`. It will prompt you to enter your password. Your default login shell is `/bin/bash` now. **You must log out and log back in to see this change.** The following is quoted from man page: > The chsh command changes the user login shell. This determin...
6381229
How to kill all processes matching a name?
347
2011-06-17 04:07:56
<p>Say I want to kill every process containing the word amarok. I can print out the commands I want to execute. But how do I actually make the shell execute them. ie.</p> <pre><code>ps aux | grep -ie amarok | awk '{print "kill -9 " $2}' Output: kill -9 3052 kill -9 3071 kill -9 3076 kill -9 3077 kill -9 3079 kill -9 3...
356,420
357,198
2025-07-25 15:20:26
6,381,266
571
2011-06-17 04:16:19
263,969
2011-06-17 04:16:19
https://stackoverflow.com/q/6381229
https://stackoverflow.com/a/6381266
<p>From <code>man 1 pkill</code></p> <pre><code>-f The pattern is normally only matched against the process name. When -f is set, the full command line is used. </code></pre> <p>Which means, for example, if we see these lines in <code>ps aux</code>:</p> <pre><code>apache 24268 0.0 2.6 388152 27116 ? ...
<p>From <code>man 1 pkill</code></p> <pre><code>-f The pattern is normally only matched against the process name. When -f is set, the full command line is used. </code></pre> <p>Which means, for example, if we see these lines in <code>ps aux</code>:</p> <pre><code>apache 24268 0.0 2.6 388152 27116 ? ...
390, 1231, 5779
command-line, kill, shell
<h1>How to kill all processes matching a name?</h1> <p>Say I want to kill every process containing the word amarok. I can print out the commands I want to execute. But how do I actually make the shell execute them. ie.</p> <pre><code>ps aux | grep -ie amarok | awk '{print "kill -9 " $2}' Output: kill -9 3052 kill -9 3...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
315
bash
# How to kill all processes matching a name? Say I want to kill every process containing the word amarok. I can print out the commands I want to execute. But how do I actually make the shell execute them. ie. ``` ps aux | grep -ie amarok | awk '{print "kill -9 " $2}' Output: kill -9 3052 kill -9 3071 kill -9 3076 kil...
From `man 1 pkill` ``` -f The pattern is normally only matched against the process name. When -f is set, the full command line is used. ``` Which means, for example, if we see these lines in `ps aux`: ``` apache 24268 0.0 2.6 388152 27116 ? S Jun13 0:10 /usr/sbin/httpd apache 24272 0.0 ...