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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
8055501 | How to copy in bash all directory and files recursive? | 246 | 2011-11-08 18:42:16 | <p>I have script:</p>
<pre><code>find ./SourceFolder/ -maxdepth 4 -exec cp -R '{}' ./DestFolder/ \;
</code></pre>
<p><code>SourceDir</code> contains also sub-folders.</p>
<p>Problem that in <code>DestFolder</code> not only all tree, but in up level all another levels and files.</p>
<p>How to fix ?</p>
| 290,166 | 710,818 | 2022-02-10 07:25:19 | 8,055,544 | 459 | 2011-11-08 18:45:54 | 643,827 | 2011-11-08 18:45:54 | https://stackoverflow.com/q/8055501 | https://stackoverflow.com/a/8055544 | <pre><code>cp -r ./SourceFolder ./DestFolder
</code></pre>
| <pre><code>cp -r ./SourceFolder ./DestFolder </code></pre> | 218, 387, 4330 | bash, copy, directory | <h1>How to copy in bash all directory and files recursive?</h1>
<p>I have script:</p>
<pre><code>find ./SourceFolder/ -maxdepth 4 -exec cp -R '{}' ./DestFolder/ \;
</code></pre>
<p><code>SourceDir</code> contains also sub-folders.</p>
<p>Problem that in <code>DestFolder</code> not only all tree, but in up level all ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 417 | bash | # How to copy in bash all directory and files recursive?
I have script:
```
find ./SourceFolder/ -maxdepth 4 -exec cp -R '{}' ./DestFolder/ \;
```
`SourceDir` contains also sub-folders.
Problem that in `DestFolder` not only all tree, but in up level all another levels and files.
How to fix ? | ```
cp -r ./SourceFolder ./DestFolder
``` |
12331633 | How to gzip all files in all sub-directories into one compressed file in bash | 239 | 2012-09-08 14:32:35 | <blockquote>
<p><strong>Possible Duplicate:</strong><br>
<a href="https://stackoverflow.com/questions/3341131/gzipping-up-a-set-of-directories-and-creating-a-tar-compressed-file">gzipping up a set of directories and creating a tar compressed file</a> </p>
</blockquote>
<p><a href="https://stackoverflow.com/ques... | 481,959 | 787,794 | 2016-03-04 16:56:53 | 12,331,698 | 459 | 2012-09-08 14:40:20 | 1,143,309 | 2014-10-20 21:34:38 | https://stackoverflow.com/q/12331633 | https://stackoverflow.com/a/12331698 | <pre><code>tar -zcvf compressFileName.tar.gz folderToCompress
</code></pre>
<p>everything in folderToCompress will go to compressFileName</p>
<p>Edit: After review and comments I realized that people may get confused with compressFileName without an extension. If you want you can use .tar.gz extension(as suggested) w... | <pre><code>tar -zcvf compressFileName.tar.gz folderToCompress </code></pre> <p>everything in folderToCompress will go to compressFileName</p> <p>Edit: After review and comments I realized that people may get confused with compressFileName without an extension. If you want you can use .tar.gz extension(as suggested) w... | 58, 387, 390, 1930 | bash, gzip, linux, shell | <h1>How to gzip all files in all sub-directories into one compressed file in bash</h1>
<blockquote>
<p><strong>Possible Duplicate:</strong><br>
<a href="https://stackoverflow.com/questions/3341131/gzipping-up-a-set-of-directories-and-creating-a-tar-compressed-file">gzipping up a set of directories and creating a ta... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 418 | bash | # How to gzip all files in all sub-directories into one compressed file in bash
> **Possible Duplicate:**
> [gzipping up a set of directories and creating a tar compressed file](https://stackoverflow.com/questions/3341131/gzipping-up-a-set-of-directories-and-creating-a-tar-compressed-file)
[This post](https://stack... | ```
tar -zcvf compressFileName.tar.gz folderToCompress
```
everything in folderToCompress will go to compressFileName
Edit: After review and comments I realized that people may get confused with compressFileName without an extension. If you want you can use .tar.gz extension(as suggested) with the compressFileName |
44104043 | How can I see the command history across all PowerShell sessions in Windows Server 2016? | 242 | 2017-05-22 03:05:27 | <p>Where can you view the full history from all sessions in Windows Server 2016?</p>
<p>The following PowerShell command only includes the commands from the current session:</p>
<pre><code>Get-History
</code></pre>
| 267,899 | 6,611,106 | 2022-05-19 15:47:35 | 44,104,044 | 457 | 2017-05-22 03:05:27 | 6,611,106 | 2018-12-18 07:03:44 | https://stackoverflow.com/q/44104043 | https://stackoverflow.com/a/44104044 | <p>In PowerShell enter the following command:</p>
<pre><code>(Get-PSReadlineOption).HistorySavePath
</code></pre>
<p>This gives you the path where all of the history is saved. Then open the path in a text editor.</p>
<p>Try <code>cat (Get-PSReadlineOption).HistorySavePath</code> to list the history in PowerShell.</p... | <p>In PowerShell enter the following command:</p> <pre><code>(Get-PSReadlineOption).HistorySavePath </code></pre> <p>This gives you the path where all of the history is saved. Then open the path in a text editor.</p> <p>Try <code>cat (Get-PSReadlineOption).HistorySavePath</code> to list the history in PowerShell.</p... | 526, 114816 | powershell, windows-server-2016 | <h1>How can I see the command history across all PowerShell sessions in Windows Server 2016?</h1>
<p>Where can you view the full history from all sessions in Windows Server 2016?</p>
<p>The following PowerShell command only includes the commands from the current session:</p>
<pre><code>Get-History
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 419 | bash | # How can I see the command history across all PowerShell sessions in Windows Server 2016?
Where can you view the full history from all sessions in Windows Server 2016?
The following PowerShell command only includes the commands from the current session:
```
Get-History
``` | In PowerShell enter the following command:
```
(Get-PSReadlineOption).HistorySavePath
```
This gives you the path where all of the history is saved. Then open the path in a text editor.
Try `cat (Get-PSReadlineOption).HistorySavePath` to list the history in PowerShell. |
8938120 | How to run cron once, daily at 10pm | 246 | 2012-01-20 07:35:08 | <p>I had entered:</p>
<pre><code>* 22 * * * test > /dev/null
</code></pre>
<p>However, I am being notified via email that this is running every minute.
I am confused I guess because I thought this was correct for what I am wanting.</p>
| 514,189 | null | 2021-02-15 04:06:21 | 8,938,143 | 454 | 2012-01-20 07:37:05 | 598,442 | 2019-06-03 07:01:42 | https://stackoverflow.com/q/8938120 | https://stackoverflow.com/a/8938143 | <p>It's running every minute of the hour 22 I guess. Try the following to run it every first minute of the hour 22:</p>
<pre><code>0 22 * * * ....
</code></pre>
| <p>It's running every minute of the hour 22 I guess. Try the following to run it every first minute of the hour 22:</p> <pre><code>0 22 * * * .... </code></pre> | 387, 601, 5163, 9299 | bash, cron, scheduled-tasks, system | <h1>How to run cron once, daily at 10pm</h1>
<p>I had entered:</p>
<pre><code>* 22 * * * test > /dev/null
</code></pre>
<p>However, I am being notified via email that this is running every minute.
I am confused I guess because I thought this was correct for what I am wanting.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 420 | bash | # How to run cron once, daily at 10pm
I had entered:
```
* 22 * * * test > /dev/null
```
However, I am being notified via email that this is running every minute.
I am confused I guess because I thought this was correct for what I am wanting. | It's running every minute of the hour 22 I guess. Try the following to run it every first minute of the hour 22:
```
0 22 * * * ....
``` |
3925337 | find without recursion | 300 | 2010-10-13 15:31:15 | <p>Is it possible to use the <code>find</code> command in some way that it will not recurse into the sub-directories? For example,</p>
<pre><code>DirsRoot
|-->SubDir1
| |-OtherFile1
|-->SubDir2
| |-OtherFile2
|-File1
|-File2
</code></pre>
<p>And the result of something like <code>find DirsRoot -... | 175,180 | 323,129 | 2021-07-13 07:45:48 | 3,925,376 | 453 | 2010-10-13 15:35:25 | 390,989 | 2020-08-19 20:34:13 | https://stackoverflow.com/q/3925337 | https://stackoverflow.com/a/3925376 | <p>I think you'll get what you want with the <code>-maxdepth 1</code> option, based on your current command structure. If not, you can try looking at the <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?find" rel="noreferrer">man page</a> for <code>find</code>.</p>
<p>Relevant entry (for convenience's sake):</p>
<pre clas... | <p>I think you'll get what you want with the <code>-maxdepth 1</code> option, based on your current command structure. If not, you can try looking at the <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?find" rel="noreferrer">man page</a> for <code>find</code>.</p> <p>Relevant entry (for convenience's sake):</p> <pre clas... | 34, 390, 10193 | find, shell, unix | <h1>find without recursion</h1>
<p>Is it possible to use the <code>find</code> command in some way that it will not recurse into the sub-directories? For example,</p>
<pre><code>DirsRoot
|-->SubDir1
| |-OtherFile1
|-->SubDir2
| |-OtherFile2
|-File1
|-File2
</code></pre>
<p>And the result of some... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 421 | bash | # find without recursion
Is it possible to use the `find` command in some way that it will not recurse into the sub-directories? For example,
```
DirsRoot
|-->SubDir1
| |-OtherFile1
|-->SubDir2
| |-OtherFile2
|-File1
|-File2
```
And the result of something like `find DirsRoot --do-not-recurse -type... | I think you'll get what you want with the `-maxdepth 1` option, based on your current command structure. If not, you can try looking at the [man page](http://unixhelp.ed.ac.uk/CGI/man-cgi?find) for `find`.
Relevant entry (for convenience's sake):
```
-maxdepth levels
Descend at most levels (a non-negative i... |
791765 | Unable to forward search Bash history similarly as with CTRL-r | 288 | 2009-04-26 22:53:57 | <p>I am trying to search my bash history similarly as with <kbd>CTRL</kbd>-<kbd>r</kbd>, but to forward direction.</p>
<p>It has been a pain for me, when I just hit once too often <kbd>CTRL</kbd>-<kbd>r</kbd>, to find the previous command again.</p>
<p>How can you forward search your Bash history similarly as in reve... | 92,579 | 54,964 | 2023-06-13 17:40:05 | 791,800 | 453 | 2009-04-26 23:09:34 | 23,118 | 2011-06-20 17:25:06 | https://stackoverflow.com/q/791765 | https://stackoverflow.com/a/791800 | <p>You can search forward as well. From the bash info manual, "<a href="http://www.gnu.org/software/bash/manual/html_node/Searching.html" rel="noreferrer">8.2.5 Searching for Commands in the History</a>":</p>
<blockquote>
<p>To search backward in the history for a particular string, type <code>C-r</code>.
Typing <... | <p>You can search forward as well. From the bash info manual, "<a href="http://www.gnu.org/software/bash/manual/html_node/Searching.html" rel="noreferrer">8.2.5 Searching for Commands in the History</a>":</p> <blockquote> <p>To search backward in the history for a particular string, type <code>C-r</code>. Typing <... | 387, 816 | bash, search | <h1>Unable to forward search Bash history similarly as with CTRL-r</h1>
<p>I am trying to search my bash history similarly as with <kbd>CTRL</kbd>-<kbd>r</kbd>, but to forward direction.</p>
<p>It has been a pain for me, when I just hit once too often <kbd>CTRL</kbd>-<kbd>r</kbd>, to find the previous command again.</... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 422 | bash | # Unable to forward search Bash history similarly as with CTRL-r
I am trying to search my bash history similarly as with `CTRL`-`r`, but to forward direction.
It has been a pain for me, when I just hit once too often `CTRL`-`r`, to find the previous command again.
How can you forward search your Bash history similar... | You can search forward as well. From the bash info manual, "[8.2.5 Searching for Commands in the History](http://www.gnu.org/software/bash/manual/html_node/Searching.html)":
> To search backward in the history for a particular string, type `C-r`.
> Typing `C-s` searches forward through the history.
The problem with C... |
926069 | Add up a column of numbers at the Unix shell | 242 | 2009-05-29 13:50:41 | <p>Given a list of files in <code>files.txt</code>, I can get a list of their sizes like this:</p>
<pre><code>cat files.txt | xargs ls -l | cut -c 23-30
</code></pre>
<p>which produces something like this:</p>
<pre><code> 151552
319488
1536000
225280
</code></pre>
<p>How can I get the <strong>total</strong> o... | 232,004 | 21,886 | 2023-12-05 05:53:38 | 18,141,152 | 453 | 2013-08-09 06:27:15 | 154,770 | 2015-01-17 05:09:11 | https://stackoverflow.com/q/926069 | https://stackoverflow.com/a/18141152 | <pre><code>... | paste -sd+ - | bc
</code></pre>
<p>is the shortest one I've found (from the <a href="http://www.unixcl.com/2009/11/sum-of-numbers-in-file-unix.html">UNIX Command Line</a> blog).</p>
<p><strong>Edit:</strong> added the <code>-</code> argument for portability, thanks @Dogbert and @Owen.</p>
| <pre><code>... | paste -sd+ - | bc </code></pre> <p>is the shortest one I've found (from the <a href="http://www.unixcl.com/2009/11/sum-of-numbers-in-file-unix.html">UNIX Command Line</a> blog).</p> <p><strong>Edit:</strong> added the <code>-</code> argument for portability, thanks @Dogbert and @Owen.</p> | 34, 58, 390 | linux, shell, unix | <h1>Add up a column of numbers at the Unix shell</h1>
<p>Given a list of files in <code>files.txt</code>, I can get a list of their sizes like this:</p>
<pre><code>cat files.txt | xargs ls -l | cut -c 23-30
</code></pre>
<p>which produces something like this:</p>
<pre><code> 151552
319488
1536000
225280
</code... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 423 | bash | # Add up a column of numbers at the Unix shell
Given a list of files in `files.txt`, I can get a list of their sizes like this:
```
cat files.txt | xargs ls -l | cut -c 23-30
```
which produces something like this:
```
151552
319488
1536000
225280
```
How can I get the **total** of all those numbers? | ```
... | paste -sd+ - | bc
```
is the shortest one I've found (from the [UNIX Command Line](http://www.unixcl.com/2009/11/sum-of-numbers-in-file-unix.html) blog).
**Edit:** added the `-` argument for portability, thanks @Dogbert and @Owen. |
1447625 | List files with certain extensions with ls and grep | 217 | 2009-09-19 03:21:21 | <p>I just want to get the files from the current dir and only output .mp4 .mp3 .exe files nothing else.
So I thought I could just do this:</p>
<pre><code>ls | grep \.mp4$ | grep \.mp3$ | grep \.exe$
</code></pre>
<p>But no, as the first grep will output just mp4's therefor the other 2 grep's won't be used.</p>
<p>An... | 366,851 | 138,541 | 2020-11-10 03:04:04 | 1,447,974 | 453 | 2009-09-19 06:45:15 | 145,190 | 2009-09-19 06:45:15 | https://stackoverflow.com/q/1447625 | https://stackoverflow.com/a/1447974 | <p>Why not:</p>
<pre><code>ls *.{mp3,exe,mp4}
</code></pre>
<p>I'm not sure where I learned it - but I've been using this.</p>
| <p>Why not:</p> <pre><code>ls *.{mp3,exe,mp4} </code></pre> <p>I'm not sure where I learned it - but I've been using this.</p> | 369, 387, 390, 1271 | bash, grep, macos, shell | <h1>List files with certain extensions with ls and grep</h1>
<p>I just want to get the files from the current dir and only output .mp4 .mp3 .exe files nothing else.
So I thought I could just do this:</p>
<pre><code>ls | grep \.mp4$ | grep \.mp3$ | grep \.exe$
</code></pre>
<p>But no, as the first grep will output jus... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 424 | bash | # List files with certain extensions with ls and grep
I just want to get the files from the current dir and only output .mp4 .mp3 .exe files nothing else.
So I thought I could just do this:
```
ls | grep \.mp4$ | grep \.mp3$ | grep \.exe$
```
But no, as the first grep will output just mp4's therefor the other 2 grep... | Why not:
```
ls *.{mp3,exe,mp4}
```
I'm not sure where I learned it - but I've been using this. |
13296863 | Difference between wait and sleep | 384 | 2012-11-08 20:04:50 | <p>What is difference between <code>wait</code> and <code>sleep</code>?</p>
| 585,279 | 248,959 | 2026-01-23 11:13:28 | 13,296,927 | 451 | 2012-11-08 20:08:12 | 688,213 | 2017-10-09 13:14:41 | https://stackoverflow.com/q/13296863 | https://stackoverflow.com/a/13296927 | <p><code>wait</code> waits for a process to finish; <code>sleep</code> sleeps for a certain amount of seconds.</p>
| <p><code>wait</code> waits for a process to finish; <code>sleep</code> sleeps for a certain amount of seconds.</p> | 387, 390, 2221, 21289 | bash, shell, sleep, wait | <h1>Difference between wait and sleep</h1>
<p>What is difference between <code>wait</code> and <code>sleep</code>?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 425 | bash | # Difference between wait and sleep
What is difference between `wait` and `sleep`? | `wait` waits for a process to finish; `sleep` sleeps for a certain amount of seconds. |
3231804 | In Bash, how to add "Are you sure [Y/n]" to any command or alias? | 298 | 2010-07-12 19:58:32 | <p>In this particular case, I'd like to add a confirm in Bash for</p>
<pre>
Are you sure? [Y/n]
</pre>
<p>for Mercurial's <code>hg push ssh://username@www.example.com//somepath/morepath</code>, which is actually an alias. Is there a standard command that can be added to the alias to achieve it?</p>
<p>The reason is... | 276,088 | 325,418 | 2023-06-13 13:48:16 | 3,232,082 | 449 | 2010-07-12 20:36:50 | 26,428 | 2019-11-17 09:43:18 | https://stackoverflow.com/q/3231804 | https://stackoverflow.com/a/3232082 | <p>These are more compact and versatile forms of <a href="https://stackoverflow.com/questions/3231804/in-bash-how-to-add-are-you-sure-y-n-to-any-command-or-alias/3231821#3231821"><strong>Hamish's</strong> answer</a>. They handle any mixture of upper and lower case letters:</p>
<pre><code>read -r -p "Are you sure? [y/N... | <p>These are more compact and versatile forms of <a href="https://stackoverflow.com/questions/3231804/in-bash-how-to-add-are-you-sure-y-n-to-any-command-or-alias/3231821#3231821"><strong>Hamish's</strong> answer</a>. They handle any mixture of upper and lower case letters:</p> <pre><code>read -r -p "Are you sure? [y/N... | 387, 1448, 12884 | alias, bash, confirmation | <h1>In Bash, how to add "Are you sure [Y/n]" to any command or alias?</h1>
<p>In this particular case, I'd like to add a confirm in Bash for</p>
<pre>
Are you sure? [Y/n]
</pre>
<p>for Mercurial's <code>hg push ssh://username@www.example.com//somepath/morepath</code>, which is actually an alias. Is there a standard ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 426 | bash | # In Bash, how to add "Are you sure [Y/n]" to any command or alias?
In this particular case, I'd like to add a confirm in Bash for
```
Are you sure? [Y/n]
```
for Mercurial's `hg push ssh://username@www.example.com//somepath/morepath`, which is actually an alias. Is there a standard command that can be added to the ... | These are more compact and versatile forms of [**Hamish's** answer](https://stackoverflow.com/questions/3231804/in-bash-how-to-add-are-you-sure-y-n-to-any-command-or-alias/3231821#3231821). They handle any mixture of upper and lower case letters:
```
read -r -p "Are you sure? [y/N] " response
case "$response" in
[... |
6840332 | Rename multiple files by replacing a particular pattern in the filenames using a shell script | 265 | 2011-07-27 06:38:51 | <blockquote>
<p>Write a simple script that will automatically rename a number of files. As an example we want the file *001.jpg renamed to user defined string + 001.jpg (ex: MyVacation20110725_001.jpg) The usage for this script is to get the digital camera photos to have file names that make some sense.</p>
</blockqu... | 328,379 | 567,797 | 2022-07-01 06:00:18 | 6,840,404 | 449 | 2011-07-27 06:45:46 | 303,363 | 2020-09-22 09:42:27 | https://stackoverflow.com/q/6840332 | https://stackoverflow.com/a/6840404 | <p>An example to help you get off the ground.</p>
<pre><code>for f in *.jpg; do mv "$f" "$(echo "$f" | sed s/IMG/VACATION/)"; done
</code></pre>
<p>In this example, I am assuming that all your image files contain the string <code>IMG</code> and you want to replace <code>IMG</code> with <co... | <p>An example to help you get off the ground.</p> <pre><code>for f in *.jpg; do mv "$f" "$(echo "$f" | sed s/IMG/VACATION/)"; done </code></pre> <p>In this example, I am assuming that all your image files contain the string <code>IMG</code> and you want to replace <code>IMG</code> with <co... | 390 | shell | <h1>Rename multiple files by replacing a particular pattern in the filenames using a shell script</h1>
<blockquote>
<p>Write a simple script that will automatically rename a number of files. As an example we want the file *001.jpg renamed to user defined string + 001.jpg (ex: MyVacation20110725_001.jpg) The usage for... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 427 | bash | # Rename multiple files by replacing a particular pattern in the filenames using a shell script
> Write a simple script that will automatically rename a number of files. As an example we want the file *001.jpg renamed to user defined string + 001.jpg (ex: MyVacation20110725_001.jpg) The usage for this script is to get... | An example to help you get off the ground.
```
for f in *.jpg; do mv "$f" "$(echo "$f" | sed s/IMG/VACATION/)"; done
```
In this example, I am assuming that all your image files contain the string `IMG` and you want to replace `IMG` with `VACATION`.
The shell automatically evaluates `*.jpg` to all the matching files... |
1294590 | How to remove all .svn directories from my application directories | 196 | 2009-08-18 15:20:56 | <p>One of the missions of an export tool I have in my application, is to clean all <code>.svn</code> directories from my application directory tree. I am looking for a recursive command in the Linux shell that will traverse the entire tree and delete the <code>.svn</code> files.</p>
<p>I am not using export, as this s... | 158,081 | 67,153 | 2016-09-22 07:06:20 | 1,294,608 | 449 | 2009-08-18 15:23:53 | 148,621 | 2016-09-22 07:06:20 | https://stackoverflow.com/q/1294590 | https://stackoverflow.com/a/1294608 | <p>Try this:</p>
<pre><code>find . -name .svn -exec rm -rf '{}' \;
</code></pre>
<p>Before running a command like that, I often like to run this first:</p>
<pre><code>find . -name .svn -exec ls '{}' \;
</code></pre>
| <p>Try this:</p> <pre><code>find . -name .svn -exec rm -rf '{}' \; </code></pre> <p>Before running a command like that, I often like to run this first:</p> <pre><code>find . -name .svn -exec ls '{}' \; </code></pre> | 58, 390 | linux, shell | <h1>How to remove all .svn directories from my application directories</h1>
<p>One of the missions of an export tool I have in my application, is to clean all <code>.svn</code> directories from my application directory tree. I am looking for a recursive command in the Linux shell that will traverse the entire tree and ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 428 | bash | # How to remove all .svn directories from my application directories
One of the missions of an export tool I have in my application, is to clean all `.svn` directories from my application directory tree. I am looking for a recursive command in the Linux shell that will traverse the entire tree and delete the `.svn` fi... | Try this:
```
find . -name .svn -exec rm -rf '{}' \;
```
Before running a command like that, I often like to run this first:
```
find . -name .svn -exec ls '{}' \;
``` |
4034896 | Find all files with a filename beginning with a specified string? | 252 | 2010-10-27 15:26:44 | <p>I have a directory with roughly 100000 files in it, and I want to perform some function on all files beginning with a specified string, which may match tens of thousands of files.</p>
<p>I have tried</p>
<pre><code>ls mystring*
</code></pre>
<p>but this returns with the bash error 'Too many arguments'. My next p... | 414,366 | 450,460 | 2024-11-05 20:10:51 | 4,034,907 | 448 | 2010-10-27 15:27:50 | 125,816 | 2020-08-25 08:15:30 | https://stackoverflow.com/q/4034896 | https://stackoverflow.com/a/4034907 | <p>Use <code>find</code> with a wildcard:</p>
<pre class="lang-sh prettyprint-override"><code>find . -name 'mystring*'
</code></pre>
| <p>Use <code>find</code> with a wildcard:</p> <pre class="lang-sh prettyprint-override"><code>find . -name 'mystring*' </code></pre> | 387, 10193 | bash, find | <h1>Find all files with a filename beginning with a specified string?</h1>
<p>I have a directory with roughly 100000 files in it, and I want to perform some function on all files beginning with a specified string, which may match tens of thousands of files.</p>
<p>I have tried</p>
<pre><code>ls mystring*
</code></pre... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 429 | bash | # Find all files with a filename beginning with a specified string?
I have a directory with roughly 100000 files in it, and I want to perform some function on all files beginning with a specified string, which may match tens of thousands of files.
I have tried
```
ls mystring*
```
but this returns with the bash err... | Use `find` with a wildcard:
```
find . -name 'mystring*'
``` |
216995 | How can I use inverse or negative wildcards when pattern matching in a unix/linux shell? | 400 | 2008-10-19 21:16:26 | <p>Say I want to copy the contents of a directory excluding files and folders whose names contain the word 'Music'.</p>
<pre><code>cp [exclude-matches] *Music* /target_directory
</code></pre>
<p>What should go in place of [exclude-matches] to accomplish this?</p>
| 249,521 | 4,812 | 2019-08-23 17:47:28 | 217,017 | 447 | 2008-10-19 21:32:42 | 5,190 | 2019-05-24 09:54:19 | https://stackoverflow.com/q/216995 | https://stackoverflow.com/a/217017 | <p>In Bash you can do it by enabling the <code>extglob</code> option, like this (replace <code>ls</code> with <code>cp</code> and add the target directory, of course)</p>
<pre><code>~/foobar> shopt extglob
extglob off
~/foobar> ls
abar afoo bbar bfoo
~/foobar> ls !(b*)
-bash: !: event not found
~/fo... | <p>In Bash you can do it by enabling the <code>extglob</code> option, like this (replace <code>ls</code> with <code>cp</code> and add the target directory, of course)</p> <pre><code>~/foobar> shopt extglob extglob off ~/foobar> ls abar afoo bbar bfoo ~/foobar> ls !(b*) -bash: !: event not found ~/fo... | 387, 390, 3889, 12282 | bash, glob, pattern-matching, shell | <h1>How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?</h1>
<p>Say I want to copy the contents of a directory excluding files and folders whose names contain the word 'Music'.</p>
<pre><code>cp [exclude-matches] *Music* /target_directory
</code></pre>
<p>What should go in place o... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 430 | bash | # How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?
Say I want to copy the contents of a directory excluding files and folders whose names contain the word 'Music'.
```
cp [exclude-matches] *Music* /target_directory
```
What should go in place of [exclude-matches] to accomplish... | In Bash you can do it by enabling the `extglob` option, like this (replace `ls` with `cp` and add the target directory, of course)
```
~/foobar> shopt extglob
extglob off
~/foobar> ls
abar afoo bbar bfoo
~/foobar> ls !(b*)
-bash: !: event not found
~/foobar> shopt -s extglob # Enables extglob
~/foobar> ls !... |
3497123 | Run git pull over all subdirectories | 358 | 2010-08-16 20:39:35 | <p>How can I update multiple git repositories from their shared parent's directory without <code>cd</code>'ing into each repo's root directory? I have the following which are all separate git repositories (<strong>not</strong> submodules):</p>
<pre><code>/plugins/cms
/plugins/admin
/plugins/chart
</code></pre>
<p>I ... | 208,644 | 268,074 | 2021-10-09 16:50:30 | 12,495,234 | 447 | 2012-09-19 13:00:13 | 1,658,147 | 2013-01-26 22:01:07 | https://stackoverflow.com/q/3497123 | https://stackoverflow.com/a/12495234 | <p>Run the following from the parent directory, <code>plugins</code> in this case:</p>
<pre><code>find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull origin master \;
</code></pre>
<p>To clarify:</p>
<ul>
<li><code>find .</code> searches the current directory</li>
<li><code>-type d</code> to... | <p>Run the following from the parent directory, <code>plugins</code> in this case:</p> <pre><code>find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull origin master \; </code></pre> <p>To clarify:</p> <ul> <li><code>find .</code> searches the current directory</li> <li><code>-type d</code> to... | 119, 387 | bash, git | <h1>Run git pull over all subdirectories</h1>
<p>How can I update multiple git repositories from their shared parent's directory without <code>cd</code>'ing into each repo's root directory? I have the following which are all separate git repositories (<strong>not</strong> submodules):</p>
<pre><code>/plugins/cms
/plu... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 431 | bash | # Run git pull over all subdirectories
How can I update multiple git repositories from their shared parent's directory without `cd`'ing into each repo's root directory? I have the following which are all separate git repositories (**not** submodules):
```
/plugins/cms
/plugins/admin
/plugins/chart
```
I want to upda... | Run the following from the parent directory, `plugins` in this case:
```
find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull origin master \;
```
To clarify:
- `find .` searches the current directory
- `-type d` to find directories, not files
- `-depth 1` for a maximum depth of one sub-direc... |
9964823 | How to check if a file is empty in Bash? | 372 | 2012-04-01 13:41:05 | <p>I have a file called <em><strong>diff.txt</strong></em>. I want to check whether it is empty.</p>
<p>I wrote a bash script something like below, but I couldn't get it work.</p>
<pre><code>if [ -s diff.txt ]
then
touch empty.txt
rm full.txt
else
touch full.txt
rm empty.txt
fi
</code></... | 659,307 | 1,149,061 | 2024-09-05 13:38:33 | 9,964,890 | 445 | 2012-04-01 13:52:14 | 1,275,653 | 2024-09-05 13:37:54 | https://stackoverflow.com/q/9964823 | https://stackoverflow.com/a/9964890 | <p>try this:</p>
<pre class="lang-sh prettyprint-override"><code>#!/bin/bash -e
if [ -s diff.txt ]; then
# The file is not-empty.
rm -f empty.txt
touch full.txt
else
# The file is empty.
rm -f full.txt
touch empty.txt
fi
</code></pre>
<p>Notice incidentally, that I have ... | <p>try this:</p> <pre class="lang-sh prettyprint-override"><code>#!/bin/bash -e if [ -s diff.txt ]; then # The file is not-empty. rm -f empty.txt touch full.txt else # The file is empty. rm -f full.txt touch empty.txt fi </code></pre> <p>Notice incidentally, that I have ... | 34, 58, 387, 14528, 58757 | bash, file-handling, is-empty, linux, unix | <h1>How to check if a file is empty in Bash?</h1>
<p>I have a file called <em><strong>diff.txt</strong></em>. I want to check whether it is empty.</p>
<p>I wrote a bash script something like below, but I couldn't get it work.</p>
<pre><code>if [ -s diff.txt ]
then
touch empty.txt
rm full.txt
else
... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 432 | bash | # How to check if a file is empty in Bash?
I have a file called ***diff.txt***. I want to check whether it is empty.
I wrote a bash script something like below, but I couldn't get it work.
```
if [ -s diff.txt ]
then
touch empty.txt
rm full.txt
else
touch full.txt
rm empty.txt
fi
``` | try this:
```
#!/bin/bash -e
if [ -s diff.txt ]; then
# The file is not-empty.
rm -f empty.txt
touch full.txt
else
# The file is empty.
rm -f full.txt
touch empty.txt
fi
```
Notice incidentally, that I have swapped the roles of `empty.txt` and `full.txt`, as [@Matthias... |
48492422 | How to grep for case insensitive string in a file? | 263 | 2018-01-28 23:17:54 | <p>I have a file <code>file1</code> which ends with
<code>Success...</code> OR
<code>success...</code></p>
<p>I want to <code>grep</code> for the word <code>success</code> in a way which is not case sensitive way.</p>
<p>I have written the following command but it is case sensitive</p>
<pre><code>cat file1 | grep "... | 259,628 | 9,196,242 | 2023-01-24 16:10:03 | 48,492,465 | 445 | 2018-01-28 23:22:17 | 58,866 | 2018-01-28 23:22:17 | https://stackoverflow.com/q/48492422 | https://stackoverflow.com/a/48492465 | <p>You can use the <code>-i</code> flag which makes your pattern case insensitive:</p>
<pre><code>grep -iF "success..." file1
</code></pre>
<p>Also, there is no need for <code>cat</code>. <code>grep</code> takes a file with the syntax <code>grep <pattern> <file></code>. I also used the <code>-F</code> fla... | <p>You can use the <code>-i</code> flag which makes your pattern case insensitive:</p> <pre><code>grep -iF "success..." file1 </code></pre> <p>Also, there is no need for <code>cat</code>. <code>grep</code> takes a file with the syntax <code>grep <pattern> <file></code>. I also used the <code>-F</code> fla... | 387, 390, 990, 1271, 5282 | awk, bash, grep, sed, shell | <h1>How to grep for case insensitive string in a file?</h1>
<p>I have a file <code>file1</code> which ends with
<code>Success...</code> OR
<code>success...</code></p>
<p>I want to <code>grep</code> for the word <code>success</code> in a way which is not case sensitive way.</p>
<p>I have written the following command bu... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 433 | bash | # How to grep for case insensitive string in a file?
I have a file `file1` which ends with
`Success...` OR
`success...`
I want to `grep` for the word `success` in a way which is not case sensitive way.
I have written the following command but it is case sensitive
```
cat file1 | grep "success\.\.\."
```
How can i ... | You can use the `-i` flag which makes your pattern case insensitive:
```
grep -iF "success..." file1
```
Also, there is no need for `cat`. `grep` takes a file with the syntax `grep <pattern> <file>`. I also used the `-F` flag to search for a fixed string to avoid escaping the ellipsis. |
8513133 | How do I find all of the symlinks in a directory tree? | 418 | 2011-12-14 23:19:48 | <p>I'm trying to find all of the symlinks within a directory tree for my website. I know that I can use <code>find</code> to do this but I can't figure out how to recursively check the directories.</p>
<p>I've tried this command:</p>
<pre><code>find /var/www/ -type l
</code></pre>
<p>… and later I discovered ... | 568,613 | 332,904 | 2023-06-10 05:44:25 | 8,513,194 | 443 | 2011-12-14 23:27:06 | 938,615 | 2023-06-10 05:44:25 | https://stackoverflow.com/q/8513133 | https://stackoverflow.com/a/8513194 | <p>This will recursively traverse the <code>/path/to/folder</code> directory and list only the symbolic links:</p>
<pre><code>ls -lR /path/to/folder | grep '^l'
</code></pre>
<p>If your intention is to follow the symbolic links too, you should use your <code>find</code> command but you should include the <code>-L</code... | <p>This will recursively traverse the <code>/path/to/folder</code> directory and list only the symbolic links:</p> <pre><code>ls -lR /path/to/folder | grep '^l' </code></pre> <p>If your intention is to follow the symbolic links too, you should use your <code>find</code> command but you should include the <code>-L</code... | 387, 1122, 10193 | bash, find, symlink | <h1>How do I find all of the symlinks in a directory tree?</h1>
<p>I'm trying to find all of the symlinks within a directory tree for my website. I know that I can use <code>find</code> to do this but I can't figure out how to recursively check the directories.</p>
<p>I've tried this command:</p>
<pre><code>find /var... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 434 | bash | # How do I find all of the symlinks in a directory tree?
I'm trying to find all of the symlinks within a directory tree for my website. I know that I can use `find` to do this but I can't figure out how to recursively check the directories.
I've tried this command:
```
find /var/www/ -type l
```
… and later I disco... | This will recursively traverse the `/path/to/folder` directory and list only the symbolic links:
```
ls -lR /path/to/folder | grep '^l'
```
If your intention is to follow the symbolic links too, you should use your `find` command but you should include the `-L` option; in fact the `find` man page says:
```
-L ... |
5767062 | How to check if a symlink exists | 312 | 2011-04-23 21:27:43 | <p>I'm trying to check if a symlink exists in bash. Here's what I've tried.</p>
<pre><code>mda=/usr/mda
if [ ! -L $mda ]; then
echo "=> File doesn't exist"
fi
mda='/usr/mda'
if [ ! -L $mda ]; then
echo "=> File doesn't exist"
fi
</code></pre>
<p>However, that doesn't work.
If '!' is left out, it never tr... | 474,692 | 114,865 | 2021-01-24 14:37:37 | 5,767,077 | 443 | 2011-04-23 21:31:01 | 678,832 | 2017-05-30 18:30:25 | https://stackoverflow.com/q/5767062 | https://stackoverflow.com/a/5767077 | <p><code>-L</code> returns true if the "file" exists and is a symbolic link (the linked file may or may not exist). You want <code>-f</code> (returns true if file exists and is a regular file) or maybe just <code>-e</code> (returns true if file exists regardless of type).</p>
<p>According to the <a href="http://linux.... | <p><code>-L</code> returns true if the "file" exists and is a symbolic link (the linked file may or may not exist). You want <code>-f</code> (returns true if file exists and is a regular file) or maybe just <code>-e</code> (returns true if file exists regardless of type).</p> <p>According to the <a href="http://linux.... | 387, 1122 | bash, symlink | <h1>How to check if a symlink exists</h1>
<p>I'm trying to check if a symlink exists in bash. Here's what I've tried.</p>
<pre><code>mda=/usr/mda
if [ ! -L $mda ]; then
echo "=> File doesn't exist"
fi
mda='/usr/mda'
if [ ! -L $mda ]; then
echo "=> File doesn't exist"
fi
</code></pre>
<p>However, that does... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 435 | bash | # How to check if a symlink exists
I'm trying to check if a symlink exists in bash. Here's what I've tried.
```
mda=/usr/mda
if [ ! -L $mda ]; then
echo "=> File doesn't exist"
fi
mda='/usr/mda'
if [ ! -L $mda ]; then
echo "=> File doesn't exist"
fi
```
However, that doesn't work.
If '!' is left out, it never ... | `-L` returns true if the "file" exists and is a symbolic link (the linked file may or may not exist). You want `-f` (returns true if file exists and is a regular file) or maybe just `-e` (returns true if file exists regardless of type).
According to the [GNU manpage](http://linux.die.net/man/1/test), `-h` is identical... |
17066250 | Create timestamp variable in bash script | 647 | 2013-06-12 13:03:33 | <p>I am trying to create a timestamp variable in a shell script to make the logging a little easier. I want to create the variable at the beginning of the script and have it print out the current time whenever I issue <code>echo $timestamp</code>. It proving to be more difficult then I thought. Here are some things I'v... | 1,057,177 | 2,448,918 | 2023-11-24 08:35:47 | 17,066,329 | 441 | 2013-06-12 13:06:17 | 2,442,087 | 2020-09-03 05:18:54 | https://stackoverflow.com/q/17066250 | https://stackoverflow.com/a/17066329 | <p>In order to get the current timestamp and not the time of when a fixed variable is defined, the trick is to use a function and <strong>not</strong> a variable:</p>
<pre><code>#!/bin/bash
# Define a timestamp function
timestamp() {
date +"%T" # current time
}
# do something...
timestamp # print timestam... | <p>In order to get the current timestamp and not the time of when a fixed variable is defined, the trick is to use a function and <strong>not</strong> a variable:</p> <pre><code>#!/bin/bash # Define a timestamp function timestamp() { date +"%T" # current time } # do something... timestamp # print timestam... | 276, 387, 2177 | bash, timestamp, variables | <h1>Create timestamp variable in bash script</h1>
<p>I am trying to create a timestamp variable in a shell script to make the logging a little easier. I want to create the variable at the beginning of the script and have it print out the current time whenever I issue <code>echo $timestamp</code>. It proving to be more ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 436 | bash | # Create timestamp variable in bash script
I am trying to create a timestamp variable in a shell script to make the logging a little easier. I want to create the variable at the beginning of the script and have it print out the current time whenever I issue `echo $timestamp`. It proving to be more difficult then I tho... | In order to get the current timestamp and not the time of when a fixed variable is defined, the trick is to use a function and **not** a variable:
```
#!/bin/bash
# Define a timestamp function
timestamp() {
date +"%T" # current time
}
# do something...
timestamp # print timestamp
# do something else...
timestamp #... |
5431909 | Returning a boolean from a Bash function | 320 | 2011-03-25 11:39:08 | <p>I want to write a bash function that check if a file has certain properties and returns true or false. Then I can use it in my scripts in the "if". But what should I return?</p>
<pre><code>function myfun(){ ... return 0; else return 1; fi;}
</code></pre>
<p>then I use it like this:</p>
<pre><code>if myfun filenam... | 352,421 | 120,496 | 2023-10-03 01:51:06 | 5,431,932 | 440 | 2011-03-25 11:41:58 | 616,700 | 2017-07-02 20:01:32 | https://stackoverflow.com/q/5431909 | https://stackoverflow.com/a/5431932 | <p>Use 0 for true and 1 for false.</p>
<p>Sample:</p>
<pre><code>#!/bin/bash
isdirectory() {
if [ -d "$1" ]
then
# 0 = true
return 0
else
# 1 = false
return 1
fi
}
if isdirectory $1; then echo "is directory"; else echo "nopes"; fi
</code></pre>
<p><strong>Edit</strong></p>
<p>From @amich... | <p>Use 0 for true and 1 for false.</p> <p>Sample:</p> <pre><code>#!/bin/bash isdirectory() { if [ -d "$1" ] then # 0 = true return 0 else # 1 = false return 1 fi } if isdirectory $1; then echo "is directory"; else echo "nopes"; fi </code></pre> <p><strong>Edit</strong></p> <p>From @amich... | 387, 390, 2848 | bash, boolean, shell | <h1>Returning a boolean from a Bash function</h1>
<p>I want to write a bash function that check if a file has certain properties and returns true or false. Then I can use it in my scripts in the "if". But what should I return?</p>
<pre><code>function myfun(){ ... return 0; else return 1; fi;}
</code></pre>
<p>then I ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 437 | bash | # Returning a boolean from a Bash function
I want to write a bash function that check if a file has certain properties and returns true or false. Then I can use it in my scripts in the "if". But what should I return?
```
function myfun(){ ... return 0; else return 1; fi;}
```
then I use it like this:
```
if myfun f... | Use 0 for true and 1 for false.
Sample:
```
#!/bin/bash
isdirectory() {
if [ -d "$1" ]
then
# 0 = true
return 0
else
# 1 = false
return 1
fi
}
if isdirectory $1; then echo "is directory"; else echo "nopes"; fi
```
**Edit**
From @amichair's comment, these are also possible
```
isdirector... |
7739645 | Install MySQL on Ubuntu without a password prompt | 315 | 2011-10-12 11:52:32 | <p>How do I write a script to install MySQL server on Ubuntu?</p>
<p><code>sudo apt-get install mysql</code> will install, but it will also ask for a password to be entered in the console.</p>
<p>How do I do this in a non-interactive way? That is, write a script that can provide the password? </p>
<pre><code>#!/bin/... | 150,633 | 941,994 | 2023-04-01 20:22:56 | 7,740,571 | 440 | 2011-10-12 13:08:49 | 430,749 | 2023-04-01 20:22:56 | https://stackoverflow.com/q/7739645 | https://stackoverflow.com/a/7740571 | <pre><code>sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password your_password'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password your_password'
sudo apt-get -y install mysql-server
</code></pre>
<p>For specific versions, such as <code>... | <pre><code>sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password your_password' sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password your_password' sudo apt-get -y install mysql-server </code></pre> <p>For specific versions, such as <code>... | 21, 387, 549, 2080 | apt, bash, mysql, ubuntu | <h1>Install MySQL on Ubuntu without a password prompt</h1>
<p>How do I write a script to install MySQL server on Ubuntu?</p>
<p><code>sudo apt-get install mysql</code> will install, but it will also ask for a password to be entered in the console.</p>
<p>How do I do this in a non-interactive way? That is, write a scr... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 438 | bash | # Install MySQL on Ubuntu without a password prompt
How do I write a script to install MySQL server on Ubuntu?
`sudo apt-get install mysql` will install, but it will also ask for a password to be entered in the console.
How do I do this in a non-interactive way? That is, write a script that can provide the password?... | ```
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password your_password'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password your_password'
sudo apt-get -y install mysql-server
```
For specific versions, such as `mysql-server-5.6`, you'll need to specify ... |
18522647 | Run ssh and immediately execute command | 279 | 2013-08-29 23:38:32 | <p>I'm trying to find UNIX or bash command to run a command after connecting to an ssh server. For example:</p>
<pre><code>ssh name@ip "tmux list-sessions"
</code></pre>
<p>The above code works, it lists the sessions, but it then immediately disconnects. Putting it in the sshrc on the server side works, but I need ... | 404,311 | 1,956,786 | 2025-05-22 20:13:36 | 18,522,811 | 440 | 2013-08-29 23:56:53 | 1,491,895 | 2023-09-30 19:30:29 | https://stackoverflow.com/q/18522647 | https://stackoverflow.com/a/18522811 | <pre><code>ssh destination -t 'command; bash -l'
</code></pre>
<p>will execute the command and then start up a login shell when it completes. For example:</p>
<pre><code>ssh -t user@domain.example 'cd /some/path; bash -l'
</code></pre>
| <pre><code>ssh destination -t 'command; bash -l' </code></pre> <p>will execute the command and then start up a login shell when it completes. For example:</p> <pre><code>ssh -t user@domain.example 'cd /some/path; bash -l' </code></pre> | 34, 386, 387 | bash, ssh, unix | <h1>Run ssh and immediately execute command</h1>
<p>I'm trying to find UNIX or bash command to run a command after connecting to an ssh server. For example:</p>
<pre><code>ssh name@ip "tmux list-sessions"
</code></pre>
<p>The above code works, it lists the sessions, but it then immediately disconnects. Putting it i... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 439 | bash | # Run ssh and immediately execute command
I'm trying to find UNIX or bash command to run a command after connecting to an ssh server. For example:
```
ssh name@ip "tmux list-sessions"
```
The above code works, it lists the sessions, but it then immediately disconnects. Putting it in the sshrc on the server side work... | ```
ssh destination -t 'command; bash -l'
```
will execute the command and then start up a login shell when it completes. For example:
```
ssh -t user@domain.example 'cd /some/path; bash -l'
``` |
1767384 | How can I get a recursive full-path listing, one line per file? | 680 | 2009-11-19 23:39:07 | <p>How can I spit out a flat list of recursive one-per-line paths?</p>
<p>For example, I just want a flat listing of files with their full paths:</p>
<pre><code>/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow
/home/dreftymac/stackoverflow/alpha.txt
/home/dreftymac/stackov... | 815,490 | 42,223 | 2024-02-24 17:54:00 | 1,767,559 | 438 | 2009-11-20 00:20:17 | 131,527 | 2017-01-06 19:08:26 | https://stackoverflow.com/q/1767384 | https://stackoverflow.com/a/1767559 | <p>If you really want to use <code>ls</code>, then format its output using awk:</p>
<pre><code>ls -R /path | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }'
</code></pre>
| <p>If you really want to use <code>ls</code>, then format its output using awk:</p> <pre><code>ls -R /path | awk ' /:$/&&f{s=$0;f=0} /:$/&&!f{sub(/:$/,"");s=$0;f=1;next} NF&&f{ print s"/"$0 }' </code></pre> | 387, 1231, 3589, 10193 | bash, command-line, find, ls | <h1>How can I get a recursive full-path listing, one line per file?</h1>
<p>How can I spit out a flat list of recursive one-per-line paths?</p>
<p>For example, I just want a flat listing of files with their full paths:</p>
<pre><code>/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stac... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 440 | bash | # How can I get a recursive full-path listing, one line per file?
How can I spit out a flat list of recursive one-per-line paths?
For example, I just want a flat listing of files with their full paths:
```
/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow
/home/dreftymac... | If you really want to use `ls`, then format its output using awk:
```
ls -R /path | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }'
``` |
16362402 | Save file to specific folder with curl command | 345 | 2013-05-03 15:23:29 | <p>In a shell script, I want to download a file from some URL and save it to a specific folder. What is the specific CLI flag I should use to download files to a specific folder with the <code>curl</code> command, or how else do I get that result?</p>
| 518,046 | 1,940,895 | 2024-09-18 17:29:04 | 16,363,115 | 438 | 2013-05-03 16:00:40 | 2,269,047 | 2018-09-23 06:08:46 | https://stackoverflow.com/q/16362402 | https://stackoverflow.com/a/16363115 | <p>I don't think you can give a path to curl, but you can CD to the location, download and CD back.</p>
<pre><code>cd target/path && { curl -O URL ; cd -; }
</code></pre>
<p>Or using subshell.</p>
<pre><code>(cd target/path && curl -O URL)
</code></pre>
<p>Both ways will only download if path exists... | <p>I don't think you can give a path to curl, but you can CD to the location, download and CD back.</p> <pre><code>cd target/path && { curl -O URL ; cd -; } </code></pre> <p>Or using subshell.</p> <pre><code>(cd target/path && curl -O URL) </code></pre> <p>Both ways will only download if path exists... | 218, 387, 390, 1554 | bash, curl, directory, shell | <h1>Save file to specific folder with curl command</h1>
<p>In a shell script, I want to download a file from some URL and save it to a specific folder. What is the specific CLI flag I should use to download files to a specific folder with the <code>curl</code> command, or how else do I get that result?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 441 | bash | # Save file to specific folder with curl command
In a shell script, I want to download a file from some URL and save it to a specific folder. What is the specific CLI flag I should use to download files to a specific folder with the `curl` command, or how else do I get that result? | I don't think you can give a path to curl, but you can CD to the location, download and CD back.
```
cd target/path && { curl -O URL ; cd -; }
```
Or using subshell.
```
(cd target/path && curl -O URL)
```
Both ways will only download if path exists. `-O` keeps remote file name. After download it will return to ori... |
23118341 | How to get key names from JSON using jq | 313 | 2014-04-16 19:21:23 | <p><code>curl http://testhost.test.com:8080/application/app/version | jq '.version' | jq '.[]'</code></p>
<p>The above command outputs only the values as below:</p>
<pre><code>"madireddy@test.com"
"2323"
"test"
"02-03-2014-13:41"
"application"
</code></pre>
<p>How c... | 350,799 | 2,476,906 | 2024-12-06 23:15:56 | 23,118,607 | 438 | 2014-04-16 19:36:11 | 548,225 | 2023-02-07 19:16:32 | https://stackoverflow.com/q/23118341 | https://stackoverflow.com/a/23118607 | <p>To get the keys in the order they appear in the original JSON use:</p>
<pre class="lang-bash prettyprint-override"><code>jq 'keys_unsorted' file.json
</code></pre>
<p>If you want the keys sorted alphanumerically, you can use:</p>
<pre class="lang-bash prettyprint-override"><code>jq 'keys' file.json
</code></pre>
<p>... | <p>To get the keys in the order they appear in the original JSON use:</p> <pre class="lang-bash prettyprint-override"><code>jq 'keys_unsorted' file.json </code></pre> <p>If you want the keys sorted alphanumerically, you can use:</p> <pre class="lang-bash prettyprint-override"><code>jq 'keys' file.json </code></pre> <p>... | 34, 390, 1508, 5350, 105170 | jq, json, key, shell, unix | <h1>How to get key names from JSON using jq</h1>
<p><code>curl http://testhost.test.com:8080/application/app/version | jq '.version' | jq '.[]'</code></p>
<p>The above command outputs only the values as below:</p>
<pre><code>"madireddy@test.com"
"2323"
"test"
"02-03-2014-13:41"... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 442 | bash | # How to get key names from JSON using jq
`curl http://testhost.test.com:8080/application/app/version | jq '.version' | jq '.[]'`
The above command outputs only the values as below:
```
"madireddy@test.com"
"2323"
"test"
"02-03-2014-13:41"
"application"
```
How can I get the key names instead like the below:
`... | To get the keys in the order they appear in the original JSON use:
```
jq 'keys_unsorted' file.json
```
If you want the keys sorted alphanumerically, you can use:
```
jq 'keys' file.json
```
Complete example
```
$ cat file.json
{ "Created-By" : "Apache Maven", "Build-Number" : "", "Archiver-Version" : "Plexus Arch... |
2135478 | How to simulate the environment cron executes a script with? | 284 | 2010-01-25 20:42:21 | <p>I normally have several problems with how cron executes scripts as they normally don't have my environment setup. Is there a way to invoke bash(?) in the same way cron does so I could test scripts before installing them?</p>
| 109,124 | 96,094 | 2021-10-14 07:29:11 | 2,546,509 | 437 | 2010-03-30 15:34:41 | 23,630 | 2021-10-14 07:29:11 | https://stackoverflow.com/q/2135478 | https://stackoverflow.com/a/2546509 | <p>Add this to your crontab (temporarily):</p>
<pre><code>* * * * * env > ~/cronenv
</code></pre>
<p>After it runs, do this:</p>
<pre><code>env - `cat ~/cronenv` /bin/sh
</code></pre>
<p>This assumes that your cron runs /bin/sh, which is the default regardless of the user's default shell.</p>
<p>Footnote: if <code>e... | <p>Add this to your crontab (temporarily):</p> <pre><code>* * * * * env > ~/cronenv </code></pre> <p>After it runs, do this:</p> <pre><code>env - `cat ~/cronenv` /bin/sh </code></pre> <p>This assumes that your cron runs /bin/sh, which is the default regardless of the user's default shell.</p> <p>Footnote: if <code>e... | 387, 531, 601 | bash, cron, scripting | <h1>How to simulate the environment cron executes a script with?</h1>
<p>I normally have several problems with how cron executes scripts as they normally don't have my environment setup. Is there a way to invoke bash(?) in the same way cron does so I could test scripts before installing them?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 443 | bash | # How to simulate the environment cron executes a script with?
I normally have several problems with how cron executes scripts as they normally don't have my environment setup. Is there a way to invoke bash(?) in the same way cron does so I could test scripts before installing them? | Add this to your crontab (temporarily):
```
* * * * * env > ~/cronenv
```
After it runs, do this:
```
env - `cat ~/cronenv` /bin/sh
```
This assumes that your cron runs /bin/sh, which is the default regardless of the user's default shell.
Footnote: if `env` contains more advanced config, eg `PS1=$(__git_ps1 " (%s)... |
7216358 | `date` command on OS X doesn't have ISO 8601 `-I` option? | 270 | 2011-08-27 18:05:05 | <p>In a Bash script, I want to print the current datetime in <a href="http://en.wikipedia.org/wiki/ISO_8601" rel="nofollow noreferrer">ISO 8601</a> format (preferably UTC), and it seems that this should be as simple as <code>date -I</code>:</p>
<p><a href="http://ss64.com/bash/date.html" rel="nofollow noreferrer">http:... | 117,670 | 132,978 | 2025-03-05 22:10:22 | 7,216,394 | 435 | 2011-08-27 18:12:30 | 495,041 | 2015-12-08 23:36:41 | https://stackoverflow.com/q/7216358 | https://stackoverflow.com/a/7216394 | <p>You could use </p>
<pre><code>date "+%Y-%m-%d"
</code></pre>
<p>Or for a fully <a href="http://en.wikipedia.org/wiki/ISO_8601" rel="noreferrer">ISO-8601 compliant date</a>, use one of the following formats:</p>
<pre><code>date -u +"%Y-%m-%dT%H:%M:%SZ"
</code></pre>
<p>Output:</p>
<pre><code>2011-08-27T23:22:37Z... | <p>You could use </p> <pre><code>date "+%Y-%m-%d" </code></pre> <p>Or for a fully <a href="http://en.wikipedia.org/wiki/ISO_8601" rel="noreferrer">ISO-8601 compliant date</a>, use one of the following formats:</p> <pre><code>date -u +"%Y-%m-%dT%H:%M:%SZ" </code></pre> <p>Output:</p> <pre><code>2011-08-27T23:22:37Z... | 369, 387, 8340 | bash, iso8601, macos | <h1>`date` command on OS X doesn't have ISO 8601 `-I` option?</h1>
<p>In a Bash script, I want to print the current datetime in <a href="http://en.wikipedia.org/wiki/ISO_8601" rel="nofollow noreferrer">ISO 8601</a> format (preferably UTC), and it seems that this should be as simple as <code>date -I</code>:</p>
<p><a hr... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 444 | bash | # `date` command on OS X doesn't have ISO 8601 `-I` option?
In a Bash script, I want to print the current datetime in [ISO 8601](http://en.wikipedia.org/wiki/ISO_8601) format (preferably UTC), and it seems that this should be as simple as `date -I`:
<http://ss64.com/bash/date.html>
But this doesn't seem to work on m... | You could use
```
date "+%Y-%m-%d"
```
Or for a fully [ISO-8601 compliant date](http://en.wikipedia.org/wiki/ISO_8601), use one of the following formats:
```
date -u +"%Y-%m-%dT%H:%M:%SZ"
```
Output:
```
2011-08-27T23:22:37Z
```
*or*
```
date +%Y-%m-%dT%H:%M:%S%z
```
Output:
```
2011-08-27T15:22:37-0800
``` |
42586120 | Copy Files from Windows to Windows Subsystem for Linux (WSL) | 243 | 2017-03-03 18:33:33 | <p>I have enabled developer mode and installed <code>Bash on Ubuntu on Windows</code>.</p>
<p>My home directory can be found under <code>%localappdata%\Lxss\home\<ubuntu.username>\</code>, i have created a sub-directory called Pictures such that the full path should be</p>
<p>on windows: <code>C:\Users\<wind... | 475,983 | 6,741,760 | 2022-12-23 01:57:38 | 42,586,455 | 435 | 2017-03-03 18:55:47 | 7,112,888 | 2022-12-23 01:57:38 | https://stackoverflow.com/q/42586120 | https://stackoverflow.com/a/42586455 | <p>You should be able to access your windows system under the <code>/mnt</code> directory. For example inside of bash, use this to get to your pictures directory:</p>
<pre class="lang-bash prettyprint-override"><code>cd /mnt/c/Users/<windows.username>/Pictures
</code></pre>
| <p>You should be able to access your windows system under the <code>/mnt</code> directory. For example inside of bash, use this to get to your pictures directory:</p> <pre class="lang-bash prettyprint-override"><code>cd /mnt/c/Users/<windows.username>/Pictures </code></pre> | 58, 64, 387, 107329, 119526 | bash, linux, windows, windows-10, windows-subsystem-for-linux | <h1>Copy Files from Windows to Windows Subsystem for Linux (WSL)</h1>
<p>I have enabled developer mode and installed <code>Bash on Ubuntu on Windows</code>.</p>
<p>My home directory can be found under <code>%localappdata%\Lxss\home\<ubuntu.username>\</code>, i have created a sub-directory called Pictures such th... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 445 | bash | # Copy Files from Windows to Windows Subsystem for Linux (WSL)
I have enabled developer mode and installed `Bash on Ubuntu on Windows`.
My home directory can be found under `%localappdata%\Lxss\home\<ubuntu.username>\`, i have created a sub-directory called Pictures such that the full path should be
on windows: `C:\... | You should be able to access your windows system under the `/mnt` directory. For example inside of bash, use this to get to your pictures directory:
```
cd /mnt/c/Users/<windows.username>/Pictures
``` |
38906626 | Curl to return http status code along with the response | 445 | 2016-08-11 21:46:37 | <p>I use curl to get http headers to find http status code and also return response. I get the http headers with the command</p>
<pre><code>curl -I http://localhost
</code></pre>
<p>To get the response, I use the command </p>
<pre><code>curl http://localhost
</code></pre>
<p>As soon as use the -I flag, I get only t... | 1,001,437 | 819,742 | 2024-07-23 15:46:17 | 38,906,765 | 434 | 2016-08-11 21:59:24 | 819,742 | 2022-02-12 16:35:01 | https://stackoverflow.com/q/38906626 | https://stackoverflow.com/a/38906765 | <p>I was able to get a solution by looking at the curl doc which specifies to use <code>-</code> for the output to get the output to stdout.</p>
<pre><code>curl -o - -I http://localhost
</code></pre>
<p>To get the response with just the http return code, I could just do</p>
<pre><code>curl -o /dev/null -s -w "%{h... | <p>I was able to get a solution by looking at the curl doc which specifies to use <code>-</code> for the output to get the output to stdout.</p> <pre><code>curl -o - -I http://localhost </code></pre> <p>To get the response with just the http return code, I could just do</p> <pre><code>curl -o /dev/null -s -w "%{h... | 390, 1554 | curl, shell | <h1>Curl to return http status code along with the response</h1>
<p>I use curl to get http headers to find http status code and also return response. I get the http headers with the command</p>
<pre><code>curl -I http://localhost
</code></pre>
<p>To get the response, I use the command </p>
<pre><code>curl http://loc... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 446 | bash | # Curl to return http status code along with the response
I use curl to get http headers to find http status code and also return response. I get the http headers with the command
```
curl -I http://localhost
```
To get the response, I use the command
```
curl http://localhost
```
As soon as use the -I flag, I get... | I was able to get a solution by looking at the curl doc which specifies to use `-` for the output to get the output to stdout.
```
curl -o - -I http://localhost
```
To get the response with just the http return code, I could just do
```
curl -o /dev/null -s -w "%{http_code}\n" http://localhost
``` |
18460123 | How to add leading zeros for for-loop in shell? | 243 | 2013-08-27 08:03:23 | <p>I have a basic number for loop which increments the variable num by 1 over each iteration...</p>
<pre><code>for (( num=1; num<=5; num++ ))
do
echo $num
done
</code></pre>
<p>Which outputs:</p>
<pre><code>1
2
3
4
5
</code></pre>
<p>I'm trying to make it produce the output (add leading zero before $num):</p>
... | 270,909 | 1,993,064 | 2020-11-04 03:01:26 | 18,460,742 | 434 | 2013-08-27 08:37:25 | 612,462 | 2020-07-13 10:34:55 | https://stackoverflow.com/q/18460123 | https://stackoverflow.com/a/18460742 | <p>Use the following syntax:</p>
<pre><code>$ for i in {01..05}; do echo "$i"; done
01
02
03
04
05
</code></pre>
<p>Disclaimer: Leading zeros only work in <code>>=bash-4</code>.</p>
<p>If you want to use <code>printf</code>, nothing prevents you from putting its result in a variable for further use:</p>
<p... | <p>Use the following syntax:</p> <pre><code>$ for i in {01..05}; do echo "$i"; done 01 02 03 04 05 </code></pre> <p>Disclaimer: Leading zeros only work in <code>>=bash-4</code>.</p> <p>If you want to use <code>printf</code>, nothing prevents you from putting its result in a variable for further use:</p> <p... | 387, 2531 | bash, for-loop | <h1>How to add leading zeros for for-loop in shell?</h1>
<p>I have a basic number for loop which increments the variable num by 1 over each iteration...</p>
<pre><code>for (( num=1; num<=5; num++ ))
do
echo $num
done
</code></pre>
<p>Which outputs:</p>
<pre><code>1
2
3
4
5
</code></pre>
<p>I'm trying to make it... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 447 | bash | # How to add leading zeros for for-loop in shell?
I have a basic number for loop which increments the variable num by 1 over each iteration...
```
for (( num=1; num<=5; num++ ))
do
echo $num
done
```
Which outputs:
```
1
2
3
4
5
```
I'm trying to make it produce the output (add leading zero before $num):
```
01
... | Use the following syntax:
```
$ for i in {01..05}; do echo "$i"; done
01
02
03
04
05
```
Disclaimer: Leading zeros only work in `>=bash-4`.
If you want to use `printf`, nothing prevents you from putting its result in a variable for further use:
```
$ foo=$(printf "%02d" 5)
$ echo "${foo}"
05
``` |
5677201 | How to pipe stdout while keeping it on screen ? (and not to a output file) | 314 | 2011-04-15 13:05:36 | <p>I would like to pipe standard output of a program while keeping it on screen.</p>
<p>With a simple example (<code>echo</code> use here is just for illustration purpose) :</p>
<p><code>$ echo 'ee' | foo</code><br>
<code>ee</code> <- the output I would like to see</p>
<p>I know tee could copy stdout to file but... | 110,585 | 611,407 | 2020-11-25 13:37:43 | 5,677,265 | 432 | 2011-04-15 13:10:00 | 211,665 | 2019-02-05 15:24:28 | https://stackoverflow.com/q/5677201 | https://stackoverflow.com/a/5677265 | <p>Here is a solution that works at on any Unix / Linux implementation, assuming it cares to follow the <code>POSIX</code> standard. It works on some non Unix environments like <code>cygwin</code> too.</p>
<pre><code>echo 'ee' | tee /dev/tty | foo
</code></pre>
<p>Reference: <a href="http://pubs.opengroup.org/online... | <p>Here is a solution that works at on any Unix / Linux implementation, assuming it cares to follow the <code>POSIX</code> standard. It works on some non Unix environments like <code>cygwin</code> too.</p> <pre><code>echo 'ee' | tee /dev/tty | foo </code></pre> <p>Reference: <a href="http://pubs.opengroup.org/online... | 387, 390, 4867, 5813, 84917 | bash, output, pipe, shell, stdout | <h1>How to pipe stdout while keeping it on screen ? (and not to a output file)</h1>
<p>I would like to pipe standard output of a program while keeping it on screen.</p>
<p>With a simple example (<code>echo</code> use here is just for illustration purpose) :</p>
<p><code>$ echo 'ee' | foo</code><br>
<code>ee</code> &... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 448 | bash | # How to pipe stdout while keeping it on screen ? (and not to a output file)
I would like to pipe standard output of a program while keeping it on screen.
With a simple example (`echo` use here is just for illustration purpose) :
`$ echo 'ee' | foo`
`ee` <- the output I would like to see
I know tee could copy std... | Here is a solution that works at on any Unix / Linux implementation, assuming it cares to follow the `POSIX` standard. It works on some non Unix environments like `cygwin` too.
```
echo 'ee' | tee /dev/tty | foo
```
Reference: [The Open Group Base Specifications Issue 7
IEEE Std 1003.1, 2013 Edition, §10.1](http://pu... |
8409946 | How do I make this file.sh executable via double click? | 213 | 2011-12-07 03:10:48 | <p>First off I'm using Mac.</p>
<p>Next, I need to execute this "file.sh" we will call it. Everytime I need to execute it I have to open Terminal and type:</p>
<pre><code>cd /Users/Jacob/Documents/folderWithFileInIt
bash file.sh
</code></pre>
<p>This is okay, but I feel like it would be a lot quicker if I mak... | 372,799 | 798,681 | 2023-05-31 16:52:48 | 9,650,209 | 432 | 2012-03-10 21:09:47 | 754,997 | 2022-05-03 16:03:01 | https://stackoverflow.com/q/8409946 | https://stackoverflow.com/a/9650209 | <p>By default, <code>*.sh</code> files are opened in a text editor (Xcode or TextEdit). To create a shell script that will execute in Terminal when you open it, name it with the “command” extension, e.g., <code>file.command</code>. By default, these are sent to Terminal, which will execute the file as a shell script.</... | <p>By default, <code>*.sh</code> files are opened in a text editor (Xcode or TextEdit). To create a shell script that will execute in Terminal when you open it, name it with the “command” extension, e.g., <code>file.command</code>. By default, these are sent to Terminal, which will execute the file as a shell script.</... | 369, 387, 391, 1135 | bash, executable, macos, terminal | <h1>How do I make this file.sh executable via double click?</h1>
<p>First off I'm using Mac.</p>
<p>Next, I need to execute this "file.sh" we will call it. Everytime I need to execute it I have to open Terminal and type:</p>
<pre><code>cd /Users/Jacob/Documents/folderWithFileInIt
bash file.sh
</code></pre>
<p... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 449 | bash | # How do I make this file.sh executable via double click?
First off I'm using Mac.
Next, I need to execute this "file.sh" we will call it. Everytime I need to execute it I have to open Terminal and type:
```
cd /Users/Jacob/Documents/folderWithFileInIt
bash file.sh
```
This is okay, but I feel like it would be a lo... | By default, `*.sh` files are opened in a text editor (Xcode or TextEdit). To create a shell script that will execute in Terminal when you open it, name it with the “command” extension, e.g., `file.command`. By default, these are sent to Terminal, which will execute the file as a shell script.
You will also need to ens... |
2338765 | Is there a way to make a link clickable in the OSX Terminal? | 191 | 2010-02-26 00:44:35 | <p>I am planning on developing an Mxmlc to Textmate formatter, one that formats mxmlc errors as clickable links, so you can open them up quickly in Textmate as Textmate has a url scheme e.g.: txmt://open/?url=file://~/.bash_profile&line=11&column=2.</p>
<p>I am wondering if it is possible to display links in ... | 66,876 | 281,716 | 2021-03-26 10:03:36 | 2,339,538 | 432 | 2010-02-26 04:37:26 | 231,031 | 2012-07-07 15:06:42 | https://stackoverflow.com/q/2338765 | https://stackoverflow.com/a/2339538 | <p><strong>Before OSX Lion:</strong></p>
<p><code>cmd</code>+<code>shift</code>+<code>double-click</code> on a URL in Terminal.app and it will open in the default program.</p>
<p><strong>OSX Lion:</strong></p>
<p><code>cmd</code>+<code>double-click</code> (otherwise you will enter fullscreen mode).</p>
| <p><strong>Before OSX Lion:</strong></p> <p><code>cmd</code>+<code>shift</code>+<code>double-click</code> on a URL in Terminal.app and it will open in the default program.</p> <p><strong>OSX Lion:</strong></p> <p><code>cmd</code>+<code>double-click</code> (otherwise you will enter fullscreen mode).</p> | 369, 387, 391, 1342 | bash, macos, terminal, textmate | <h1>Is there a way to make a link clickable in the OSX Terminal?</h1>
<p>I am planning on developing an Mxmlc to Textmate formatter, one that formats mxmlc errors as clickable links, so you can open them up quickly in Textmate as Textmate has a url scheme e.g.: txmt://open/?url=file://~/.bash_profile&line=11&c... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 450 | bash | # Is there a way to make a link clickable in the OSX Terminal?
I am planning on developing an Mxmlc to Textmate formatter, one that formats mxmlc errors as clickable links, so you can open them up quickly in Textmate as Textmate has a url scheme e.g.: txmt://open/?url=file://~/.bash_profile&line=11&column=2.
I am won... | **Before OSX Lion:**
`cmd`+`shift`+`double-click` on a URL in Terminal.app and it will open in the default program.
**OSX Lion:**
`cmd`+`double-click` (otherwise you will enter fullscreen mode). |
40230184 | How to do multiline shell script in Ansible | 198 | 2016-10-25 01:55:40 | <p>right now I am using a shell script in ansible that would be much more readable if it was on multiple lines</p>
<pre><code>- name: iterate user groups
shell: groupmod -o -g {{ item['guid'] }} {{ item['username'] }} ....more stuff to do
with_items: "{{ users }}"
</code></pre>
<p>Just not sure how to allow multi... | 253,338 | 1,914,652 | 2024-07-26 15:47:53 | 40,230,416 | 430 | 2016-10-25 02:25:33 | 147,356 | 2024-07-26 15:47:53 | https://stackoverflow.com/q/40230184 | https://stackoverflow.com/a/40230416 | <p>Ansible uses YAML syntax in its playbooks. YAML has a number of block operators:</p>
<ul>
<li><p>The <code>></code> is a folding block operator. That is, it joins multiple lines together by spaces. The following syntax:</p>
<pre><code> key: >
This text
has multiple
lines
</code></pre>
<p>Would ... | <p>Ansible uses YAML syntax in its playbooks. YAML has a number of block operators:</p> <ul> <li><p>The <code>></code> is a folding block operator. That is, it joins multiple lines together by spaces. The following syntax:</p> <pre><code> key: > This text has multiple lines </code></pre> <p>Would ... | 390, 86394 | ansible, shell | <h1>How to do multiline shell script in Ansible</h1>
<p>right now I am using a shell script in ansible that would be much more readable if it was on multiple lines</p>
<pre><code>- name: iterate user groups
shell: groupmod -o -g {{ item['guid'] }} {{ item['username'] }} ....more stuff to do
with_items: "{{ users }... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 451 | bash | # How to do multiline shell script in Ansible
right now I am using a shell script in ansible that would be much more readable if it was on multiple lines
```
- name: iterate user groups
shell: groupmod -o -g {{ item['guid'] }} {{ item['username'] }} ....more stuff to do
with_items: "{{ users }}"
```
Just not sur... | Ansible uses YAML syntax in its playbooks. YAML has a number of block operators:
- The `>` is a folding block operator. That is, it joins multiple lines together by spaces. The following syntax:
```
key: >
This text
has multiple
lines
```
Would assign the value `This text has multiple lin... |
23512852 | Grep 'binary file matches'. How to get normal grep output? | 247 | 2014-05-07 08:40:00 | <p>I've got a grep script that searches through a directory recursively.</p>
<pre><code>grep -n -R -e 'search term' -e 'second search term' ./
</code></pre>
<p>However the results I get are the following. Notice there are found matches in JPGs but no actual result. </p>
<pre><code>Binary file ./jpg/00015928.jpg mat... | 295,390 | 2,383,596 | 2022-05-20 07:24:49 | 23,512,981 | 429 | 2014-05-07 08:46:13 | 390,913 | 2014-05-07 08:46:13 | https://stackoverflow.com/q/23512852 | https://stackoverflow.com/a/23512981 | <p>Try:</p>
<pre><code>grep --text
</code></pre>
<p>or</p>
<pre><code>grep -a
</code></pre>
<p>for short. This is equivalent to <code>--binary-files=text</code> and it should show the matches in binary files.</p>
| <p>Try:</p> <pre><code>grep --text </code></pre> <p>or</p> <pre><code>grep -a </code></pre> <p>for short. This is equivalent to <code>--binary-files=text</code> and it should show the matches in binary files.</p> | 387, 390, 1271 | bash, grep, shell | <h1>Grep 'binary file matches'. How to get normal grep output?</h1>
<p>I've got a grep script that searches through a directory recursively.</p>
<pre><code>grep -n -R -e 'search term' -e 'second search term' ./
</code></pre>
<p>However the results I get are the following. Notice there are found matches in JPGs but n... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 452 | bash | # Grep 'binary file matches'. How to get normal grep output?
I've got a grep script that searches through a directory recursively.
```
grep -n -R -e 'search term' -e 'second search term' ./
```
However the results I get are the following. Notice there are found matches in JPGs but no actual result.
```
Binary file ... | Try:
```
grep --text
```
or
```
grep -a
```
for short. This is equivalent to `--binary-files=text` and it should show the matches in binary files. |
2153882 | How can I shuffle the lines of a text file on the Unix command line or in a shell script? | 349 | 2010-01-28 10:49:08 | <p>I want to shuffle the lines of a text file randomly and create a new file. The file may have several thousands of lines.</p>
<p>How can I do that with <code>cat</code>, <code>awk</code>, <code>cut</code>, etc?</p>
| 214,966 | 252,684 | 2019-09-29 04:33:48 | 2,153,897 | 427 | 2010-01-28 10:51:34 | 73,070 | 2015-05-05 22:09:22 | https://stackoverflow.com/q/2153882 | https://stackoverflow.com/a/2153897 | <p>You can use <a href="http://linux.die.net/man/1/shuf" rel="noreferrer"><code>shuf</code></a>. On some systems at least (doesn't appear to be in POSIX).</p>
<p>As jleedev pointed out: <code>sort -R</code> might also be an option. On some systems at least; well, you get the picture. <a href="https://stackoverflow.com... | <p>You can use <a href="http://linux.die.net/man/1/shuf" rel="noreferrer"><code>shuf</code></a>. On some systems at least (doesn't appear to be in POSIX).</p> <p>As jleedev pointed out: <code>sort -R</code> might also be an option. On some systems at least; well, you get the picture. <a href="https://stackoverflow.com... | 197, 390, 990, 1231, 4570 | awk, command-line, random, shell, shuffle | <h1>How can I shuffle the lines of a text file on the Unix command line or in a shell script?</h1>
<p>I want to shuffle the lines of a text file randomly and create a new file. The file may have several thousands of lines.</p>
<p>How can I do that with <code>cat</code>, <code>awk</code>, <code>cut</code>, etc?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 453 | bash | # How can I shuffle the lines of a text file on the Unix command line or in a shell script?
I want to shuffle the lines of a text file randomly and create a new file. The file may have several thousands of lines.
How can I do that with `cat`, `awk`, `cut`, etc? | You can use [`shuf`](http://linux.die.net/man/1/shuf). On some systems at least (doesn't appear to be in POSIX).
As jleedev pointed out: `sort -R` might also be an option. On some systems at least; well, you get the picture. [It has been pointed out](https://stackoverflow.com/questions/2153882/how-can-i-shuffle-the-li... |
1915636 | Is there a way to 'uniq' by column? | 255 | 2009-12-16 16:03:16 | <p>I have a .csv file like this:</p>
<pre><code>stack2@domain.example,2009-11-27 01:05:47.893000000,domain.example,127.0.0.1
overflow@domain2.example,2009-11-27 00:58:29.793000000,domain2.example,255.255.255.0
overflow@domain2.example,2009-11-27 00:58:29.646465785,domain2.example,256.255.255.0
...
</code></pre>
<p>I ha... | 251,312 | 233,071 | 2023-08-02 06:28:50 | 1,915,750 | 427 | 2009-12-16 16:14:50 | 172,211 | 2022-06-20 10:42:13 | https://stackoverflow.com/q/1915636 | https://stackoverflow.com/a/1915750 | <pre><code>sort -u -t, -k1,1 file
</code></pre>
<ul>
<li><code>-u</code> for unique</li>
<li><code>-t,</code> so comma is the delimiter</li>
<li><code>-k1,1</code> for the key field 1</li>
</ul>
<p>Test result:</p>
<pre><code>overflow@domain2.example,2009-11-27 00:58:29.793000000,xx3.net,255.255.255.0
stack2@domain.exa... | <pre><code>sort -u -t, -k1,1 file </code></pre> <ul> <li><code>-u</code> for unique</li> <li><code>-t,</code> so comma is the delimiter</li> <li><code>-k1,1</code> for the key field 1</li> </ul> <p>Test result:</p> <pre><code>overflow@domain2.example,2009-11-27 00:58:29.793000000,xx3.net,255.255.255.0 stack2@domain.exa... | 58, 134, 390, 35617 | linux, shell, sorting, uniq | <h1>Is there a way to 'uniq' by column?</h1>
<p>I have a .csv file like this:</p>
<pre><code>stack2@domain.example,2009-11-27 01:05:47.893000000,domain.example,127.0.0.1
overflow@domain2.example,2009-11-27 00:58:29.793000000,domain2.example,255.255.255.0
overflow@domain2.example,2009-11-27 00:58:29.646465785,domain2.ex... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 454 | bash | # Is there a way to 'uniq' by column?
I have a .csv file like this:
```
stack2@domain.example,2009-11-27 01:05:47.893000000,domain.example,127.0.0.1
overflow@domain2.example,2009-11-27 00:58:29.793000000,domain2.example,255.255.255.0
overflow@domain2.example,2009-11-27 00:58:29.646465785,domain2.example,256.255.255.0... | ```
sort -u -t, -k1,1 file
```
- `-u` for unique
- `-t,` so comma is the delimiter
- `-k1,1` for the key field 1
Test result:
```
overflow@domain2.example,2009-11-27 00:58:29.793000000,xx3.net,255.255.255.0
stack2@domain.example,2009-11-27 01:05:47.893000000,xx2.net,127.0.0.1
``` |
954302 | How to make a program continue to run after log out from ssh? | 207 | 2009-06-05 04:48:05 | <blockquote>
<p><strong>Possible Duplicate:</strong><br>
<a href="https://stackoverflow.com/questions/285015/linux-prevent-a-background-process-from-being-stopped-after-closing-ssh-client">Prevent a background process from being stopped after closing SSH client</a> </p>
</blockquote>
<p>I have a program that ta... | 255,463 | 104,015 | 2024-09-24 17:26:49 | 954,415 | 425 | 2009-06-05 05:47:35 | 26,428 | 2024-09-24 17:26:49 | https://stackoverflow.com/q/954302 | https://stackoverflow.com/a/954415 | <p>Assuming that you have a program running in the foreground, press <kbd>Ctrl</kbd>+<kbd>Z</kbd>, then:</p>
<pre><code>[1]+ Stopped myprogram
$ disown -h %1
$ bg 1
[1]+ myprogram &
$ logout
</code></pre>
<p>If there is only one job, then you don't need to specify the job number. Just use <code>dis... | <p>Assuming that you have a program running in the foreground, press <kbd>Ctrl</kbd>+<kbd>Z</kbd>, then:</p> <pre><code>[1]+ Stopped myprogram $ disown -h %1 $ bg 1 [1]+ myprogram & $ logout </code></pre> <p>If there is only one job, then you don't need to specify the job number. Just use <code>dis... | 58, 386, 387 | bash, linux, ssh | <h1>How to make a program continue to run after log out from ssh?</h1>
<blockquote>
<p><strong>Possible Duplicate:</strong><br>
<a href="https://stackoverflow.com/questions/285015/linux-prevent-a-background-process-from-being-stopped-after-closing-ssh-client">Prevent a background process from being stopped after cl... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 455 | bash | # How to make a program continue to run after log out from ssh?
> **Possible Duplicate:**
> [Prevent a background process from being stopped after closing SSH client](https://stackoverflow.com/questions/285015/linux-prevent-a-background-process-from-being-stopped-after-closing-ssh-client)
I have a program that take... | Assuming that you have a program running in the foreground, press `Ctrl`+`Z`, then:
```
[1]+ Stopped myprogram
$ disown -h %1
$ bg 1
[1]+ myprogram &
$ logout
```
If there is only one job, then you don't need to specify the job number. Just use `disown -h` and `bg`.
## Explanation of the above steps... |
9733338 | Remove first and last quote (") from a variable | 497 | 2012-03-16 07:14:36 | <p>Below is the snippet of a shell script from a larger script. It removes the quotes from the string that is held by a variable. I am doing it using sed, but is it efficient? If not, then what is the efficient way?</p>
<pre><code>#!/bin/sh
opt="\"html\\test\\\""
temp=`echo $opt | sed 's/.\(.*\)/\1/' | sed 's/\(.*\).... | 621,181 | 1,263,746 | 2025-04-18 21:07:33 | 9,733,456 | 424 | 2012-03-16 07:27:23 | 579,590 | 2020-02-21 11:43:35 | https://stackoverflow.com/q/9733338 | https://stackoverflow.com/a/9733456 | <p>There's a simpler and more efficient way, using the native shell prefix/suffix removal feature:</p>
<pre><code>temp="${opt%\"}"
temp="${temp#\"}"
echo "$temp"
</code></pre>
<p><code>${opt%\"}</code> will remove the suffix <code>"</code> (escaped with a backslash to prevent shell interpretation).</p>
<p><code>${te... | <p>There's a simpler and more efficient way, using the native shell prefix/suffix removal feature:</p> <pre><code>temp="${opt%\"}" temp="${temp#\"}" echo "$temp" </code></pre> <p><code>${opt%\"}</code> will remove the suffix <code>"</code> (escaped with a backslash to prevent shell interpretation).</p> <p><code>${te... | 34, 139, 387, 390, 5282 | bash, sed, shell, string, unix | <h1>Remove first and last quote (") from a variable</h1>
<p>Below is the snippet of a shell script from a larger script. It removes the quotes from the string that is held by a variable. I am doing it using sed, but is it efficient? If not, then what is the efficient way?</p>
<pre><code>#!/bin/sh
opt="\"html\\test\\\... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 456 | bash | # Remove first and last quote (") from a variable
Below is the snippet of a shell script from a larger script. It removes the quotes from the string that is held by a variable. I am doing it using sed, but is it efficient? If not, then what is the efficient way?
```
#!/bin/sh
opt="\"html\\test\\\""
temp=`echo $opt |... | There's a simpler and more efficient way, using the native shell prefix/suffix removal feature:
```
temp="${opt%\"}"
temp="${temp#\"}"
echo "$temp"
```
`${opt%\"}` will remove the suffix `"` (escaped with a backslash to prevent shell interpretation).
`${temp#\"}` will remove the prefix `"` (escaped with a backslash ... |
11307257 | Is there a bash command which counts files? | 322 | 2012-07-03 08:35:10 | <p>Is there a bash command which counts the number of files that match a pattern?</p>
<p>For example, I want to get the count of all files in a directory which match this pattern: <code>log*</code></p>
| 240,928 | 431,769 | 2024-05-13 10:11:29 | 11,307,364 | 423 | 2012-07-03 08:41:54 | 1,477,247 | 2024-05-13 10:11:29 | https://stackoverflow.com/q/11307257 | https://stackoverflow.com/a/11307364 | <p>This simple one-liner should work in any shell, not just bash:</p>
<pre><code>ls -1q log* | wc -l
</code></pre>
<p><code>ls -1q</code> will give you one line per file, even if they contain whitespace or special characters such as newlines.</p>
<p>The output is piped to <code>wc -l</code> which counts the number of l... | <p>This simple one-liner should work in any shell, not just bash:</p> <pre><code>ls -1q log* | wc -l </code></pre> <p><code>ls -1q</code> will give you one line per file, even if they contain whitespace or special characters such as newlines.</p> <p>The output is piped to <code>wc -l</code> which counts the number of l... | 387 | bash | <h1>Is there a bash command which counts files?</h1>
<p>Is there a bash command which counts the number of files that match a pattern?</p>
<p>For example, I want to get the count of all files in a directory which match this pattern: <code>log*</code></p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 457 | bash | # Is there a bash command which counts files?
Is there a bash command which counts the number of files that match a pattern?
For example, I want to get the count of all files in a directory which match this pattern: `log*` | This simple one-liner should work in any shell, not just bash:
```
ls -1q log* | wc -l
```
`ls -1q` will give you one line per file, even if they contain whitespace or special characters such as newlines.
The output is piped to `wc -l` which counts the number of lines. |
158044 | How to use 'find' to search for files created on a specific date? | 320 | 2008-10-01 14:56:37 | <p>How do I use the UNIX command <code>find</code> to search for files created on a specific date?</p>
| 523,041 | 473 | 2022-08-09 16:28:01 | 158,235 | 422 | 2008-10-01 15:30:48 | 9,595 | 2015-07-28 03:51:12 | https://stackoverflow.com/q/158044 | https://stackoverflow.com/a/158235 | <p>As pointed out by Max, you can't, but checking files modified or accessed is not all that hard. I wrote a <a href="http://virtuelvis.com/2008/10/how-to-use-find-to-search-for-files-created-on-a-specific-date/" rel="noreferrer">tutorial</a> about this, as late as today. The essence of which is to use <code>-newerXY<... | <p>As pointed out by Max, you can't, but checking files modified or accessed is not all that hard. I wrote a <a href="http://virtuelvis.com/2008/10/how-to-use-find-to-search-for-files-created-on-a-specific-date/" rel="noreferrer">tutorial</a> about this, as late as today. The essence of which is to use <code>-newerXY<... | 34, 387, 10193 | bash, find, unix | <h1>How to use 'find' to search for files created on a specific date?</h1>
<p>How do I use the UNIX command <code>find</code> to search for files created on a specific date?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 458 | bash | # How to use 'find' to search for files created on a specific date?
How do I use the UNIX command `find` to search for files created on a specific date? | As pointed out by Max, you can't, but checking files modified or accessed is not all that hard. I wrote a [tutorial](http://virtuelvis.com/2008/10/how-to-use-find-to-search-for-files-created-on-a-specific-date/) about this, as late as today. The essence of which is to use `-newerXY` and `! -newerXY`:
Example: To find ... |
4561895 | How to recursively find the latest modified file in a directory? | 295 | 2010-12-30 10:37:34 | <p>It seems that <code>ls</code> doesn't sort the files correctly when doing a recursive call:</p>
<pre><code>ls -altR . | head -n 3
</code></pre>
<p>How can I find the most recently modified file in a directory (including subdirectories)?</p>
| 280,347 | 553,820 | 2024-01-31 02:40:12 | 4,561,987 | 422 | 2010-12-30 10:52:50 | 212,846 | 2021-01-08 17:50:23 | https://stackoverflow.com/q/4561895 | https://stackoverflow.com/a/4561987 | <pre><code>find . -type f -printf '%T@ %p\n' \
| sort -n | tail -1 | cut -f2- -d" "
</code></pre>
<p>For a huge tree, it might be hard for <code>sort</code> to keep everything in memory.</p>
<p><code>%T@</code> gives you the modification time like a unix timestamp, <code>sort -n</code> sorts numerically, <cod... | <pre><code>find . -type f -printf '%T@ %p\n' \ | sort -n | tail -1 | cut -f2- -d" " </code></pre> <p>For a huge tree, it might be hard for <code>sort</code> to keep everything in memory.</p> <p><code>%T@</code> gives you the modification time like a unix timestamp, <code>sort -n</code> sorts numerically, <cod... | 99, 387, 10193 | bash, filesystems, find | <h1>How to recursively find the latest modified file in a directory?</h1>
<p>It seems that <code>ls</code> doesn't sort the files correctly when doing a recursive call:</p>
<pre><code>ls -altR . | head -n 3
</code></pre>
<p>How can I find the most recently modified file in a directory (including subdirectories)?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 459 | bash | # How to recursively find the latest modified file in a directory?
It seems that `ls` doesn't sort the files correctly when doing a recursive call:
```
ls -altR . | head -n 3
```
How can I find the most recently modified file in a directory (including subdirectories)? | ```
find . -type f -printf '%T@ %p\n' \
| sort -n | tail -1 | cut -f2- -d" "
```
For a huge tree, it might be hard for `sort` to keep everything in memory.
`%T@` gives you the modification time like a unix timestamp, `sort -n` sorts numerically, `tail -1` takes the last line (highest timestamp), `cut -f2 -d" "` cuts ... |
1921279 | How to get a variable value if variable name is stored as string? | 226 | 2009-12-17 11:59:32 | <p>How can I retrieve a bash variable value if I have the variable name as string? <pre><code>var1="this is the real value"
a="var1"
Do something to get value of var1 just using variable a.
</code></pre></p>
<h3>Context:</h3>
<p>I have some AMI's (<a href="http://aws.amazon.com/ec2/" rel="noreferrer... | 193,950 | 195,802 | 2024-11-19 11:34:41 | 1,921,337 | 421 | 2009-12-17 12:09:46 | 5,981 | 2019-03-22 12:41:29 | https://stackoverflow.com/q/1921279 | https://stackoverflow.com/a/1921337 | <p>You can use <code>${!a}</code>:</p>
<pre><code>var1="this is the real value"
a="var1"
echo "${!a}" # outputs 'this is the real value'
</code></pre>
<p>This is an example of indirect <a href="http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html" rel="noreferrer">parameter expansion</a>:<... | <p>You can use <code>${!a}</code>:</p> <pre><code>var1="this is the real value" a="var1" echo "${!a}" # outputs 'this is the real value' </code></pre> <p>This is an example of indirect <a href="http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html" rel="noreferrer">parameter expansion</a>:<... | 139, 387 | bash, string | <h1>How to get a variable value if variable name is stored as string?</h1>
<p>How can I retrieve a bash variable value if I have the variable name as string? <pre><code>var1="this is the real value"
a="var1"
Do something to get value of var1 just using variable a.
</code></pre></p>
<h3>Context:</h3>... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 460 | bash | # How to get a variable value if variable name is stored as string?
How can I retrieve a bash variable value if I have the variable name as string?
```
var1="this is the real value"
a="var1"
Do something to get value of var1 just using variable a.
```
### Context:
I have some AMI's ([Amazon Machine Image](http://aw... | You can use `${!a}`:
```
var1="this is the real value"
a="var1"
echo "${!a}" # outputs 'this is the real value'
```
This is an example of indirect [parameter expansion](http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html):
> The basic form of parameter expansion is `${parameter}`. The va... |
3211595 | Renaming files in a folder to sequential numbers | 348 | 2010-07-09 10:06:06 | <p>I want to rename the files in a directory to sequential numbers. Based on creation date of the files.</p>
<p>For Example <code>sadf.jpg</code> to <code>0001.jpg</code>, <code>wrjr3.jpg</code> to <code>0002.jpg</code> and so on, the number of leading zeroes depending on the total amount of files (no need for extra z... | 379,578 | 259,316 | 2024-11-19 12:56:14 | 3,211,670 | 419 | 2010-07-09 10:18:16 | 377,927 | 2024-11-19 12:56:14 | https://stackoverflow.com/q/3211595 | https://stackoverflow.com/a/3211670 | <p>Try to use a loop, <code>let</code>, and <code>printf</code> for the padding:</p>
<pre class="lang-bash prettyprint-override"><code>a=1
for i in *.jpg; do
new=$(printf "%04d.jpg" "$a") #04 pad to length of 4
mv -i -- "$i" "$new"
let a="$a+1"
done
</code></pre>
... | <p>Try to use a loop, <code>let</code>, and <code>printf</code> for the padding:</p> <pre class="lang-bash prettyprint-override"><code>a=1 for i in *.jpg; do new=$(printf "%04d.jpg" "$a") #04 pad to length of 4 mv -i -- "$i" "$new" let a="$a+1" done </code></pre> ... | 387, 4510, 13475 | bash, batch-rename, rename | <h1>Renaming files in a folder to sequential numbers</h1>
<p>I want to rename the files in a directory to sequential numbers. Based on creation date of the files.</p>
<p>For Example <code>sadf.jpg</code> to <code>0001.jpg</code>, <code>wrjr3.jpg</code> to <code>0002.jpg</code> and so on, the number of leading zeroes d... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 461 | bash | # Renaming files in a folder to sequential numbers
I want to rename the files in a directory to sequential numbers. Based on creation date of the files.
For Example `sadf.jpg` to `0001.jpg`, `wrjr3.jpg` to `0002.jpg` and so on, the number of leading zeroes depending on the total amount of files (no need for extra zer... | Try to use a loop, `let`, and `printf` for the padding:
```
a=1
for i in *.jpg; do
new=$(printf "%04d.jpg" "$a") #04 pad to length of 4
mv -i -- "$i" "$new"
let a="$a+1"
done
```
using the `-i` flag prevents automatically overwriting existing files, and using `--` prevents `mv` from interpreting filenames with ... |
15595374 | What's the difference between nohup and ampersand | 336 | 2013-03-24 05:01:36 | <p>Both <code>nohup myprocess.out &</code> or <code>myprocess.out &</code> set myprocess.out to run in the background. After I shutdown the terminal, the process is still running.
What's the difference between them?</p>
| 230,429 | 748,731 | 2020-02-17 14:21:15 | 15,595,391 | 419 | 2013-03-24 05:04:20 | 1,643,939 | 2014-06-02 12:55:15 | https://stackoverflow.com/q/15595374 | https://stackoverflow.com/a/15595391 | <p><code>nohup</code> catches the hangup signal (see <code>man 7 signal</code>) while the ampersand doesn't (except the shell is confgured that way or doesn't send <code>SIGHUP</code> at all).</p>
<p>Normally, when running a command using <code>&</code> and exiting the shell afterwards, the shell will terminate th... | <p><code>nohup</code> catches the hangup signal (see <code>man 7 signal</code>) while the ampersand doesn't (except the shell is confgured that way or doesn't send <code>SIGHUP</code> at all).</p> <p>Normally, when running a command using <code>&</code> and exiting the shell afterwards, the shell will terminate th... | 58, 387, 18966 | bash, linux, nohup | <h1>What's the difference between nohup and ampersand</h1>
<p>Both <code>nohup myprocess.out &</code> or <code>myprocess.out &</code> set myprocess.out to run in the background. After I shutdown the terminal, the process is still running.
What's the difference between them?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 462 | bash | # What's the difference between nohup and ampersand
Both `nohup myprocess.out &` or `myprocess.out &` set myprocess.out to run in the background. After I shutdown the terminal, the process is still running.
What's the difference between them? | `nohup` catches the hangup signal (see `man 7 signal`) while the ampersand doesn't (except the shell is confgured that way or doesn't send `SIGHUP` at all).
Normally, when running a command using `&` and exiting the shell afterwards, the shell will terminate the sub-command with the hangup signal (`kill -SIGHUP <pid>`... |
40027395 | Passing bash variable to jq | 278 | 2016-10-13 17:28:55 | <p>I have written a script to retrieve certain value from <code>file.json</code>. It works if I provide the value to jq <code>select</code>, but the variable doesn't seem to work (or I don't know how to use it).</p>
<pre><code>#!/bin/sh
#this works ***
projectID=$(cat file.json | jq -r '.resource[] | select(.username... | 319,182 | 7,015,163 | 2024-08-07 07:56:02 | 40,027,637 | 419 | 2016-10-13 17:42:40 | 997,358 | 2023-03-04 16:39:37 | https://stackoverflow.com/q/40027395 | https://stackoverflow.com/a/40027637 | <p>Consider also passing in the shell variable (EMAILID) as a jq variable (here also EMAILID, for the sake of illustration):</p>
<pre><code> projectID=$(jq -r --arg EMAILID "$EMAILID" '
.resource[]
| select(.username==$EMAILID)
| .id' file.json)
</code></pre>
<h3>Postscript</h3>
<p>... | <p>Consider also passing in the shell variable (EMAILID) as a jq variable (here also EMAILID, for the sake of illustration):</p> <pre><code> projectID=$(jq -r --arg EMAILID "$EMAILID" ' .resource[] | select(.username==$EMAILID) | .id' file.json) </code></pre> <h3>Postscript</h3> <p>... | 387, 1508, 9013, 105170 | bash, environment-variables, jq, json | <h1>Passing bash variable to jq</h1>
<p>I have written a script to retrieve certain value from <code>file.json</code>. It works if I provide the value to jq <code>select</code>, but the variable doesn't seem to work (or I don't know how to use it).</p>
<pre><code>#!/bin/sh
#this works ***
projectID=$(cat file.json | ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 463 | bash | # Passing bash variable to jq
I have written a script to retrieve certain value from `file.json`. It works if I provide the value to jq `select`, but the variable doesn't seem to work (or I don't know how to use it).
```
#!/bin/sh
#this works ***
projectID=$(cat file.json | jq -r '.resource[] | select(.username=="my... | Consider also passing in the shell variable (EMAILID) as a jq variable (here also EMAILID, for the sake of illustration):
```
projectID=$(jq -r --arg EMAILID "$EMAILID" '
.resource[]
| select(.username==$EMAILID)
| .id' file.json)
```
### Postscript
For the record, another possibility wou... |
8742783 | Returning value from called function in a shell script | 207 | 2012-01-05 12:52:57 | <p>I want to return the value from a function called in a shell script. Perhaps I am missing the syntax. I tried using the global variables. But that is also not working. The code is:</p>
<pre><code>lockdir="somedir"
test() {
retval=""
if mkdir "$lockdir"
then # Directory did not exist, but it was ... | 558,213 | 1,097,890 | 2019-12-02 08:26:46 | 8,743,103 | 419 | 2012-01-05 13:13:57 | 938,111 | 2017-10-13 16:51:56 | https://stackoverflow.com/q/8742783 | https://stackoverflow.com/a/8743103 | <p>A Bash function can't return a string directly like you want it to. You can do three things:</p>
<ol>
<li>Echo a string</li>
<li>Return an exit status, which is a number, not a string</li>
<li>Share a variable</li>
</ol>
<p>This is also true for some other shells.</p>
<p>Here's how to do each of those options:</p... | <p>A Bash function can't return a string directly like you want it to. You can do three things:</p> <ol> <li>Echo a string</li> <li>Return an exit status, which is a number, not a string</li> <li>Share a variable</li> </ol> <p>This is also true for some other shells.</p> <p>Here's how to do each of those options:</p... | 390, 9206 | return-value, shell | <h1>Returning value from called function in a shell script</h1>
<p>I want to return the value from a function called in a shell script. Perhaps I am missing the syntax. I tried using the global variables. But that is also not working. The code is:</p>
<pre><code>lockdir="somedir"
test() {
retval=""
if mkdir "... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 464 | bash | # Returning value from called function in a shell script
I want to return the value from a function called in a shell script. Perhaps I am missing the syntax. I tried using the global variables. But that is also not working. The code is:
```
lockdir="somedir"
test() {
retval=""
if mkdir "$lockdir"
th... | A Bash function can't return a string directly like you want it to. You can do three things:
1. Echo a string
2. Return an exit status, which is a number, not a string
3. Share a variable
This is also true for some other shells.
Here's how to do each of those options:
## 1. Echo strings
```
lockdir="somedir"
testl... |
1224766 | How do I rename the extension for a bunch of files? | 572 | 2009-08-03 21:42:10 | <p>In a directory, I have a bunch of <code>*.html</code> files. I'd like to rename them all to <code>*.txt</code></p>
<p>How can I do that?<br />
I use the Bash shell.</p>
| 443,329 | 89,566 | 2023-12-19 15:10:06 | 1,224,786 | 418 | 2009-08-03 21:46:11 | 59,062 | 2017-09-14 11:59:00 | https://stackoverflow.com/q/1224766 | https://stackoverflow.com/a/1224786 | <p>For an better solution (with only bash functionality, as opposed to external calls), see <a href="https://stackoverflow.com/questions/1224766/bash-rename-files/1225236#1225236">one of the other answers</a>.</p>
<hr>
<p>The following would do and does not require the system to have the <code>rename</code> program (... | <p>For an better solution (with only bash functionality, as opposed to external calls), see <a href="https://stackoverflow.com/questions/1224766/bash-rename-files/1225236#1225236">one of the other answers</a>.</p> <hr> <p>The following would do and does not require the system to have the <code>rename</code> program (... | 387, 4510, 12895 | bash, file-rename, rename | <h1>How do I rename the extension for a bunch of files?</h1>
<p>In a directory, I have a bunch of <code>*.html</code> files. I'd like to rename them all to <code>*.txt</code></p>
<p>How can I do that?<br />
I use the Bash shell.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 465 | bash | # How do I rename the extension for a bunch of files?
In a directory, I have a bunch of `*.html` files. I'd like to rename them all to `*.txt`
How can I do that?
I use the Bash shell. | For an better solution (with only bash functionality, as opposed to external calls), see [one of the other answers](https://stackoverflow.com/questions/1224766/bash-rename-files/1225236#1225236).
---
The following would do and does not require the system to have the `rename` program (although you would most often hav... |
17203122 | How to run one command if a previous command succeeds, otherwise run another command | 313 | 2013-06-19 23:42:26 | <p>I am trying to check if a process (assume it is called <code>some_process</code>) is running on a server. If it is, then echo 1, otherwise echo 0.</p>
<p>This is the command that I am using but it only works partially (more info below). Note that I need to write the script in one line.</p>
<pre><code>ps aux | grep... | 632,742 | 1,512,056 | 2023-02-27 11:56:27 | 17,203,203 | 418 | 2013-06-19 23:52:01 | 140,750 | 2017-06-16 19:22:08 | https://stackoverflow.com/q/17203122 | https://stackoverflow.com/a/17203203 | <p>There is no need to explicitly check <code>$?</code>. Just do:</p>
<pre><code>ps aux | grep some_proces[s] > /tmp/test.txt && echo 1 || echo 0
</code></pre>
<p>Note that this relies on echo not failing, which is certainly not guaranteed. A more reliable way to write this is:</p>
<pre><code>if ps aux... | <p>There is no need to explicitly check <code>$?</code>. Just do:</p> <pre><code>ps aux | grep some_proces[s] > /tmp/test.txt && echo 1 || echo 0 </code></pre> <p>Note that this relies on echo not failing, which is certainly not guaranteed. A more reliable way to write this is:</p> <pre><code>if ps aux... | 387 | bash | <h1>How to run one command if a previous command succeeds, otherwise run another command</h1>
<p>I am trying to check if a process (assume it is called <code>some_process</code>) is running on a server. If it is, then echo 1, otherwise echo 0.</p>
<p>This is the command that I am using but it only works partially (mor... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 466 | bash | # How to run one command if a previous command succeeds, otherwise run another command
I am trying to check if a process (assume it is called `some_process`) is running on a server. If it is, then echo 1, otherwise echo 0.
This is the command that I am using but it only works partially (more info below). Note that I ... | There is no need to explicitly check `$?`. Just do:
```
ps aux | grep some_proces[s] > /tmp/test.txt && echo 1 || echo 0
```
Note that this relies on echo not failing, which is certainly not guaranteed. A more reliable way to write this is:
```
if ps aux | grep some_proces[s] > /tmp/test.txt; then echo 1; else echo ... |
3487265 | PowerShell script to return versions of .NET Framework on a machine? | 229 | 2010-08-15 12:02:02 | <p>What would a PowerShell script be to return versions of the .NET Framework on a machine?</p>
<p>My first guess is something involving WMI. Is there something better?</p>
<p>It should be a one-liner to return only the latest version for each installation of .NET [on each line].</p>
| 451,745 | 327,196 | 2024-07-03 16:43:31 | 3,495,491 | 417 | 2010-08-16 17:09:24 | 8,718 | 2020-01-12 23:59:38 | https://stackoverflow.com/q/3487265 | https://stackoverflow.com/a/3495491 | <p>If you're going to use the registry you have to recurse in order to get the full version for the 4.x Framework. The earlier answers both return the root number on my system for .NET 3.0 (where the WCF and WPF numbers, which are nested under 3.0, are higher -- I can't explain that), and fail to return anything for 4.... | <p>If you're going to use the registry you have to recurse in order to get the full version for the 4.x Framework. The earlier answers both return the root number on my system for .NET 3.0 (where the WCF and WPF numbers, which are nested under 3.0, are higher -- I can't explain that), and fail to return anything for 4.... | 1, 526, 5792 | .net, powershell, version | <h1>PowerShell script to return versions of .NET Framework on a machine?</h1>
<p>What would a PowerShell script be to return versions of the .NET Framework on a machine?</p>
<p>My first guess is something involving WMI. Is there something better?</p>
<p>It should be a one-liner to return only the latest version for e... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 468 | bash | # PowerShell script to return versions of .NET Framework on a machine?
What would a PowerShell script be to return versions of the .NET Framework on a machine?
My first guess is something involving WMI. Is there something better?
It should be a one-liner to return only the latest version for each installation of .NE... | If you're going to use the registry you have to recurse in order to get the full version for the 4.x Framework. The earlier answers both return the root number on my system for .NET 3.0 (where the WCF and WPF numbers, which are nested under 3.0, are higher -- I can't explain that), and fail to return anything for 4.0 .... |
1429556 | Command to get nth line of STDOUT | 277 | 2009-09-15 20:53:09 | <p>Is there any bash command that will let you get the nth line of STDOUT?</p>
<p>That is to say, something that would take this</p>
<pre><code>$ ls -l
-rw-r--r--@ 1 root wheel my.txt
-rw-r--r--@ 1 root wheel files.txt
-rw-r--r--@ 1 root wheel here.txt
</code></pre>
<p>and do something like</p>
<pre><code>$ ls -... | 229,528 | 4,668 | 2019-06-18 06:08:31 | 1,429,628 | 416 | 2009-09-15 21:06:11 | 15,168 | 2009-09-15 21:39:20 | https://stackoverflow.com/q/1429556 | https://stackoverflow.com/a/1429628 | <p>Using <code>sed</code>, just for variety:</p>
<pre><code>ls -l | sed -n 2p
</code></pre>
<p>Using this alternative, which looks more efficient since it stops reading the input when the required line is printed, may generate a SIGPIPE in the feeding process, which may in turn generate an unwanted error message:</p>... | <p>Using <code>sed</code>, just for variety:</p> <pre><code>ls -l | sed -n 2p </code></pre> <p>Using this alternative, which looks more efficient since it stops reading the input when the required line is printed, may generate a SIGPIPE in the feeding process, which may in turn generate an unwanted error message:</p>... | 387, 390, 4867, 5282 | bash, sed, shell, stdout | <h1>Command to get nth line of STDOUT</h1>
<p>Is there any bash command that will let you get the nth line of STDOUT?</p>
<p>That is to say, something that would take this</p>
<pre><code>$ ls -l
-rw-r--r--@ 1 root wheel my.txt
-rw-r--r--@ 1 root wheel files.txt
-rw-r--r--@ 1 root wheel here.txt
</code></pre>
<p>a... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 469 | bash | # Command to get nth line of STDOUT
Is there any bash command that will let you get the nth line of STDOUT?
That is to say, something that would take this
```
$ ls -l
-rw-r--r--@ 1 root wheel my.txt
-rw-r--r--@ 1 root wheel files.txt
-rw-r--r--@ 1 root wheel here.txt
```
and do something like
```
$ ls -l | magi... | Using `sed`, just for variety:
```
ls -l | sed -n 2p
```
Using this alternative, which looks more efficient since it stops reading the input when the required line is printed, may generate a SIGPIPE in the feeding process, which may in turn generate an unwanted error message:
```
ls -l | sed -n -e '2{p;q}'
```
I've... |
19454837 | bash (or zsh) HISTSIZE vs. HISTFILESIZE? | 256 | 2013-10-18 16:43:37 | <p>What is the difference in <code>HISTSIZE</code> vs. <code>HISTFILESIZE</code>?</p>
<p>They are used to extend bash history beyond the default 500 lines.</p>
<p>There seems to be a lack of clarity here and in other forums about why they are both needed. (<a href="https://www.unix.com/unix-for-dummies-questions-and-an... | 103,149 | 583,834 | 2025-11-03 18:16:10 | 19,454,838 | 416 | 2013-10-18 16:43:37 | 583,834 | 2025-11-03 18:16:10 | https://stackoverflow.com/q/19454837 | https://stackoverflow.com/a/19454838 | <h2>Short answer:</h2>
<p><code>HISTSIZE</code> is the number of lines or commands that are stored in memory in a history list while your bash session is ongoing.</p>
<p><code>HISTFILESIZE</code> is the number of lines or commands that (a) are allowed in the history file at startup time of a session, and (b) are stored... | <h2>Short answer:</h2> <p><code>HISTSIZE</code> is the number of lines or commands that are stored in memory in a history list while your bash session is ongoing.</p> <p><code>HISTFILESIZE</code> is the number of lines or commands that (a) are allowed in the history file at startup time of a session, and (b) are stored... | 34, 58, 387 | bash, linux, unix | <h1>bash (or zsh) HISTSIZE vs. HISTFILESIZE?</h1>
<p>What is the difference in <code>HISTSIZE</code> vs. <code>HISTFILESIZE</code>?</p>
<p>They are used to extend bash history beyond the default 500 lines.</p>
<p>There seems to be a lack of clarity here and in other forums about why they are both needed. (<a href="http... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 470 | bash | # bash (or zsh) HISTSIZE vs. HISTFILESIZE?
What is the difference in `HISTSIZE` vs. `HISTFILESIZE`?
They are used to extend bash history beyond the default 500 lines.
There seems to be a lack of clarity here and in other forums about why they are both needed. ([Example 1](https://www.unix.com/unix-for-dummies-questi... | ## Short answer:
`HISTSIZE` is the number of lines or commands that are stored in memory in a history list while your bash session is ongoing.
`HISTFILESIZE` is the number of lines or commands that (a) are allowed in the history file at startup time of a session, and (b) are stored in the history file at the end of y... |
2220301 | How to evaluate http response codes from bash/shell script? | 287 | 2010-02-08 08:25:33 | <p>I have the feeling that I'm missing the obvious, but have not succeeded with <code>man [curl|wget]</code> or google ("http" makes such a bad search term). I'm looking for a quick&dirty fix to one of our webservers that frequently fails, returning status code 500 with an error message. Once this happens, it needs... | 400,902 | 13,447 | 2022-05-23 10:21:49 | 2,220,646 | 415 | 2010-02-08 09:48:46 | 26,428 | 2020-06-25 07:27:15 | https://stackoverflow.com/q/2220301 | https://stackoverflow.com/a/2220646 | <p>I haven't tested this on a 500 code, but it works on others like 200, 302 and 404.</p>
<pre><code>response=$(curl --write-out '%{http_code}' --silent --output /dev/null servername)
</code></pre>
<p>Note, format provided for --write-out should be quoted.
As suggested by @ibai, add <code>--head</code> to make a HEAD o... | <p>I haven't tested this on a 500 code, but it works on others like 200, 302 and 404.</p> <pre><code>response=$(curl --write-out '%{http_code}' --silent --output /dev/null servername) </code></pre> <p>Note, format provided for --write-out should be quoted. As suggested by @ibai, add <code>--head</code> to make a HEAD o... | 387, 390, 5210, 42676 | bash, http, http-headers, shell | <h1>How to evaluate http response codes from bash/shell script?</h1>
<p>I have the feeling that I'm missing the obvious, but have not succeeded with <code>man [curl|wget]</code> or google ("http" makes such a bad search term). I'm looking for a quick&dirty fix to one of our webservers that frequently fails, returni... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 471 | bash | # How to evaluate http response codes from bash/shell script?
I have the feeling that I'm missing the obvious, but have not succeeded with `man [curl|wget]` or google ("http" makes such a bad search term). I'm looking for a quick&dirty fix to one of our webservers that frequently fails, returning status code 500 with ... | I haven't tested this on a 500 code, but it works on others like 200, 302 and 404.
```
response=$(curl --write-out '%{http_code}' --silent --output /dev/null servername)
```
Note, format provided for --write-out should be quoted.
As suggested by @ibai, add `--head` to make a HEAD only request. This will save time whe... |
19242275 | RE error: illegal byte sequence on Mac OS X | 283 | 2013-10-08 08:03:05 | <p>I'm trying to replace a string in a Makefile on Mac OS X for cross-compiling to iOS. The string has embedded double quotes. The command is:</p>
<pre><code>sed -i "" 's|"iphoneos-cross","llvm-gcc:-O3|"iphoneos-cross","clang:-Os|g' Configure
</code></pre>
<p>And the error is:</p>
<pre><code>sed: RE error: illegal b... | 233,745 | 608,639 | 2023-04-15 01:41:22 | 23,584,470 | 415 | 2014-05-10 17:53:19 | 45,375 | 2018-05-15 04:38:23 | https://stackoverflow.com/q/19242275 | https://stackoverflow.com/a/23584470 | <p><sup>A sample command that exhibits the symptom: <code>sed 's/./@/' <<<$'\xfc'</code> fails, because byte <code>0xfc</code> is not a valid UTF-8 char.<br>
Note that, by contrast, <em>GNU</em> <code>sed</code> (Linux, but also installable on macOS) simply passes the invalid byte through, without reporting an... | <p><sup>A sample command that exhibits the symptom: <code>sed 's/./@/' <<<$'\xfc'</code> fails, because byte <code>0xfc</code> is not a valid UTF-8 char.<br> Note that, by contrast, <em>GNU</em> <code>sed</code> (Linux, but also installable on macOS) simply passes the invalid byte through, without reporting an... | 18, 369, 387, 5282 | bash, macos, regex, sed | <h1>RE error: illegal byte sequence on Mac OS X</h1>
<p>I'm trying to replace a string in a Makefile on Mac OS X for cross-compiling to iOS. The string has embedded double quotes. The command is:</p>
<pre><code>sed -i "" 's|"iphoneos-cross","llvm-gcc:-O3|"iphoneos-cross","clang:-Os|g' Configure
</code></pre>
<p>And t... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 472 | bash | # RE error: illegal byte sequence on Mac OS X
I'm trying to replace a string in a Makefile on Mac OS X for cross-compiling to iOS. The string has embedded double quotes. The command is:
```
sed -i "" 's|"iphoneos-cross","llvm-gcc:-O3|"iphoneos-cross","clang:-Os|g' Configure
```
And the error is:
```
sed: RE error: ... | A sample command that exhibits the symptom: `sed 's/./@/' <<<$'\xfc'` fails, because byte `0xfc` is not a valid UTF-8 char.
Note that, by contrast, *GNU* `sed` (Linux, but also installable on macOS) simply passes the invalid byte through, without reporting an error.
Using the **[formerly accepted answer](https://sta... |
673055 | Correct Bash and shell script variable capitalization | 323 | 2009-03-23 11:34:29 | <p>I run across many shell scripts with variables in all caps, and I've always thought that there is a severe misunderstanding with that. My understanding is that, by convention (and perhaps by necessity long ago), <strong>environment variables</strong> are in all-caps.</p>
<p>But in modern scripting environments like... | 109,236 | 2,938 | 2025-10-09 09:01:21 | 673,940 | 414 | 2009-03-23 15:53:24 | 58,803 | 2018-05-11 17:07:44 | https://stackoverflow.com/q/673055 | https://stackoverflow.com/a/673940 | <p>By convention, environment variables (<code>PAGER</code>, <code>EDITOR</code>, ...) and internal shell variables (<code>SHELL</code>, <code>BASH_VERSION</code>, ...) are capitalized. All other variable names should be lower case.</p>
<p>Remember that variable names are case-sensitive; this convention avoids acciden... | <p>By convention, environment variables (<code>PAGER</code>, <code>EDITOR</code>, ...) and internal shell variables (<code>SHELL</code>, <code>BASH_VERSION</code>, ...) are capitalized. All other variable names should be lower case.</p> <p>Remember that variable names are case-sensitive; this convention avoids acciden... | 387, 390, 531, 7028, 11789 | bash, capitalization, naming-conventions, scripting, shell | <h1>Correct Bash and shell script variable capitalization</h1>
<p>I run across many shell scripts with variables in all caps, and I've always thought that there is a severe misunderstanding with that. My understanding is that, by convention (and perhaps by necessity long ago), <strong>environment variables</strong> ar... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 473 | bash | # Correct Bash and shell script variable capitalization
I run across many shell scripts with variables in all caps, and I've always thought that there is a severe misunderstanding with that. My understanding is that, by convention (and perhaps by necessity long ago), **environment variables** are in all-caps.
But in ... | By convention, environment variables (`PAGER`, `EDITOR`, ...) and internal shell variables (`SHELL`, `BASH_VERSION`, ...) are capitalized. All other variable names should be lower case.
Remember that variable names are case-sensitive; this convention avoids accidentally overriding environmental and internal variables.... |
15580144 | How to concatenate multiple lines of output to one line? | 298 | 2013-03-22 21:27:51 | <p>If I run the command <code>cat file | grep pattern</code>, I get many lines of output. How do you concatenate all lines into one line, effectively replacing each <code>"\n"</code> with <code>"\" "</code> (end with <code>"</code> followed by space)?</p>
<p><code>cat file | grep pattern | xargs sed s/\n/ /g</code>
i... | 459,499 | 266,457 | 2024-05-17 09:01:03 | 15,580,184 | 412 | 2013-03-22 21:31:18 | 1,066,031 | 2013-03-22 21:42:19 | https://stackoverflow.com/q/15580144 | https://stackoverflow.com/a/15580184 | <p>Use <code>tr '\n' ' '</code> to translate all newline characters to spaces:</p>
<pre><code>$ grep pattern file | tr '\n' ' '
</code></pre>
<p>Note: <code>grep</code> reads files, <code>cat</code> concatenates files. Don't <code>cat file | grep</code>!</p>
<p><strong>Edit:</strong></p>
<p><code>tr</code> can only... | <p>Use <code>tr '\n' ' '</code> to translate all newline characters to spaces:</p> <pre><code>$ grep pattern file | tr '\n' ' ' </code></pre> <p>Note: <code>grep</code> reads files, <code>cat</code> concatenates files. Don't <code>cat file | grep</code>!</p> <p><strong>Edit:</strong></p> <p><code>tr</code> can only... | 58, 387, 1271, 5282, 18990 | bash, grep, linux, sed, tr | <h1>How to concatenate multiple lines of output to one line?</h1>
<p>If I run the command <code>cat file | grep pattern</code>, I get many lines of output. How do you concatenate all lines into one line, effectively replacing each <code>"\n"</code> with <code>"\" "</code> (end with <code>"</code> followed by space)?</p... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 474 | bash | # How to concatenate multiple lines of output to one line?
If I run the command `cat file | grep pattern`, I get many lines of output. How do you concatenate all lines into one line, effectively replacing each `"\n"` with `"\" "` (end with `"` followed by space)?
`cat file | grep pattern | xargs sed s/\n/ /g`
isn't w... | Use `tr '\n' ' '` to translate all newline characters to spaces:
```
$ grep pattern file | tr '\n' ' '
```
Note: `grep` reads files, `cat` concatenates files. Don't `cat file | grep`!
**Edit:**
`tr` can only handle single character translations. You could use `awk` to change the output record separator like:
```
$... |
4419952 | Difference between return and exit in Bash functions | 561 | 2010-12-12 01:28:10 | <p>What is the difference between the <code>return</code> and <code>exit</code> statement in Bash functions with respect to exit codes?</p>
| 612,771 | 241,866 | 2022-05-08 12:50:02 | 4,419,956 | 411 | 2010-12-12 01:29:39 | 62,365 | 2018-09-28 08:50:47 | https://stackoverflow.com/q/4419952 | https://stackoverflow.com/a/4419956 | <p>From <code>man bash</code> on <code>return [n]</code>;</p>
<blockquote>
<p>Causes a function to stop executing and return the value specified by n to its caller. If n is omitted, the return status is that of the last command executed in the function body.</p>
</blockquote>
<p>... on <code>exit [n]</code>:<... | <p>From <code>man bash</code> on <code>return [n]</code>;</p> <blockquote> <p>Causes a function to stop executing and return the value specified by n to its caller. If n is omitted, the return status is that of the last command executed in the function body.</p> </blockquote> <p>... on <code>exit [n]</code>:<... | 387, 4705, 5569, 9206, 10871 | bash, exit, function, return, return-value | <h1>Difference between return and exit in Bash functions</h1>
<p>What is the difference between the <code>return</code> and <code>exit</code> statement in Bash functions with respect to exit codes?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 475 | bash | # Difference between return and exit in Bash functions
What is the difference between the `return` and `exit` statement in Bash functions with respect to exit codes? | From `man bash` on `return [n]`;
> Causes a function to stop executing and return the value specified by n to its caller. If n is omitted, the return status is that of the last command executed in the function body.
... on `exit [n]`:
> Cause the shell to exit with a status of n. If n is omitted, the exit status is ... |
29142 | Getting ssh to execute a command in the background on target machine | 375 | 2008-08-26 22:55:58 | <p>This is a follow-on question to the <a href="https://stackoverflow.com/questions/29061/how-do-you-use-ssh-in-a-shell-script">How do you use ssh in a shell script?</a> question. If I want to execute a command on the remote machine that runs in the background on that machine, how do I get the ssh command to return? ... | 416,595 | 171 | 2023-04-11 20:24:07 | 29,172 | 408 | 2008-08-26 23:18:41 | 23 | 2023-04-11 20:24:07 | https://stackoverflow.com/q/29142 | https://stackoverflow.com/a/29172 | <p>This should solve your problem:</p>
<pre><code>nohup myprogram > foo.log 2> foo.err < /dev/null &
</code></pre>
<hr />
<p>The syntax and unusual use of <code>< /dev/null</code> are explained especially well <a href="https://stackoverflow.com/a/19956266/23">in this answer</a>, quoted here for your con... | <p>This should solve your problem:</p> <pre><code>nohup myprogram > foo.log 2> foo.err < /dev/null & </code></pre> <hr /> <p>The syntax and unusual use of <code>< /dev/null</code> are explained especially well <a href="https://stackoverflow.com/a/19956266/23">in this answer</a>, quoted here for your con... | 386, 387, 2829 | bash, csh, ssh | <h1>Getting ssh to execute a command in the background on target machine</h1>
<p>This is a follow-on question to the <a href="https://stackoverflow.com/questions/29061/how-do-you-use-ssh-in-a-shell-script">How do you use ssh in a shell script?</a> question. If I want to execute a command on the remote machine that run... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 476 | bash | # Getting ssh to execute a command in the background on target machine
This is a follow-on question to the [How do you use ssh in a shell script?](https://stackoverflow.com/questions/29061/how-do-you-use-ssh-in-a-shell-script) question. If I want to execute a command on the remote machine that runs in the background o... | This should solve your problem:
```
nohup myprogram > foo.log 2> foo.err < /dev/null &
```
---
The syntax and unusual use of `< /dev/null` are explained especially well [in this answer](https://stackoverflow.com/a/19956266/23), quoted here for your convenience.
> `< /dev/null` is used to instantly send EOF to the p... |
53785383 | How to change Node.js version with nvm | 311 | 2018-12-14 18:52:09 | <p>I'm using <a href="https://en.wikipedia.org/wiki/Yeoman_(software)" rel="noreferrer">Yeoman</a> to create a project. When I try to use <a href="https://en.wikipedia.org/wiki/Gulp.js" rel="noreferrer">Gulp.js</a> I run the command <code>gulp serve</code>. An error tells me that I need an older version of Node.js (8.9... | 767,687 | 5,711,367 | 2024-11-12 08:31:10 | 53,785,482 | 408 | 2018-12-14 19:00:43 | 1,118,978 | 2022-09-30 14:56:33 | https://stackoverflow.com/q/53785383 | https://stackoverflow.com/a/53785482 | <p><code>nvm install 8.10.0</code> is for installing proposed node version locally.</p>
<p>In order to use it:</p>
<pre class="lang-none prettyprint-override"><code>nvm use 8.10.0
</code></pre>
<p>Note that you need to run this command as administrator.</p>
<p>You can always set default Node.js version:</p>
<pre class=... | <p><code>nvm install 8.10.0</code> is for installing proposed node version locally.</p> <p>In order to use it:</p> <pre class="lang-none prettyprint-override"><code>nvm use 8.10.0 </code></pre> <p>Note that you need to run this command as administrator.</p> <p>You can always set default Node.js version:</p> <pre class=... | 390, 46426, 81748 | node.js, nvm, shell | <h1>How to change Node.js version with nvm</h1>
<p>I'm using <a href="https://en.wikipedia.org/wiki/Yeoman_(software)" rel="noreferrer">Yeoman</a> to create a project. When I try to use <a href="https://en.wikipedia.org/wiki/Gulp.js" rel="noreferrer">Gulp.js</a> I run the command <code>gulp serve</code>. An error tells... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 477 | bash | # How to change Node.js version with nvm
I'm using [Yeoman](https://en.wikipedia.org/wiki/Yeoman_(software)) to create a project. When I try to use [Gulp.js](https://en.wikipedia.org/wiki/Gulp.js) I run the command `gulp serve`. An error tells me that I need an older version of Node.js (8.9.4), knowing that I've insta... | `nvm install 8.10.0` is for installing proposed node version locally.
In order to use it:
```
nvm use 8.10.0
```
Note that you need to run this command as administrator.
You can always set default Node.js version:
```
nvm alias default 8.10.0
``` |
2314750 | How to assign the output of a Bash command to a variable? | 238 | 2010-02-22 22:25:31 | <p>I have a problem putting the content of <code>pwd</code> command into a shell variable that I'll use later. </p>
<p>Here is my shell code (the loop doesn't stop):</p>
<pre><code>#!/bin/bash
pwd= `pwd`
until [ $pwd = "/" ]
do
echo $pwd
ls && cd .. && ls
$pwd= `pwd`
... | 452,342 | 262,854 | 2019-07-05 13:01:43 | 2,314,765 | 408 | 2010-02-22 22:27:15 | 102,371 | 2019-07-05 13:01:43 | https://stackoverflow.com/q/2314750 | https://stackoverflow.com/a/2314765 | <p>Try:</p>
<pre><code>pwd=`pwd`
</code></pre>
<p>or</p>
<pre><code>pwd=$(pwd)
</code></pre>
<blockquote>
<p>Notice no spaces after the equals sign.</p>
</blockquote>
<p>Also as Mr. Weiss points out; you don't assign to <code>$pwd</code>, you assign to <code>pwd</code>.</p>
| <p>Try:</p> <pre><code>pwd=`pwd` </code></pre> <p>or</p> <pre><code>pwd=$(pwd) </code></pre> <blockquote> <p>Notice no spaces after the equals sign.</p> </blockquote> <p>Also as Mr. Weiss points out; you don't assign to <code>$pwd</code>, you assign to <code>pwd</code>.</p> | 387, 390 | bash, shell | <h1>How to assign the output of a Bash command to a variable?</h1>
<p>I have a problem putting the content of <code>pwd</code> command into a shell variable that I'll use later. </p>
<p>Here is my shell code (the loop doesn't stop):</p>
<pre><code>#!/bin/bash
pwd= `pwd`
until [ $pwd = "/" ]
do
echo $pwd
... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 478 | bash | # How to assign the output of a Bash command to a variable?
I have a problem putting the content of `pwd` command into a shell variable that I'll use later.
Here is my shell code (the loop doesn't stop):
```
#!/bin/bash
pwd= `pwd`
until [ $pwd = "/" ]
do
echo $pwd
ls && cd .. && ls
$pwd=... | Try:
```
pwd=`pwd`
```
or
```
pwd=$(pwd)
```
> Notice no spaces after the equals sign.
Also as Mr. Weiss points out; you don't assign to `$pwd`, you assign to `pwd`. |
7450818 | Rename all files in directory from $filename_h to $filename_half? | 205 | 2011-09-16 22:00:04 | <p>Dead simple.</p>
<p>How do I rename </p>
<pre><code>05_h.png
06_h.png
</code></pre>
<p>to </p>
<pre><code>05_half.png
06_half.png
</code></pre>
<p>At least, I think it's simple, but it's hard to Google for this kind of thing unless you already know. </p>
<p>Thanks....</p>
| 273,632 | 806,539 | 2022-11-11 12:28:59 | 7,451,880 | 408 | 2011-09-17 01:32:59 | 716,256 | 2016-08-26 22:11:46 | https://stackoverflow.com/q/7450818 | https://stackoverflow.com/a/7451880 | <p>Just use bash, no need to call external commands.</p>
<pre><code>for file in *_h.png
do
mv "$file" "${file/_h.png/_half.png}"
done
</code></pre>
<p><strong>Do not add <code>#!/bin/sh</code></strong></p>
<p>For those that need that one-liner:</p>
<pre><code>for file in *.png; do mv "$file" "${file/_h.png/_half.... | <p>Just use bash, no need to call external commands.</p> <pre><code>for file in *_h.png do mv "$file" "${file/_h.png/_half.png}" done </code></pre> <p><strong>Do not add <code>#!/bin/sh</code></strong></p> <p>For those that need that one-liner:</p> <pre><code>for file in *.png; do mv "$file" "${file/_h.png/_half.... | 387, 390 | bash, shell | <h1>Rename all files in directory from $filename_h to $filename_half?</h1>
<p>Dead simple.</p>
<p>How do I rename </p>
<pre><code>05_h.png
06_h.png
</code></pre>
<p>to </p>
<pre><code>05_half.png
06_half.png
</code></pre>
<p>At least, I think it's simple, but it's hard to Google for this kind of thing unless you a... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 479 | bash | # Rename all files in directory from $filename_h to $filename_half?
Dead simple.
How do I rename
```
05_h.png
06_h.png
```
to
```
05_half.png
06_half.png
```
At least, I think it's simple, but it's hard to Google for this kind of thing unless you already know.
Thanks.... | Just use bash, no need to call external commands.
```
for file in *_h.png
do
mv "$file" "${file/_h.png/_half.png}"
done
```
**Do not add `#!/bin/sh`**
For those that need that one-liner:
```
for file in *.png; do mv "$file" "${file/_h.png/_half.png}"; done
``` |
11532157 | Remove duplicate lines without sorting | 168 | 2012-07-17 23:14:11 | <p>I have a utility script in Python:</p>
<pre><code>#!/usr/bin/env python
import sys
unique_lines = []
duplicate_lines = []
for line in sys.stdin:
if line in unique_lines:
duplicate_lines.append(line)
else:
unique_lines.append(line)
sys.stdout.write(line)
# optionally do something with duplicate_lines... | 92,948 | 916,332 | 2024-07-28 11:40:51 | 11,532,197 | 408 | 2012-07-17 23:17:57 | 494,061 | 2024-07-28 11:40:51 | https://stackoverflow.com/q/11532157 | https://stackoverflow.com/a/11532197 | <p>The UNIX Bash Scripting blog suggests:</p>
<pre><code>awk '!x[$0]++'
</code></pre>
<p>This command is telling awk which lines to print. The variable <code>$0</code> holds the entire contents of a line and square brackets are array access. So, for each line of the file, the node of the array <code>x</code> is increme... | <p>The UNIX Bash Scripting blog suggests:</p> <pre><code>awk '!x[$0]++' </code></pre> <p>This command is telling awk which lines to print. The variable <code>$0</code> holds the entire contents of a line and square brackets are array access. So, for each line of the file, the node of the array <code>x</code> is increme... | 390, 531, 10052, 35617 | filter, scripting, shell, uniq | <h1>Remove duplicate lines without sorting</h1>
<p>I have a utility script in Python:</p>
<pre><code>#!/usr/bin/env python
import sys
unique_lines = []
duplicate_lines = []
for line in sys.stdin:
if line in unique_lines:
duplicate_lines.append(line)
else:
unique_lines.append(line)
sys.stdout.write(line... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 480 | bash | # Remove duplicate lines without sorting
I have a utility script in Python:
```
#!/usr/bin/env python
import sys
unique_lines = []
duplicate_lines = []
for line in sys.stdin:
if line in unique_lines:
duplicate_lines.append(line)
else:
unique_lines.append(line)
sys.stdout.write(line)
# optionally do so... | The UNIX Bash Scripting blog suggests:
```
awk '!x[$0]++'
```
This command is telling awk which lines to print. The variable `$0` holds the entire contents of a line and square brackets are array access. So, for each line of the file, the node of the array `x` is incremented and the line printed if the content of tha... |
17029902 | Using curl POST with variables defined in bash script functions | 284 | 2013-06-10 17:52:11 | <p>When I echo I get this, which runs when I enter it into the terminal</p>
<pre><code>curl -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST --data '{"account":{"email":"akdgdtk@test.com","screenName":"akdgdtk","type":"NIKE","passwordSettings":{"password":"Starwars1","passwordConfirm":... | 470,947 | 1,595,883 | 2025-03-02 10:10:51 | 17,032,673 | 407 | 2013-06-10 20:54:57 | 2,245,910 | 2025-03-02 10:10:51 | https://stackoverflow.com/q/17029902 | https://stackoverflow.com/a/17032673 | <p>You don't need to pass the quotes enclosing the custom headers to curl. Also, your variables in the middle of the <code>data</code> argument should be quoted.</p>
<p>First, write a function that generates the post data of your script. This saves you from all sort of headaches concerning shell quoting and makes it ea... | <p>You don't need to pass the quotes enclosing the custom headers to curl. Also, your variables in the middle of the <code>data</code> argument should be quoted.</p> <p>First, write a function that generates the post data of your script. This saves you from all sort of headaches concerning shell quoting and makes it ea... | 387, 1508, 1554, 50067 | bash, curl, javascript-objects, json | <h1>Using curl POST with variables defined in bash script functions</h1>
<p>When I echo I get this, which runs when I enter it into the terminal</p>
<pre><code>curl -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST --data '{"account":{"email":"akdgdtk@test.com","screenName":"akdgdtk","t... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 481 | bash | # Using curl POST with variables defined in bash script functions
When I echo I get this, which runs when I enter it into the terminal
```
curl -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST --data '{"account":{"email":"akdgdtk@test.com","screenName":"akdgdtk","type":"NIKE","passwor... | You don't need to pass the quotes enclosing the custom headers to curl. Also, your variables in the middle of the `data` argument should be quoted.
First, write a function that generates the post data of your script. This saves you from all sort of headaches concerning shell quoting and makes it easier to read and mai... |
3411048 | [ :Unexpected operator in shell programming | 242 | 2010-08-05 01:03:54 | <p>My code:</p>
<pre><code> #!/bin/sh
#filename:choose.sh
read choose
[ "$choose" == "y" -o "$choose" == "Y" ] && echo "Yes" && exit 0
[ "$choose" == "n" -o "$choose" == "N" ] && echo "No" && exit 0
echo "Wrong Input" && exit 0
</code></pre>
<p>But when ... | 253,436 | 399,846 | 2023-04-11 23:49:29 | 3,411,061 | 406 | 2010-08-05 01:08:02 | 54,017 | 2023-04-11 23:49:29 | https://stackoverflow.com/q/3411048 | https://stackoverflow.com/a/3411061 | <p>There is no mistake in your <strong>bash</strong> script. But you are executing it with <strong><code>sh</code></strong> which has a less extensive syntax</p>
<p>So, you'll need run <code>bash ./choose.sh</code> instead, or convert the script to use POSIX compliant <code>sh</code> commands only, such as <code>=</cod... | <p>There is no mistake in your <strong>bash</strong> script. But you are executing it with <strong><code>sh</code></strong> which has a less extensive syntax</p> <p>So, you'll need run <code>bash ./choose.sh</code> instead, or convert the script to use POSIX compliant <code>sh</code> commands only, such as <code>=</cod... | 58, 387, 390 | bash, linux, shell | <h1>[ :Unexpected operator in shell programming</h1>
<p>My code:</p>
<pre><code> #!/bin/sh
#filename:choose.sh
read choose
[ "$choose" == "y" -o "$choose" == "Y" ] && echo "Yes" && exit 0
[ "$choose" == "n" -o "$choose" == "N" ] && echo "No" && exit 0
echo "Wrong... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 482 | bash | # [ :Unexpected operator in shell programming
My code:
```
#!/bin/sh
#filename:choose.sh
read choose
[ "$choose" == "y" -o "$choose" == "Y" ] && echo "Yes" && exit 0
[ "$choose" == "n" -o "$choose" == "N" ] && echo "No" && exit 0
echo "Wrong Input" && exit 0
```
But when I execute
```
s... | There is no mistake in your **bash** script. But you are executing it with **`sh`** which has a less extensive syntax
So, you'll need run `bash ./choose.sh` instead, or convert the script to use POSIX compliant `sh` commands only, such as `=` between strings instead of `==`. |
15984414 | Bash Script: count unique lines in file | 190 | 2013-04-13 04:57:52 | <h1>Situation:</h1>
<p>I have a large file (millions of lines) containing IP addresses and ports from a several hour network capture, one ip/port per line. Lines are of this format:</p>
<pre><code>ip.ad.dre.ss[:port]
</code></pre>
<h1>Desired result:</h1>
<p>There is an entry for each packet I received while loggi... | 172,192 | 1,462,604 | 2020-02-06 11:57:48 | 15,984,450 | 406 | 2013-04-13 05:04:20 | 494,061 | 2015-11-20 15:40:30 | https://stackoverflow.com/q/15984414 | https://stackoverflow.com/a/15984450 | <p>You can use the <code>uniq</code> command to get counts of sorted repeated lines:</p>
<pre><code>sort ips.txt | uniq -c
</code></pre>
<p>To get the most frequent results at top (thanks to Peter Jaric):</p>
<pre><code>sort ips.txt | uniq -c | sort -bgr
</code></pre>
| <p>You can use the <code>uniq</code> command to get counts of sorted repeated lines:</p> <pre><code>sort ips.txt | uniq -c </code></pre> <p>To get the most frequent results at top (thanks to Peter Jaric):</p> <pre><code>sort ips.txt | uniq -c | sort -bgr </code></pre> | 387 | bash | <h1>Bash Script: count unique lines in file</h1>
<h1>Situation:</h1>
<p>I have a large file (millions of lines) containing IP addresses and ports from a several hour network capture, one ip/port per line. Lines are of this format:</p>
<pre><code>ip.ad.dre.ss[:port]
</code></pre>
<h1>Desired result:</h1>
<p>There i... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 483 | bash | # Bash Script: count unique lines in file
# Situation:
I have a large file (millions of lines) containing IP addresses and ports from a several hour network capture, one ip/port per line. Lines are of this format:
```
ip.ad.dre.ss[:port]
```
# Desired result:
There is an entry for each packet I received while logg... | You can use the `uniq` command to get counts of sorted repeated lines:
```
sort ips.txt | uniq -c
```
To get the most frequent results at top (thanks to Peter Jaric):
```
sort ips.txt | uniq -c | sort -bgr
``` |
63423584 | How to fix error- nodemon.ps1 cannot be loaded because running scripts is disabled on this system, (without security risk)? | 167 | 2020-08-15 07:20:10 | <p>Error on terminal: <code>nodemon.ps1</code> cannot be loaded because running scripts is disabled on this system. For more
information, see <strong>about_Execution_Policies</strong> at
<a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell... | 389,304 | 13,513,100 | 2024-05-28 13:25:25 | 63,424,744 | 406 | 2020-08-15 09:56:26 | 7,417,433 | 2023-06-10 13:21:05 | https://stackoverflow.com/q/63423584 | https://stackoverflow.com/a/63424744 | <p>I'd recommend using <code>RemoteSigned</code> as opposed to <code>Unrestricted</code>, and limiting the policy to the <code>CurrentUser</code> if possible.</p>
<p>Run Powershell as Admin, and then:</p>
<pre><code>Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
</code></pre>
<p><a href="https://learn.microsoft.co... | <p>I'd recommend using <code>RemoteSigned</code> as opposed to <code>Unrestricted</code>, and limiting the policy to the <code>CurrentUser</code> if possible.</p> <p>Run Powershell as Admin, and then:</p> <pre><code>Set-ExecutionPolicy RemoteSigned -Scope CurrentUser </code></pre> <p><a href="https://learn.microsoft.co... | 526, 19909, 46426, 94504 | node.js, nodemon, powershell, web-development-server | <h1>How to fix error- nodemon.ps1 cannot be loaded because running scripts is disabled on this system, (without security risk)?</h1>
<p>Error on terminal: <code>nodemon.ps1</code> cannot be loaded because running scripts is disabled on this system. For more
information, see <strong>about_Execution_Policies</strong> at
... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 484 | bash | # How to fix error- nodemon.ps1 cannot be loaded because running scripts is disabled on this system, (without security risk)?
Error on terminal: `nodemon.ps1` cannot be loaded because running scripts is disabled on this system. For more
information, see **about_Execution_Policies** at
<https://learn.microsoft.com/en-u... | I'd recommend using `RemoteSigned` as opposed to `Unrestricted`, and limiting the policy to the `CurrentUser` if possible.
Run Powershell as Admin, and then:
```
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
```
[RemoteSigned](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/... |
15783701 | Which characters need to be escaped when using Bash? | 322 | 2013-04-03 09:32:50 | <p>Is there any comprehensive list of characters that need to be escaped in Bash? Can it be checked just with <code>sed</code>?</p>
<p>In particular, I was checking whether <code>%</code> needs to be escaped or not. I tried</p>
<pre><code>echo "h%h" | sed 's/%/i/g'
</code></pre>
<p>and worked fine, without escaping ... | 670,310 | 1,983,854 | 2025-06-22 00:34:06 | 20,053,121 | 404 | 2013-11-18 16:47:08 | 1,073,695 | 2025-06-22 00:11:16 | https://stackoverflow.com/q/15783701 | https://stackoverflow.com/a/20053121 | <p>There are two easy and safe rules which work not only in <code>sh</code>, but also Bash.</p>
<h2>1. Put the whole string in single quotes</h2>
<p>This works for all chars except single quote itself. To escape the single quote, close the quoting before it, insert the single quote, and reopen the quoting.</p>
<pre cla... | <p>There are two easy and safe rules which work not only in <code>sh</code>, but also Bash.</p> <h2>1. Put the whole string in single quotes</h2> <p>This works for all chars except single quote itself. To escape the single quote, close the quoting before it, insert the single quote, and reopen the quoting.</p> <pre cla... | 34, 387, 390, 4804, 12725 | bash, escaping, shell, special-characters, unix | <h1>Which characters need to be escaped when using Bash?</h1>
<p>Is there any comprehensive list of characters that need to be escaped in Bash? Can it be checked just with <code>sed</code>?</p>
<p>In particular, I was checking whether <code>%</code> needs to be escaped or not. I tried</p>
<pre><code>echo "h%h" | sed ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 485 | bash | # Which characters need to be escaped when using Bash?
Is there any comprehensive list of characters that need to be escaped in Bash? Can it be checked just with `sed`?
In particular, I was checking whether `%` needs to be escaped or not. I tried
```
echo "h%h" | sed 's/%/i/g'
```
and worked fine, without escaping ... | There are two easy and safe rules which work not only in `sh`, but also Bash.
## 1. Put the whole string in single quotes
This works for all chars except single quote itself. To escape the single quote, close the quoting before it, insert the single quote, and reopen the quoting.
```
'I'\''m a s@fe $tring which ends... |
8737638 | Assign output to variable in Bash | 234 | 2012-01-05 04:27:08 | <p>I'm trying to assign the output of cURL into a variable like so:</p>
<pre><code>#!/bin/sh
$IP=`curl automation.whatismyip.com/n09230945.asp`
echo $IP
sed s/IP/$IP/ nsupdate.txt | nsupdate
</code></pre>
<p>However, when I run the script the following happens:</p>
<blockquote>
<pre><code>./update.sh: 3: =[my ip add... | 418,778 | 374,794 | 2021-03-23 07:59:09 | 8,737,671 | 404 | 2012-01-05 04:30:45 | 1,072,112 | 2013-02-15 15:23:05 | https://stackoverflow.com/q/8737638 | https://stackoverflow.com/a/8737671 | <p>In shell, you don't put a $ in front of a variable you're assigning. You only use $IP when you're referring to the variable.</p>
<pre><code>#!/bin/bash
IP=$(curl automation.whatismyip.com/n09230945.asp)
echo "$IP"
sed "s/IP/$IP/" nsupdate.txt | nsupdate
</code></pre>
| <p>In shell, you don't put a $ in front of a variable you're assigning. You only use $IP when you're referring to the variable.</p> <pre><code>#!/bin/bash IP=$(curl automation.whatismyip.com/n09230945.asp) echo "$IP" sed "s/IP/$IP/" nsupdate.txt | nsupdate </code></pre> | 387, 1554 | bash, curl | <h1>Assign output to variable in Bash</h1>
<p>I'm trying to assign the output of cURL into a variable like so:</p>
<pre><code>#!/bin/sh
$IP=`curl automation.whatismyip.com/n09230945.asp`
echo $IP
sed s/IP/$IP/ nsupdate.txt | nsupdate
</code></pre>
<p>However, when I run the script the following happens:</p>
<blockqu... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 486 | bash | # Assign output to variable in Bash
I'm trying to assign the output of cURL into a variable like so:
```
#!/bin/sh
$IP=`curl automation.whatismyip.com/n09230945.asp`
echo $IP
sed s/IP/$IP/ nsupdate.txt | nsupdate
```
However, when I run the script the following happens:
> ```
> ./update.sh: 3: =[my ip address]: not... | In shell, you don't put a $ in front of a variable you're assigning. You only use $IP when you're referring to the variable.
```
#!/bin/bash
IP=$(curl automation.whatismyip.com/n09230945.asp)
echo "$IP"
sed "s/IP/$IP/" nsupdate.txt | nsupdate
``` |
14637979 | How to permanently set $PATH on Linux/Unix | 1,112 | 2013-02-01 00:57:08 | <p>On Linux, how can I add a directory to the $PATH so it remains persistent across different sessions?</p>
<h4>Background</h4>
<p>I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:</p>
<pre><code>export PATH=$PATH:/path/to/dir
</code></pre>
<p>This works, however each time I ex... | 2,244,383 | 49,153 | 2021-09-18 18:08:52 | 26,962,251 | 403 | 2014-11-16 21:29:08 | 450,989 | 2021-07-25 10:27:12 | https://stackoverflow.com/q/14637979 | https://stackoverflow.com/a/26962251 | <p>There are multiple ways to do it. The actual solution depends on the purpose.</p>
<p>The variable values are usually stored in either a list of assignments or a shell script that is run at the start of the system or user session. In case of the shell script you must use a specific shell syntax and <code>export</code... | <p>There are multiple ways to do it. The actual solution depends on the purpose.</p> <p>The variable values are usually stored in either a list of assignments or a shell script that is run at the start of the system or user session. In case of the shell script you must use a specific shell syntax and <code>export</code... | 34, 58, 387, 3791, 6268 | bash, linux, path, unix, zsh | <h1>How to permanently set $PATH on Linux/Unix</h1>
<p>On Linux, how can I add a directory to the $PATH so it remains persistent across different sessions?</p>
<h4>Background</h4>
<p>I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:</p>
<pre><code>export PATH=$PATH:/path/to/dir... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 487 | bash | # How to permanently set $PATH on Linux/Unix
On Linux, how can I add a directory to the $PATH so it remains persistent across different sessions?
#### Background
I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:
```
export PATH=$PATH:/path/to/dir
```
This works, however ea... | There are multiple ways to do it. The actual solution depends on the purpose.
The variable values are usually stored in either a list of assignments or a shell script that is run at the start of the system or user session. In case of the shell script you must use a specific shell syntax and `export` or `set` commands.... |
9640660 | Is there a way to exit a Bash script, but not quitting the terminal? | 287 | 2012-03-09 20:38:24 | <p>When I use <code>exit</code> command in a shell script, the script will terminate the terminal (the prompt). Is there a way to terminate a script and then staying in the terminal?</p>
<p>My script <code>run.sh</code> is expected to execute by directly being sourced, or sourced from another script.</p>
<p>To be more ... | 420,376 | 486,720 | 2025-09-08 21:02:55 | 9,640,736 | 403 | 2012-03-09 20:44:19 | 309,058 | 2012-03-09 20:44:19 | https://stackoverflow.com/q/9640660 | https://stackoverflow.com/a/9640736 | <p>The "problem" really is that you're sourcing and not executing the script. When you source a file, its contents will be executed in the current shell, instead of spawning a subshell. So everything, including exit, will affect the current shell.</p>
<p>Instead of using <code>exit</code>, you will want to use <code>r... | <p>The "problem" really is that you're sourcing and not executing the script. When you source a file, its contents will be executed in the current shell, instead of spawning a subshell. So everything, including exit, will affect the current shell.</p> <p>Instead of using <code>exit</code>, you will want to use <code>r... | 58, 387 | bash, linux | <h1>Is there a way to exit a Bash script, but not quitting the terminal?</h1>
<p>When I use <code>exit</code> command in a shell script, the script will terminate the terminal (the prompt). Is there a way to terminate a script and then staying in the terminal?</p>
<p>My script <code>run.sh</code> is expected to execute... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 488 | bash | # Is there a way to exit a Bash script, but not quitting the terminal?
When I use `exit` command in a shell script, the script will terminate the terminal (the prompt). Is there a way to terminate a script and then staying in the terminal?
My script `run.sh` is expected to execute by directly being sourced, or source... | The "problem" really is that you're sourcing and not executing the script. When you source a file, its contents will be executed in the current shell, instead of spawning a subshell. So everything, including exit, will affect the current shell.
Instead of using `exit`, you will want to use `return`. |
1444406 | How to delete duplicate lines in a file without sorting it in Unix | 212 | 2009-09-18 12:58:26 | <p>Is there a way to delete duplicate lines in a file in Unix?</p>
<p>I can do it with <code>sort -u</code> and <code>uniq</code> commands, but I want to use <code>sed</code> or <code>awk</code>.</p>
<p>Is that possible?</p>
| 146,550 | 134,713 | 2025-07-18 19:32:56 | 1,444,448 | 403 | 2009-09-18 13:07:47 | 44,620 | 2021-10-28 20:01:23 | https://stackoverflow.com/q/1444406 | https://stackoverflow.com/a/1444448 | <pre><code>awk '!seen[$0]++' file.txt
</code></pre>
<p><code>seen</code> is an associative array that AWK will pass every line of the file to. If a line isn't in the array then <code>seen[$0]</code> will evaluate to false. The <code>!</code> is the logical NOT operator and will invert the false to true. AWK will print ... | <pre><code>awk '!seen[$0]++' file.txt </code></pre> <p><code>seen</code> is an associative array that AWK will pass every line of the file to. If a line isn't in the array then <code>seen[$0]</code> will evaluate to false. The <code>!</code> is the logical NOT operator and will invert the false to true. AWK will print ... | 34, 390, 531, 990, 5282 | awk, scripting, sed, shell, unix | <h1>How to delete duplicate lines in a file without sorting it in Unix</h1>
<p>Is there a way to delete duplicate lines in a file in Unix?</p>
<p>I can do it with <code>sort -u</code> and <code>uniq</code> commands, but I want to use <code>sed</code> or <code>awk</code>.</p>
<p>Is that possible?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 489 | bash | # How to delete duplicate lines in a file without sorting it in Unix
Is there a way to delete duplicate lines in a file in Unix?
I can do it with `sort -u` and `uniq` commands, but I want to use `sed` or `awk`.
Is that possible? | ```
awk '!seen[$0]++' file.txt
```
`seen` is an associative array that AWK will pass every line of the file to. If a line isn't in the array then `seen[$0]` will evaluate to false. The `!` is the logical NOT operator and will invert the false to true. AWK will print the lines where the expression evaluates to true.
T... |
7093860 | PHP shell_exec() vs exec() | 410 | 2011-08-17 13:42:37 | <p>I'm struggling to understand the difference between <code>shell_exec()</code> and <code>exec()</code>...</p>
<p>I've always used <code>exec()</code> to execute server side commands, when would I use <code>shell_exec()</code>?</p>
<p>Is <code>shell_exec()</code> just a shorthand for <code>exec()</code>? It seems t... | 532,868 | 197,606 | 2019-04-29 06:13:41 | 7,093,892 | 402 | 2011-08-17 13:45:29 | 23,528 | 2016-04-25 22:39:15 | https://stackoverflow.com/q/7093860 | https://stackoverflow.com/a/7093892 | <p><code>shell_exec</code> returns all of the output stream as a string. <code>exec</code> returns the last line of the output by default, but can provide all output as an array specifed as the second parameter.</p>
<p>See</p>
<ul>
<li><a href="http://php.net/manual/en/function.shell-exec.php" rel="noreferrer">http:/... | <p><code>shell_exec</code> returns all of the output stream as a string. <code>exec</code> returns the last line of the output by default, but can provide all output as an array specifed as the second parameter.</p> <p>See</p> <ul> <li><a href="http://php.net/manual/en/function.shell-exec.php" rel="noreferrer">http:/... | 5, 390, 1796, 7932 | command, exec, php, shell | <h1>PHP shell_exec() vs exec()</h1>
<p>I'm struggling to understand the difference between <code>shell_exec()</code> and <code>exec()</code>...</p>
<p>I've always used <code>exec()</code> to execute server side commands, when would I use <code>shell_exec()</code>?</p>
<p>Is <code>shell_exec()</code> just a shorthand ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 490 | bash | # PHP shell_exec() vs exec()
I'm struggling to understand the difference between `shell_exec()` and `exec()`...
I've always used `exec()` to execute server side commands, when would I use `shell_exec()`?
Is `shell_exec()` just a shorthand for `exec()`? It seems to be the same thing with fewer parameters. | `shell_exec` returns all of the output stream as a string. `exec` returns the last line of the output by default, but can provide all output as an array specifed as the second parameter.
See
- <http://php.net/manual/en/function.shell-exec.php>
- <http://php.net/manual/en/function.exec.php> |
17325293 | Invoke-WebRequest, POST with parameters | 264 | 2013-06-26 16:13:46 | <p>I'm attempting to POST to a uri, and send the parameter <code>username=me</code></p>
<pre><code>Invoke-WebRequest -Uri http://example.com/foobar -Method POST
</code></pre>
<p>How do I pass the parameters using the method POST?</p>
| 479,287 | 36,545 | 2024-06-21 20:51:27 | 17,330,952 | 402 | 2013-06-26 21:40:00 | 1,327,244 | 2013-06-26 21:40:00 | https://stackoverflow.com/q/17325293 | https://stackoverflow.com/a/17330952 | <p>Put your parameters in a hash table and pass them like this:</p>
<pre><code>$postParams = @{username='me';moredata='qwerty'}
Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body $postParams
</code></pre>
| <p>Put your parameters in a hash table and pass them like this:</p> <pre><code>$postParams = @{username='me';moredata='qwerty'} Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body $postParams </code></pre> | 364, 526 | powershell, rest | <h1>Invoke-WebRequest, POST with parameters</h1>
<p>I'm attempting to POST to a uri, and send the parameter <code>username=me</code></p>
<pre><code>Invoke-WebRequest -Uri http://example.com/foobar -Method POST
</code></pre>
<p>How do I pass the parameters using the method POST?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 491 | bash | # Invoke-WebRequest, POST with parameters
I'm attempting to POST to a uri, and send the parameter `username=me`
```
Invoke-WebRequest -Uri http://example.com/foobar -Method POST
```
How do I pass the parameters using the method POST? | Put your parameters in a hash table and pass them like this:
```
$postParams = @{username='me';moredata='qwerty'}
Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body $postParams
``` |
38005341 | The response content cannot be parsed because the Internet Explorer engine is not available, or | 231 | 2016-06-24 03:53:47 | <p>I need to download a channel 9 series using powershell, however the scripts I have tried have errors:</p>
<ol>
<li><p>This script</p>
<pre><code>$url="https://channel9.msdn.com/blogs/OfficeDevPnP/feed/mp4high"
$rss=invoke-webrequest -uri $url
$destination="D:\Videos\OfficePnP"
[xml]$rss.Content|foreach{
$_.Sel... | 288,674 | 1,014,217 | 2023-01-03 04:07:28 | 38,054,505 | 402 | 2016-06-27 12:52:45 | 4,803,536 | 2019-12-31 19:50:57 | https://stackoverflow.com/q/38005341 | https://stackoverflow.com/a/38054505 | <p>In your invoke web request just use the parameter <code>-UseBasicParsing</code></p>
<p>e.g. in your script (line 2) you should use:</p>
<pre><code>$rss = Invoke-WebRequest -Uri $url -UseBasicParsing
</code></pre>
<p>According to <a href="https://learn.microsoft.com/en-us/previous-versions/powershell/module/Micros... | <p>In your invoke web request just use the parameter <code>-UseBasicParsing</code></p> <p>e.g. in your script (line 2) you should use:</p> <pre><code>$rss = Invoke-WebRequest -Uri $url -UseBasicParsing </code></pre> <p>According to <a href="https://learn.microsoft.com/en-us/previous-versions/powershell/module/Micros... | 526, 1189 | internet-explorer, powershell | <h1>The response content cannot be parsed because the Internet Explorer engine is not available, or</h1>
<p>I need to download a channel 9 series using powershell, however the scripts I have tried have errors:</p>
<ol>
<li><p>This script</p>
<pre><code>$url="https://channel9.msdn.com/blogs/OfficeDevPnP/feed/mp4high"
... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 492 | bash | # The response content cannot be parsed because the Internet Explorer engine is not available, or
I need to download a channel 9 series using powershell, however the scripts I have tried have errors:
1. This script
```
$url="https://channel9.msdn.com/blogs/OfficeDevPnP/feed/mp4high"
$rss=invoke-webrequest -... | In your invoke web request just use the parameter `-UseBasicParsing`
e.g. in your script (line 2) you should use:
```
$rss = Invoke-WebRequest -Uri $url -UseBasicParsing
```
According to [the documentation](https://learn.microsoft.com/en-us/previous-versions/powershell/module/Microsoft.PowerShell.Utility/Invoke-WebR... |
2559076 | How do I redirect output to a variable in shell? | 267 | 2010-04-01 08:33:16 | <p>I have a script like that</p>
<pre><code>genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5
</code></pre>
<p>I want to get stream generated by genhash in a variable. How do I redirect it into a variable <code>$hash</code> to compare inside a conditional?</p>
<pre><code>if [ $hash -ne 0 ]
then ... | 444,251 | 294,700 | 2019-02-08 08:07:07 | 2,559,087 | 401 | 2010-04-01 08:35:36 | null | 2010-04-01 08:35:36 | https://stackoverflow.com/q/2559076 | https://stackoverflow.com/a/2559087 | <p>Use the <code>$( ... )</code> construct:</p>
<pre><code>hash=$(genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5)
</code></pre>
| <p>Use the <code>$( ... )</code> construct:</p> <pre><code>hash=$(genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5) </code></pre> | 387, 390 | bash, shell | <h1>How do I redirect output to a variable in shell?</h1>
<p>I have a script like that</p>
<pre><code>genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5
</code></pre>
<p>I want to get stream generated by genhash in a variable. How do I redirect it into a variable <code>$hash</code> to compare inside... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 493 | bash | # How do I redirect output to a variable in shell?
I have a script like that
```
genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5
```
I want to get stream generated by genhash in a variable. How do I redirect it into a variable `$hash` to compare inside a conditional?
```
if [ $hash -ne 0 ]
th... | Use the `$( ... )` construct:
```
hash=$(genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5)
``` |
19858600 | Accessing last x characters of a string in Bash | 222 | 2013-11-08 11:58:34 | <p>I found out that with <code>${string:0:3}</code> one can access the first 3 characters of a string. Is there a equivalently easy method to access the last three characters? </p>
| 255,644 | 2,675,099 | 2024-07-26 17:58:50 | 19,858,692 | 401 | 2013-11-08 12:03:28 | 1,815,797 | 2021-02-21 14:16:46 | https://stackoverflow.com/q/19858600 | https://stackoverflow.com/a/19858692 | <p>Last three characters of <code>string</code>:</p>
<pre><code>${string: -3}
</code></pre>
<p>or</p>
<pre><code>${string:(-3)}
</code></pre>
<p>(mind the space between <code>:</code> and <code>-3</code> in the first form).</p>
<p>Please refer to the <a href="http://www.gnu.org/software/bash/manual/bashref.html#Shell-P... | <p>Last three characters of <code>string</code>:</p> <pre><code>${string: -3} </code></pre> <p>or</p> <pre><code>${string:(-3)} </code></pre> <p>(mind the space between <code>:</code> and <code>-3</code> in the first form).</p> <p>Please refer to the <a href="http://www.gnu.org/software/bash/manual/bashref.html#Shell-P... | 139, 387, 5352 | bash, extract, string | <h1>Accessing last x characters of a string in Bash</h1>
<p>I found out that with <code>${string:0:3}</code> one can access the first 3 characters of a string. Is there a equivalently easy method to access the last three characters? </p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 494 | bash | # Accessing last x characters of a string in Bash
I found out that with `${string:0:3}` one can access the first 3 characters of a string. Is there a equivalently easy method to access the last three characters? | Last three characters of `string`:
```
${string: -3}
```
or
```
${string:(-3)}
```
(mind the space between `:` and `-3` in the first form).
Please refer to the [Shell Parameter Expansion in the reference manual](http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion):
```
${parameter:offse... |
7069682 | How to get arguments with flags in Bash | 410 | 2011-08-15 19:26:50 | <p>I know that I can easily get positioned parameters like this in bash:</p>
<p><code>$0</code> or <code>$1</code></p>
<p>I want to be able to use flag options like this to specify for what each parameter is used:</p>
<pre><code>mysql -u user -h host
</code></pre>
<p>What is the best way to get <code>-u param</code... | 467,451 | 471,891 | 2025-02-12 14:05:06 | 7,069,755 | 400 | 2011-08-15 19:32:20 | 168,175 | 2019-07-24 05:04:43 | https://stackoverflow.com/q/7069682 | https://stackoverflow.com/a/7069755 | <p>This is the idiom I usually use:</p>
<pre><code>while test $# -gt 0; do
case "$1" in
-h|--help)
echo "$package - attempt to capture frames"
echo " "
echo "$package [options] application [arguments]"
echo " "
echo "options:"
echo "-h, --help show brief help"
... | <p>This is the idiom I usually use:</p> <pre><code>while test $# -gt 0; do case "$1" in -h|--help) echo "$package - attempt to capture frames" echo " " echo "$package [options] application [arguments]" echo " " echo "options:" echo "-h, --help show brief help" ... | 387, 390 | bash, shell | <h1>How to get arguments with flags in Bash</h1>
<p>I know that I can easily get positioned parameters like this in bash:</p>
<p><code>$0</code> or <code>$1</code></p>
<p>I want to be able to use flag options like this to specify for what each parameter is used:</p>
<pre><code>mysql -u user -h host
</code></pre>
<p... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 496 | bash | # How to get arguments with flags in Bash
I know that I can easily get positioned parameters like this in bash:
`$0` or `$1`
I want to be able to use flag options like this to specify for what each parameter is used:
```
mysql -u user -h host
```
What is the best way to get `-u param` value and `-h param` value by... | This is the idiom I usually use:
```
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "$package - attempt to capture frames"
echo " "
echo "$package [options] application [arguments]"
echo " "
echo "options:"
echo "-h, --help show brief help"
echo "-a,... |
3027518 | How to do a non-greedy match in grep? | 248 | 2010-06-12 04:43:59 | <p>I want to grep the shortest match and the pattern should be something like:</p>
<pre><code><car ... model=BMW ...>
...
...
...
</car>
</code></pre>
<p>... means any character and the input is multiple lines. </p>
| 189,812 | 305,555 | 2020-03-24 14:47:07 | 3,027,524 | 400 | 2010-06-12 04:47:31 | 61,974 | 2013-02-04 13:02:15 | https://stackoverflow.com/q/3027518 | https://stackoverflow.com/a/3027524 | <p>You're looking for a non-greedy (or lazy) match. To get a non-greedy match in regular expressions you need to use the modifier <code>?</code> after the quantifier. For example you can change <code>.*</code> to <code>.*?</code>.</p>
<p>By default <code>grep</code> doesn't support non-greedy modifiers, but you can us... | <p>You're looking for a non-greedy (or lazy) match. To get a non-greedy match in regular expressions you need to use the modifier <code>?</code> after the quantifier. For example you can change <code>.*</code> to <code>.*?</code>.</p> <p>By default <code>grep</code> doesn't support non-greedy modifiers, but you can us... | 18, 390, 1231, 1271, 74222 | command-line, grep, regex, regex-greedy, shell | <h1>How to do a non-greedy match in grep?</h1>
<p>I want to grep the shortest match and the pattern should be something like:</p>
<pre><code><car ... model=BMW ...>
...
...
...
</car>
</code></pre>
<p>... means any character and the input is multiple lines. </p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 497 | bash | # How to do a non-greedy match in grep?
I want to grep the shortest match and the pattern should be something like:
```
<car ... model=BMW ...>
...
...
...
</car>
```
... means any character and the input is multiple lines. | You're looking for a non-greedy (or lazy) match. To get a non-greedy match in regular expressions you need to use the modifier `?` after the quantifier. For example you can change `.*` to `.*?`.
By default `grep` doesn't support non-greedy modifiers, but you can use `grep -P` to use the Perl syntax. |
2709458 | How to replace spaces in file names using a bash script | 364 | 2010-04-25 18:53:31 | <p>Can anyone recommend a safe solution to recursively replace spaces with underscores in file and directory names starting from a given root directory? For example:</p>
<pre><code>$ tree
.
|-- a dir
| `-- file with spaces.txt
`-- b dir
|-- another file with spaces.txt
`-- yet another file with spaces.pdf
</... | 367,301 | 45,112 | 2024-02-29 03:44:54 | 2,709,619 | 399 | 2010-04-25 19:37:37 | 26,428 | 2023-03-06 10:26:46 | https://stackoverflow.com/q/2709458 | https://stackoverflow.com/a/2709619 | <p>Use <code>rename</code> (aka <code>prename</code>) which is a Perl script which may be on your system already. Do it in two steps:</p>
<pre><code>find . -name "* *" -type d | rename 's/ /_/g' # do the directories first
find . -name "* *" -type f | rename 's/ /_/g'
</code></pre>
<p>Based on <st... | <p>Use <code>rename</code> (aka <code>prename</code>) which is a Perl script which may be on your system already. Do it in two steps:</p> <pre><code>find . -name "* *" -type d | rename 's/ /_/g' # do the directories first find . -name "* *" -type f | rename 's/ /_/g' </code></pre> <p>Based on <st... | 58, 387, 1062, 1084 | bash, filenames, linux, whitespace | <h1>How to replace spaces in file names using a bash script</h1>
<p>Can anyone recommend a safe solution to recursively replace spaces with underscores in file and directory names starting from a given root directory? For example:</p>
<pre><code>$ tree
.
|-- a dir
| `-- file with spaces.txt
`-- b dir
|-- another... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 498 | bash | # How to replace spaces in file names using a bash script
Can anyone recommend a safe solution to recursively replace spaces with underscores in file and directory names starting from a given root directory? For example:
```
$ tree
.
|-- a dir
| `-- file with spaces.txt
`-- b dir
|-- another file with spaces.tx... | Use `rename` (aka `prename`) which is a Perl script which may be on your system already. Do it in two steps:
```
find . -name "* *" -type d | rename 's/ /_/g' # do the directories first
find . -name "* *" -type f | rename 's/ /_/g'
```
Based on **Jürgen's** answer and able to handle multiple layers of files and di... |
8473121 | Execute a command without keeping it in history | 285 | 2011-12-12 10:48:21 | <p>When doing software development, there is often need to include confidential information in command line commands. Typical example is setting credentials for deploying the project to a server as environment variables.</p>
<p><strong>How can I execute some commands, when I don't want to store them in the command hist... | 165,935 | 786,284 | 2022-12-13 10:34:17 | 8,473,153 | 399 | 2011-12-12 10:50:54 | 1,017,884 | 2021-05-17 22:18:13 | https://stackoverflow.com/q/8473121 | https://stackoverflow.com/a/8473153 | <p>Start your command with a space and it won't be included in the history.</p>
<p>Be aware that this does require the environment variable <code>$HISTCONTROL</code> to be set.</p>
<ul>
<li><p>Check that the following command returns <code>ignorespace</code> or
<code>ignoreboth</code>:</p>
<pre><code> echo $HISTCONTROL... | <p>Start your command with a space and it won't be included in the history.</p> <p>Be aware that this does require the environment variable <code>$HISTCONTROL</code> to be set.</p> <ul> <li><p>Check that the following command returns <code>ignorespace</code> or <code>ignoreboth</code>:</p> <pre><code> echo $HISTCONTROL... | 34, 58, 387, 1231 | bash, command-line, linux, unix | <h1>Execute a command without keeping it in history</h1>
<p>When doing software development, there is often need to include confidential information in command line commands. Typical example is setting credentials for deploying the project to a server as environment variables.</p>
<p><strong>How can I execute some comm... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 499 | bash | # Execute a command without keeping it in history
When doing software development, there is often need to include confidential information in command line commands. Typical example is setting credentials for deploying the project to a server as environment variables.
**How can I execute some commands, when I don't wa... | Start your command with a space and it won't be included in the history.
Be aware that this does require the environment variable `$HISTCONTROL` to be set.
- Check that the following command returns `ignorespace` or
`ignoreboth`:
```
echo $HISTCONTROL
```
- To add the environment variable if missing, the fo... |
26376516 | Laravel PHP command not found | 253 | 2014-10-15 07:10:37 | <p>I installed Laravel using Composer without problems, but when I try to run <code>laravel</code> in the terminal, I get the following error:</p>
<blockquote>
<p>-bash: laravel: command not found</p>
</blockquote>
<p>The docs say that I need to do the following:</p>
<blockquote>
<p>Make sure to place the ~/.composer/v... | 327,161 | 1,147,273 | 2023-06-02 23:45:48 | 26,376,773 | 399 | 2014-10-15 07:24:55 | 1,147,273 | 2020-06-02 11:58:00 | https://stackoverflow.com/q/26376516 | https://stackoverflow.com/a/26376773 | <p>Ok, I did that and it works:</p>
<pre><code>nano ~/.bash_profile
</code></pre>
<p>And paste</p>
<pre><code>export PATH=~/.composer/vendor/bin:$PATH
</code></pre>
<p>do <code>source ~/.bash_profile</code> and enjoy ;)</p>
<p><strong>Important</strong>: If you want to know the difference between <em>bash_profile... | <p>Ok, I did that and it works:</p> <pre><code>nano ~/.bash_profile </code></pre> <p>And paste</p> <pre><code>export PATH=~/.composer/vendor/bin:$PATH </code></pre> <p>do <code>source ~/.bash_profile</code> and enjoy ;)</p> <p><strong>Important</strong>: If you want to know the difference between <em>bash_profile... | 5, 387, 1796, 75151, 78546 | bash, command, composer-php, laravel, php | <h1>Laravel PHP command not found</h1>
<p>I installed Laravel using Composer without problems, but when I try to run <code>laravel</code> in the terminal, I get the following error:</p>
<blockquote>
<p>-bash: laravel: command not found</p>
</blockquote>
<p>The docs say that I need to do the following:</p>
<blockquote>
... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 500 | bash | # Laravel PHP command not found
I installed Laravel using Composer without problems, but when I try to run `laravel` in the terminal, I get the following error:
> -bash: laravel: command not found
The docs say that I need to do the following:
> Make sure to place the ~/.composer/vendor/bin directory in your PATH so... | Ok, I did that and it works:
```
nano ~/.bash_profile
```
And paste
```
export PATH=~/.composer/vendor/bin:$PATH
```
do `source ~/.bash_profile` and enjoy ;)
**Important**: If you want to know the difference between *bash_profile* and *bashrc* please check this [link](http://www.joshstaiger.org/archives/2005/07/ba... |
249578 | How to delete files older than X hours | 270 | 2008-10-30 08:31:20 | <p>My bash script deletes old files older than 1 day:</p>
<pre><code>find $LOCATION -name $REQUIRED_FILES -type f -mtime +1 -delete
</code></pre>
<p>What if I need finer resolution than days, say like 6 hours old? Is there a way to do that, like there is using find and -mtime?</p>
| 295,937 | 13,523 | 2025-10-17 20:20:23 | 249,591 | 398 | 2008-10-30 08:39:01 | 6,521 | 2018-11-11 08:09:42 | https://stackoverflow.com/q/249578 | https://stackoverflow.com/a/249591 | <p>Does your <a href="http://man7.org/linux/man-pages/man1/find.1.html" rel="noreferrer"><code>find</code></a> have the <code>-mmin</code> option? That can let you test the number of mins since last modification:</p>
<pre><code>find $LOCATION -name $REQUIRED_FILES -type f -mmin +360 -delete
</code></pre>
<p>Or maybe ... | <p>Does your <a href="http://man7.org/linux/man-pages/man1/find.1.html" rel="noreferrer"><code>find</code></a> have the <code>-mmin</code> option? That can let you test the number of mins since last modification:</p> <pre><code>find $LOCATION -name $REQUIRED_FILES -type f -mmin +360 -delete </code></pre> <p>Or maybe ... | 387 | bash | <h1>How to delete files older than X hours</h1>
<p>My bash script deletes old files older than 1 day:</p>
<pre><code>find $LOCATION -name $REQUIRED_FILES -type f -mtime +1 -delete
</code></pre>
<p>What if I need finer resolution than days, say like 6 hours old? Is there a way to do that, like there is using find and -m... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 501 | bash | # How to delete files older than X hours
My bash script deletes old files older than 1 day:
```
find $LOCATION -name $REQUIRED_FILES -type f -mtime +1 -delete
```
What if I need finer resolution than days, say like 6 hours old? Is there a way to do that, like there is using find and -mtime? | Does your [`find`](http://man7.org/linux/man-pages/man1/find.1.html) have the `-mmin` option? That can let you test the number of mins since last modification:
```
find $LOCATION -name $REQUIRED_FILES -type f -mmin +360 -delete
```
Or maybe look at using [`tmpwatch`](http://linux.about.com/library/cmd/blcmdl8_tmpwatc... |
33247228 | How to use `jq` in a shell pipeline? | 244 | 2015-10-20 21:35:51 | <p>I can't seem to get <code>jq</code> to behave "normally" in a shell pipeline. For example:</p>
<pre><code>$ curl -s https://api.github.com/users/octocat/repos | jq | cat
</code></pre>
<p>results in <code>jq</code> simply printing out its help text*. The same thing happens if I try to redirect <code>jq</c... | 145,057 | 209,050 | 2021-05-21 18:11:25 | 33,247,259 | 398 | 2015-10-20 21:38:23 | 1,126,841 | 2015-10-20 21:38:23 | https://stackoverflow.com/q/33247228 | https://stackoverflow.com/a/33247259 | <p>You need to supply a filter as an argument. To pass the JSON through unmodified other than the pretty printing <code>jq</code> provides by default, use the identity filter <code>.</code>:</p>
<pre><code>curl -s https://api.github.com/users/octocat/repos | jq '.' | cat
</code></pre>
| <p>You need to supply a filter as an argument. To pass the JSON through unmodified other than the pretty printing <code>jq</code> provides by default, use the identity filter <code>.</code>:</p> <pre><code>curl -s https://api.github.com/users/octocat/repos | jq '.' | cat </code></pre> | 390, 5813, 105170 | jq, pipe, shell | <h1>How to use `jq` in a shell pipeline?</h1>
<p>I can't seem to get <code>jq</code> to behave "normally" in a shell pipeline. For example:</p>
<pre><code>$ curl -s https://api.github.com/users/octocat/repos | jq | cat
</code></pre>
<p>results in <code>jq</code> simply printing out its help text*. The same ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 502 | bash | # How to use `jq` in a shell pipeline?
I can't seem to get `jq` to behave "normally" in a shell pipeline. For example:
```
$ curl -s https://api.github.com/users/octocat/repos | jq | cat
```
results in `jq` simply printing out its help text*. The same thing happens if I try to redirect `jq`'s output to a file:
```
... | You need to supply a filter as an argument. To pass the JSON through unmodified other than the pretty printing `jq` provides by default, use the identity filter `.`:
```
curl -s https://api.github.com/users/octocat/repos | jq '.' | cat
``` |
8184167 | Prompt for user input in PowerShell | 243 | 2011-11-18 14:41:54 | <p>I want to prompt the user for a series of inputs, including a password and a filename.</p>
<p>I have an example of using <code>host.ui.prompt</code>, which seems sensible, but I can't understand the return.</p>
<p>Is there a better way to get user input in PowerShell?</p>
| 566,971 | 7,211 | 2023-11-28 06:24:54 | 8,184,861 | 398 | 2011-11-18 15:35:22 | 291,709 | 2016-03-09 19:34:24 | https://stackoverflow.com/q/8184167 | https://stackoverflow.com/a/8184861 | <p><a href="http://technet.microsoft.com/en-us/library/hh849945.aspx" rel="noreferrer"><code>Read-Host</code></a> is a simple option for getting string input from a user.</p>
<pre><code>$name = Read-Host 'What is your username?'
</code></pre>
<p>To hide passwords you can use:</p>
<pre><code>$pass = Read-Host 'What i... | <p><a href="http://technet.microsoft.com/en-us/library/hh849945.aspx" rel="noreferrer"><code>Read-Host</code></a> is a simple option for getting string input from a user.</p> <pre><code>$name = Read-Host 'What is your username?' </code></pre> <p>To hide passwords you can use:</p> <pre><code>$pass = Read-Host 'What i... | 526 | powershell | <h1>Prompt for user input in PowerShell</h1>
<p>I want to prompt the user for a series of inputs, including a password and a filename.</p>
<p>I have an example of using <code>host.ui.prompt</code>, which seems sensible, but I can't understand the return.</p>
<p>Is there a better way to get user input in PowerShell?</... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 503 | bash | # Prompt for user input in PowerShell
I want to prompt the user for a series of inputs, including a password and a filename.
I have an example of using `host.ui.prompt`, which seems sensible, but I can't understand the return.
Is there a better way to get user input in PowerShell? | [`Read-Host`](http://technet.microsoft.com/en-us/library/hh849945.aspx) is a simple option for getting string input from a user.
```
$name = Read-Host 'What is your username?'
```
To hide passwords you can use:
```
$pass = Read-Host 'What is your password?' -AsSecureString
```
To convert the password to plain text:... |
17368067 | Length of string in bash | 636 | 2013-06-28 15:14:59 | <p>How do you get the length of a string stored in a variable and assign that to another variable?</p>
<pre><code>myvar="some string"
echo ${#myvar}
# 11
</code></pre>
<p>How do you set another variable to the output <code>11</code>?</p>
| 776,139 | 539,490 | 2024-02-17 08:08:10 | 31,009,961 | 397 | 2015-06-23 17:50:56 | 1,765,658 | 2024-02-17 08:08:10 | https://stackoverflow.com/q/17368067 | https://stackoverflow.com/a/31009961 | <h2>UTF-8 string length</h2>
<h3>By using <code>wc</code></h3>
<p>by using <code>wc</code>, you could (from <code>man bc</code>):</p>
<blockquote>
<pre><code> -c, --bytes
print the byte counts
-m, --chars
print the character counts
</code></pre>
</blockquote>
<p>So you could under <a href="/qu... | <h2>UTF-8 string length</h2> <h3>By using <code>wc</code></h3> <p>by using <code>wc</code>, you could (from <code>man bc</code>):</p> <blockquote> <pre><code> -c, --bytes print the byte counts -m, --chars print the character counts </code></pre> </blockquote> <p>So you could under <a href="/qu... | 276, 387, 14970 | bash, string-length, variables | <h1>Length of string in bash</h1>
<p>How do you get the length of a string stored in a variable and assign that to another variable?</p>
<pre><code>myvar="some string"
echo ${#myvar}
# 11
</code></pre>
<p>How do you set another variable to the output <code>11</code>?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 504 | bash | # Length of string in bash
How do you get the length of a string stored in a variable and assign that to another variable?
```
myvar="some string"
echo ${#myvar}
# 11
```
How do you set another variable to the output `11`? | ## UTF-8 string length
### By using `wc`
by using `wc`, you could (from `man bc`):
> ```
> -c, --bytes
> print the byte counts
>
> -m, --chars
> print the character counts
> ```
So you could under [posix](/questions/tagged/posix "show questions tagged 'posix'") [shell](/questions/tagged/sh... |
4471364 | How do I list the functions defined in my shell? | 328 | 2010-12-17 14:24:52 | <p>I can type <code>alias</code> to show a list of all the aliases.</p>
<p>But for functions, all I can do is grep my <code>.bash_profile</code>.</p>
<p>That only gets the ones in that file, not those defined in subsidiary files or dynamically.</p>
<p>Is there a more convenient way to find out what functions are curren... | 151,090 | 233,928 | 2024-07-15 17:38:11 | 4,471,405 | 397 | 2010-12-17 14:28:26 | 21,755 | 2018-04-09 19:49:49 | https://stackoverflow.com/q/4471364 | https://stackoverflow.com/a/4471405 | <p><code>declare -F</code></p>
<blockquote>
<p>Function names and definitions may be listed with the <code>-f</code> option to the
<code>declare</code> builtin command (see Bash Builtins). The <code>-F</code> option to <code>declare</code>
will list the function names only
(and optionally the source file and l... | <p><code>declare -F</code></p> <blockquote> <p>Function names and definitions may be listed with the <code>-f</code> option to the <code>declare</code> builtin command (see Bash Builtins). The <code>-F</code> option to <code>declare</code> will list the function names only (and optionally the source file and l... | 34, 387, 390, 5569 | bash, function, shell, unix | <h1>How do I list the functions defined in my shell?</h1>
<p>I can type <code>alias</code> to show a list of all the aliases.</p>
<p>But for functions, all I can do is grep my <code>.bash_profile</code>.</p>
<p>That only gets the ones in that file, not those defined in subsidiary files or dynamically.</p>
<p>Is there a... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 505 | bash | # How do I list the functions defined in my shell?
I can type `alias` to show a list of all the aliases.
But for functions, all I can do is grep my `.bash_profile`.
That only gets the ones in that file, not those defined in subsidiary files or dynamically.
Is there a more convenient way to find out what functions a... | `declare -F`
> Function names and definitions may be listed with the `-f` option to the
> `declare` builtin command (see Bash Builtins). The `-F` option to `declare`
> will list the function names only
> (and optionally the source file and line number).
[Bash Reference Manual](http://gnu.org/software/bash/manual/bash... |
18006581 | How to append contents of multiple files into one file | 235 | 2013-08-01 23:54:08 | <p>I want to copy the contents of five files to one file as is. I tried doing it using cp for each file. But that overwrites the contents copied from the previous file. I also tried </p>
<pre><code>paste -d "\n" 1.txt 0.txt
</code></pre>
<p>and it did not work.</p>
<p>I want my script to add the newline at the end o... | 500,591 | 2,643,274 | 2021-11-30 12:00:32 | 18,006,605 | 397 | 2013-08-01 23:56:58 | 1,973,259 | 2013-08-01 23:56:58 | https://stackoverflow.com/q/18006581 | https://stackoverflow.com/a/18006605 | <p>You need the <code>cat</code> (short for concatenate) command, with shell redirection (<code>></code>) into your output file </p>
<pre><code>cat 1.txt 2.txt 3.txt > 0.txt
</code></pre>
| <p>You need the <code>cat</code> (short for concatenate) command, with shell redirection (<code>></code>) into your output file </p> <pre><code>cat 1.txt 2.txt 3.txt > 0.txt </code></pre> | 34, 58, 387 | bash, linux, unix | <h1>How to append contents of multiple files into one file</h1>
<p>I want to copy the contents of five files to one file as is. I tried doing it using cp for each file. But that overwrites the contents copied from the previous file. I also tried </p>
<pre><code>paste -d "\n" 1.txt 0.txt
</code></pre>
<p>and it did no... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 506 | bash | # How to append contents of multiple files into one file
I want to copy the contents of five files to one file as is. I tried doing it using cp for each file. But that overwrites the contents copied from the previous file. I also tried
```
paste -d "\n" 1.txt 0.txt
```
and it did not work.
I want my script to add t... | You need the `cat` (short for concatenate) command, with shell redirection (`>`) into your output file
```
cat 1.txt 2.txt 3.txt > 0.txt
``` |
2462317 | Bash: Syntax error: redirection unexpected | 220 | 2010-03-17 12:58:55 | <p>I do this in a script:</p>
<pre><code>read direc <<< $(basename `pwd`)
</code></pre>
<p>and I get:</p>
<pre><code>Syntax error: redirection unexpected
</code></pre>
<p>in an ubuntu machine</p>
<pre><code>/bin/bash --version
GNU bash, version 4.0.33(1)-release (x86_64-pc-linux-gnu)
</code></pre>
<p>whi... | 263,941 | 171,546 | 2024-05-01 03:59:39 | 2,462,357 | 396 | 2010-03-17 13:05:02 | 68,587 | 2021-09-16 13:33:49 | https://stackoverflow.com/q/2462317 | https://stackoverflow.com/a/2462357 | <p>Does your script reference <code>/bin/bash</code> or <code>/bin/sh</code> in its hash bang line? The default system shell in Ubuntu is <a href="http://linux.die.net/man/1/dash" rel="noreferrer">dash</a>, not <a href="http://linux.die.net/man/1/bash" rel="noreferrer">bash</a>, so if you have <code>#!/bin/sh</code> th... | <p>Does your script reference <code>/bin/bash</code> or <code>/bin/sh</code> in its hash bang line? The default system shell in Ubuntu is <a href="http://linux.die.net/man/1/dash" rel="noreferrer">dash</a>, not <a href="http://linux.die.net/man/1/bash" rel="noreferrer">bash</a>, so if you have <code>#!/bin/sh</code> th... | 387, 549 | bash, ubuntu | <h1>Bash: Syntax error: redirection unexpected</h1>
<p>I do this in a script:</p>
<pre><code>read direc <<< $(basename `pwd`)
</code></pre>
<p>and I get:</p>
<pre><code>Syntax error: redirection unexpected
</code></pre>
<p>in an ubuntu machine</p>
<pre><code>/bin/bash --version
GNU bash, version 4.0.33(1)... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 507 | bash | # Bash: Syntax error: redirection unexpected
I do this in a script:
```
read direc <<< $(basename `pwd`)
```
and I get:
```
Syntax error: redirection unexpected
```
in an ubuntu machine
```
/bin/bash --version
GNU bash, version 4.0.33(1)-release (x86_64-pc-linux-gnu)
```
while I do not get this error in another ... | Does your script reference `/bin/bash` or `/bin/sh` in its hash bang line? The default system shell in Ubuntu is [dash](http://linux.die.net/man/1/dash), not [bash](http://linux.die.net/man/1/bash), so if you have `#!/bin/sh` then your script will be using a different shell than you expect. Dash does not have the `<<<`... |
17420994 | How can I match a string with a regex in Bash? | 255 | 2013-07-02 08:35:05 | <p>I am trying to write a bash script that contains a function so when given a <code>.tar</code>, <code>.tar.bz2</code>, <code>.tar.gz</code> etc. file it uses tar with the relevant switches to decompress the file.</p>
<p>I am using if elif then statements which test the filename to see what it ends with and I cannot ... | 515,836 | 1,587,462 | 2019-08-20 20:21:30 | 17,421,041 | 394 | 2013-07-02 08:37:37 | 7,412 | 2013-07-02 08:37:37 | https://stackoverflow.com/q/17420994 | https://stackoverflow.com/a/17421041 | <p>To match regexes you need to use the <code>=~</code> operator.</p>
<p>Try this:</p>
<pre><code>[[ sed-4.2.2.tar.bz2 =~ tar.bz2$ ]] && echo matched
</code></pre>
<p>Alternatively, you can use wildcards (instead of regexes) with the <code>==</code> operator:</p>
<pre><code>[[ sed-4.2.2.tar.bz2 == *tar.bz2 ... | <p>To match regexes you need to use the <code>=~</code> operator.</p> <p>Try this:</p> <pre><code>[[ sed-4.2.2.tar.bz2 =~ tar.bz2$ ]] && echo matched </code></pre> <p>Alternatively, you can use wildcards (instead of regexes) with the <code>==</code> operator:</p> <pre><code>[[ sed-4.2.2.tar.bz2 == *tar.bz2 ... | 18, 139, 387 | bash, regex, string | <h1>How can I match a string with a regex in Bash?</h1>
<p>I am trying to write a bash script that contains a function so when given a <code>.tar</code>, <code>.tar.bz2</code>, <code>.tar.gz</code> etc. file it uses tar with the relevant switches to decompress the file.</p>
<p>I am using if elif then statements which ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 508 | bash | # How can I match a string with a regex in Bash?
I am trying to write a bash script that contains a function so when given a `.tar`, `.tar.bz2`, `.tar.gz` etc. file it uses tar with the relevant switches to decompress the file.
I am using if elif then statements which test the filename to see what it ends with and I ... | To match regexes you need to use the `=~` operator.
Try this:
```
[[ sed-4.2.2.tar.bz2 =~ tar.bz2$ ]] && echo matched
```
Alternatively, you can use wildcards (instead of regexes) with the `==` operator:
```
[[ sed-4.2.2.tar.bz2 == *tar.bz2 ]] && echo matched
```
If portability is not a concern, I recommend using ... |
707646 | Echo equivalent in PowerShell for script testing | 366 | 2009-04-01 22:49:06 | <p>I would like to output variables and values out in a <a href="http://en.wikipedia.org/wiki/Windows_PowerShell" rel="noreferrer">PowerShell</a> script by setting up flags and seeing the data matriculate throughout the script. </p>
<p>How would I do this? </p>
<p>For example, what would be the PowerShell equivalent... | 1,120,526 | 18,853 | 2021-08-03 15:25:04 | 707,666 | 392 | 2009-04-01 22:59:29 | 67,392 | 2018-03-09 19:28:28 | https://stackoverflow.com/q/707646 | https://stackoverflow.com/a/707666 | <p>There are several ways:</p>
<p><a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host" rel="noreferrer"><code>Write-Host</code></a>: Write directly to the console, not included in function/cmdlet output. Allows foreground and background colour to be set.</p>
<p><a href... | <p>There are several ways:</p> <p><a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host" rel="noreferrer"><code>Write-Host</code></a>: Write directly to the console, not included in function/cmdlet output. Allows foreground and background colour to be set.</p> <p><a href... | 526 | powershell | <h1>Echo equivalent in PowerShell for script testing</h1>
<p>I would like to output variables and values out in a <a href="http://en.wikipedia.org/wiki/Windows_PowerShell" rel="noreferrer">PowerShell</a> script by setting up flags and seeing the data matriculate throughout the script. </p>
<p>How would 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 | 509 | bash | # Echo equivalent in PowerShell for script testing
I would like to output variables and values out in a [PowerShell](http://en.wikipedia.org/wiki/Windows_PowerShell) script by setting up flags and seeing the data matriculate throughout the script.
How would I do this?
For example, what would be the PowerShell equiva... | There are several ways:
[`Write-Host`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host): Write directly to the console, not included in function/cmdlet output. Allows foreground and background colour to be set.
[`Write-Debug`](https://learn.microsoft.com/en-us/powershell/mo... |
38882654 | docker entrypoint running bash script gets "permission denied" | 308 | 2016-08-10 20:11:04 | <p>I'm trying to dockerize my node.js app. When the container is built I want it to run a <code>git clone</code> and then start the node server. Therefore I put these operations in a .sh script. And run the script as a single command in the ENTRYPOINT:</p>
<pre><code>FROM ubuntu:14.04
RUN apt-get update && ap... | 516,015 | 4,840,567 | 2024-07-01 04:59:26 | 38,882,798 | 391 | 2016-08-10 20:19:26 | 14,122 | 2016-08-10 20:28:33 | https://stackoverflow.com/q/38882654 | https://stackoverflow.com/a/38882798 | <ol>
<li><p>"Permission denied" prevents your script from being invoked <em>at all</em>. Thus, the only syntax that could be possibly pertinent is that of the first line (the "shebang"), which should look like <code>#!/usr/bin/env bash</code>, or <code>#!/bin/bash</code>, or similar depending on your target's filesyste... | <ol> <li><p>"Permission denied" prevents your script from being invoked <em>at all</em>. Thus, the only syntax that could be possibly pertinent is that of the first line (the "shebang"), which should look like <code>#!/usr/bin/env bash</code>, or <code>#!/bin/bash</code>, or similar depending on your target's filesyste... | 387, 390, 90304 | bash, docker, shell | <h1>docker entrypoint running bash script gets "permission denied"</h1>
<p>I'm trying to dockerize my node.js app. When the container is built I want it to run a <code>git clone</code> and then start the node server. Therefore I put these operations in a .sh script. And run the script as a single command in the ENTRYPO... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 510 | bash | # docker entrypoint running bash script gets "permission denied"
I'm trying to dockerize my node.js app. When the container is built I want it to run a `git clone` and then start the node server. Therefore I put these operations in a .sh script. And run the script as a single command in the ENTRYPOINT:
```
FROM ubunt... | 1. "Permission denied" prevents your script from being invoked *at all*. Thus, the only syntax that could be possibly pertinent is that of the first line (the "shebang"), which should look like `#!/usr/bin/env bash`, or `#!/bin/bash`, or similar depending on your target's filesystem layout.
2. Most likely the filesyste... |
5072996 | How to get all groups that a user is a member of? | 218 | 2011-02-22 00:57:01 | <p>PowerShell's <a href="https://technet.microsoft.com/en-us/library/ee617193.aspx" rel="noreferrer"><code>Get-ADGroupMember</code></a> cmdlet returns members of a specific group. Is there a cmdlet or property to get all the groups that a particular user is a member of?</p>
| 1,684,415 | 423,278 | 2024-06-25 00:13:04 | 15,570,061 | 391 | 2013-03-22 12:14:27 | 2,199,155 | 2023-03-10 17:15:20 | https://stackoverflow.com/q/5072996 | https://stackoverflow.com/a/15570061 | <p><code>Get-ADPrincipalGroupMembership</code> from the <a href="https://learn.microsoft.com/en-us/powershell/module/activedirectory/" rel="noreferrer">Active Directory module</a> will do this. You'll need that module, or RSAT on Windows 10+, installed to run the command below.</p>
<pre><code>Get-ADPrincipalGroupMember... | <p><code>Get-ADPrincipalGroupMembership</code> from the <a href="https://learn.microsoft.com/en-us/powershell/module/activedirectory/" rel="noreferrer">Active Directory module</a> will do this. You'll need that module, or RSAT on Windows 10+, installed to run the command below.</p> <pre><code>Get-ADPrincipalGroupMember... | 64, 526, 2631, 10401, 24067 | active-directory, cmd, powershell, powershell-2.0, windows | <h1>How to get all groups that a user is a member of?</h1>
<p>PowerShell's <a href="https://technet.microsoft.com/en-us/library/ee617193.aspx" rel="noreferrer"><code>Get-ADGroupMember</code></a> cmdlet returns members of a specific group. Is there a cmdlet or property to get all the groups that a particular user is a m... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 511 | bash | # How to get all groups that a user is a member of?
PowerShell's [`Get-ADGroupMember`](https://technet.microsoft.com/en-us/library/ee617193.aspx) cmdlet returns members of a specific group. Is there a cmdlet or property to get all the groups that a particular user is a member of? | `Get-ADPrincipalGroupMembership` from the [Active Directory module](https://learn.microsoft.com/en-us/powershell/module/activedirectory/) will do this. You'll need that module, or RSAT on Windows 10+, installed to run the command below.
```
Get-ADPrincipalGroupMembership username | select name
name
----
Domain Users
... |
3085295 | How do I get only directories using Get-ChildItem? | 311 | 2010-06-21 14:03:30 | <p>I'm using PowerShell 2.0 and I want to pipe out all the subdirectories of a certain path. The following command outputs all files and directories, but I can't figure out how to filter out the files.</p>
<pre><code>Get-ChildItem c:\mypath -Recurse
</code></pre>
<p>I've tried using <code>$_.Attributes</code> to get ... | 676,299 | 231,929 | 2025-04-09 08:07:52 | 3,085,524 | 389 | 2010-06-21 14:31:42 | 61,396 | 2021-07-22 11:00:54 | https://stackoverflow.com/q/3085295 | https://stackoverflow.com/a/3085524 | <p><strong>For PowerShell 3.0 and greater:</strong></p>
<pre><code>Get-ChildItem -Directory
</code></pre>
<p>You can also use the aliases <code>dir</code>, <code>ls</code>, and <code>gci</code></p>
<hr />
<p><strong>For PowerShell versions less than 3.0:</strong></p>
<p>The <code>FileInfo</code> object returned by <cod... | <p><strong>For PowerShell 3.0 and greater:</strong></p> <pre><code>Get-ChildItem -Directory </code></pre> <p>You can also use the aliases <code>dir</code>, <code>ls</code>, and <code>gci</code></p> <hr /> <p><strong>For PowerShell versions less than 3.0:</strong></p> <p>The <code>FileInfo</code> object returned by <cod... | 526, 24067 | powershell, powershell-2.0 | <h1>How do I get only directories using Get-ChildItem?</h1>
<p>I'm using PowerShell 2.0 and I want to pipe out all the subdirectories of a certain path. The following command outputs all files and directories, but I can't figure out how to filter out the files.</p>
<pre><code>Get-ChildItem c:\mypath -Recurse
</code></... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 512 | bash | # How do I get only directories using Get-ChildItem?
I'm using PowerShell 2.0 and I want to pipe out all the subdirectories of a certain path. The following command outputs all files and directories, but I can't figure out how to filter out the files.
```
Get-ChildItem c:\mypath -Recurse
```
I've tried using `$_.Att... | **For PowerShell 3.0 and greater:**
```
Get-ChildItem -Directory
```
You can also use the aliases `dir`, `ls`, and `gci`
---
**For PowerShell versions less than 3.0:**
The `FileInfo` object returned by `Get-ChildItem` has a "base" property, `PSIsContainer`. You want to select only those items.
```
Get-ChildItem -... |
16787916 | Find the files existing in one directory but not in the other | 295 | 2013-05-28 08:59:22 | <p>I'm trying to find the files existing in one directory but not in the other, I tried to use this command:</p>
<pre><code>diff -q dir1 dir2
</code></pre>
<p>The problem with the above command that it finds both the files in <code>dir1</code> but not in <code>dir2</code> as well as the files in <code>dir2</code> but... | 451,793 | 1,826,552 | 2019-10-07 01:08:14 | 16,788,549 | 389 | 2013-05-28 09:30:29 | 718,925 | 2016-04-26 12:41:41 | https://stackoverflow.com/q/16787916 | https://stackoverflow.com/a/16788549 | <pre><code>diff -r dir1 dir2 | grep dir1 | awk '{print $4}' > difference1.txt
</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><p><code>diff -r dir1 dir2</code> shows which files are only in dir1 and those only in dir2 and also the changes of the files present in both directories if any.</p></li>
<li><p... | <pre><code>diff -r dir1 dir2 | grep dir1 | awk '{print $4}' > difference1.txt </code></pre> <p><strong>Explanation:</strong></p> <ul> <li><p><code>diff -r dir1 dir2</code> shows which files are only in dir1 and those only in dir2 and also the changes of the files present in both directories if any.</p></li> <li><p... | 58, 387, 606 | bash, diff, linux | <h1>Find the files existing in one directory but not in the other</h1>
<p>I'm trying to find the files existing in one directory but not in the other, I tried to use this command:</p>
<pre><code>diff -q dir1 dir2
</code></pre>
<p>The problem with the above command that it finds both the files in <code>dir1</code> but... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 513 | bash | # Find the files existing in one directory but not in the other
I'm trying to find the files existing in one directory but not in the other, I tried to use this command:
```
diff -q dir1 dir2
```
The problem with the above command that it finds both the files in `dir1` but not in `dir2` as well as the files in `dir2... | ```
diff -r dir1 dir2 | grep dir1 | awk '{print $4}' > difference1.txt
```
**Explanation:**
- `diff -r dir1 dir2` shows which files are only in dir1 and those only in dir2 and also the changes of the files present in both directories if any.
- `diff -r dir1 dir2 | grep dir1` shows which files are only in dir1
- `awk`... |
8988824 | Generating random number between 1 and 10 in Bash Shell Script | 232 | 2012-01-24 14:49:05 | <p>How would I generate an inclusive random number between 1 to 10 in Bash Shell Script?</p>
<p>Would it be <code>$(RANDOM 1+10)</code>?</p>
| 445,768 | 1,167,245 | 2024-11-07 17:44:45 | 8,988,890 | 389 | 2012-01-24 14:53:04 | 1,073,222 | 2016-02-11 20:21:18 | https://stackoverflow.com/q/8988824 | https://stackoverflow.com/a/8988890 | <pre><code>$(( ( RANDOM % 10 ) + 1 ))
</code></pre>
<p><strong>EDIT.</strong> Changed brackets into parenthesis according to the comment.
<a href="http://web.archive.org/web/20150206070451/http://islandlinux.org/howto/generate-random-numbers-bash-scripting">http://web.archive.org/web/20150206070451/http://islandlinux... | <pre><code>$(( ( RANDOM % 10 ) + 1 )) </code></pre> <p><strong>EDIT.</strong> Changed brackets into parenthesis according to the comment. <a href="http://web.archive.org/web/20150206070451/http://islandlinux.org/howto/generate-random-numbers-bash-scripting">http://web.archive.org/web/20150206070451/http://islandlinux... | 197, 387, 390 | bash, random, shell | <h1>Generating random number between 1 and 10 in Bash Shell Script</h1>
<p>How would I generate an inclusive random number between 1 to 10 in Bash Shell Script?</p>
<p>Would it be <code>$(RANDOM 1+10)</code>?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 514 | bash | # Generating random number between 1 and 10 in Bash Shell Script
How would I generate an inclusive random number between 1 to 10 in Bash Shell Script?
Would it be `$(RANDOM 1+10)`? | ```
$(( ( RANDOM % 10 ) + 1 ))
```
**EDIT.** Changed brackets into parenthesis according to the comment.
<http://web.archive.org/web/20150206070451/http://islandlinux.org/howto/generate-random-numbers-bash-scripting> |
2193584 | Copy folder recursively, excluding some folders | 232 | 2010-02-03 16:39:27 | <p>I am trying to write a simple bash script that will copy the entire contents of a folder including hidden files and folders into another folder, but I want to exclude certain specific folders. How could I achieve this?</p>
| 162,734 | 134,482 | 2024-03-17 23:05:45 | 2,194,500 | 387 | 2010-02-03 18:45:10 | 103,842 | 2015-03-11 13:41:47 | https://stackoverflow.com/q/2193584 | https://stackoverflow.com/a/2194500 | <p>Use rsync:</p>
<pre><code>rsync -av --exclude='path1/to/exclude' --exclude='path2/to/exclude' source destination
</code></pre>
<p>Note that using <code>source</code> and <code>source/</code> are different. A trailing slash means to copy the contents of the folder <code>source</code> into <code>destination</code>.... | <p>Use rsync:</p> <pre><code>rsync -av --exclude='path1/to/exclude' --exclude='path2/to/exclude' source destination </code></pre> <p>Note that using <code>source</code> and <code>source/</code> are different. A trailing slash means to copy the contents of the folder <code>source</code> into <code>destination</code>.... | 34, 387, 390, 531 | bash, scripting, shell, unix | <h1>Copy folder recursively, excluding some folders</h1>
<p>I am trying to write a simple bash script that will copy the entire contents of a folder including hidden files and folders into another folder, but I want to exclude certain specific folders. How could I achieve this?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 515 | bash | # Copy folder recursively, excluding some folders
I am trying to write a simple bash script that will copy the entire contents of a folder including hidden files and folders into another folder, but I want to exclude certain specific folders. How could I achieve this? | Use rsync:
```
rsync -av --exclude='path1/to/exclude' --exclude='path2/to/exclude' source destination
```
Note that using `source` and `source/` are different. A trailing slash means to copy the contents of the folder `source` into `destination`. Without the trailing slash, it means copy the folder `source` into `des... |
2241063 | Bash script to set up a temporary SSH tunnel | 154 | 2010-02-10 23:16:04 | <p>On Cygwin, I want a Bash script to:</p>
<ol>
<li>Create an SSH tunnel to a remote server. </li>
<li>Do some work locally that uses the tunnel. </li>
<li>Then shut down the tunnel.</li>
</ol>
<p>The shutdown part has me perplexed.</p>
<p>Currently, I have a lame solution. In one shell I run the following to create... | 84,122 | 814 | 2022-03-08 06:33:14 | 15,198,031 | 387 | 2013-03-04 09:13:38 | 2,131,094 | 2022-03-08 06:33:14 | https://stackoverflow.com/q/2241063 | https://stackoverflow.com/a/15198031 | <p>You can do this cleanly with an ssh 'control socket'. To talk to an already-running SSH process and get it's pid, kill it etc. Use the 'control socket' (-M for master and -S for socket) as follows:</p>
<pre><code>$ ssh -M -S my-ctrl-socket -fNT -L 50000:localhost:3306 jm@sampledomain.com
$ ssh -S my-ctrl-socket -O ... | <p>You can do this cleanly with an ssh 'control socket'. To talk to an already-running SSH process and get it's pid, kill it etc. Use the 'control socket' (-M for master and -S for socket) as follows:</p> <pre><code>$ ssh -M -S my-ctrl-socket -fNT -L 50000:localhost:3306 jm@sampledomain.com $ ssh -S my-ctrl-socket -O ... | 386, 387 | bash, ssh | <h1>Bash script to set up a temporary SSH tunnel</h1>
<p>On Cygwin, I want a Bash script to:</p>
<ol>
<li>Create an SSH tunnel to a remote server. </li>
<li>Do some work locally that uses the tunnel. </li>
<li>Then shut down the tunnel.</li>
</ol>
<p>The shutdown part has me perplexed.</p>
<p>Currently, I have a lam... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 516 | bash | # Bash script to set up a temporary SSH tunnel
On Cygwin, I want a Bash script to:
1. Create an SSH tunnel to a remote server.
2. Do some work locally that uses the tunnel.
3. Then shut down the tunnel.
The shutdown part has me perplexed.
Currently, I have a lame solution. In one shell I run the following to create... | You can do this cleanly with an ssh 'control socket'. To talk to an already-running SSH process and get it's pid, kill it etc. Use the 'control socket' (-M for master and -S for socket) as follows:
```
$ ssh -M -S my-ctrl-socket -fNT -L 50000:localhost:3306 jm@sampledomain.com
$ ssh -S my-ctrl-socket -O check jm@sampl... |
7248031 | Meaning of $? (dollar question mark) in shell scripts | 357 | 2011-08-30 18:33:47 | <p>What does </p>
<pre><code>echo $?
</code></pre>
<p>mean in shell programming?</p>
| 339,050 | 661,857 | 2025-05-11 21:33:38 | 7,248,048 | 386 | 2011-08-30 18:35:39 | 576,875 | 2022-06-14 02:15:53 | https://stackoverflow.com/q/7248031 | https://stackoverflow.com/a/7248048 | <p>This is the exit status of the last executed command.</p>
<p>For example the command <code>true</code> always returns a status of <code>0</code> and <code>false</code> always returns a status of <code>1</code>:</p>
<pre><code>true
echo $? # echoes 0
false
echo $? # echoes 1
</code></pre>
<p>From the manual: (acessib... | <p>This is the exit status of the last executed command.</p> <p>For example the command <code>true</code> always returns a status of <code>0</code> and <code>false</code> always returns a status of <code>1</code>:</p> <pre><code>true echo $? # echoes 0 false echo $? # echoes 1 </code></pre> <p>From the manual: (acessib... | 390, 531, 12725 | scripting, shell, special-characters | <h1>Meaning of $? (dollar question mark) in shell scripts</h1>
<p>What does </p>
<pre><code>echo $?
</code></pre>
<p>mean in shell programming?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 517 | bash | # Meaning of $? (dollar question mark) in shell scripts
What does
```
echo $?
```
mean in shell programming? | This is the exit status of the last executed command.
For example the command `true` always returns a status of `0` and `false` always returns a status of `1`:
```
true
echo $? # echoes 0
false
echo $? # echoes 1
```
From the manual: (acessible by calling `man bash` in your shell)
> `?` Expands to the exit st... |
3517162 | How to undo the effect of "set -e" which makes bash exit immediately if any command fails? | 252 | 2010-08-18 22:13:37 | <p>After entering <code>set -e</code> in an interactive bash shell, bash will exit immediately if any command exits with non-zero. How can I undo this effect?</p>
| 68,067 | 424,592 | 2023-11-30 21:07:27 | 3,517,181 | 386 | 2010-08-18 22:17:06 | 388,520 | 2010-08-18 22:17:06 | https://stackoverflow.com/q/3517162 | https://stackoverflow.com/a/3517181 | <p>With <code>set +e</code>. Yeah, it's backward that you <em>enable</em> shell options with <code>set -</code> and <em>disable</em> them with <code>set +</code>. Historical raisins, donchanow.</p>
| <p>With <code>set +e</code>. Yeah, it's backward that you <em>enable</em> shell options with <code>set -</code> and <em>disable</em> them with <code>set +</code>. Historical raisins, donchanow.</p> | 387, 10871 | bash, exit | <h1>How to undo the effect of "set -e" which makes bash exit immediately if any command fails?</h1>
<p>After entering <code>set -e</code> in an interactive bash shell, bash will exit immediately if any command exits with non-zero. How can I undo this effect?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 518 | bash | # How to undo the effect of "set -e" which makes bash exit immediately if any command fails?
After entering `set -e` in an interactive bash shell, bash will exit immediately if any command exits with non-zero. How can I undo this effect? | With `set +e`. Yeah, it's backward that you *enable* shell options with `set -` and *disable* them with `set +`. Historical raisins, donchanow. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.