User Tools

Site Tools


bash_notes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
bash_notes [2022/04/06 22:38] – [list file names within a specific range] rajubash_notes [2023/12/28 08:49] (current) – [other pages in this wiki] raju
Line 1: Line 1:
 ===== tasks ===== ===== tasks =====
-==== list file names within a specific range ==== +==== dummy ==== 
-In bash, we can use a ''sequence expression'' to list files within a specific range. For example +  * [[list files within a specific range]]
-<code> +
-$ echo file{18..21}.txt +
-file18.txt file19.txt file20.txt file21.txt +
-</code>+
  
-The sequence expression takes the form ''{x..y[..incr]}'', where x and y are either integers or single characters, and incr, an optional increment, is an integer. It is part of Bash's brace expansion feature.+===== dummy ===== 
 +==== other pages in this wiki ==== 
 +  * [[bash scripting]] 
 +  * [[shell scripting notes]]
  
-Ref+==== useful links ==== 
-  * https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html - documentation on Bash's brace expansion feature. +  * https://explainshell.com/ - useful for understanding complex shell commands 
-  * https://superuser.com/questions/236484/list-files-numbered-in-a-specific-range - where I came across the above snippet. Dennis Williamson's reply is pretty comprehensive; provides alternatives solutions; addresses corner cases; high information density. It is worth reading it end to end.+  * https://mywiki.wooledge.org/ArithmeticExpression Arithmetic Expansion 
 +  * https://misc.flogisoft.com/bash/tip_colors_and_formatting escape characters, color prompts, echo colored strings 
 +  * http://ezprompt.net/ - Easy bash prompt generator
  
-Possible use cases:+==== documentation links ==== 
 +  * ignoredups, erasedups - https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html search for ignoredups .
  
-  * To list files <code> +==== what is the difference between "ls > dirlist 2>&1" and "ls 2>&1 > dirlist"? ==== 
-ls file{18..21}.txt+The command 
 +<code> 
 +ls > dirlist 2>&1
 </code> </code>
- +directs both standard output (file descriptor 1) and standard error (file descriptor 2) to the file dirlist, while the command 
-  * Use a for loop to run a command on each of them <code> +<code> 
-for file in file{18..21}.txt +ls 2>&1 > dirlist
-...+
 </code> </code>
 +directs only the standard output to file dirlist, because the standard error was made a copy of the standard output before the standard output was redirected to dirlist.
  
-tags | list file names with a range+To conclude, the order of redirections is significant. 
 + 
 +Ref:- https://www.gnu.org/software/bash/manual/html_node/Redirections.html -> 5th paragraph.
  
-===== dummy ===== 
-==== useful links ==== 
-  * https://mywiki.wooledge.org/ArithmeticExpression - Arithmetic Expansion  
 ===== getopts ===== ===== getopts =====
 ==== useful articles ==== ==== useful articles ====
bash_notes.1649284701.txt.gz · Last modified: 2022/04/06 22:38 by raju