Questions tagged [for]

A tag to denote any questions relating to for loops

Filter by
Sorted by
Tagged with
2
votes
2answers
65 views

Extract all the traffic corresponding to a request with a parameter

For each line of access.log with the pattern /mypattern: www.example.com:80 192.0.2.17 - - [29/Sep/2017:13:49:02 +0200] "GET /mypattern?foo=bar&iptosearch=198.51.100.5 I would like to extract ...
2
votes
3answers
556 views

list oldest file in directories in a loop

I have a set of files in a structure like so; regions ├── ap-northeast-1 │   └── sg-66497903 │   ├── sg-66497903-2017-10-03-Tue-12.39.json │   ├── sg-66497903-2017-10-03-Tue-12.42.json │   ...
0
votes
3answers
53 views

for detects the spaces in the file as multiple lines

I am now developing a script to copy some files from directory A to multiple directories B following some directives , the script is working fine , but when it comes to files with spaces on them , he ...
3
votes
2answers
996 views

Range variables in for loop custom definable

I have a question with range variables in for loops. In for loops I know you can use {..} to define a range. But I want it to be customer definable. So the script asks for a range value, and I want it ...
3
votes
2answers
2k views

Using a For Loop to Sort and Save Files Using Grep

I have a huge number of files that are numbered in a way such as file_01_01.out where the first number is the group that the file belongs to, and the second is number of the file in the group - so ...
3
votes
3answers
3k views

How run a for loop on 5 month?

I created this loop that activate my script only on 1 month (20170301 - 20170331): for ((i = 20170301; i<=20170331; i++)) ; do /home/jul/exp/prod/client/apps/scripts/runCer client-layer-name $i; ...
3
votes
1answer
413 views

Break running for-loop after current iteration

I frequently execute programs that take a long time on several remote servers: for NUM in {1..100}; do ssh host-{NUM}.mydomain.com /usr/bin/takesalongtime; done Most of the time I let this run in ...
1
vote
1answer
724 views

combine multiple same echo's to one echo from for loop in bash

I'm creating a monitoring script that looks at results from multiple hosts in for loop and then echo's OK or NOT OK... If all resulted to OK then I want to combine the message to single OK else NOT OK....
0
votes
4answers
198 views

ksh loop: “for dir in find .. do” not working, unlike “for dir in ls .. do”

Both of the following lines do return the list of dirs of one of my directories. The first of them use a name pattern whereas I need to choose dirs given they really are directories to then process ...
1
vote
2answers
1k views

Batch renaming TV Series

My iTunes library is on a NAS (WD MyCloud 4TB), and I have a number of TV series, organised by iTunes library in the usual: 'TV Shows' folder: TV Show 1 |------------- Series 1 |-----...
0
votes
2answers
817 views

Iterate over some pattern using shell

I have a file to process and get some entries from it. The file format is: Example Input: String1:"Hello" String2:"World" String3:"Question" End String1:"HelloAgain" String5:"Answer" End String1:...
2
votes
3answers
11k views

Use sed to find and replace a string in multiple files [duplicate]

I'm trying to pass a list of files with a known set of characters to sed for a find and replace. For a directory containing multiple .xml files: ls -la file1.xml file2.xml file3.xml Each ...
1
vote
1answer
337 views

Scripting with 'for' and grep/egrep

I'm trying to learn some more Linux, and from experience the best way is to try to bang your head against the wall. So now that I've done a task manually a few times I would like to automate it. This ...
0
votes
1answer
208 views

Infinite Loop in Nested For Loop

I'm working on the following script to parse some files for me. The script actually does what I want - I get the correct information in the correct files. It seems that the outermost for loop runs the ...
1
vote
1answer
110 views

How to avoid repeating sed commands when adding sites to Varnish?

This is my script to install Varnish. I run it each time I raise up a new server environment on a VPS. cd ~ apt-get update apt-get install varnish -y sed -i 's/Listen 80/Listen 8080/g' /etc/apache2/...
0
votes
1answer
1k views

How to run a script in Sudo and automatically fill in my password

I have Bash for windows (linux subsystem) and I'm in the process of creating some powershell scripts (just for fun) for apps I run on Linux so I can just launch them from my desktop. It is all ...
5
votes
1answer
3k views

How to exit a bash loop by keyboard input?

I have a bash lopp as #!/bin/bash for (( c=0; c<=1000000; c++ )) do SOME STUFF HERE done I interrupt the long loop by a keyboard input like Ctrl+C but Ctrl+C simply terminates the script. I am ...
5
votes
2answers
200 views

What happens when files are added/removed in the middle of a “for f in *” sh loop?

I found one for loop example online. Now I want to use it in my code but I am not sure how does this loop operates for entry in "$search_dir"/* do echo "$entry" done Now I want to ask that Does ...
-2
votes
2answers
131 views

How to make for loop with $1 - 1 cycles [closed]

I am trying to make sh script which uses 'sed' to make bubblesort algorithm I am strugling, how to make for loop for n in $1 do echo $n done How can I change body of the for loop to make just n-...
7
votes
1answer
4k views

In what order does a bash FOR IN loop pick up files in a folder?

Say I have a below for in loop: for i in /apps/textfiles/*.txt do do something done Now say I have 50 files inside /apps/textfiles/ In what order will the files be picked?
2
votes
1answer
597 views

If condition works for first time but not for the second time

I have defined the below for and if condition in a function. For the first time it runs well. Second time it doesn't gets executed after the for loop, it just writes the echo statement. Why does it ...
3
votes
4answers
593 views

Fetch all filenames with specific number of characters

I want to change all filenames they have the exact length of 16 characters (digits and lower case letters). I tried [0-9a-z]{16} and [0-9a-z]\{16\} for the placeholder regX in the following snippet, ...
2
votes
5answers
8k views

Script to go through $PATH folders and see what executable files are available on your system

Here is what I have so far: #!/bin/bash for file in $PATH ; do # Scanning files in $PATH if [ -x ] ; then #Check if executable echo "Executable File" else ...
14
votes
4answers
184k views

Loop through a folder and list files

I have a folder named 'sample' and it has 3 files in it. I want to write a shell script which will read these files inside the sample folder and post it to an HTTP site using curl. I have written the ...
0
votes
2answers
2k views

Go to the beginning of the loop in bash

I am using an if condition inside a for loop. If the if condition returns yes, then I'd like to go to the start of the for loop again. Is this possible in bash? #!/bin/bash for i in /apps/incoming/*....
0
votes
1answer
143 views

Copy multiple files from different directories while renaming?

I have several files to copy from various directories but each file has the same name so I need to copy them to a single directory and rename each in turn as "expect1, expect2, expect3", etc. The code ...
0
votes
1answer
138 views

How to execute a script based on a file

I am trying to write a script that will connect to Oracle from a jump server (i.e) I will execute my script from a jump server, In my script I will have a config file as below #USERNAME PASSWORD ...
6
votes
1answer
1k views

Shell variables in a for loop

I have a difficulty getting over this man bash passage. If the control variable in a for loop has the nameref attribute, the list of words can be a list of shell variables, ...
13
votes
4answers
17k views

Loop through files excluding directories

I need my script to do something to every file in the current directory excluding any sub-directories. For example, in the current path, there are 5 files, but 1 of them is a folder (a sub-directory)....
3
votes
2answers
448 views

How to grep through multiple files using lines from two input files as start and end of pattern?

I would like to write a simple script that would use grep to search through a list of files. The current code looks something similar to this: a= file1.txt b= file2.txt for do grep '$a.*$b' /...
2
votes
6answers
702 views

Iterating through folders in numerical order

I have a bunch of folders which are labelled in this way: conf1 conf2 ... But the order in the home directory is like conf1 conf10 conf100 conf101 ... conf2 conf20 conf200 conf201 ... ...
0
votes
1answer
234 views

Print several lines in the same file through a for loop in bash

I would like to ask you how can I add a set of strings containing three values each, one after the other, in the same file (e.g. CONV.inp)? I would like to type N number of lines with the respective ...
1
vote
1answer
795 views

Loop through all files in a given subdirectory, name the output by the looped file

I have thousands of *csv files in a certain subdirectory. There's a simple in line executable which I use to work with these files, which I pipe into a new file: executable file1.csv standard.csv &...
1
vote
2answers
251 views

Deleting LARGE number of files (exec, loops..) [duplicate]

I have a very large number of files to delete, which go by the following format: esymac_logEvents.log.5_2017-Feb-06_02-39-17.Z_2017-Feb-08_02-39-14.Z_2017-Feb-09_02-39-14.Z_2017-Feb-11_02-39-11....
-1
votes
4answers
593 views

creating/deleting temp files (names at array) [closed]

Hello I'm want to create temp files and then delete them. I have name of variables in ARRAY. Creating is working but I have problems with delete them. Thanks FILES=( a b c d e f g h) for i in "${...
2
votes
1answer
6k views

Using for loop with find command

I want to read all java Versions on my system. for i in 'find / -name java 2>/dev/null' do echo $i checking $i -version done I receive an error: find: paths must precede expression: 2>/dev/...
0
votes
1answer
76 views

Can list of directories be parsed into a find command?

Suppose I want to unzip files recursively that have the pattern ${file}*.zip in some directory /home/A/XML using find command find /home/A/XML -type f -name "${file}*.zip" -exec unzip '{}' -d /target/...
0
votes
4answers
304 views

How to repeatedly alternate between two (or more) commands?

I'd like to repeatedly iterate over, alternate between, and/or cycle through multiple commands; in a kind of pattern or loop. The desired end result could be thought of as not totally unlike watch. I'...
2
votes
1answer
9k views

Remove everything within directory using for loop

I am trying to remove large amount of mails (mostly mail delivery failed) from my server using rm -rf /home/*/mail/new/* And I am getting -bash: /usr/bin/rm: Argument list too long I tried using ...
-1
votes
2answers
84 views

For loop for creating files having consecutive names

I want to create files like song1.mp3,song2.mp3,song3.mp3... in my home directory.How to create this using for loop?
1
vote
3answers
5k views

Count number of lines in each file within current directory using a for loop?

If a parameter is provided as an input, script should only check number of lines for that input file, otherwise script should display number of lines for each file within that directory. The script ...
4
votes
2answers
745 views

Interrupting a for-loop in bash, but only after an iteration is completed?

Is there a way to interrupt/stop a for-loop in bash, but only after the current iteration is completed - ie. "between" done and do? I'm running a for-loop on several directories, with a command ...
12
votes
4answers
7k views

How do I get 0-padded numbers in {} (brace expansion)?

I'm trying to write a simple script to retrieve mem and swap usage from a list of hosts. Currently the only way I've been able to achieve this is to write 3 seperate scripts: for a in {1..9}; do echo ...
-4
votes
5answers
932 views

Unix for loop not working

I've a simple bash script. PFB : CWD=/cygdrive/c/Users/abhisek.samanta/Desktop/New_folder cd $CWD for i in `cat req_files.txt` do echo "file_found : $i">a.txt done Note : req_files.txt is having ...
2
votes
1answer
62 views

for loop executes in a weird way

Quick question: I have to write a simple script and part of it is adding up every value in a column -> sum of every column and everything. So file 1 2 5 1 2 1 should return column1: 3 column2: 4 ...
0
votes
2answers
81 views

For loop with quoted entries

A for loop that steps through a list of quoted headers: for h in "header" "header 2"; do echo $h done returns header header 2 I'd like to reference the headers somewhere else to clean it up, ...
190
votes
8answers
25k views

Why is looping over find's output bad practice?

This question is inspired by Why is using a shell loop to process text considered bad practice ? I see these constructs for file in `find . -type f -name ...`; do smth with ${file}; done and for ...
1
vote
1answer
27 views

imagemagick convertion in segments

How can I convert 5 pictures at a time followed by a wait, then the next 5 pictures in a directory, on N pictures. Here's my code: #!/bin/bash for i in *.jpg; do xload -update 1 & convert ...
4
votes
2answers
615 views

For loop inside another doesn't work

#! /bin/bash for (( l = 1 ; l <= 50; ++l )) ; do for (( k = 1 ; k <= 1000; ++k )) ; do sed -n '$l,$lp' $k.dat >> ~/Escritorio/$l.txt done done The script is located ...
-1
votes
1answer
79 views

Why this expression does not expand in Bash for-for structure?

This works #!/bin/bash dir="/home/masi/Documents/CSV/Case/" targetDir="/tmp/" id=118 channel=1 filenameTarget=$targetDir"P"$id"C"$channel".csv" cat $dir"P"$id"C"$channel"T"*".csv" > $...

1
4
5
6 7 8