Executing a command on multiple files and keeping the same name
EDIT: Suppose, in general, that the two targets are located in two different paths, for example "path / to / folder2" and "path / to / folder3", and with that in mind, you can always specify the list used in for loop you you can try:
for i in $(ls path/to/folder3 | grep .less); do . /path/to/folder1/script.sh $(echo "path/to/folder3/$i $( echo "path/to/folder2/$i" | sed -e s/.less/.css/)") ; done
It is also a pity for the cruelty and, perhaps, not an elegant solution.
source to share
IMHO, a more concise, realistic and intuitive solution is to use GNU Parallel
. Your team will:
parallel command {} {.}.css ::: *.less
So, let's say your "command" ls -l
and you have these files in your directory:
Freddy Frog.css
Freddy Frog.less
a.css
a.less
then your team will
parallel ls -l {} {.}.css ::: *.less
-rw-r--r-- 1 mark staff 0 7 Aug 08:09 Freddy Frog.css
-rw-r--r-- 1 mark staff 0 7 Aug 08:09 Freddy Frog.less
-rw-r--r-- 1 mark staff 0 7 Aug 08:09 a.css
-rw-r--r-- 1 mark staff 0 7 Aug 08:09 a.less
The advantage is primarily that it is nice, concise syntax and one-liner. Second, it will run commands in parallel using as many cores as your processor (s) to keep it faster. If you do, you may want the option to -k
keep the outputs in order from different commands.
If you need it to run across many folders in the hierarchy, you can pass the filenames like this:
find <somepleace> -name \*.less | parallel <command> {} {.}.css
To understand these last two points (piping and ordering), take a look at this example:
seq 1 10 | parallel echo
6
7
8
5
4
9
3
2
1
10
And now with -k
, to keep order:
seq 1 10 | parallel -k echo
1
2
3
4
5
6
7
8
9
10
If for some reason you want to run jobs one by one in sequence, just add a switch -j 1
to the command parallel
to set the number of parallel jobs to 1.
Try this on your Linux box as it usually installs there GNU Parallel
. On a Mac under OS X, the easiest way to install GNU Parallel
is with help homebrew
- ask before trying if you're not familiar.
source to share